gpt4 book ai didi

R:似然比检验比较两个模型,但是缺失数据使得两个模型不在同一维度

转载 作者:行者123 更新时间:2023-12-04 11:46:05 25 4
gpt4 key购买 nike

我正在尝试在两个模型之间进行似然比检验。

glm.model1 <- glm(result ~ height + weight )
glm.model2 <- glm(result ~ hight + weight + speed + speed : height + speed : weight )
require(lmtest)
a <- lrtest(glm.model1, glm.model2)

我得到了以下错误:

Error in lrtest.default(glm.model1, glm.model2) : 
models were not all fitted to the same size of dataset

我知道我的一些“速度”数据丢失了,但是高度和体重数据都没有丢失,所以由于模型 2 包含变量“速度”但模型 1 没有,所以模型 2 的数据点被 glm 删除了由于失踪。因此,当我在模型 2 和模型 1 之间进行似然比检验时,数据维度不相等,最终得到如上所示的错误消息。有没有一种方法可以查看模型 2 中删除了哪些数据点,因此在我的简化模型中我可以包含一些脚本来删除相同的数据点以保持数据的维度相同?

这是我尝试过的:

1) 添加 na.action = na.pass 以保留模型 2 中所有缺失的数据,但它不起作用。

2) 尝试过:

glm.model1 <- glm(result ~ height + weight + speed - speed )
## This does work and it gets rid of the sample with "speed" missing, but this is like cheating.

以下是每个模型的摘要:

summary(glm.model1)

......

Null deviance: 453061 on 1893 degrees of freedom
Residual deviance: 439062 on 1891 degrees of freedom
AIC: 15698

Number of Fisher Scoring iterations: 2

Fisher 评分迭代次数:2

summary(glm.model2)

......
Null deviance: 451363 on 1887 degrees of freedom
Residual deviance: 437137 on 1882 degrees of freedom
(6 observations deleted due to missingness) ## This is what I want to look at:
AIC: 15652
Number of Fisher Scoring iterations: 2

如何查看已删除的观察结果并写入脚本以删除其他模型中的相同观察结果?谢谢!

最佳答案

您可以使用 subset glm() 的参数功能:

glm.model1 <- glm(result ~ height + weight, subset=!is.na(speed) )

关于R:似然比检验比较两个模型,但是缺失数据使得两个模型不在同一维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18412829/

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