gpt4 book ai didi

r - 在 R 中的两个 glm 模型之间执行 anova() 时如何提取 p 值

转载 作者:行者123 更新时间:2023-12-04 11:14:09 26 4
gpt4 key购买 nike

所以,我试图比较两个模型,fit1 和 fit2。

最初,我只是在做 anova(fit1,fit2),这产生了我理解的输出(包括 p 值)。

但是,当我将我的模型从基于 lm() 的模型切换到基于 glm() 的模型时,anova(fit1,fit2) 现在产生了自由度残差、残差偏差和 Df 偏差,我无法解释(资源解释这些指标似乎很少)。我希望为两个模型之间的比较提取 p 值,但由于某种原因 anova(fit1,fit2, test='Chisq') 不起作用。有什么建议?

我意识到,根据我的 glms 中的链接函数,卡方可能不是最合适的测试,但我在适当的上下文中使用了 'F' 以及类似的失望。

这个问题其他人熟悉吗?建议?非常感谢!

例子:

make_and_compare_models <- function(fitness_trait_name, data_frame_name, vector_for_multiple_regression, predictor_for_single_regression, fam){
fit1<-glm(formula=as.formula(paste(fitness_trait_name,"~", paste(vector_for_multiple_regression, sep="+"))), family=fam, data=data_frame_name)
print ("summary fit 1")
print(summary(fit1))
fit2<- glm(data=data_frame_name, formula=as.formula(paste(fitness_trait_name,"~",predictor_for_single_regression)), family=fam)

print("summary fit 2")
print(summary(fit2))
print("model comparison stats:")
mod_test<-anova(fit2,fit1)

##suggestion #1
print(anova(fit2,fit1, test="Chisq"))

#suggestion #2
print ("significance:")
print (1-pchisq( abs(mod_test$Deviance[2]),df=abs(mod_test$Df[2])))

}


data<-structure(list(ID = c(1L, 2L, 4L, 7L, 9L, 10L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L, 27L, 28L, 29L,
31L, 34L, 37L, 38L, 39L, 40L, 41L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 52L, 55L, 56L, 59L, 60L, 61L, 62L, 63L, 65L, 66L, 67L, 68L,
69L, 71L), QnWeight_initial = c(158L, 165L, 137L, 150L, 153L,
137L, 158L, 163L, 159L, 151L, 145L, 144L, 157L, 144L, 133L, 148L,
151L, 151L, 147L, 158L, 178L, 164L, 134L, 151L, 148L, 142L, 127L,
179L, 162L, 150L, 151L, 153L, 163L, 155L, 163L, 170L, 149L, 165L,
128L, 134L, 145L, 147L, 148L, 160L, 131L, 155L, 169L, 143L, 123L,
151L), Survived_eclosion = c(0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Days_wrkr_eclosion_minus20 = c(NA,
1L, NA, 3L, 0L, 2L, 0L, 1L, 0L, 0L, 0L, 1L, NA, 0L, 7L, 1L, 0L,
1L, 0L, 1L, 2L, 2L, NA, 2L, 3L, 2L, 2L, NA, 0L, 1L, NA, NA, 0L,
0L, 0L, 0L, 3L, 3L, 3L, 1L, 0L, 2L, NA, 1L, 0L, 1L, 1L, 3L, 1L,
2L), MLH = c(0.5, 0.666666667, 0.555555556, 0.25, 1, 0.5, 0.333333333,
0.7, 0.5, 0.7, 0.5, 0.666666667, 0.375, 0.4, 0.5, 0.333333333,
0.4, 0.375, 0.3, 0.5, 0.3, 0.2, 0.4, 0.875, 0.6, 0.4, 0.222222222,
0.222222222, 0.6, 0.6, 0.3, 0.4, 0.714285714, 0.4, 0.3, 0.6,
0.4, 0.7, 0.625, 0.555555556, 0.25, 0.5, 0.5, 0.6, 0.25, 0.428571429,
0.3, 0.25, 0.375, 0.555555556), Acon5 = c(0.35387674, 0.35387674,
0.35387674, 0.35387674, 0.35387674, 0.35387674, 0.35387674, 0,
0, 1, 0, 1, 0.35387674, 0, 0, 0.35387674, 1, 1, 0, 0, 0, 1, 0,
0.35387674, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0,
0, 0, 1, 0, 0, 0, 1, 0, 0.35387674), Baez = c(1, 1, 1, 0.467836257,
1, 1, 0, 0, 1, 1, 0, 0.467836257, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0.467836257, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1), C294 = c(0, 1, 0, 0, 1,
0.582542694, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0.582542694, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1), C316 = c(1, 1, 0, 0, 0.519685039,
0.519685039, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0.519685039, 0,
1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0.519685039, 1, 0, 1,
1, 0, 0.519685039, 1, 0.519685039, 1, 1, 1, 0.519685039, 0.519685039,
0, 0.519685039, 0.519685039, 0), i_120_PigTail = c(1, 1, 0, 1,
0.631236443, 0.631236443, 1, 1, 1, 1, 1, 0, 0.631236443, 1, 1,
1, 0, 0.631236443, 1, 1, 1, 0, 0, 1, 1, 1, 0.631236443, 0, 1,
1, 0, 1, 0.631236443, 1, 0, 1, 0, 0, 1, 0.631236443, 0.631236443,
0, 1, 0, 0.631236443, 0.631236443, 1, 0.631236443, 0.631236443,
1), i129 = c(0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), Jackstraw_PigTail = c(0L, 1L, 1L, 0L,
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L,
0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Neil_Young = c(0.529636711,
0, 1, 0, 0.529636711, 0.529636711, 1, 1, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1), Ramble = c(0, 0, 0,
0, 0.215163934, 0.215163934, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.215163934, 0,
0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0.215163934, 0, 0, 0, 0), Sol_18 = c(1,
0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0.404669261,
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1)), .Names = c("ID", "QnWeight_initial",
"Survived_eclosion", "Days_wrkr_eclosion_minus20", "MLH", "Acon5",
"Baez", "C294", "C316", "i_120_PigTail", "i129", "Jackstraw_PigTail",
"Neil_Young", "Ramble", "Sol_18"), class = "data.frame", row.names = c(NA,
-50L))

make_and_compare_models("QnWeight_initial", data, c("Acon5","Baez","C294","C316","i_120_PigTail","i129","Jackstraw_PigTail","Neil_Young","Ramble","Sol_18"), "MLH", "gaussian")

最佳答案

“更大”或更复杂模型与嵌套或“简化”模型之间的偏差差异(渐近地)作为卡方变量分布,这两个模型的自由度存在差异。因此,您将提取偏差估计和自由度的差异,并将其与 pchisq(deviance, diff(df) ) 进行比较。 “p 值”只是 1 减去该值。

> 1-pchisq(3.84,1)
[1] 0.05004352

如果您运行 glm 帮助页面中的第一个示例,然后添加一个没有“处理”变量的简化模型,您将得到:
glm.D93.o <- glm(counts ~ outcome, family=poisson())
anova.res <-anova(glm.D93, glm.D93.o)
anova.res
#------------
Analysis of Deviance Table

Model 1: counts ~ outcome + treatment
Model 2: counts ~ outcome
Resid. Df Resid. Dev Df Deviance
1 4 5.1291
2 6 5.1291 -2 -2.6645e-15
#---------------
str(anova.res)
Classes ‘anova’ and 'data.frame': 2 obs. of 4 variables:
$ Resid. Df : num 4 6
$ Resid. Dev: num 5.13 5.13
$ Df : num NA -2
$ Deviance : num NA -2.66e-15
- attr(*, "heading")= chr "Analysis of Deviance Table\n" "Model 1: counts ~ outcome + treatment\nModel 2: counts ~ outcome"

因此,在查看对象本身如何存储事物之后,这给出了“结果”的 p 值:
 1-pchisq( abs(anova.res$Deviance[2]), abs(anova.res$Df[2]))
[1] 1

这将是治疗+结果模型与仅治疗模型的相应程序:
> glm.D93.t <- glm(counts ~ treatment, family=poisson())
> anova.res2 <-anova(glm.D93, glm.D93.t)
> 1-pchisq( abs(anova.res2$Deviance[2]), abs(anova.res2$Df[2]))
[1] 0.06547071

关于r - 在 R 中的两个 glm 模型之间执行 anova() 时如何提取 p 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13237824/

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