| drop_levels | R Documentation |
Drops unseen factor levels in newdata for which predictions are required from a lm or multinom model fit.
drop_levels(fit,
newdata)
fit |
A fitted |
newdata |
A |
A data.frame like newdata with unseen factor levels replaced by NA.
This function is so far untested for models other than lm or multinom, though it may still work for other classes.
Keefe Murphy - <keefe.murphy@mu.ie>
drop_constants
data(ais)
hema <- as.matrix(ais[,3:7])
BMI <- ais$BMI
sport <- ais$sport
sub <- ais$sport != "Row"
# Fit a linear model
mod <- lm(hema ~ BMI + sport, data=ais, subset=sub)
# Make predictions
pred1 <- try(predict(mod, newdata=ais), silent=TRUE)
inherits(pred1, "try-error") #TRUE
# Remove unused levels and try again
pred2 <- try(predict(mod, newdata=drop_levels(mod, ais)), silent=TRUE)
inherits(pred2, "try-error") #FALSE
anyNA(pred2) #TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.