如果(ncol(x)==1){:参数长度为零,则xts时间序列与错误的关系



我是R领域的新手,但我正在努力度过难关。我正在使用我通过以下代码设置的时间序列数据集:

myts <- as.xts(df[,-1], order.by = as.POSIXct(df$DATE_3, format="%Y-%m-%d %H:%M:%S"))

=>我的数据系列是2006年至2017年的每小时数据

我做了简单的lm回归,没有遇到任何麻烦。然后我开始测试统计假设,我在测试序列的平稳性和异方差性方面没有问题,然而,在序列相关性方面,我尝试使用以下代码:

library(car)
dwt(didreg6) 'didreg6 is the name of one of my regressions

但是我收到了这个错误。

Error in if (ncol(x) == 1) { : argument is of length zero

我试着回溯((是否找到了解决错误的线索。我收到了:

1: dwt(didreg6)
2: durbinWatsonTest(...)
3: durbinWatsonTest.lm(...)
4: matrix(sample(residuals, n * reps, replace = TRUE), n, reps)
5: as.vector(data)
6: as.vector(x, mode)
7: as.vector.zoo(x, mode)
8: as.vector(as.matrix(x), mode = mode)
9: as.matrix(x)
10: as.matrix.xts(x)

由于我无法确定问题出在哪里,我尝试了Ljung Box,因为我认为,也许它会起作用。

Res<-residuals(didreg6)
Box.test(Res, lag = 1, type = "Ljung-Box")

然而,我收到了另一个错误,我想这与时间序列有关。

Error in if (frequency > 1 && abs(frequency - round(frequency)) < ts.eps) frequency 
<-   round(frequency) : 
missing value where TRUE/FALSE needed

之后,因为我发现我的数据序列误差是异方差的,所以我想使用HAC误差而不是标准误差。我输入了这个代码:

library(estimatr)
didreg6_robust <- lm_robust(lnEL ~ sum of my explanatory variables, data = myts,      
se_type = "stata")
summary(didreg6_robust)

但同样的错误再次出现:

Error in if (ncol(x) == 1) { : argument is of length zero

最终,我尝试对HAC错误使用不同的编码:

didreg6 <- lm(lnEL ~ sum of my explanatory variables, data = myts)       
summary(didreg6)
library(lmtest)
library(sandwich)
coeftest(didreg6, df = Inf, vcov = vcovHC(didreg6, type = "HC0"))

但是,如果(ncol(x(==1({:自变量长度为零,则仍然收到错误遵循traceback((:

1: coeftest(didreg7_coef, df = Inf, vcov = vcovHC(didreg7_coef, type = "HC0"))
2: coeftest.default(didreg7_coef, df = Inf, vcov = vcovHC(didreg7_coef, type = "HC0"))
3: vcovHC(didreg7_coef, type = "HC0")
4: vcovHC.default(didreg7_coef, type = "HC0")
5: meatHC(x, type = type, omega = omega)
6: estfun(x, ...)
7: estfun.lm(x, ...)
8: as.vector(res)
9: as.vector(x, mode)
10: as.vector.zoo(x, mode)
11: as.vector(as.matrix(x), mode = mode)
12: as.matrix(x)
13: as.matrix.xts(x)

我读过很多关于这个错误的文章,但都没有涉及这个问题,错误可能与时间序列有关。我在整个数据集中没有丢失的数据。("1:ncol(x(中的错误:长度为0的参数";当在R(中使用Amelia时,但我认为错误在第一列,因此是我存储日期和时间的列。

以下是我的数据集预览,也许它将有助于解决这个问题:数据预览

此外,我真的很绝望,因为我不知道这个仍然弹出的错误的原因是什么,以及如何处理它

我犯的错误在哪里?

将它从xts转换回数值数组解决了这个问题。是的,问题似乎出在xts对象上。您可以按如下方式解决:

fit_dw <- lm( as.numeric(xts1) ~ as.numeric(xts2) + as.numeric(xts3) )durbinWatsonTest(fit_dw )

其中,xts1、xts2、xts3是包含一个时间序列的xts对象。

最新更新