本文介绍了我想用不同的样本大小 n、不同的 SD 值和不同的 theta 值模拟一阶 MA(1) 的移动平均过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 mean = 0 但变化来模拟一些时间序列数据:在数学上,一阶移动平均过程,MA(1)表示为

I want to simulate some time series data with mean = 0 but varying:Mathematically, moving average process of order one, MA(1) is presented as

$$x_t=\mu+\varepsilon_{t}+\theta_{1}\varepsilon_{t-1}$$

$x_t$ 是 MA(1) 过程$\mu$ 是在我的情况下可以为零的平均值(就像回归方程中的截距一样)$\varepsilon_{t}$ 是误差项$\theta_{1}$ 是一个需要指定的常量(在我的例子中,一个介于 +-1 之间的不同数字).示例:在 $x=a+b*x_{i}$ 的简单回归方程中,$theta$ 就像 $b$

$x_t$ is the MA(1) process$\mu$ is the mean which can be zero in my case (just like intercept in regression equation)$\varepsilon_{t}$ is the error term$\theta_{1}$ is a constant which need be specified (in my case, a varying number in between +-1). Example: in simple regression equation of $x=a+b*x_{i}$, $theta$ is like the $b$

  1. 数字 N = 15、20、30、50、100、200.

标准偏差SD=1、4、9、16、25.

和 theta 值 \theta = +-0.2, +-0.4, +-0.6, +-0.8, +-0.9, +-0.95, +-0.99

set.seed(123)
# here I am only using first sample size 15
n <- 15
# white noise:
wnsd1<-ts(rnorm(n, mean=0, sd=1^2))
wnsd4<-ts(rnorm(n, mean=0, sd=2^2))
wnsd9<-ts(rnorm(n, mean=0, sd=3^2))
wnsd16<-ts(rnorm(n, mean=0, sd=4^2))
wnsd25<-ts(rnorm(n, mean=0, sd=5^2))
# initialise the first two values:
ma1 <- wnsd1[1:2]
# loop through and create the 3:15th values:
for(i in 3:n){
# here I only use when SD=1
ma1[i] <- wnsd1[i - 1]  * 0.2 + wnsd1[i]
}

  #turn them into time series, and for the last two, "integrate" them via cumulative sum
  ma1 <- ts(ma1)

我想要一种成熟的方法来改变样本大小 N、标准差 SD 和 MA(1) \theta 的估计

I want a mature way of varying the sample size N, the standard deviation SD and the estimate of MA(1) \theta

推荐答案

这是一个好的方法.请注意,我不知道 phi 是如何使用的,因为它没有在代码中明确显示.如果你修改你的代码,我会尝试解决它.​​

Here's an OK way. Note, I do not know how phi is used as it wasn't explicitly in the code. If you modify your code, I would try to address it.

N <- c(15L, 20L)
SD = c(1, 2)^2
phi = c(0.2, 0.4)

set.seed(123)
res <- lapply(N,
      function(n)
           lapply(SD,
                function(s.d.) {
                  wn <- ts(rnorm(n, 0, s.d.))
                  ar1 <- ma1 <- arma11 <- arma22 <- vector('numeric', n)
                  ar1 <- ma1 <- arma11 <- arma22 <- wn[1:2]

                  for (i in 3:n) {
                    ar1[i]      <- ar1[i - 1] * 0.2 + wn[i]
                    ma1[i]      <- wn[i - 1]  * 2.8000 + wn[i]
                    arma11[i]   <- arma11[i - 1] * 0.2 + wn[i - 1] * 2.80003769654 + wn[i]
                    arma22[i]   <- arma22[i - 1] * 0.862537 + arma22[i - 2]  * (-0.3) + 0.2 * wn[i - 1] - 0.3 * wn[i -
                                                                                                        2] + wn[i]
                  }

                  #turn them into time series, and for the last two, "integrate" them via cumulative sum
                  return(data.frame(ar1 = ts(ar1),
                                    ma1 = ts(ma1),
                                    arma11 = ts(arma11),
                                    arima111 = ts(cumsum(arma11)),
                                    arima222 = ts(cumsum(cumsum(arma22)))
                                    ))
                }))
res <- setNames(lapply(res, setNames, paste('SD', SD, sep = '_')), paste('n', N, sep = '_'))
res

结果 - 仅截断为一种组合:

Result - truncated to only one combination:

$n_15
$n_15$SD_1
          ar1         ma1      arma11    arima111   arima222
1  -0.5604756 -0.56047565 -0.56047565 -0.56047565 -0.5604756
2  -0.2301775 -0.23017749 -0.23017749 -0.79065314 -1.3511288
3   1.5126728  0.91421134  0.86816717  0.07751403 -0.4913603
4   0.3730430  4.43489167  4.60858386  4.68609790  2.3123144
5   0.2038963  0.32671123  1.24843066  5.93452856  5.9733306
6   1.7558443  2.07707065  2.32676165  8.26129021 11.5104337
7   0.8120851  5.26309817  5.72851515 13.98980536 19.1736717
8  -1.1026442  0.02550414  1.17122455 15.16102991 26.4205560
9  -0.9073817 -4.22902431 -3.99482709 11.16620282 31.5923395
10 -0.6271383 -2.36884996 -3.16784126  7.99836155 34.8956636
11  1.0986541 -0.02377172 -0.65735677  7.34100478 38.5509080
12  0.5795447  3.78724286  3.65581765 10.99682243 43.8085632
13  0.5166804  1.40825017  2.13942726 13.13624969 50.4482906
14  0.2140188  1.23284278  1.66074334 14.79699303 57.8822760
15 -0.5130374 -0.24592953  0.08622331 14.88321634 64.9327807

编辑:此方法类似,但使用显式 for 循环而不是 lapply 并且仅返回 ma 变量:

Edit: This approach is similar but uses explicit for loops instead of lapply and only returns the ma variable:

N <- c(15L, 20L)
SD = c(1, 2) ^ 2
phi = c(0.2, 0.4)

res <- vector('list', length(N))
names(res) <- paste('N', N, sep = '_')

set.seed(123L)
for (i in seq_along(N)){
  res[[i]] <- vector('list', length(SD))
  names(res[[i]]) <- paste('SD', SD, sep = '_')

  ma <- matrix(NA_real_, nrow = N[i], ncol = length(phi)) 

  for (j in seq_along(SD)){
    wn <- rnorm(N[i], mean = 0, sd = SD[j])
    ma[1:2, ] <- wn[1:2]

    for (k in 3:N[i]){
      ma[k, ] <- wn[k - 1L] * phi + wn[k]
    }
    colnames(ma) <- paste('ma_theta', phi, sep = '_')
    res[[i]][[j]] <- ma
  }
}

res

$N_15
$N_15$SD_1
      ma_theta_0.2 ma_theta_0.4
 [1,]   0.68374552   0.68374552
 [2,]  -0.06082195  -0.06082195
 [3,]   0.62079632   0.60863193
 [4,]   1.46210976   1.58870190
 [5,]   0.27439361   0.54149714
 [6,]   1.01901666   1.02047467
 [7,]  -0.98492231  -0.78141058
 [8,]  -0.95929125  -1.19697805
 [9,]   1.37489682   1.23057594
[10,]   0.68123152   0.98507506
[11,]  -1.97674523  -1.90126763
[12,]  -1.77448202  -2.18492658
[13,]  -0.47358851  -0.74639600
[14,]   0.82562320   0.78546700
[15,]   0.07127263   0.24442851

$N_15$SD_4
      ma_theta_0.2 ma_theta_0.4
 [1,]    2.4967499    2.4967499
 [2,]    3.8360215    3.8360215
 [3,]    7.4514236    8.2186279
 [4,]    1.5609108    2.8977547
 [5,]   -0.1631142   -0.1183009
 [6,]   -7.0545350   -7.0961205
 [7,]   -1.0052795   -2.4078694
 [8,]   -2.2079382   -2.1284761
 [9,]   -4.3535184   -4.8109984
[10,]   -1.4988326   -2.2780403
[11,]    3.9158477    3.7719227
[12,]   -7.1590394   -6.3470849
[13,]   -3.3033159   -4.8975147
[14,]    0.1247257   -0.2170977
[15,]   -3.4795205   -3.3862106


$N_20
$N_20$SD_1
      ma_theta_0.2 ma_theta_0.4
 [1,]   0.33390294    0.3339029
 [2,]   0.41142992    0.4114299
 [3,]   0.04924982    0.1315358
 [4,]  -2.47250543   -2.4791127
 [5,]   2.07827851    1.5850989
 [6,]   0.30899237    0.8232840
 [7,]   0.61013343    0.5690736
 [8,]   0.40400515    0.5342438
 [9,]   1.07942653    1.1341798
[10,]   1.02259409    1.2275287
[11,]  -0.04626128    0.1172706
[12,]   0.33620914    0.2942505
[13,]  -0.86977528   -0.7941417
[14,]   0.66784124    0.4787595
[15,]  -0.28965374   -0.1182691
[16,]   2.32456569    2.2323580
[17,]  -1.16769422   -0.6843396
[18,]  -0.79419702   -1.1244068
[19,]   0.73258241    0.6397850
[20,]   0.67520852    0.8402845

$N_20$SD_4
      ma_theta_0.2 ma_theta_0.4
 [1,]  -2.35792415  -2.35792415
 [2,]  -3.98712297  -3.98712297
 [3,]  -0.21952177  -1.01694637
 [4,]   0.05835091   0.17393147
 [5,]  -7.17257088  -7.18401681
 [6,]  -1.29402072  -2.72624571
 [7,]   0.78856212   0.81620297
 [8,]   0.85108984   1.00327409
 [9,]  -4.08028705  -3.94050594
[10,]   1.06051948   0.21650585
[11,]   5.89518717   6.27609379
[12,]   2.92780172   4.03065783
[13,]  -4.17736476  -3.81237564
[14,]  -2.65105266  -3.55952343
[15,]   1.03589810   0.68738173
[16,]  -2.31129963  -2.03441673
[17,]  -9.14822185  -9.66585835
[18,]   1.81088621   0.08476914
[19,]  -2.61050979  -1.90310913
[20,]  -2.95782317  -3.62140526

这篇关于我想用不同的样本大小 n、不同的 SD 值和不同的 theta 值模拟一阶 MA(1) 的移动平均过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:33