I have data about insurance; age, sex, BMI, children, smoker, region and charges. Sex, smoker and region are factors. sex: male, female, smoker: yes, no, region: northeast, southeast, southwest, northwest.
m2 <- lm(charges ~ age + sex + bmi + children + smoker + region)
After fitting linear regression model with data I need to predict: male, age=40, bmi=30, smoker=yes, region=northwest. I have tried to factor categorical variables after reading the data
data$sex <- as.factor(data$sex)
data$region <- as.factor(data$region)
Using the predict function:
predict(m2, list(age=40, sex=factor(male), bmi=30, children=2, smoker=factor(yes),
region=factor(northwest)), int="p", level=0.98)
I only get errors. Please help
smoker
variable will only have one level (yes
) 2) try passing the new data as a data.frame not a list – arvi1000smoker=factor(yes)
will look for an object calledyes
. perhaps you mean something likesmoker = factor('yes', levels = c('yes', 'no'))
. – arvi1000