我正试图用AR(1)过程将Cochrane Orcutt过程应用于我的数据。当我使用来自cochrane.orcutt包的orcutt函数时,将返回以下错误:
lm.fit(x,y,偏移=偏移,singular.ok = singular.ok,.)中的错误:0(非NA)情况
我首先确保所有的数据集都没有不完整的案例。
library(orcutt) foo <- na.omit(foo) model <- lm(1-ef ~ as.factor(dtype) + as.factor(p) + inc + ed + marg + as.factor(period) + as.factor(id), data = foo) cochrane.orcutt(model)
代码行cochrane.orcutt(model)会产生错误。
到目前为止,我尝试过的是:
1)为了进一步解决问题,我从第一个自变量开始,一个接一个地添加它们,以查看代码在哪里崩溃。除了"as.factor(id)“之外,它们都单独和组合地工作,这似乎是一个令人反感的变量。
2)我想可能是关于id变量的字符串性质,所以我创建了一个因子变量,其数字代表每个个体(在下面的数据中称为id2 )。当我不将变量"id2“转换为一个因子变量并将其作为一个数值变量运行时,代码在完整模型和二元模型中都没有问题。
当我在二元回归中回归"as.factor(id2)“变量上的"1-ef”,然后执行Cochrane过程时,R在过程中停止。
3)然后我认为可能是未使用的级别导致了问题,所以我尝试:foo$id <- droplevels(foo$id),并尝试将id变量转换为字符变量。这两种方法都没有解决这个问题。
我试图解决以下问题的总结:
据我所知,有一个因素"id“变量导致了cochrane.orcutt函数的错误。要么是R暂停,要么是它产生错误,说明没有非NA情况(我已经确定na.omit不是这样的情况)。
在所有上述情况下,R没有问题运行回归,只有Cochrane Orcutt估计。
有什么想法吗?谢谢你的帮助!
下面是演示错误的数据子集:
foo <- structure(list(ef = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9232, 1, 1, 1, 1, 1, 1, 0.68, 0.847222222222222), dtype = c("5", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "5", "5"), p = c("0", "1", "1", "1", "0", "0", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "0", "0"), inc = c(65327L, 38517L, 38888L, 39147L, 62022L, 63938L, 74663L, 37203L, 36548L, 57582L, 50425L, 50880L, 50372L, 51780L, 54763L, 54341L, 53988L, 36379L, 37290L), ed = c(0.400399151, 0.140407741, 0.158387284488, 0.167404993465, 0.278612155, 0.307604827205, 0.459637019, 0.174936986, 0.165744029552, 0.379544685, 0.23472389, 0.301241844, 0.296150413967, 0.308115565224, 0.229996365, 0.244502536866, 0.251595155313, 0.241661234658, 0.257054165719), marg = c(-27.04, 100, 48.14, 100, -28.98, -38.96, 39.94, 100, 100, -1.62, 31.7, 22.86, 8.72, 19.5, 31.94, 22.78, 40.36, -59.26, -50.7), period = c("112", "112", "113", "114", "112", "113", "112", "112", "113", "112", "112", "112", "113", "114", "112", "113", "114", "113", "114"), id = c("A000022", "A000055", "A000055", "A000055", "A000210", "A000210", "A000358", "A000361", "A000361", "A000362", "A000365", "A000367", "A000367", "A000367", "A000369", "A000369", "A000369", "A000370", "A000370"), id2 = c(1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 8, 9, 9, 9, 10, 10)), .Names = c("ef", "dtype", "p", "inc", "ed", "marg", "period", "id", "id2"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), na.action = structure(12L, .Names = "12", class = "omit"), class = "data.frame")
发布于 2018-04-16 20:11:22
coef(model)有一些NA值,表示lm可以处理的过参数化,但是cochrane.orcutt不能。
正如您已经发现的,您需要删除一些预测器。
https://stackoverflow.com/questions/49864105
复制相似问题