ECO 4051 Financial Econometrics
Assignment 2: Regressing financial data
March 3, 2022
Q1
getSymbols(c("^GSPC", "PEP", "WY", "RSG", "CPRT", "PNR", "ENPH"), from="2000-01-01", periodicity="monthly")## [1] "^GSPC" "PEP" "WY" "RSG" "CPRT" "PNR" "ENPH"
mydata <- merge(Ad(GSPC), Ad(PEP), Ad(WY), Ad(RSG), Ad(CPRT), Ad(PNR))
myret <- 100 * diff(log(mydata))
names(myret) <- c( "GSPC", "PEP", "WY", "RSG", "CPRT", "PNR")
head(myret)## GSPC PEP WY RSG CPRT PNR
## 2000-01-01 NA NA NA NA NA NA
## 2000-02-01 -2.031300 -6.039582 -10.892102 -8.894793 21.279611 4.081527
## 2000-03-01 9.232375 8.213574 10.511658 1.729208 -18.380834 7.527632
## 2000-04-01 -3.127991 5.508394 -6.570839 22.428543 -1.438885 3.153748
## 2000-05-01 -2.215875 10.348449 -7.284755 18.687775 2.857326 3.830267
## 2000-06-01 2.365163 8.816309 -14.032085 -3.664868 -10.379656 -9.997904
tail(myret)## GSPC PEP WY RSG CPRT PNR
## 2021-11-01 -0.8368626 -1.1326297 6.5635962 -1.753783 -6.7457682 -0.1072847
## 2021-12-01 4.2688648 8.3589387 9.0682607 5.293646 4.3540748 -0.8996837
## 2022-01-01 -5.4018230 0.5640306 -1.3853892 -8.501069 -15.9628844 -13.6685707
## 2022-02-01 -3.1862761 -5.8013054 -3.9092059 -5.954813 -5.0540268 -9.2122076
## 2022-03-01 3.5148286 2.1986844 0.9724619 9.676030 2.0858449 -6.6024706
## 2022-04-01 -0.6688424 3.6975969 0.4568546 2.418510 -0.4713442 -1.8431090
Q2
M<-cor(myret, use='complete.obs')
corrplot(M, method="number")Discussion-> Correlation
- All the stock returns move in the same direction as the market but
at varying strengths: as the market returns increases and decreases, the
stock returns for the five companies increase and decrease in the same
direction.
- The “RSG” (waste disposal company) returns has the smallest positive correlation coefficient to the market returns
- On the other hand, the “PNR” (water treatment company) returns is the most strongly correlated with the market
Q3
myret.df<-data.frame(Date=time(myret), coredata(myret))
longdf<- myret.df %>% tidyr::gather(stocks, returns,-Date,-GSPC)
ggplot(longdf, aes(GSPC, returns))+geom_point()+geom_smooth(method="lm",se=FALSE)+facet_wrap(vars(stocks))+theme_grey()## Warning: Removed 5 rows containing non-finite values (stat_smooth).
## Warning: Removed 5 rows containing missing values (geom_point).
Q4
fit<-list()
fit$PEP<-lm(PEP~GSPC, myret)
fit$WY<-lm(WY~GSPC, myret)
fit$RSG<-lm(RSG~GSPC, myret)
fit$CPRT<-lm(CPRT~GSPC, myret)
fit$PNR<-lm(PNR~GSPC, myret)
names<-c("PEP","WY","RSG","CPRT","PNR")
reg.df<-data.frame(stock=names, beta= sapply(fit, function(x) beta = coeftest(x)[2]),
R2=sapply(fit, function(x) R2 = summary(x)$r.squared))
print(reg.df)## stock beta R2
## PEP PEP 0.5034126 0.2300881
## WY WY 1.2846893 0.3172091
## RSG RSG 0.6380280 0.1883881
## CPRT CPRT 0.9435716 0.2008048
## PNR PNR 1.0788246 0.3387038
knitr::kable(as.data.frame(reg.df), digits=3)| stock | beta | R2 | |
|---|---|---|---|
| PEP | PEP | 0.503 | 0.230 |
| WY | WY | 1.285 | 0.317 |
| RSG | RSG | 0.638 | 0.188 |
| CPRT | CPRT | 0.944 | 0.201 |
| PNR | PNR | 1.079 | 0.339 |
Discussion: Stock Regression
- All the stocks are affected by the S&P500’s movements since
their beta is high, however there are other ommitted variables that
explain the variance in their average returns since the S&P500’s R
squared for them is low:
- The regression is subject to ommitted variable bias thus, when more regressors are added, the stocks exposure to the S&P500 should reduce.
- Due to their high beta, they’re very volatile thus allowing for high monthly average returns
Q5
p1<-ggplot(reg.df, aes(stock, beta)) +
geom_bar(stat="Identity", fill="#F9C8B0", color="darkorange4") +
geom_line(aes(x=stock, y=R2, group=1), data=reg.df, color="blue2", size=1.5) +
theme_grey() + geom_hline(yintercept = 1, color="black", linetype="dashed")+
labs(x="stock", y="beta", title="Beta and R square") +
theme(axis.text.x = element_text(angle = 90))
p<-ggplotly(p1)## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
p