在对时间序列进行拟合操作时,发生:Error in ts(x):对象不是矩阵的错误,而直接在arima()函数中使用时没有问题的。
> sample<-c2
> sample
 [1] 0.00 0.00 0.00 0.00 0.00 0.00 0.06 0.09 0.20 0.09 0.08 0.14 0.14 0.23
[15] 0.08 0.06 0.12 0.20 0.14 0.11 0.20 0.14 0.17 0.15 0.18 0.15 0.20 0.12
[29] 0.23 0.08 0.12 0.08 0.23 0.12 0.08 0.17 0.18 0.17 0.12 0.17 0.14 0.18
[43] 0.11 0.27 0.06
> fitted(arima(sample,order=c(4,0,3)))
Error in ts(x) : 对象不是矩阵
> arima(sample,order=c(4,0,3))
Call:
arima(x = sample, order = c(4, 0, 3))
Coefficients:
         ar1     ar2      ar3     ar4      ma1     ma2     ma3  intercept
      0.6740  0.0666  -0.4026  0.4924  -0.6160  0.2129  0.3564     0.1097
s.e.  0.2761  0.3073   0.2510  0.1724   0.3005  0.3112  0.2361     0.0380
sigma^2 estimated as 0.002756:  log likelihood = 67.68,  aic = -117.37
> 
原因为:数据的变量名与基础包中的sample函数同名,在fitted()函数中未能正确处理。
在R语言中sample是基础包中的一个函数,注意在R程序中变量名不要使用与之同名的名称,否则不会得到正确的结果。
解决的办法是函数不要与之同名,至于为什么会在fitted()函数中sample()函数没有被正确处理,可能和fitted()函数的本身
使用help(sample)可以查看sample()函数的使用帮助。
| sample {base} | R Documentation | 
sample takes a sample of the specified size from the elementsof 
x using either with or without replacement.
sample(x, size, replace = FALSE, prob = NULL) sample.int(n, size = n, replace = FALSE, prob = NULL)
| x | Either a vector of one or more elements from which to choose,or a positive integer. See ‘Details.’ | 
| n | a positive number, the number of items to choose from. See‘Details.’ | 
| size | a non-negative integer giving the number of items to choose. | 
| replace | Should sampling be with replacement? | 
| prob | A vector of probability weights for obtaining the elementsof the vector being sampled. | 
版权声明:本文为博主原创文章,未经博主允许不得转载。
R语言学习笔记-Error in ts(x):对象不是矩阵问题解决
原文:http://blog.csdn.net/hongweigg/article/details/47776691