在ldply()里面使用summary(glm-object)和summarize() - 函数

时间:2013-02-19 12:57:41

标签: r extract plyr summary glm

如何在ldply() - summarize-function中使用summary-function来提取p值?

示例数据:

(数据框“嘌呤霉素”已预先安装)

library(reshape2)
library(plyr)
Puromycin.m <- melt( Puromycin , id=c("state")  )
Puro.models <-  dlply( Puromycin.m , .(variable)  , glm , formula =  state ~ value  , 
family = binomial  )  

我可以使用提取的结果构建此数据框:

ldply( Puro.models  ,  summarise ,  "n in each model" = length(fitted.values) ,   
"Coefficients" = coefficients[2] )

但我不能以同样的方式提取p值。我认为这样可行,但事实并非如此:

    ldply( Puro.models  ,  summarise ,  
    "n in each model" = length(fitted.values) , 
    "Coefficients" = coefficients[2], 
    "P-value" = function(x) summary(x)$coef[2,4]              )

如何将p值提取到该数据框:)请帮忙!

1 个答案:

答案 0 :(得分:5)

你为什么不直接拿到它们?

library(reshape2)
library(plyr)
Puromycin.m <- melt( Puromycin , id=c("state")  )
Puro.models <-  ddply( Puromycin.m , .(variable), function(x) {
    t <- glm(x, formula = state ~ value, family="binomial")
    data.frame(n = length(t$fitted.values), 
                coef = coefficients(t)[2], 
                pval = summary(t)$coef[2,4])
})

> Puro.models
#   variable  n        coef      pval
# 1     conc 23 -0.55300908 0.6451550
# 2     rate 23 -0.01555023 0.1272184