我应该转换 ar.ols
的输出吗?是某种类型的 predict
能接受吗?
y=rnorm(100, 0,1)
z=rnorm(100, 0,1)
yz=cbind(y,z)
> output = ar.ols(yz, aic = F, order.max = 2, demean = F, intercept = T)
> predict(output, n.ahead = 2, se.fit = F)
x=as.data.frame(yz) # x is a data frame, and otherwise same as yz.
> output = ar.ols(x, aic = F, order.max = 2, demean = F, intercept = T)
> predict(output, n.ahead = 2, se.fit = F)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
谢谢!
最佳答案
所以错误来自 predict.ar
.如果您将运行 ?predict
你会看到它是一个通用函数,它“调用依赖于第一个参数的类的特定方法”
所以
class(output)
[1] "ar"
和
methods(predict)
# [1] predict.ar* predict.Arima* predict.arima0* predict.glm predict.HoltWinters*
# [6] predict.lm predict.loess* predict.mlm* predict.nls* predict.poly*
# [11] predict.ppr* predict.prcomp* predict.princomp* predict.smooth.spline* predict.smooth.spline.fit*
# [16] predict.StructTS*
# Non-visible functions are asterisked
告诉你我们正在寻找第一种方法
下一次尝试是在该方法中查找错误消息。之前的操作通知我们
predict.ar
是不可见的函数,所以我们需要结合getAnywhere
和 capture.output
和一些正则表达式函数以查找错误消息,但不幸的是这不起作用grep("array", capture.output(getAnywhere("predict.ar")))
## integer(0)
这意味着错误来自在
predict.ar
内运行的其他函数。 .(正如@hadley 提到的)我们需要使用
traceback()
为了确定导致它的内部功能predict(output, n.ahead = 2, se.fit = F)
# Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
traceback()
# 6: array(STATS, dims[perm])
# 5: aperm(array(STATS, dims[perm]), order(perm))
# 4: sweep(newdata, 2L, object$x.mean, check.margin = FALSE)
# 3: rbind(sweep(newdata, 2L, object$x.mean, check.margin = FALSE),
# matrix(rep.int(0, nser), n.ahead, nser, byrow = TRUE))
# 2: predict.ar(output, n.ahead = 2, se.fit = F)
# 1: predict(output, n.ahead = 2, se.fit = F)
这很好地向我们展示了函数调用的工作流程:
调用
predict
-> 识别对象的类并调用对应的方法predict.ar
-> rbind
预先分配的矩阵(大小为 ncol(x)
* n.ahead
)使用 sweep
以均值为中心的数据-> 在对数据进行均值中心化时(使用 sweep
),转置一些 array
并创建一个新的 array
而最后一个操作返回错误。所以基本上都是
sweep
函数是减去 yz
的平均值来自 yz
(均值居中 - 可以通过运行 scale(yz, scale = FALSE)
来完成,所以不确定为什么他们首先使用 sweep
。也许是 dmean = FALSE
特殊情况?)。在您的情况下,您指定了 dmean = FALSE
所以它从两列中删除零(这是非常不必要的操作,在这种情况下可能应该避免)。相比all.equal(t(t(yz) - colMeans(yz)), sweep(yz, 2L, colMeans(yz)))
## [1] TRUE
唯一的问题是
sweep
对数组进行操作,因此它会尝试将您的数据转换为数组,同时通过传递 dim
来指定正确的维度。属性来自 yz
并创建一个 array
对于进一步的操作,即类似dims <- dim(yz)
perm <- c(2L, seq_along(dims)[-2L])
array(colMeans(yz), dims[perm])
这适用于很好的矩阵,因为所有矩阵都有
dim
根据定义属性。虽然
data.frame
s 没有 dim
属性,dim(x)
函数仍然足够聪明,可以计算暗淡本身,所以这工作得很好dim(x)
## [1] 100 2
唯一的问题是
predict.ar
函数去掉 class
属性来自 x
在到达 sweep
之前的过程中的某个地方,所以这就是 matrix
的区别所在和 data.frame
在那件事上很重要class(x) <- NULL
dim(x)
## NULL
class(x)
## [1] "list"
尽管
class(yz) <- NULL
dim(yz)
## [1] 100 2
class(yz)
## [1] "matrix"
注意,
x
刚变成list
具有不同的元素,例如向量和属性,而 matrix
由于其 dim
保留了其原始结构属性,所以 class
函数仍然可以识别它是一个矩阵,而 x
完全变形和class
不能再处理了。如果您想知道如何
class
有效,真正发生了什么,请参阅我的回答 here无论如何,虽然这仍然有效
STATS <- colMeans(yz)
class(yz) <- NULL
dims <- dim(yz)
perm <- c(2L, seq_along(dims)[-2L])
array(STATS, dims[perm])
这现在返回您之前看到的错误
x <- as.data.frame(yz)
STATS <- colMeans(x)
class(x) <- NULL
dims <- dim(x)
perm <- c(2L, seq_along(dims)[-2L])
array(STATS, dims[perm])
# Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
我会让你高兴地进一步挖掘 in the rabbit hole为了更好地了解如何
dim
作品。因此,为了结束这一点(如@joran 的评论中所述)- 请始终首先阅读文档 .如果您仔细查看
?ar.ols
, x
应该是一个单变量或多变量时间序列。在示例中,x
始终是类 ts
的对象而从来没有 data.frame
.因此,虽然我同意在您指定
demean = FALSE
的特定情况下,这个错误一开始就不应该发生,知道你在做什么仍然是一个更好的做法。换句话说,这就是经典XY problem问题的类型。
关于r - 使用 predict() 函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23271878/