【问题标题】:R: likelihood ratio test comparing two models, however missing data made the two models not in the same dimensionR:比较两个模型的似然比检验,但是缺少数据使两个模型不在同一维度上
【发布时间】:2013-08-27 02:17:16
【问题描述】:

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

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. 

以下是每个模型的摘要:

总结(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

总结(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

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

【问题讨论】:

  • 您可以使用is.na() 来检查速度变量中缺失的情况。请参阅here了解可能的解决方案。
  • 第二个公式也有拼写错误。

标签: r


【解决方案1】:

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

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

【讨论】:

  • 最好对最复杂模型中的列进行子集化:!is.na(speed[ , c('height', 'weight', 'speed') ] ) 或在同一数据参数上使用 complete.cases
猜你喜欢
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多