我已經閱讀過類似的文章,但我的問題沒有通過給出的答案來解決。我想做一個簡單的線性迴歸,看看咬傷發生率是否與區,區(疫苗或對照)和年份有關。正如你在輸出中看到的一個區域RORYA被給出NA係數,並且我得到「係數:(1沒有因奇點而定義)」的信息。 我已經閱讀了這篇文章,它看起來與它的共線性有關。 給出的一個解決方案是給調用添加-1,這會刪除攔截,但不能解決我的問題,因爲RORYA區域在摘要輸出中仍具有NAs。在線性迴歸模型中產生的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