R 多元线性回归的预测计算

R 多元线性回归的预测计算,r,statistics,prediction,R,Statistics,Prediction,我有保险方面的资料;年龄、性别、体重指数、儿童、吸烟者、地区和费用。性别、吸烟者和地区是影响因素。性别:男,女,吸烟者:是,否,地区:东北,东南,西南,西北 m2 <- lm(charges ~ age + sex + bmi + children + smoker + region) 我只会出错。请帮助不要重新定义因子,只需使用预测中引号中的因子级别即可 predict(m2, list(age=40, sex="male", bmi=30, children=2,

我有保险方面的资料;年龄、性别、体重指数、儿童、吸烟者、地区和费用。性别、吸烟者和地区是影响因素。性别:男,女,吸烟者:是,否,地区:东北,东南,西南,西北

m2 <- lm(charges ~ age + sex + bmi + children + smoker + region)

我只会出错。请帮助

不要重新定义因子,只需使用
预测
中引号中的因子级别即可

predict(m2, list(age=40, sex="male", bmi=30, children=2, smoker="yes", 
                 region="northwest"), int="p", level=0.98)
#         fit       lwr      upr
# 1 -1.978994 -9.368242 5.410254
资料


dat 1)新数据中的系数杠杆是否与旧数据中的水平匹配?在您的示例中,例如,
smoker
变量将只有一个级别(
yes
)2)尝试将新数据作为数据帧而不是列表传递。此外,传递新数据时,需要引用字符串
smoker=factor(yes)
将查找名为
yes
的对象。也许你的意思是像吸烟者=因子('yes',水平=c('yes','no'))
predict(m2, list(age=40, sex=factor(male), bmi=30, children=2, smoker=factor(yes), 
                 region=factor(northwest)), int="p", level=0.98)
predict(m2, list(age=40, sex="male", bmi=30, children=2, smoker="yes", 
                 region="northwest"), int="p", level=0.98)
#         fit       lwr      upr
# 1 -1.978994 -9.368242 5.410254
dat <- structure(list(charges = c(1.37095844714667, -0.564698171396089, 
0.363128411337339, 0.63286260496104, 0.404268323140999, -0.106124516091484, 
1.51152199743894, -0.0946590384130976, 2.01842371387704, -0.062714099052421
), age = c(20L, 58L, 44L, 53L, 22L, 51L, 20L, 75L, 59L, 41L), 
    sex = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("female", 
    "male"), class = "factor"), bmi = c(25.3024309248682, 24.6058854935878, 
    25.7881406228236, 25.6707038267505, 24.0508191903124, 25.036135738485, 
    27.115755613237, 25.1674409043556, 24.1201634714689, 25.9469131749433
    ), children = c(4L, 1L, 5L, 1L, 1L, 4L, 0L, 0L, 3L, 4L), 
    smoker = c("no", "yes", "yes", "no", "no", "yes", "yes", 
    "yes", "yes", "no"), region = structure(c(1L, 2L, 2L, 3L, 
    1L, 2L, 3L, 3L, 3L, 2L), .Label = c("northeast", "northwest", 
    "southeast"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")