【问题标题】:Simulate 100 AR(2) time series模拟 100 个 AR(2) 时间序列
【发布时间】:2018-11-10 21:25:59
【问题描述】:

我需要 R 编程方面的帮助。

模拟 100 个 AR(2) 时间序列,样本大小为 n=50,e_t ~ N(0,1)。

【问题讨论】:

    标签: r time-series


    【解决方案1】:
    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))
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    使用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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多