【问题标题】:NA produced in linear regression modelNA 在线性回归模型中产生
【发布时间】:2014-09-20 15:48:35
【问题描述】:

我已阅读与此类似的帖子,但我的问题并没有通过给出的答案解决。我想做一个简单的线性回归,看看咬伤发生率是否与地区、区域(vacc 或对照)和年份有关。正如您在输出中看到的,RORYA 区之一被赋予了 NA 系数,我收到消息“系数:(由于奇异性而未定义 1)”。 我已经阅读了这个,它似乎与因素的共线性有关。 给出的一种解决方案是在调用中添加 -1,这消除了拦截但没有解决我的问题,因为 RORYA 区在摘要输出中仍然有 NA。

我尝试过的另一个解决方案是更改调用中解释变量的顺序。这确实改变了一些事情……Rorya 区突然有了系数,但 Zone 变量变成了 NA'd。两者都不好,因为我希望所有解释变量都有一个系数。

我想知道是否有人可能知道为什么会发生这种情况以及是否有解决此问题的方法,以便所有变量都可以具有系数?

提前致谢。

一个可重现的例子:

df <- structure(list(DISTRICT = structure(c(1L, 6L, 5L, 3L, 2L, 4L, 
1L, 6L, 5L, 3L, 2L, 4L, 1L, 6L, 5L, 3L, 2L, 4L, 1L, 6L, 5L, 3L, 
2L, 4L), .Label = c("BUNDA", "MASWA", "MUSOMA", "RORYA", "SERENGETI", 
"TARIME"), class = "factor"), zone = structure(c(2L, 2L, 2L, 
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
2L, 2L, 1L, 1L, 1L), .Label = c("c", "v"), class = "factor"), 
year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("2010", 
"2011", "2012", "2013"), class = "factor"), bites = c(7.461327937, 
NA, NA, NA, 35.16164185, 26.39109338, 57.89990479, 1.47191729, 
3.608371422, 51.36718605, NA, 16.21167165, 46.85713945, 15.89670673, 
5.212092054, 259.8137381, 30.80276062, 20.73585909, 10.44585911, 
9.420270656, 7.617673001, 307.4586643, 27.31565565, 30.16124958
), deaths = c(0, NA, NA, NA, 0, 1.508062479, 0.298453117, 
0, 0, 0, NA, 2.262093719, 0.298453117, 0.294383458, 0, 2.233355915, 
0.581184163, 1.131046859, 0.298453117, 0.588766916, 1.202790474, 
2.977807887, 0, 1.885078099)), .Names = c("DISTRICT", "zone", 
"year", "bites", "deaths"), row.names = c(NA, -24L), class = "data.frame")

代码:

summary(df )
names(df)
attach(df)
is.numeric(year)
df$year  <- as.factor(as.character(df$year))
is.factor(df$year)

model1 <- lm(bites ~   zone + DISTRICT-1 +year, data = df)
summary(model1)

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.0

loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4     gtable_0.1.2     MASS_7.3-34      munsell_0.4.2   plyr_1.8.1       proto_0.3-10     Rcpp_0.11.2     
[9] reshape2_1.4     scales_0.2.4     stringr_0.6.2    tools_3.1.0     

【问题讨论】:

    标签: r


    【解决方案1】:

    当您拟合具有因子(在本例中为 DISTRICT)且没有截距的模型时,方法是为每个因子创建一个虚拟二元变量。所以在内部有一个二进制 0/1 值用于“在 DISTRICT RORYA 中的情况”。现在,每种情况下的zone 变量都是c,因此DISTRICT==RORYAzone 之间完全一致。这些变量完全共线,因此发出警告:

    Coefficients: (1 not defined because of singularities)
    

    并且无法定义这两个参数。于是NA出现了。

    在您的数据中,所有地区都是区域之一:

    > table(df$DISTRICT, df$zone)
    
                c v
      BUNDA     0 4
      MASWA     4 0
      MUSOMA    4 0
      RORYA     4 0
      SERENGETI 0 4
      TARIME    0 4
    

    因此,在任何分析中包含zoneDISTRICT 变量都会遇到麻烦。您无法为zone任何 区设置系数,因为根本没有足够的信息来区分任何区及其区域。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2021-08-09
      • 2019-10-09
      • 2021-01-11
      • 2018-04-28
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多