This report aims to analyze the relationship between the Ruble-USD exchange rate and various financial indicators, such as the S&P 500 index and the price of gold. We will perform descriptive analytics on the Ruble-USD exchange rate over time, calculate basic descriptive statistics, visualize key trends, and examine the correlation between selected financial variables. Additionally, we will apply a simple linear regression model to predict the Ruble-USD exchange rate based on the S&P 500 index and gold prices.
The dataset contains daily data for the period from January 11, 2006,
to March 12, 2020.
It includes the following variables:
library(readxl)
library(ggplot2)
file_path <- 'C:/Users/Valery/Desktop/domashka/1.xlsx'
data <- read_excel(file_path)
head(data)
Below is a table that classifies the variables in the dataset, indicating whether they are quantitative or qualitative, their measurement levels, and appropriate types of charts.
Create a table for variable classifications
variable_info <- data.frame( Variable = c("t", "Date", "Day", "Month", "Year", "RubPerUSD", "SP500", "Gold", "Silver", "Brent", "Cocoa"), Quantitative_or_Qualitative = c("Qualitative", "Qualitative", "Qualitative", "Qualitative", "Qualitative", "Quantitative", "Quantitative", "Quantitative", "Quantitative", "Quantitative", "Quantitative"), Measurement_Level = c("Counts", "Nominal", "Ordinal", "Ordinal", "Ordinal", "Ratio", "Ratio", "Ratio", "Ratio", "Ratio", "Ratio"), Appropriate_Charts = c("Bar plot", "Bar plot", "Bar plot", "Bar plot", "Bar plot", "Line chart, Boxplot", "Line chart, Boxplot", "Line chart, Boxplot", "Line chart, Boxplot", "Line chart, Boxplot", "Line chart, Boxplot") )
variable_info
We will explore three key variables: RubPerUSD, SP500, and Gold. Below are the visualizations and descriptive statistics for these variables.
ggplot(data, aes(x = Date, y = RubPerUSD)) + geom_line(color = "blue") + labs(title = "Ruble-USD Exchange Rate", x = "Date", y = "Rub per USD") + theme_minimal()
ggplot(data, aes(x = Date, y = SP500)) + geom_line(color = "green") + labs(title = "S&P 500 Index", x = "Date", y = "SP500 Value") + theme_minimal()
ggplot(data, aes(x = Date, y = Gold)) + geom_line(color = "gold") + labs(title = "Gold Price (USD per ounce)", x = "Date", y = "Gold Price (USD)") + theme_minimal()
For the selected variables, we will calculate the mean, median, and standard deviation.
Calculating mean, median, and standard deviation for
RubPerUSD
mean_rub <- mean(data$RubPerUSD, na.rm = TRUE)
median_rub <- median(data$RubPerUSD, na.rm = TRUE)
sd_rub <- sd(data$RubPerUSD, na.rm = TRUE)
Calculating mean, median, and standard deviation for SP500
mean_sp500 <- mean(data$SP500, na.rm = TRUE)
median_sp500 <- median(data$SP500, na.rm = TRUE)
sd_sp500 <- sd(data$SP500, na.rm = TRUE)
Calculating mean, median, and standard deviation for Gold
mean_gold <- mean(data$Gold, na.rm = TRUE)
median_gold <- median(data$Gold, na.rm = TRUE)
sd_gold <- sd(data$Gold, na.rm = TRUE)
Displaying the results
mean_rub
## [1] 42.03178
median_rub
## [1] 32.36895
sd_rub
## [1] 16.46466
mean_sp500
## [1] 1773.56
median_sp500
## [1] 1539.59
sd_sp500
## [1] 626.9194
mean_gold
## [1] 1192.991
median_gold
## [1] 1241.675
sd_gold
## [1] 308.2245
Now, lets calculate the correlation matrix for RubPerUSD, SP500, and Gold to understand the relationships between these variables.
Calculate the correlation matrix for the selected variables: RubPerUSD, SP500, and Gold
cor_matrix <- cor(data[c("RubPerUSD", "SP500", "Gold")], use = "complete.obs")
Display the correlation matrix
cor_matrix
## RubPerUSD SP500 Gold
## RubPerUSD 1.0000000 0.8479783 0.2827953
## SP500 0.8479783 1.0000000 0.3033126
## Gold 0.2827953 0.3033126 1.0000000
We will create a combined line chart to compare RubPerUSD, SP500, Gold, and Brent over time.
Combined Line Chart for RubPerUSD, SP500, Gold, and Brent
ggplot(data) +
geom_line(aes(x = Date, y = RubPerUSD, color = "RubPerUSD")) +
geom_line(aes(x = Date, y = SP500, color = "SP500")) +
geom_line(aes(x = Date, y = Gold, color = "Gold")) +
geom_line(aes(x = Date, y = Brent, color = "Brent")) +
labs(title = "Comparing Exchange Rate, S&P 500, Gold Price, and Brent Oil Price",
x = "Date", y = "Value") +
theme_minimal() +
scale_color_manual(values = c("blue", "green", "gold", "brown"))
## Warning: Removed 14 rows containing missing values or values outside the scale range
## (`geom_line()`).
In this section, we will perform linear regression with RubPerUSD as the dependent variable and SP500 and Gold as independent variables.
Linear Regression Model: RubPerUSD as dependent variable
model <- lm(RubPerUSD ~ SP500 + Gold, data = data)
Display the model summary
summary(model)
##
## Call:
## lm(formula = RubPerUSD ~ SP500 + Gold, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.232 -6.649 -1.819 3.604 38.816
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.1274566 0.7248986 1.555 0.11998
## SP500 0.0220244 0.0002786 79.068 < 2e-16 ***
## Gold 0.0015030 0.0005661 2.655 0.00798 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.71 on 2738 degrees of freedom
## (51 пропущенное наблюдение удалено)
## Multiple R-squared: 0.7198, Adjusted R-squared: 0.7196
## F-statistic: 3517 on 2 and 2738 DF, p-value: < 2.2e-16
Plotting the regression results
ggplot(data, aes(x = SP500, y = RubPerUSD)) +
geom_point() +
geom_smooth(method = "lm", aes(color = "SP500 vs RubPerUSD")) +
labs(title = "Linear Regression: SP500 vs RubPerUSD", x = "SP500 Index", y = "RubPerUSD") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 51 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 51 rows containing missing values or values outside the scale range
## (`geom_point()`).
This report explored the relationship between the Ruble-USD exchange rate and financial indicators such as the S&P 500 index and gold prices.
Through descriptive analysis, correlation analysis, and linear regression modeling, we found that SP500 and Gold are significant predictors of the RubPerUSD exchange rate.
Visualizations helped to better understand the trends and dynamics between these variables.
Appendix: R Code for Calculations and Plots
Full code for data processing, statistical calculations, and plotting