【发布时间】:2018-11-10 21:25:59
【问题描述】:
我需要 R 编程方面的帮助。
模拟 100 个 AR(2) 时间序列,样本大小为 n=50,e_t ~ N(0,1)。
【问题讨论】:
标签: r time-series
我需要 R 编程方面的帮助。
模拟 100 个 AR(2) 时间序列,样本大小为 n=50,e_t ~ N(0,1)。
【问题讨论】:
标签: r time-series
library(FitAR)
set.seed(54321)
n=50
phi <- c(0.1,0.5)
count <- 0
for(i in 1:100){
yt <- unclass(arima.sim(n=n,list(ar=phi),innov=rnorm(n,0,1)))
p=SelectModel(as.ts(yt), lag.max = 20, Criterion = "BIC", Best=1)
fit.monthly <- arima(yt, order = c(p, 0, 0))
my_coefficients =fit.monthly$coef
my_coefficients=my_coefficients[!names(my_coefficients) == 'intercept']
print(my_coefficients)
if(length(my_coefficients) == 2){
count <- count + 1
}
}
print(paste0("AR(2) model count is: ", count))
【讨论】:
ts.extend包中的rGARMA函数您可以使用 ts.extend 包从任何固定高斯 ARMA 模型生成随机向量。这个包使用计算的随机向量的自相关矩阵直接从多元正态分布生成随机向量,因此它从精确分布中给出随机向量,并且不需要“老化”迭代。这是来自 AR(2) 模型的示例。
#Load the package
library(ts.extend)
#Set parameters
AR <- c(0.9, -0.2)
m <- 50
#Generate n = 100 random vectors from this model
set.seed(1)
SERIES <- rGARMA(n = 100, m = m, ar = AR, errorvar = 1)
#Plot the series using ggplot2 graphics
library(ggplot2)
plot(SERIES)
【讨论】: