【问题标题】:Using summary(glm-object) inside ldply() with the summarise() - function在 ldply() 中使用 summary(glm-object) 和 summarise() - 函数
【发布时间】:2013-02-19 12:57:41
【问题描述】:

如何使用 ldply()-summarise-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 值提取到该数据框 :) 请帮忙!

【问题讨论】:

    标签: r extract plyr summary glm


    【解决方案1】:

    为什么不直接获取呢?

    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
    

    【讨论】:

    • 太好了,谢谢!您是否有任何建议阅读以更好地理解在 ddply() 中使用函数?
    • 我非常喜欢this presentation。这是一个非常好的初步阅读。但是,我认为Hadley's documentation 是无可替代的。希望这会有所帮助。
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2016-08-07
    • 1970-01-01
    • 2011-12-07
    • 2019-12-07
    • 1970-01-01
    相关资源
    最近更新 更多