2

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

4

1 回答 1

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
于 2013-02-19T13:25:55.600 回答