R:用日期模拟ts对象

时间:2014-03-29 09:21:20

标签: r

想知道如何生成时间序列并同时指定日期。我正在尝试这个

series = as.ts(arima.sim(model = list(ar = c(0.12, -.36)), 
                         n = 1990 - 1875, sd = sqrt(4)),
                   start = 1875, deltat = 1)

但是这并没有返回一个ts对象来计算1875年的年份。通过我的计算,这应该有效。任何建议表示赞赏。

1 个答案:

答案 0 :(得分:1)

你是对的。我重新键入vs剪切/粘贴你的代码,这是start参数和使用as.ts vs ts只是问题:

asim <- arima.sim(model = list(ar = c(0.12, -.36)), 
                  n = 1990 - 1875, sd = sqrt(4))
series <- ts(asim, start = c(1875, 1), deltat = 1)

print(series)
Time Series:
Start = 1875 
End = 1989 
Frequency = 1 
  [1] -1.22873543 -2.87876290 -3.00367322 -0.93120214  1.76854684  0.93874091 -2.32494289
  [8]  1.14892019  1.87773156  1.48735536 -0.84149973 -3.69650397  1.20710878  2.14151424
 [15] -2.58376182 -2.97501726  2.77019523  4.50829433  0.35603642 -1.95517140 -1.12792253
 [22]  1.64063413  2.25654663 -0.51293345  1.07829896 -1.77134896  2.38908172  4.29362478
 [29] -1.55577635  1.17953083  3.39823289  1.11846543 -0.92758706 -1.24158935 -2.39831233
 [36]  4.24302415  2.93797283 -0.75916084 -0.66967525  2.85022663 -0.18190842 -5.39057660
 [43]  0.08454559  2.01667062 -3.17054706 -3.77788365  0.19987174  2.87106608 -0.33844973
 [50]  1.20917997 -1.00509230  3.23130604  5.80269444  3.33781468  2.67050526  1.85130774
 [57] -0.46065144 -2.79539368  0.29784271 -4.51945793  0.61091013  2.56372897 -4.66101520
 [64]  2.43024521  0.04428268 -1.19454953 -3.10583191  4.55208114  6.00037902 -3.32996632
 [71]  2.22167610  1.07499343  1.89873604  2.04067084 -3.43648828 -0.53093294  0.66225057
 [78] -2.30214366  0.78945348  0.35241170 -0.68250626  1.39801271 -1.01914282 -0.33615058
 [85]  0.92311887  1.66289752 -0.83158693 -0.74454853  6.53884660  1.53567335 -2.16745416
 [92] -0.01540633 -1.25032821 -0.02958796  3.18116493 -2.07512219 -1.40620668 -0.78869155
 [99]  2.30251140 -2.23997817  0.34824690  4.81898402 -0.38751197 -5.74540148 -0.37754295
[106]  2.59869857 -1.90175430  0.37994317 -1.27326292 -3.96302760 -2.01928982  2.57643462
[113]  2.62600151 -4.20987173  0.46388883

由于asim已经是ts类对象,as.ts正在从中提取tsp属性,而不是从输入参数创建它。使用ts可创建新的tsp属性。