options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("forecast", repos = "https://cloud.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/d_/mt52wgnd2rnbdnw9k1hp2t5r0000gn/T//RtmpVALmUH/downloaded_packages

#3.2

# Load the required library for ACF and PACF
library(forecast)
## Warning: package 'forecast' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Set the seed for reproducibility
set.seed(1)

# Simulate the ARMA process for Model (a)
sim_data_a <- arima.sim(n = 1000, model = list(ar = c(-0.2, 0.48)), sd = 1)

# Plot ACF
acf_plot <- acf(sim_data_a, main="ACF for Model (a)", lag.max = 40, plot = TRUE)

# Plot PACF
pacf_plot <- pacf(sim_data_a, main="PACF for Model (a)", lag.max = 40, plot = TRUE)

# Calculate ACF and PACF values without plotting
acf_values <- acf(sim_data_a, plot = FALSE)
pacf_values <- pacf(sim_data_a, plot = FALSE)

# Print formatted output for ACF and PACF values
cat("INFO: ACF/PACF: New Univariate Project\n ITSM: : (ACF/PACF)\n # of Lags = 40\n  Model Autocorrelations:\n")
## INFO: ACF/PACF: New Univariate Project
##  ITSM: : (ACF/PACF)
##  # of Lags = 40
##   Model Autocorrelations:
cat("Gamma (0) = ", acf_values$acf[1], "\n")
## Gamma (0) =  1
for (i in 1:length(acf_values$acf)) {
  cat(ifelse(i == 1, "", " "), format(acf_values$acf[i], digits = 4), "\n")
}
##  1 
##   -0.3899 
##   0.4916 
##   -0.2586 
##   0.2093 
##   -0.1075 
##   0.05155 
##   -0.01035 
##   0.01202 
##   0.04416 
##   -0.02327 
##   0.06871 
##   -0.08588 
##   0.101 
##   -0.1045 
##   0.1399 
##   -0.1128 
##   0.0973 
##   -0.132 
##   0.08122 
##   -0.08191 
##   0.04222 
##   -0.067 
##   0.01841 
##   -0.02498 
##   -0.02301 
##   0.02366 
##   -0.05995 
##   0.0638 
##   -0.07403 
##   0.07644
# Print formatted output for PACF values
cat("\nModel Partial Autocorrelations:\n")
## 
## Model Partial Autocorrelations:
for (i in 1:length(pacf_values$acf)) {
  cat(format(pacf_values$acf[i], digits = 4), "\n")
}
## -0.3899 
## 0.4005 
## 0.01751 
## -0.06324 
## 0.03961 
## -0.03663 
## 0.0166 
## 0.03196 
## 0.04508 
## -0.008377 
## 0.03448 
## -0.06272 
## 0.03332 
## -0.01174 
## 0.06411 
## -0.01094 
## -0.02923 
## -0.07806 
## 0.0002443 
## 0.01791 
## -0.01105 
## -0.04543 
## -0.01751 
## 0.0009184 
## -0.0334 
## 0.02164 
## -0.01636 
## 0.01732 
## -0.006067 
## 0.00467
# Generate and plot ACF/PACF for Model (c)
set.seed(1)
sim_data_c <- arima.sim(n = 1000, model = list(ar = c(-0.6), ma = c(-1.2)), sd = 1)

# ACF and PACF calculations
acf_result_c <- acf(sim_data_c, plot = FALSE, lag.max = 40)
pacf_result_c <- pacf(sim_data_c, plot = FALSE, lag.max = 40)

# ACF and PACF plots
acf(sim_data_c, main="ACF for Model (c)", lag.max = 40)

pacf(sim_data_c, main="PACF for Model (c)", lag.max = 40)

# Custom formatted output for ACF and PACF values for Model (c)
cat("INFO: ACF/PACF: New Univariate Project\n ITSM: : (ACF/PACF)\n # of Lags = 40\n Model Autocorrelations:\n")
## INFO: ACF/PACF: New Univariate Project
##  ITSM: : (ACF/PACF)
##  # of Lags = 40
##  Model Autocorrelations:
cat("Gamma (0) = ", acf_result_c$acf[1], "\n")
## Gamma (0) =  1
for (i in seq_along(acf_result_c$acf)) {
  cat(i - 1, formatC(acf_result_c$acf[i], format = "f", digits = 4), "\n")
}
## 0 1.0000 
## 1 -0.7894 
## 2 0.4498 
## 3 -0.2299 
## 4 0.0788 
## 5 0.0236 
## 6 -0.0801 
## 7 0.0920 
## 8 -0.0822 
## 9 0.0707 
## 10 -0.0624 
## 11 0.0725 
## 12 -0.0906 
## 13 0.1013 
## 14 -0.1191 
## 15 0.1335 
## 16 -0.1230 
## 17 0.1164 
## 18 -0.1177 
## 19 0.0953 
## 20 -0.0636 
## 21 0.0499 
## 22 -0.0487 
## 23 0.0387 
## 24 -0.0159 
## 25 -0.0132 
## 26 0.0477 
## 27 -0.0814 
## 28 0.1021 
## 29 -0.1084 
## 30 0.0972 
## 31 -0.0670 
## 32 0.0432 
## 33 -0.0321 
## 34 0.0146 
## 35 0.0010 
## 36 0.0021 
## 37 -0.0227 
## 38 0.0370 
## 39 -0.0336 
## 40 0.0156
cat("\nModel Partial Autocorrelations:\n")
## 
## Model Partial Autocorrelations:
for (i in seq_along(pacf_result_c$acf)) {
  cat(i - 1, formatC(pacf_result_c$acf[i], format = "f", digits = 4), "\n")
}
## 0 -0.7894 
## 1 -0.4600 
## 2 -0.2514 
## 3 -0.2659 
## 4 -0.1338 
## 5 -0.1459 
## 6 -0.1237 
## 7 -0.1230 
## 8 -0.0801 
## 9 -0.1012 
## 10 0.0051 
## 11 -0.0477 
## 12 0.0103 
## 13 -0.0913 
## 14 -0.0263 
## 15 -0.0328 
## 16 0.0732 
## 17 0.0074 
## 18 -0.0091 
## 19 -0.0362 
## 20 0.0289 
## 21 -0.0182 
## 22 -0.0109 
## 23 0.0021 
## 24 -0.0337 
## 25 0.0139 
## 26 -0.0340 
## 27 -0.0039 
## 28 -0.0187 
## 29 -0.0311 
## 30 0.0002 
## 31 0.0212 
## 32 0.0150 
## 33 -0.0136 
## 34 -0.0096 
## 35 0.0199 
## 36 -0.0367 
## 37 -0.0228 
## 38 -0.0148 
## 39 -0.0483
# Generate and plot ACF/PACF for Model (d)
set.seed(1)
sim_data_d <- arima.sim(n = 1000, model = list(ar = c(-1.8, -0.81)), sd = 1)

# Plot ACF
acf(sim_data_d, main="ACF for Model (d)", lag.max = 40)

# Plot PACF
pacf(sim_data_d, main="PACF for Model (d)", lag.max = 40)

# Calculate ACF and PACF values without plotting
acf_values_d <- acf(sim_data_d, plot = FALSE, lag.max = 40)
pacf_values_d <- pacf(sim_data_d, plot = FALSE, lag.max = 40)

# Print formatted output for ACF and PACF values for Model (d)
cat("INFO: ACF/PACF: New Univariate Project for Model (d)\n ITSM: : (ACF/PACF)\n # of Lags = 40\n Model Autocorrelations for Model (d):\n ")
## INFO: ACF/PACF: New Univariate Project for Model (d)
##  ITSM: : (ACF/PACF)
##  # of Lags = 40
##  Model Autocorrelations for Model (d):
## 
cat("Gamma (0) = ", acf_values_d$acf[1], "\n")
## Gamma (0) =  1
for (i in seq_along(acf_values_d$acf)) {
  cat(i - 1, formatC(acf_values_d$acf[i], format = "f", digits = 4), "\n")
}
## 0 1.0000 
## 1 -0.9885 
## 2 0.9590 
## 3 -0.9157 
## 4 0.8623 
## 5 -0.8020 
## 6 0.7378 
## 7 -0.6719 
## 8 0.6060 
## 9 -0.5411 
## 10 0.4782 
## 11 -0.4181 
## 12 0.3615 
## 13 -0.3093 
## 14 0.2619 
## 15 -0.2198 
## 16 0.1835 
## 17 -0.1528 
## 18 0.1274 
## 19 -0.1074 
## 20 0.0922 
## 21 -0.0810 
## 22 0.0731 
## 23 -0.0683 
## 24 0.0657 
## 25 -0.0643 
## 26 0.0634 
## 27 -0.0622 
## 28 0.0600 
## 29 -0.0563 
## 30 0.0507 
## 31 -0.0433 
## 32 0.0344 
## 33 -0.0244 
## 34 0.0130 
## 35 -0.0010 
## 36 -0.0116 
## 37 0.0244 
## 38 -0.0374 
## 39 0.0506 
## 40 -0.0637
cat("\nModel Partial Autocorrelations for Model (d):\n")
## 
## Model Partial Autocorrelations for Model (d):
for (i in seq_along(pacf_values_d$acf)) {
  cat(formatC(pacf_values_d$acf[i], format = "f", digits = 4), "\n")
}
## -0.9885 
## -0.7941 
## 0.0063 
## -0.0160 
## -0.0426 
## -0.0001 
## -0.0031 
## -0.0278 
## 0.0196 
## 0.0038 
## -0.0002 
## 0.0306 
## -0.0278 
## -0.0005 
## -0.0273 
## 0.0527 
## 0.0463 
## 0.0209 
## -0.0435 
## -0.0288 
## 0.0166 
## 0.0182 
## -0.0189 
## -0.0450 
## 0.0161 
## -0.0309 
## 0.0139 
## -0.0384 
## 0.0020 
## -0.0179 
## -0.0083 
## 0.0095 
## -0.0005 
## -0.0516 
## -0.0445 
## -0.0302 
## -0.0085 
## -0.0358 
## 0.0184 
## 0.0240

#3.3

# Compute first six psi coefficients for Model (a)
psi_coefs_a <- ARMAtoMA(ar = c(-0.2, 0.48), ma = numeric(0), 6)
print(psi_coefs_a)
## [1] -0.200000  0.520000 -0.200000  0.289600 -0.153920  0.169792
# Compute first six psi coefficients for Model (c)
psi_coefs_c <- ARMAtoMA(ar = c(-0.6), ma = c(-1.2), 6)
print(psi_coefs_c)
## [1] -1.800000  1.080000 -0.648000  0.388800 -0.233280  0.139968
# Compute first six psi coefficients for Model (d)
psi_coefs_d <- ARMAtoMA(ar = c(-1.8, -0.81), ma = numeric(0), 6)
print(psi_coefs_d)
## [1] -1.800000  2.430000 -2.916000  3.280500 -3.542940  3.720087