df <- read_excel("Chapter7_Exercises_Data.xls", sheet=1)
setnames(df, c('date', 'unemployed'))
ggplot(df, aes(x=date)) +
geom_line(aes(y=unemployed), color='green') +
ggtitle('Unemployed Persons', '1989-2012') +
ylab('Unemployed (thousands)') +
xlab('Date')
df_acf <- acf(df$unemployed, main="Unemployment - ACF")
df_pacf <- pacf(df$unemployed, main="Unemployment - PACF")
While the ACF and PACF plots suggest an AR(1) model due to the high spike at lag 1 and the quick decay afterwards, the persistence parameter being very close to 1 indicates the series is not stationary so an autoregressive model might not be suitable to capture the dependence in this data.
ar_coef <- c(0.3, 0.7)
n <- 100
innovations <- rnorm(n, mean = 0, sd = 1)
initial_values <- c(0, 0)
ar_simulated <- stats::filter(innovations, filter = ar_coef, method = "recursive")
ar_simulated <- c(initial_values, ar_simulated)
plot(ar_simulated, type = "l")
acf(ar_simulated)
pacf(ar_simulated)
ar.sim<-arima.sim(model=list(ar=c(-0.3,-0.7)),n=100, sd = 1)
par(mfrow=c(3,1))
plot(ar.sim)
acf(ar.sim)
pacf(ar.sim)
The first model exhibits non-stationarity in its covariance, while the second model demonstrates covariance stationarity. In the first model, the autocorrelation function gradually decreases but becomes statistically significant after the seventeenth lag.
In contrast, the second model displays an irregular pattern in its autocorrelation, with most lags being statistically insignificant. This applies to both the ACF and the partial autocorrelation function (PACF). The PACF’s autocorrelations become statistically insignificant quickly after the second lag, suggesting that the model’s autocorrelations are primarily due to short-term dependencies.
p3 <- read_excel("Chapter7_Exercises_Data.xls", sheet = "Exercise 6")
names(p3)<- c ("date", "housing", "housing_inflation", "transportation", "transportation_inflation")
p3 <- na.omit(p3)
housing_inflation_ts <- ts(p3$housing_inflation, start = 1968, end= 2011, freq = 1)
transportation_inflation_ts <- ts(p3$transportation_inflation, start = 1968, end= 2011, freq = 1)
#Plot
ts.plot(housing_inflation_ts)
ts.plot(transportation_inflation_ts)
acf(housing_inflation_ts, main = "Housing Inflation ACF")
pacf(housing_inflation_ts, main = "Housing Inflation PACF")
acf(transportation_inflation_ts, main = "Transportation Inflation ACF")
pacf(transportation_inflation_ts, main = "Transportation Inflation PACF")
The PACF of housing inflation rates exhibits two spikes at lags 1 and 2, with the ACF decaying to zero, suggesting an AR(2) process. On the other hand, the transportation inflation rate PACF shows only one spike at lag 1, indicating an AR(1) process instead.
#import data
ex8 <- read_excel("Chapter7_exercises_data.xls", sheet = "Exercise 8")
colnames(ex8) <- c('date', 'cpi_all_item', 'inflation_all_item', 'cpi_less_food_and_energy', 'inflation_less_food_and_energy')
#convert to time series
inflation1_ts <- ts(ex8$inflation_all_item, start = 1958, end= 2011, freq = 1)
inflation2_ts <- ts(ex8$inflation_less_food_and_energy, start = 1958, end= 2011, freq = 1)
#compare inflation time series
tsdisplay(inflation1_ts, main = "Inflation All Item Time Series")
tsdisplay(inflation2_ts, main = "Inflation Less Food and Energy Time Series")
The ACF/PACF plot for “inflation less food and energy” exhibits spikes exceeding the confidence intervals. This means the time series values are more strongly correlated with their past values. Therefore, “inflation less food and energy” becomes easier to predict compared to “inflation all item,” which likely has a weaker relationship with its past values.
inflation1_ts <- ts(ex8$inflation_all_item, start = 1958, end= 2011, freq = 1)
inflation2_ts <- ts(ex8$inflation_less_food_and_energy, start = 1958, end= 2011, freq = 1)
arima_model1 <- auto.arima(inflation1_ts)
arima_model2 <- auto.arima(inflation2_ts)
forecast_1step_1 <- forecast(arima_model1, h = 1)
forecast_2step_1 <- forecast(arima_model1, h = 2)
forecast_1step_2 <- forecast(arima_model2, h = 1)
forecast_2step_2 <- forecast(arima_model2, h = 2)
print("1-step forecast for inflation_all_item")
## [1] "1-step forecast for inflation_all_item"
plot(forecast_1step_1)
print("2-step forecast for inflation_all_item")
## [1] "2-step forecast for inflation_all_item"
plot(forecast_2step_1)
print("1-step forecast for inflation_less_food_and_energy:")
## [1] "1-step forecast for inflation_less_food_and_energy:"
plot(forecast_1step_2)
print("2-step forecast for inflation_less_food_and_energy:")
## [1] "2-step forecast for inflation_less_food_and_energy:"
plot(forecast_2step_2)