Prolem 1
set.seed(35)
series <- arima.sim(n = 48, list(ar = 0.7))
acf_vals <- acf(series, plot = FALSE)$acf
r1 <- acf_vals[2]
r5 <- acf_vals[6]
cat("Sample r1:", r1, "\n")
Sample r1: 0.6713193
cat("Sample r5:", r5, "\n")
Sample r5: 0.3137493
set.seed(7)
series2 <- arima.sim(n = 48, list(ar = 0.7))
acf_vals2 <- acf(series2, plot = FALSE)$acf
r1_2 <- acf_vals2[2]
r5_2 <- acf_vals2[6]
cat("New Sample r1:", r1_2, "\n")
New Sample r1: 0.6512813
cat("New Sample r5:", r5_2, "\n")
New Sample r5: 0.1028463
(
set.seed(79)
n <- 48
phi <- 0.7
M <- 10000
r1_vec <- numeric(M)
r5_vec <- numeric(M)
for (i in 1:M) {
x <- arima.sim(n = n, list(ar = phi))
acf_x <- acf(x, plot = FALSE)$acf
r1_vec[i] <- acf_x[2]
r5_vec[i] <- acf_x[6]
}
# Sample variances
var_r1 <- var(r1_vec)
var_r5 <- var(r5_vec)
cat("Sample var r1:", var_r1, "\n")
Sample var r1: 0.01349169
cat("Sample varr5:", var_r5, "\n")
Sample varr5: 0.03350617
hist(r5_vec, breaks = 50, main = "Sampling Distribution of r5", xlab = "r5")
hist(r1_vec, breaks = 50, main = "Sampling Distribution of r1", xlab = "r1")
Problem 2
set.seed(35)
series <- arima.sim(n = 60, list(ma = -0.5))
acf_vals <- acf(series, plot = FALSE)$acf
r1 <- acf_vals[2]
cat("Sample r1:", r1, "\n")
Sample r1: -0.4104911
set.seed(7)
series2 <- arima.sim(n = 60, list(ma = -0.5))
acf_vals2 <- acf(series2, plot = FALSE)$acf
r1_2 <- acf_vals2[2]
cat("New Sample r1:", r1_2, "\n")
New Sample r1: -0.3328427
set.seed(79)
n <- 60
M <- 10000
r1_vec <- numeric(M)
for (i in 1:M) {
x <- arima.sim(n = n, list(ma = -0.5))
acf_x <- acf(x, plot = FALSE)$acf
r1_vec[i] <- acf_x[2]
}
var_r1_sample <- var(r1_vec)
cat("Sample Var(r1):", var_r1_sample, "\n")
Sample Var(r1): 0.009928472
# plot
hist(r1_vec, breaks = 50, main = "Sampling Distribution of r1", xlab = "r1")
Problem 10
library(TSA)
警告: 程序包‘TSA’是用R版本4.4.3 来建造的
载入程序包:‘TSA’
The following objects are masked from ‘package:stats’:
acf, arima
The following object is masked from ‘package:utils’:
tar
data(larain)
qqnorm(larain, main = "QQ-Plot of LA Rainfall")
qqline(larain)
library(MASS)
lambdas <- c(-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1)
par(mfrow = c(3, 3))
for (lambda in lambdas) {
if (lambda == 0) {
y_trans <- log(larain)
} else {
y_trans <- (larain^lambda - 1) / lambda
}
qqnorm(y_trans, main = paste("QQ-Plot (λ =", lambda, ")"))
qqline(y_trans)
}
y_best <- log(larain)
plot(y_best[-1], y_best[-length(y_best)], xlab = expression(Y[t-1]), ylab = expression(Y[t]),
main = "Lag Plot of Transformed Data")
library(TSA)
eacf(log(larain))
AR/MA
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 o o o o o o o o o o o o o o
1 o o o o o o o o o o o o o o
2 x x o o o o o o o o o o o o
3 x o o o o o o o o o o o o o
4 x o o o o o o o o o o o o o
5 x x x x x o o o o o o o o o
6 x x o o x o o o o o o o o o
7 x o x o o o o o o o o o o o