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.