This repository contains R code for analyzing the statistical relationships between gold prices and various economic indicators, including the USD Index (DXY), crude oil, S&P 500, RSI, and moving averages. The analysis includes hypothesis testing, correlation studies, seasonal trends, and Monte Carlo simulations.
File Required: Gold final PS.csv
Expected Columns:
time: Date (YYYY-MM-DD)close: Gold pricedxy_close: US Dollar Indexlight_crude_oil_close: Crude Oil pricesp500_close: S&P 500 Indexgold_RSI: Relative Strength Indexgold_SMA_10,gold_SMA_50,gold_SMA_200: Simple Moving Averages
Install required R packages:
install.packages(c("ggplot2", "BSDA", "zoo", "dplyr", "lubridate", "tidyr"))-
Clone the repository:
git clone https://github.com/yourusername/gold-price-analysis.git cd gold-price-analysis -
Open and run the R script:
gold_analysis.R— contains all analyses
-
Load data by modifying the path in the script:
data <- read.csv("path/to/Gold final PS.csv")
- Pearson correlations between gold and:
- DXY
- Crude Oil
- S&P 500
- RSI
- Z-tests on gold prices based on:
- High vs. low DXY
- Crude oil price ranges
- RSI overbought/oversold
- SMA crossover conditions
- 30-day rolling mean
- 30-day rolling volatility
- Plotting key historical periods (e.g., 2008)
- Monthly average returns
- One-way ANOVA to detect significant seasonal patterns
- Tukey’s HSD post-hoc test
- Simulates future gold prices using daily return volatility
- 1-year projection with 1,000 trials
- Line plots with moving averages
- Correlation matrix
- RSI return analysis
- Monthly return seasonality plot
- Monte Carlo simulation fan chart
├── Gold final PS.csv # Input data file
├── gold_analysis.R # Main R script
├── README.md # This README
└── /outputs # (Optional) Generated plots and results
- DXY vs Gold: Negative correlation — supports safe-haven narrative.
- Oil vs Gold: Positive in inflationary conditions.
- RSI Returns: Statistically significant overbought/oversold behavior.
- Seasonality: November–January tend to outperform (ANOVA confirmed).
- Forecasting: Simulated price bands provide risk-based outlook.
This project is licensed under the MIT License.
Contributions are welcome! Please open issues or submit pull requests.