setwd("~/Downloads")
getwd()
## [1] "/Users/daphneuz/Downloads"
jj <- read.csv("jj_quarterly_earnings.csv")
dim(jj)
## [1] 84 5
#Create Vars
t <- 1:nrow(jj)
y <- log(jj$Earnings)
# (a) Plot Earnings
plot(t, jj$Earnings,
type="l",
main="JJ Quarterly Earnings",
xlab="t",
ylab="Earnings")

# (b) Plot Log Earnings
plot(t, y,
type="l",
main="Log Earnings",
xlab="t",
ylab="log(Earnings)")

# (c) Regression: Trend Only
fit1 <- lm(y ~ t)
summary(fit1)
##
## Call:
## lm(formula = y ~ t)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.38309 -0.08569 0.00297 0.09984 0.38016
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.6677756 0.0349073 -19.13 <2e-16 ***
## t 0.0416992 0.0007134 58.45 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1585 on 82 degrees of freedom
## Multiple R-squared: 0.9766, Adjusted R-squared: 0.9763
## F-statistic: 3416 on 1 and 82 DF, p-value: < 2.2e-16
# (d) Residual + ACF
res1 <- resid(fit1)
plot(res1, type="l",
main="Residuals (Trend Only)")

acf(res1)

# (e) Regression with Quarter Dummies
fit2 <- lm(y ~ t + Q1dum + Q2dum + Q3dum + Q4dum - 1,
data=jj)
summary(fit2)
##
## Call:
## lm(formula = y ~ t + Q1dum + Q2dum + Q3dum + Q4dum - 1, data = jj)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.29318 -0.09062 -0.01180 0.08460 0.27644
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## t 0.0417930 0.0005648 74.00 <2e-16 ***
## Q1dum -0.6607215 0.0358430 -18.43 <2e-16 ***
## Q2dum -0.6325988 0.0362105 -17.47 <2e-16 ***
## Q3dum -0.5624905 0.0365829 -15.38 <2e-16 ***
## Q4dum -0.8312482 0.0369603 -22.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1254 on 79 degrees of freedom
## Multiple R-squared: 0.9935, Adjusted R-squared: 0.9931
## F-statistic: 2407 on 5 and 79 DF, p-value: < 2.2e-16
# (f) Residual + AFC
res2 <- resid(fit2)
plot(res2, type="l",
main="Residuals (With Dummies)")

acf(res2)

# (g) Fitted Values
jjfit <- fitted(fit2)
# Log scale
plot(t, y, type="l")
lines(t, jjfit, col="red", lwd=2)

# Original scale
plot(t, jj$Earnings, type="l")
lines(t, exp(jjfit), col="blue", lwd=2)

# (h) Annual Growth Rate
beta1 <- coef(fit2)["t"]
annual_growth <- (exp(4*beta1) - 1) * 100
annual_growth
## t
## 18.19577
#(i) Q3 --> Q4 Percent Change
b3 <- coef(fit2)["Q3dum"]
b4 <- coef(fit2)["Q4dum"]
(exp(b4 - b3) - 1) * 100
## Q4dum
## -23.56716
# Problem 2 (AR(1 Sim)
phi <- 0.5
sigma <- 2
n <- 1000
x <- rep(0,n)
x[1] <- rnorm(1,0,sqrt(sigma^2/(1-phi^2)))
for(i in 2:n){
x[i] <- phi*x[i-1] + rnorm(1,0,sigma)
}
acf(x)

# Problem 1 (Simulation)
set.seed(123)
phi <- 0.5
sigma <- 2
v20 <- rep(0,5000)
for(i in 1:5000){
w <- rnorm(20,0,sigma)
x <- rep(0,20)
x[1] <- w[1]
for(j in 2:20){
x[j] <- phi*x[j-1] + w[j]
}
v20[i] <- x[20]
}
hist(v20)

sd(v20)
## [1] 2.253355
sqrt(sigma^2/(1-phi^2))
## [1] 2.309401