ECO 4051 Financial Econometrics

Assignment 1: Analyzing financial data in R

Collins Hackman

Q1: downloading the data and analyzing prices

getSymbols("PKI", from="1990-01-01")
[1] "PKI"
price<-Ad(to.monthly(PKI))
length(price)
[1] 388
periodicity(price)
Monthly periodicity from Jan 1990 to Apr 2022 
head(price, 5)
         PKI.Adjusted
Jan 1990     5.414520
Feb 1990     5.452516
Mar 1990     5.680496
Apr 1990     5.537663
May 1990     6.053236
tail(price, 5)
         PKI.Adjusted
Dec 2021     200.9804
Jan 2022     172.1700
Feb 2022     179.6100
Mar 2022     174.4600
Apr 2022     172.4100
plot(price, ylab="price")

plot(log(Ad(price)), col="red", ylab="log.price")

log price and price

Q2: creating the returns and calculating the descriptive statistics

log.ret<-100*diff(log(price))
basicStats(log.ret)
            PKI.Adjusted
nobs          388.000000
NAs             1.000000
Minimum       -36.362227
Maximum        34.120887
1. Quartile    -3.438049
3. Quartile     6.039423
Mean            0.894261
Median          1.722185
Sum           346.079115
SE Mean         0.494124
LCL Mean       -0.077250
UCL Mean        1.865772
Variance       94.489288
Stdev           9.720560
Skewness       -0.560986
Kurtosis        2.243362
plot(log.ret, col="slateblue4", alpha=0.8, ylab= "stock return")

Summary Stats for log returns

Log returns for PKI’s adjusted closing prices

Q3: Plotting

date<-time(price)
p1<-ggplot(price, aes(date, PKI.Adjusted))+geom_point(alpha=0.8, color="hotpink4")+
  labs(x="year", y="stock price", caption="source: YAHOO", subtitle="Montlhy Basis")+theme_bw()
ggplotly(p1)
p2<-ggplot(log.ret, aes(date, PKI.Adjusted))+geom_line(color="darkslategrey")+
  labs(x="year", y="stock returns", caption="source: Yahoo", subtitle="Monthly Basis")
ggplotly(p2)
p3<-ggplot(log.ret, aes(PKI.Adjusted))+geom_histogram(aes(y=..density..), bins=50, color="red", fill="grey45")+ 
  stat_function(fun=dnorm, color="darkorchid",size=1.5, args=list(0.901607, 9.740497))+
  labs(x="stock returns")+theme_bw()
ggplotly(p3)

Discussion:

Q4: correlation

getSymbols("^GSPC", from="1990-01-01")
[1] "^GSPC"
SP<-Ad(to.monthly(GSPC))
GSPC.ret<-100*diff(log(SP))
PKI.GSPC<-merge(log.ret, GSPC.ret)
cor(PKI.GSPC, use='complete.obs')
              PKI.Adjusted GSPC.Adjusted
PKI.Adjusted     1.0000000     0.5001094
GSPC.Adjusted    0.5001094     1.0000000
PKIGSPC.df<-data.frame(Date=time(PKI.GSPC), coredata(PKI.GSPC))

p4<-ggplot(PKIGSPC.df, aes(log.ret, GSPC.ret))+geom_point(alpha=0.8, color="grey66")+geom_smooth(method="lm", se=FALSE)+labs(x="PKI returns", y="S&P500 returns")+
  geom_vline(xintercept=0)+geom_hline(yintercept=0)+theme_gray()
ggplotly(p4)

Correlation Discussion