簡體   English   中英

計算多元線性回歸的預測

[英]Calculating predictions for multiple linear regression

我有關於保險的數據; 年齡、性別、BMI、兒童、吸煙者、地區和費用。 性別、吸煙者和地區是因素。 性別:男,女,吸煙者:是,否,地區:東北,東南,西南,西北。

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

用數據擬合線性回歸模型后,我需要預測:男性,年齡 = 40,體重指數 = 30,吸煙者 = 是,地區 = 西北。 我試圖在閱讀數據后分解分類變量

data$sex <- as.factor(data$sex)
data$region <- as.factor(data$region)

使用預測功能:

predict(m2, list(age=40, sex=factor(male), bmi=30, children=2, smoker=factor(yes), 
                 region=factor(northwest)), int="p", level=0.98)

我只得到錯誤。 請幫忙

無需重新定義因子,只需在predict中使用引號中的因子級別即可。

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM