r - lm.fit(x, y, offset = offset, singular.ok = singular.ok,

  • 本文关键字:offset singular ok lm fit r panel-data
  • 更新时间 :
  • 英文 :


我正在尝试使用AR(1)过程将Cochrane Orcutt过程应用于我的数据。当我使用orcutt包中的cochrane.orcutt函数时,返回以下错误:

lm.fit(x, y, offset = offset, 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 Orcutt程序时,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")

coef(model)有一些 NA 值表示过度参数化,lm可以处理,但cochrane.orcutt不能。

正如您已经发现的那样,您将需要删除一些预测因子。

相关内容

最新更新