fred_qd <- read.csv('fred_qd.csv')
fred <- fred_qd
fred <- fred[-(1:2),]
timeseries <- ts(fred[,-1],start=c(1959,1),frequency = 4)
gdp_ts <- timeseries[,("GDPC1")]
consumption_ts <- timeseries[,("PCECC96")]
investment_ts <- timeseries[,("GPDIC1")]
government_ts <- timeseries[,("GCEC1")]
fred$NX <- (fred$EXPGSC1-fred$IMPGSC1)
timeseries <- ts(fred[,-1],start=c(1959,1),frequency = 4)
netexports_ts <- timeseries[,("NX")]
save.image("problem_set_2_environment.RData")
gdp_decomp <- decompose(gdp_ts)
plot(gdp_decomp)
title(sub = "Real U.S. GDP", cex.sub = 1.4)
consumption_decomp <- decompose(consumption_ts)
investment_decomp <- decompose(investment_ts)
government_decomp <- decompose(government_ts)
nx_decomp <- decompose(netexports_ts)
plot(consumption_decomp)
title(sub = "Consumption")
plot(investment_decomp)
title(sub = "Investment")
plot(government_decomp)
title(sub = "Goverment Spending")
plot(nx_decomp)
title(sub = "Net Exports")
library(mFilter)
gdp_hp <- hpfilter(gdp_ts, freq=1600, type = c("lambda","frequency"), drift=FALSE)
cons_hp <- hpfilter(consumption_ts, freq=1600, type = c("lambda","frequency"), drift=FALSE)
inv_hp <- hpfilter(investment_ts, freq=1600, type = c("lambda","frequency"), drift=FALSE)
gov_hp <- hpfilter(government_ts, freq=1600, type = c("lambda","frequency"), drift=FALSE)
nx_hp <- hpfilter(netexports_ts, freq=1600, type = c("lambda","frequency"), drift=FALSE)
plot(gdp_hp)
plot(cons_hp)
plot(inv_hp)
plot(gov_hp)
plot(nx_hp)
gdp_trends <- ts.union(gdp_decomp$trend,gdp_hp$trend)
gdp_cycles <- ts.union((gdp_decomp$seasonal+gdp_decomp$random),gdp_hp$cycle)
par(mfrow = c(1,2))
ts.plot(gdp_trends, col = c("blue", "red"))
legend("topleft", legend = c("Moving Average Trend", "HP-Filtered Trend"), col = c("blue", "red"), lty = 1, cex = 0.8, bty="n")
ts.plot(gdp_cycles, col = c("blue", "red"))
legend("bottomleft", legend = c("Moving Average Trend", "HP-Filtered Trend"), col = c("blue", "red"), lty = 1, cex = 0.8, bty="n")
decomp_trend_diff <- diff(gdp_decomp$trend)
hp_trend_diff <- diff(gdp_hp$trend)
sd_decomp <- sd(decomp_trend_diff, na.rm = TRUE)
sd_hp <- sd(hp_trend_diff, na.rm = TRUE)
print(sd_decomp)
## [1] 67.17014
print(sd_hp)
## [1] 32.93142
"acf(gdp_decomp$random)"
## [1] "acf(gdp_decomp$random)"
acf(na.omit(gdp_decomp$random), main = "ACF of GDP Decomposed Random")
acf(na.omit(consumption_decomp$random), main = "ACF of Consumption Decomposed Random")
"Consumption has more autocorrelation from lag 1 to 3, and about the same from 0 to 1"
## [1] "Consumption has more autocorrelation from lag 1 to 3, and about the same from 0 to 1"
acf(na.omit(investment_decomp$random), main = "ACF of Investment Decomposed Random")
"Investment has more autocorrelation throughout the lag from 1 to 6, and about the same from 0 to 1."
## [1] "Investment has more autocorrelation throughout the lag from 1 to 6, and about the same from 0 to 1."
acf(na.omit(government_decomp$random), main = "ACF of Government Decomposed Random")
"Government has larger autocorrelation throughout the graph as well even more than investment from 1 to 6 about the same from 0 to 1"
## [1] "Government has larger autocorrelation throughout the graph as well even more than investment from 1 to 6 about the same from 0 to 1"
acf(na.omit(nx_decomp$random), main = "ACF of Net Exports Decomposed Random")
"Net exports has the most autocorrelation throughout the graph from lag 1 to 6 and almost the same but a little less in 0 to 1"
## [1] "Net exports has the most autocorrelation throughout the graph from lag 1 to 6 and almost the same but a little less in 0 to 1"
ccf(na.omit(gdp_decomp$random), na.omit(consumption_decomp$random), main = "CCF: GDP and Investment", ylab = "Cross Correlation")
ccf(na.omit(gdp_decomp$random), na.omit(investment_decomp$random), main = "CCF: GDP and Consumption", ylab = "Cross Correlation")
ccf(na.omit(gdp_decomp$random), na.omit(government_decomp$random), main = "CCF: GDP and Government spending", ylab = "Cross Correlation")
ccf(na.omit(gdp_decomp$random), na.omit(nx_decomp$random), main = "CCF: GDP and Net Exports", ylab = "Cross Correlation")
log_gdp_forecasts <- log(gdp_ts)
gdpforcasts <- HoltWinters(log_gdp_forecasts, seasonal = "additive")
gdpforcasts
## Holt-Winters exponential smoothing with trend and additive seasonal component.
##
## Call:
## HoltWinters(x = log_gdp_forecasts, seasonal = "additive")
##
## Smoothing parameters:
## alpha: 0.9251364
## beta : 0.01530789
## gamma: 1
##
## Coefficients:
## [,1]
## a 10.0571516033
## b 0.0061558913
## s1 0.0029953979
## s2 -0.0009339552
## s3 -0.0049402847
## s4 0.0033522621
gdpforcasts$SSE
## [1] 0.03190969
plot(gdpforcasts$fitted, main = "Estimated Components")
plot(gdpforcasts, main = "Fitted Against Observed")
legend("topleft", legend = c("Observed", "Fitted"), col = c("black", "red"), lty = 1, cex = 1)
gdpforcasts_mult <- HoltWinters(log_gdp_forecasts, seasonal = "multiplicative")
gdp_add_diff <- diff(gdpforcasts$fitted)
sd_gdp_add <- sd(gdp_add_diff, na.rm = TRUE)
gdp_mult_diff <- diff(gdpforcasts_mult$fitted)
sd_gdp_mult <- sd(gdp_mult_diff, na.rm = TRUE)
sd_gdp_add
## [1] 0.00885269
sd_gdp_mult
## [1] 0.008594893
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
final_forecast <- forecast(gdpforcasts, h = 40)
plot(final_forecast, main = "US Real GDP Forecast")
## (e) What do you notice about the condence bands (80% - dark gray and
95% - light gray) as you forecast further out?