gpt4 book ai didi

r - 似然比检验 : 'models were not all fitted to the same size of dataset'

转载 作者:行者123 更新时间:2023-11-28 19:59:50 29 4
gpt4 key购买 nike

我是一个绝对的 R 初学者,在我的单变量分析的似然比检验方面需要一些帮助。这是代码:

#Univariate analysis for conscientiousness (categorical)
fit <- glm(BCS_Bin~Conscientiousness_cat,data=dat,family=binomial)
summary(fit)

#Likelihood ratio test
fit0<-glm(BCS_Bin~1, data=dat, family=binomial)
summary(fit0)
lrtest(fit, fit0)

结果是:

Call:
glm(formula = BCS_Bin ~ Conscientiousness_cat, family = binomial,
data = dat)

Deviance Residuals:
Min 1Q Median 3Q Max
-0.8847 -0.8847 -0.8439 1.5016 1.5527

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.84933 0.03461 -24.541 <2e-16 ***
Conscientiousness_catLow 0.11321 0.05526 2.049 0.0405 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 7962.1 on 6439 degrees of freedom
Residual deviance: 7957.9 on 6438 degrees of freedom
(1963 observations deleted due to missingness)
AIC: 7961.9

Number of Fisher Scoring iterations: 4

和:

Call:
glm(formula = BCS_Bin ~ 1, family = binomial, data = dat)

Deviance Residuals:
Min 1Q Median 3Q Max
-0.8524 -0.8524 -0.8524 1.5419 1.5419

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.82535 0.02379 -34.69 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 10251 on 8337 degrees of freedom
Residual deviance: 10251 on 8337 degrees of freedom
(65 observations deleted due to missingness)
AIC: 10253

Number of Fisher Scoring iterations: 4

对于我的轻轨:

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

我知道发生这种情况是因为缺少不同数量的观察结果?那是因为它是来自大型问卷的数据,与结果变量( body 状况评分/BCS)相比,评估我的预测变量(尽责性)的问题发生了更多的辍学。所以我只是有更多的 BCS 数据而不是尽责性,例如(它也为我的许多其他变量产生了相同的错误)。

最佳答案

为了运行似然比检验,只有截距的模型必须与包含 Conscientiousness_cat 的模型拟合相同的观察结果。因此,对于 Conscientiousness_cat,您需要没有缺失值的数据子集:

BCS_bin_subset = BCS_bin[complete.cases(BCS_bin[,"Conscientiousness_cat"]), ]

您可以在该数据子集上运行这两个模型,并且似然比检验应该可以正常运行。

在您的情况下,您还可以:

BCS_bin_subset = BCS_bin[!is.na(BCS_bin$Conscientiousness_cat), ]

但是,当您想要数据框的一个子集在多个变量中没有缺失值时,complete.cases 很方便。

如果您要运行多个模型,另一个更方便但也更复杂的选项是首先拟合使用 BCS_bin 中最多变量的任何模型(因为该模型将排除由于缺失导致的最大数量的观察),然后使用 update 函数将该模型更新为变量较少的模型。我们只需要确保 update 每次都使用相同的观察结果,我们使用下面定义的包装函数来做到这一点。下面是一个使用内置 mtcars 数据框的示例:

library(lmtest)

dat = mtcars

# Create some missing values in mtcars
dat[1, "wt"] = NA
dat[5, "cyl"] = NA
dat[7, "hp"] = NA

# Wrapper function to ensure the same observations are used for each
# updated model as were used in the first model
# From https://stackoverflow.com/a/37341927/496488
update_nested <- function(object, formula., ..., evaluate = TRUE){
update(object = object, formula. = formula., data = object$model, ..., evaluate = evaluate)
}

m1 = lm(mpg ~ wt + cyl + hp, data=dat)
m2 = update_nested(m1, . ~ . - wt) # Remove wt
m3 = update_nested(m1, . ~ . - cyl) # Remove cyl
m4 = update_nested(m1, . ~ . - wt - cyl) # Remove wt and cyl
m5 = update_nested(m1, . ~ . - wt - cyl - hp) # Remove all three variables (i.e., model with intercept only)

lrtest(m5,m4,m3,m2,m1)

关于r - 似然比检验 : 'models were not all fitted to the same size of dataset' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193992/

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