Part 1: Filtering

1. Load in the data fred_qd.csv

fred_qd <- read.csv('fred_qd.csv')

2. Remove the first two and last two rows of your data frame.

fred <- fred_qd
fred <- fred[-(1:2),]

3. Create individual time series objects for each of: GDPC1, PCECC96, GPDIC1, GCEC1, EXPGSC1 - IMPGSC1

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")]

4. Consult the help documentation for the save() function. Use it to save your entire work environment to a filename of your choice for future use.

save.image("problem_set_2_environment.RData")

5 .Use the decompose() command on your GDP time series, creating a new list object to contain the output.

gdp_decomp <- decompose(gdp_ts)

(a) How many elements are in this list? (Hint: In the environment pane it will say List of…)

There are 6 elelements in the frame.

(b) What do each of the list components represent?

The list components are:

X - The original time series

Seasonal - the seasonal component

Trend - The trend component

Random - The random or irregular component

Figure - represents the GDP values over time

Type - expresses that the observed data is additive

(c) Interpret the list component titled “figure”. What do these numbers say about GDP relative to trend?

The “figure” component factors in the weight of the seasonal compenent for all 4 seasons.

(d) The trend and random components have NA (R’s notation for a missing value) as the first two and last two values. Why is that?

The decompose function calculates moving averages which need a full year of data to predict. I am assuming that the first two are missing the first two seasons or quarters of the year and the last two are missing the last two seasons or quarters of the year.

6. You can plot the data, trend, seasonal component, and noise by putting the list object in the plot() command. Do so.

(a) Use the title() command to make the subtitle Real US GDP

plot(gdp_decomp)
title(sub = "Real U.S. GDP", cex.sub = 1.4)

(b) The seasonal and random graph look much different than the x and trend graphs. There is a downward spike in the random graph as well in the year 2020.

7.Similarly decompose and plot PCECC96, GPDIC1, GCEC1, and EXPGSC1 - IMPGSC1 into respective list objects.

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")

Part 2: Hodrick-Prescott Filter

2. Load the mFilter package with the library() command.

library(mFilter)

3. Consult the R documentation for how to implement the hpfilter() function using λ= 1600 and no drift.

(a) Create new environment objects containing the output of the HP lter for each of: GDPC1, PCECC96, GPDIC1, GCEC1, EXPGSC1 - IMPGSC1

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)

(b) Plotting works the same way with these new objects as it did for the moving average environment objects. Plot each of the foive results from the HP filter.

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

What are the respective standard deviations of trend growth? Which ltering technique results in a smoother trend estimate?

The standard deviation of the first differences for the moving average trend using the decompostion filtered trend is 67.170138, while for the Hodrick-Prescott filtered trend, it is 32.9314156. The filtering technique that provides the smoothest trend is the Hodrick-Prescott filter since the standard deviation is the smallest.

Part 3: Forecasting

1. Use the acf() function to plot an autocorrelation gure for the random component of GDP decomposed by the moving average filter.

"acf(gdp_decomp$random)"
## [1] "acf(gdp_decomp$random)"

(a) You received an error. Why is that?

The error accored due to missing values.

(b) Now implement the acf() function in a way that corrects for this error (two options).

acf(na.omit(gdp_decomp$random), main = "ACF of GDP Decomposed Random")

2. Do the same with the other four variables of interest. Note any dierences in text in your R Markdown file.

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"

3. One question that might be of interest is whether changes in any of these four variables can predict future changes in real GDP. For instance does an increase in investment today signal creation of manufacturing machinery that will be used to increase production next quarter or year? We can explore this with cross-correlation:

(a) Use the ccf() function to compare the random component of GDP with each of the other four random components. If γk(x,y) = E[(xt+k−µx) (yt−µy)], then the rst input to the ccf() function is the x variable and the second is the y variable. For readability, I suggest making GDP the x variable.

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")

(b) Interpret the GDP and Investment cross-correlation plot. Do these results match your expectations?

Since the highest correlation is at lag 0 for investment GDP and Investment are expected to move together. This does match my expectations.

(c) Interpret the GDP and Government Expenditures plot. What do you think this means about fiscal policy?

Since the highest correlation is at lag -2 it suggests that higher GDP leads to more government spending in the future. I think what it means for fiscal policy is that it increases in response to GDP increasing so if GDP increases this quarter fiscal policy will follow in the next quarters.

4. Why should we interpret these results as indicative rather than causal?

We should view these results as indicative because just because they correlate with one another does not mean that they are causal. Even if they have have a high correlation we can not determine which one is causing the other. There also could variables missing “ommited variable bias”. We also do only have observational data where we have no control over the variables.

Part 4: Holt-Winters Method

1. R implements the Holt-Winters method with the HoltWinters() function and a time series input. Following the example on page 42 of The Little Book of R For Time Series, perform the Holt-Winters method on the original real US GDP (not decomposed) time series:

(a) Be sure to use additive seasonality.

log_gdp_forecasts <- log(gdp_ts)
gdpforcasts <- HoltWinters(log_gdp_forecasts, seasonal = "additive")

(b) Display the estimated smoothing parameters and root mean squared error ( 1TSSE, where SSE can be retrieved as on page 29).

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

(c) Plot the estimated components.

plot(gdpforcasts$fitted, main = "Estimated Components")

(d) Plot the fitted values against the observed values.

plot(gdpforcasts, main = "Fitted Against Observed")
legend("topleft", legend = c("Observed", "Fitted"), col = c("black", "red"), lty = 1, cex = 1)

(e) With your knowledge of calculus, what do you think it means that the estimated trend (i.e., slope) is increasing over time?

This implies that the trend or slope, which is also the first derivative, is increasing over time so both the first and second derivative is positive. Exponential?

2. Compare the additive seasonality specication to the multiplicative specication for real US GDP. Which model performs better? Think carefully about which metric to use to define better.

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

According to the standard deviation of the fitted variables the multiplicative is more reliable since the standard deviation is lower.

3. Let’s use the results to forecast future real US GDP!

(a) Install the forecast package and load it into your library

library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

(c) Create a new object to store the results of forecasting real US GDP ten years in the future (40 quarters) from the additive specication.

final_forecast <- forecast(gdpforcasts, h = 40)

(d) Luckily, the plot() function is smart enough to automatically handle this new object. Plot the forecasts now.

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?

The confidence bands get wider the further out in the forecast you go.