gpt4 book ai didi

r - model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) 中的错误 : factor X has new levels

转载 作者:行者123 更新时间:2023-12-03 23:30:38 27 4
gpt4 key购买 nike

我做了一个逻辑回归:

 EW <- glm(everwrk~age_p + r_maritl, data = NH11, family = "binomial")

此外,我想为 everwrk 的每个级别预测 r_maritl
r_maritl 具有以下级别:
levels(NH11$r_maritl)
"0 Under 14 years"
"1 Married - spouse in household"
"2 Married - spouse not in household"
"3 Married - spouse in household unknown"
"4 Widowed"
"5 Divorced"
"6 Separated"
"7 Never married"
"8 Living with partner"
"9 Unknown marital status"

所以我做了:
predEW <- with(NH11,
expand.grid(r_maritl = c( "0 Under 14 years", "1 Married -
spouse in household", "2 Married - spouse not in household", "3 Married -
spouse in household unknown", "4 Widowed", "5 Divorced", "6 Separated", "7
Never married", "8 Living with partner", "9 Unknown marital status"),
age_p = mean(age_p,na.rm = TRUE)))

cbind(predEW, predict(EW, type = "response",
se.fit = TRUE, interval = "confidence",
newdata = predEW))

问题是我得到以下回应:

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor r_maritl has new levels 0 Under 14 years, Married - spouse in household unknown



样本数据:
str(NH11$age_p)
num [1:33014] 47 18 79 51 43 41 21 20 33 56 ...

str(NH11$everwrk)
Factor w/ 2 levels "2 No","1 Yes": NA NA 2 NA NA NA NA NA 2 2 ...

str(NH11$r_maritl)
Factor w/ 10 levels "0 Under 14 years",..: 6 8 5 7 2 2 8 8 8 2 ...

最佳答案

tl;dr 看起来你的因子中有一些水平没有在你的数据中表示,从模型中使用的因子中删除。事后看来,这并不奇怪,因为您将无法预测这些级别的响应。也就是说,R 并没有为您做一些像自动生成 NA 值这样的好事,这有点令人惊讶。您可以通过在构建预测框架时使用 levels(droplevels(NH11$r_maritl)) 或等效的 EW$xlevels$r_maritl 来解决此问题。

一个可重现的例子:

maritl_levels <- c( "0 Under 14 years", "1 Married - spouse in household", 
"2 Married - spouse not in household", "3 Married - spouse in household unknown",
"4 Widowed", "5 Divorced", "6 Separated", "7 Never married", "8 Living with partner",
"9 Unknown marital status")
set.seed(101)
NH11 <- data.frame(everwrk=rbinom(1000,size=1,prob=0.5),
age_p=runif(1000,20,50),
r_maritl = sample(maritl_levels,size=1000,replace=TRUE))

让我们做一个缺失的关卡:
NH11 <- subset(NH11,as.numeric(NH11$r_maritl) != 3)

拟合模型:
EW <- glm(everwrk~r_maritl+age_p,data=NH11,family=binomial)
predEW <- with(NH11,
expand.grid(r_maritl=levels(r_maritl),age_p=mean(age_p,na.rm=TRUE)))
predict(EW,newdata=predEW)

成功!

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor r_maritl has new levels 2 Married - spouse not in household


predEW <- with(NH11,
expand.grid(r_maritl=EW$xlevels$r_maritl,age_p=mean(age_p,na.rm=TRUE)))
predict(EW,newdata=predEW)

关于r - model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) 中的错误 : factor X has new levels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784594/

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