predict.lm() with an unknown factor level in test data

前端 未结 7 688
我在风中等你
我在风中等你 2020-11-28 06:22

I am fitting a model to factor data and predicting. If the newdata in predict.lm() contains a single factor level that is unknown to the model,

7条回答
  •  再見小時候
    2020-11-28 07:23

    The lme4 package will handle new levels if you set the flag allow.new.levels=TRUE when calling predict.

    Example: if your day of week factor is in a variable dow and a categorical outcome b_fail, you could run

    M0 <- lmer(b_fail ~ x + (1 | dow), data=df.your.data, family=binomial(link='logit')) M0.preds <- predict(M0, df.new.data, allow.new.levels=TRUE)

    This is an example with a random effects logistic regression. Of course, you can perform regular regression ... or most GLM models. If you want to head further down the Bayesian path, look at Gelman & Hill's excellent book and the Stan infrastructure.

提交回复
热议问题