R語言學習筆記-Error in ts(x):對象不是矩陣問題解決
1、問題
在對時間序列進行擬合操作時,發生: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
>
2、原因分析
原由於:數據的變量名與基礎包中的sample函數同名,在fitted()函數中未能正確處理。
在R語言中sample是基礎包中的一個函數,註意在R程序中變量名不要使用與之同名的名稱。否則不會得到正確的結果。
3、解決
解決的辦法是函數不要與之同名,至於為什麽會在fitted()函數中sample()函數沒有被正確處理,可能和fitted()函數的本身。
註意:還有一可能造成混淆的還有data()函數。
4、sample命令參考
使用help(sample)能夠查看sample()函數的使用幫助。
sample {base} | R Documentation |
Random Samples and Permutations
Description
sample
takes a sample of the specified size from the elementsof
x
using either with or without replacement.
Usage
sample(x, size, replace = FALSE, prob = NULL) sample.int(n, size = n, replace = FALSE, prob = NULL)
Arguments
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):對象不是矩陣問題解決