我正在尝试在两个模型之间进行似然比检验.
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)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error in lrtest.default(glm.model1, glm.model2) :
models were not all fitted to the same size of dataset
Run Code Online (Sandbox Code Playgroud)
我知道我的一些"速度"数据丢失,但没有高度和重量数据丢失,因此模型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.
Run Code Online (Sandbox Code Playgroud)
以下是每种型号的摘要:
摘要(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
Run Code Online (Sandbox Code Playgroud)
Fisher评分迭代次数:2
摘要(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
Run Code Online (Sandbox Code Playgroud)
如何查看已删除的观察结果并写入脚本以删除其他模型中的相同观察结果?谢谢!
您可以使用函数的subset参数glm():
glm.model1 <- glm(result ~ height + weight, subset=!is.na(speed) )