从R中的anova(glm)中提取剩余偏差

时间:2013-10-17 16:27:44

标签: r extract glm anova

我在R中安装了一个glm模型并拿走了anova表。我需要提取“剩余偏差”列。但它会产生错误。以下是代码:

创建数据:

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)

适合GLM:

glm.D93 <- glm(counts ~ outcome + treatment, family = quasipoisson(link = "log"))

Anova表:

av.1=anova(glm.D93)
av.1
Analysis of Deviance Table

Model: quasipoisson, link: log

Response: counts

Terms added sequentially (first to last)


          Df Deviance Resid. Df Resid. Dev
NULL                          8    10.5814
outcome    2   5.4523         6     5.1291
treatment  2   0.0000         4     5.1291

现在我需要提取“Resid.Dev”列。所以我尝试了str

> str(av.1)
Classes ‘anova’ and 'data.frame':       3 obs. of  4 variables:
 $ Df        : int  NA 2 2
 $ Deviance  : num  NA 5.45 0
 $ Resid. Df : int  8 6 4
 $ Resid. Dev: num  10.58 5.13 5.13
 - attr(*, "heading")= chr "Analysis of Deviance Table\n\nModel: quasipoisson, link: log\n\nResponse: counts\n\nTerms added sequentially (first to last)\n\"| __truncated__

最后我提取了Resid. Dev,但它给了我一个错误:

> av.1$Resid. Dev
Error: unexpected symbol in "av.1$Resid. Dev"

3 个答案:

答案 0 :(得分:4)

使用引号

> av.1$"Resid. Dev"
[1] 10.581446  5.129141  5.129141

等价

av.1[["Resid. Dev"]]

答案 1 :(得分:2)

使用[运算符访问第四列:

av.1[,4]

或者,如果您想使用$引用列名:

av.1$`Resid. Dev`
av.1$"Resid. Dev"

答案 2 :(得分:1)

您不能在$上使用不带引号的空格,因此请改用[

>av.1["Resid. Dev"]
          Resid. Dev
NULL      10.5814459
outcome    5.1291411
treatment  5.1291411