【发布时间】: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