The autocorrelations vary more in the smaller sample. As the sample size increases, the autocorrelations generally become closer to zero. All three plots are consistent with white noise because the correlations do not show a persistent pattern.
The limits become narrower as the sample size increases because the autocorrelation estimates become more precise. The estimated autocorrelations differ because each sample contains different random observations.
install.packages("fpp3")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
## âś” tibble 3.3.1 âś” tsibble 1.2.0
## âś” dplyr 1.2.1 âś” tsibbledata 0.4.1
## âś” tidyr 1.3.2 âś” ggtime 1.1.0
## âś” lubridate 1.9.5 âś” feasts 0.5.0
## âś” ggplot2 4.0.3 âś” fable 0.5.0
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## âś– lubridate::date() masks base::date()
## âś– dplyr::filter() masks stats::filter()
## âś– tsibble::intersect() masks base::intersect()
## âś– tsibble::interval() masks lubridate::interval()
## âś– dplyr::lag() masks stats::lag()
## âś– tsibble::setdiff() masks base::setdiff()
## âś– tsibble::union() masks base::union()
amazon <- gafa_stock |>
filter(Symbol == "AMZN") |>
arrange(Date) |>
mutate(Day = row_number()) |>
update_tsibble(index = Day, regular = TRUE)
amazon |>
autoplot(Close) +
labs(title = "Amazon Closing Stock Price")
amazon |>
gg_tsdisplay(Close, plot_type = "partial")
amazon |>
gg_tsdisplay(difference(Close), plot_type = "partial")
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
Amazon’s stock prices increase over time, so the original series is not
stationary. After taking the first difference, the trend is removed, but
the fluctuations become larger toward the end of the series. The ACF and
PACF show some significant correlations, suggesting that differencing
improves stationarity but does not completely remove the patterns in the
data.
turkey <- global_economy |>
filter(Country == "Turkey")
turkey |>
autoplot(GDP)
turkey |>
gg_tsdisplay(difference(log(GDP)), plot_type = "partial")
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
Turkey’s GDP increases over time, so the original series is not
stationary. I used a log transformation and first differencing to remove
the trend and stabilize the variance. The differenced series fluctuates
around a relatively constant level, and most ACF and PACF values fall
within the confidence limits. This suggests the transformed series is
reasonably stationary.
tasmania <- aus_accommodation |>
filter(State == "Tasmania") |>
summarise(Takings = sum(Takings))
tasmania |>
autoplot(Takings)
tasmania |>
features(Takings, guerrero)
tasmania |>
mutate(log_takings = log(Takings)) |>
features(log_takings, unitroot_nsdiffs)
tasmania |>
gg_tsdisplay(
difference(log(Takings), 4),
plot_type = "partial"
)
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_point()`).
tasmania |>
mutate(season_diff = difference(log(Takings), 4)) |>
features(season_diff, unitroot_ndiffs)
Tasmanian accommodation takings increase over time and show strong quarterly seasonality. I used a log transformation to stabilize the variance and seasonal differencing at lag 4 to remove the seasonal pattern. The test returned ndiffs = 0, indicating that no additional regular differencing was needed. The seasonally differenced series is reasonably stationary, although some autocorrelation remains.
souvenirs |>
autoplot(Sales)
souvenirs |>
features(Sales, guerrero)
souvenirs |>
mutate(log_sales = log(Sales)) |>
features(log_sales, unitroot_nsdiffs)
souvenirs |>
gg_tsdisplay(
difference(log(Sales), 12),
plot_type = "partial"
)
## Warning: Removed 12 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 12 rows containing missing values or values outside the scale range
## (`geom_point()`).
souvenirs |>
mutate(season_diff = difference(log(Sales), 12)) |>
features(season_diff, unitroot_ndiffs)
Monthly souvenir sales increase over time and show strong yearly seasonality. I used a log transformation to stabilize the variance and seasonal differencing at lag 12 to remove the seasonal pattern. The test returned ndiffs = 0, indicating that no additional regular differencing was needed. Some autocorrelation remains, but the series appears reasonably stationary after seasonal differencing.
set.seed(12345678)
retail <- aus_retail |>
filter(State == "Northern Territory")
retail <- retail |>
filter(Industry == sample(unique(Industry), 1))
retail |>
autoplot(Turnover)
retail |>
features(Turnover, guerrero)
retail |>
mutate(log_turnover = log(Turnover)) |>
features(log_turnover, unitroot_nsdiffs)
retail |>
mutate(log_turnover = log(Turnover)) |>
features(log_turnover, unitroot_ndiffs)
retail |>
gg_tsdisplay(
difference(log(Turnover)),
plot_type = "partial"
)
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
Retail turnover for takeaway food services in the Northern Territory generally increases over time. The Guerrero test returned a lambda of -0.0222, so I used a log transformation to stabilize the variance. The seasonal differencing test returned nsdiffs = 0, indicating that seasonal differencing was not needed. The regular differencing test returned ndiffs = 1, so I applied one regular difference. The resulting series fluctuates around zero without a clear trend, suggesting that it is reasonably stationary, although some autocorrelation remains.
set.seed(12345678)
y <- numeric(100)
e <- rnorm(100)
for (i in 2:100) {
y[i] <- 0.6 * y[i - 1] + e[i]
}
plot(y, type = "l", main = "AR(1) Simulation")
I simulated an AR(1) model with a coefficient of 0.6 and random errors. The series fluctuates around zero without a clear trend. Since the coefficient is positive, consecutive observations tend to be similar, creating short periods of persistence.
set.seed(12345678)
ar_positive <- arima.sim(list(ar = 0.6), n = 100)
ar_negative <- arima.sim(list(ar = -0.6), n = 100)
ar_strong <- arima.sim(list(ar = 0.9), n = 100)
plot(ar_positive, main = "AR(1): 0.6")
plot(ar_negative, main = "AR(1): -0.6")
plot(ar_strong, main = "AR(1): 0.9")
I simulated three AR(1) models using coefficients of 0.6, -0.6, and 0.9. The positive coefficients produce more persistent movements, while the negative coefficient causes the series to alternate more frequently. The model with a coefficient of 0.9 shows the strongest persistence, with longer periods of increases and decreases.
set.seed(12345678)
e <- rnorm(101)
y <- e[2:101] + 0.6 * e[1:100]
plot(y, type = "l", main = "MA(1) Simulation")
I simulated an MA(1) model with a coefficient of 0.6. The series fluctuates around zero without a clear trend. Since each observation depends on the current and previous random errors, the model shows short-term correlation, but the effects do not persist for many periods.
set.seed(12345678)
ma_positive <- arima.sim(list(ma = 0.6), n = 100)
ma_negative <- arima.sim(list(ma = -0.6), n = 100)
ma_strong <- arima.sim(list(ma = 0.9), n = 100)
plot(ma_positive, main = "MA(1): 0.6")
plot(ma_negative, main = "MA(1): -0.6")
plot(ma_strong, main = "MA(1): 0.9")
I simulated three MA(1) models using coefficients of 0.6, -0.6, and 0.9. The positive coefficients create positive short-term correlation, while the negative coefficient causes more alternating movements. The model with a coefficient of 0.9 has stronger short-term correlation, but unlike an AR model, the effects do not persist beyond one lag.
set.seed(12345678)
arma <- arima.sim(
model = list(ar = 0.6, ma = 0.6),
n = 100
)
plot(arma, main = "ARMA(1,1) Simulation")
I simulated an ARMA(1,1) model with AR and MA coefficients of 0.6. The series fluctuates around zero without a clear trend. Since the model combines past observations and past errors, it shows short-term fluctuations along with some persistence over time.
set.seed(12345678)
y <- numeric(100)
e <- rnorm(100)
for (i in 3:100) {
y[i] <- -0.8 * y[i - 1] +
0.3 * y[i - 2] + e[i]
}
ar2 <- y
plot(ar2, type = "l", main = "AR(2) Simulation")
I simulated an AR(2) model with coefficients of -0.8 and 0.3. The series starts with small fluctuations, but the oscillations become much larger over time. This indicates that the model is not stationary because the effects of previous observations grow instead of fading away.
plot(arma, main = "ARMA(1,1)", ylab = "Value")
plot(ar2, type = "l", main = "AR(2)", ylab = "Value")
The ARMA(1,1) model fluctuates around a relatively stable level, while the AR(2) model shows increasingly large oscillations over time. The ARMA(1,1) model is stationary, but the AR(2) model is unstable because its coefficients cause the fluctuations to grow rather than decrease.
aus_airpassengers |>
autoplot(Passengers)
fit_air <- aus_airpassengers |>
model(Auto = ARIMA(Passengers))
report(fit_air)
## Series: Passengers
## Model: ARIMA(0,2,1)
##
## Coefficients:
## ma1
## -0.8963
## s.e. 0.0594
##
## sigma^2 estimated as 4.308: log likelihood=-97.02
## AIC=198.04 AICc=198.32 BIC=201.65
The automatic ARIMA procedure selected an ARIMA(0,2,1) model for Australian air passenger numbers. The model uses second differencing to account for the upward trend and includes one moving average term with a coefficient of -0.8963. The model has an AICc of 198.32. I will check the residuals to determine whether the model adequately captures the patterns in the data.
fit_air |>
gg_tsresiduals()
fit_air |>
augment() |>
features(.innov, ljung_box, lag = 10, dof = 1)
The residuals fluctuate around zero without a clear trend, and the ACF values fall within the confidence limits. The Ljung–Box test returned a p-value of 0.6687, which is greater than 0.05. This indicates that there is no significant evidence of remaining autocorrelation, suggesting that the ARIMA(0,2,1) model adequately captures the patterns in the data.
fit_air |>
forecast(h = 10) |>
autoplot(aus_airpassengers)
The ARIMA(0,2,1) model forecasts continued growth in Australian air
passenger numbers over the next 10 years. The forecast increases
steadily, while the 80% and 95% prediction intervals become wider over
time. This shows that uncertainty increases as the forecast extends
further into the future.
fit_compare <- aus_airpassengers |>
model(
Auto = ARIMA(Passengers),
ARIMA110 = ARIMA(Passengers ~ 1 + pdq(1,1,0)),
ARIMA011 = ARIMA(Passengers ~ 1 + pdq(0,1,1)),
ARIMA212 = ARIMA(Passengers ~ 1 + pdq(2,1,2))
)
fit_compare |>
glance() |>
select(.model, AICc)
I compared four ARIMA models using AICc. The automatic ARIMA(0,2,1) model had the lowest AICc of 198.32, compared with 202.86 for ARIMA(1,1,0), 202.86 for ARIMA(0,1,1), and 206.61 for ARIMA(2,1,2). This suggests that ARIMA(0,2,1) is the best choice among the models tested. The residual diagnostics also showed no significant remaining autocorrelation.
usa <- global_economy |>
filter(Country == "United States")
usa |>
autoplot(GDP)
usa |>
features(GDP, guerrero)
usa |>
mutate(log_gdp = log(GDP)) |>
features(log_gdp, unitroot_ndiffs)
usa |>
gg_tsdisplay(
difference(log(GDP), differences = 2),
plot_type = "partial"
)
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
US GDP increases substantially over time, making the original series non-stationary. I applied a log transformation and checked the number of differences needed. The test returned ndiffs = 2. After second differencing, the series fluctuates around zero without a clear trend, and most ACF values fall within the confidence limits. This suggests that the transformed series is reasonably stationary.
fit_usa <- usa |>
model(
Auto = ARIMA(log(GDP)),
ARIMA011 = ARIMA(log(GDP) ~ 1 + pdq(0,1,1)),
ARIMA110 = ARIMA(log(GDP) ~ 1 + pdq(1,1,0)),
ARIMA021 = ARIMA(log(GDP) ~ pdq(0,2,1))
)
fit_usa |>
glance() |>
select(.model, AICc)
I compared four ARIMA models using AICc. The automatic ARIMA model and ARIMA(0,2,1) both had the lowest AICc of -275.30. ARIMA(1,1,0) had an AICc of -274.15, while ARIMA(0,1,1) had an AICc of -262.49. Based on these results, ARIMA(0,2,1) appears to be the best model among those tested.
report(fit_usa |> select(Auto))
## Series: GDP
## Model: ARIMA(0,2,1)
## Transformation: log(GDP)
##
## Coefficients:
## ma1
## -0.6353
## s.e. 0.1138
##
## sigma^2 estimated as 0.0004278: log likelihood=139.76
## AIC=-275.53 AICc=-275.3 BIC=-271.48
fit_usa |>
select(Auto) |>
gg_tsresiduals()
fit_usa |>
select(Auto) |>
augment() |>
features(.innov, ljung_box, lag = 10, dof = 1)
The Ljung–Box test returned a p-value of 0.9856, which is greater than 0.05. This indicates that there is no significant evidence of remaining autocorrelation in the residuals. Based on this test, the ARIMA model appears to capture the time dependence in US GDP reasonably well.
fit_usa |>
select(Auto) |>
forecast(h = 10) |>
autoplot(usa |> select(-Country))
The ARIMA(0,2,1) model forecasts continued growth in US GDP over the next 10 years. By 2027, the forecast reaches approximately $28 trillion. The 80% and 95% prediction intervals widen over time, indicating greater uncertainty in longer-term forecasts. The model also passed the Ljung–Box test, suggesting that it adequately captures the time dependence in the data.