gpt4 book ai didi

r - 在 ldply() 中使用 summary(glm-object) 和 summarise() - 函数

转载 作者:行者123 更新时间:2023-12-02 05:13:51 26 4
gpt4 key购买 nike

我如何使用 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 值 :) 请帮忙!

最佳答案

为什么不直接获取它们?

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

关于r - 在 ldply() 中使用 summary(glm-object) 和 summarise() - 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com