This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Load necessary packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(sandwich)
# Set seed for reproducibility
set.seed(123)
# Simulate 200 days of data
n <- 200
# Simulated variables
covid_cases <- round(runif(n, 0, 10000)) # random daily COVID cases
lockdown <- rbinom(n, 1, 0.3) # 30% chance of lockdown
gdp_growth <- rnorm(n, 2, 1) # normally distributed around 2%
# Simulate stock returns affected by these variables
stock_return <- 0.05 - 0.00001 * covid_cases - 0.03 * lockdown + 0.02 * gdp_growth + rnorm(n, 0, 1)
# Combine into a dataframe
data <- data.frame(
stock_return = stock_return,
covid_cases = covid_cases,
lockdown = as.factor(lockdown),
gdp_growth = gdp_growth
)
# View first few rows
head(data)
## stock_return covid_cases lockdown gdp_growth
## 1 0.03166019 2876 0 4.198810
## 2 -1.16123316 7883 1 3.312413
## 3 -0.59095117 4090 0 1.734855
## 4 -0.01627767 8830 0 2.543194
## 5 0.65835917 9405 0 1.585660
## 6 -1.60463148 456 1 1.523753
# Run regression
model <- lm(stock_return ~ covid_cases + lockdown + gdp_growth, data = data)
# Summary of model
summary(model)
##
## Call:
## lm(formula = stock_return ~ covid_cases + lockdown + gdp_growth,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.64650 -0.58399 0.05316 0.59506 2.24827
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.847e-01 2.020e-01 1.410 0.1602
## covid_cases -8.163e-06 2.499e-05 -0.327 0.7443
## lockdown1 -3.575e-01 1.487e-01 -2.405 0.0171 *
## gdp_growth -3.582e-02 6.851e-02 -0.523 0.6017
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9589 on 196 degrees of freedom
## Multiple R-squared: 0.02974, Adjusted R-squared: 0.01489
## F-statistic: 2.003 on 3 and 196 DF, p-value: 0.1149
# With robust standard errors
coeftest(model, vcov = vcovHC(model, type = "HC1"))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.8471e-01 2.0657e-01 1.3783 0.1697
## covid_cases -8.1634e-06 2.3525e-05 -0.3470 0.7290
## lockdown1 -3.5751e-01 1.6076e-01 -2.2238 0.0273 *
## gdp_growth -3.5816e-02 6.1567e-02 -0.5817 0.5614
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ggplot(data, aes(x = covid_cases, y = stock_return)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", col = "red") +
labs(title = "Simulated Stock Returns vs COVID Cases",
x = "COVID-19 Cases", y = "Stock Return (%)")
## `geom_smooth()` using formula = 'y ~ x'
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.