3

I fitted a glm model in R and took the anova table. I need to extract the "Residual Deviance" column. But it generates an error. Here are the codes:

Creating data:

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

Fitting GLM:

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

Anova table:

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

Now I need to extract "Resid. Dev" column. So I tried 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__

Finally I extracted Resid. Dev but it gives me an error:

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

3 回答 3

5

使用引号

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

等效地

av.1[["Resid. Dev"]]
于 2013-10-17T16:32:05.907 回答
2

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

av.1[,4]

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

av.1$`Resid. Dev`
av.1$"Resid. Dev"
于 2013-10-17T16:30:48.017 回答
1

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

>av.1["Resid. Dev"]
          Resid. Dev
NULL      10.5814459
outcome    5.1291411
treatment  5.1291411
于 2013-10-17T16:31:06.750 回答