1.理解回歸
- 確定一個唯一的因變量(需預(yù)測的值)和一個或多個數(shù)值型的自變量(預(yù)測變量)之間的關(guān)系朝墩。
- 回歸分析對數(shù)據(jù)間復(fù)雜關(guān)系建立模型墅诡,用來估計一種處理方法對結(jié)果影響和推斷未來送漠。也可用于假設(shè)檢驗(yàn)板熊。
- 線性回歸:直線回歸模型
- 簡單線性回歸:單一自變量
- 多元回歸:多變量
也可對分類變量做回歸:
- 邏輯回歸:對二元分類的結(jié)果建模
- 泊松回歸:對整型的計數(shù)數(shù)據(jù)建模
線性回歸帘饶、邏輯回歸甜无、泊松回歸以及其他許多回歸都屬于廣義線性模型(GLM)柳譬。
1)簡單線性回歸
方程就是一條直線:
做回歸分析時喳张,設(shè)計對α和β尋找參數(shù)估計(一般用a和b來表示)。
2)普通最小二乘估計
普通最小二乘法(OLS):確定α和β的最優(yōu)估計值美澳,即斜率和截距的選擇要使得誤差(y的預(yù)測值與y的真實(shí)值之間的垂直距離销部,即殘差)的平方和最小摸航。
通過演算,使得誤差平方最小的b值為:
即:
3)相關(guān)系數(shù)
Pearson相關(guān)系數(shù):
經(jīng)驗(yàn)規(guī)則(大拇指規(guī)則)來解釋相關(guān)系數(shù):
0.1-0.3弱相關(guān)舅桩;0.3-0.5中相關(guān)酱虎;0.5以上強(qiáng)相關(guān),但必須根據(jù)上下文解釋擂涛。
4)多元線性回歸
多元回歸方程:
可表示為:
經(jīng)過推導(dǎo)(略)读串,可計算向量β最佳估計:
可編寫一個簡單回歸函數(shù)reg,輸入y和x撒妈,返回一個估計的β系數(shù)矩陣:
reg <- function(y,x){
x <- as.matrix(x)
x <- cbind(Intercept=1,x)
#solve執(zhí)行矩陣逆運(yùn)算恢暖, %*%兩個矩陣相乘
solve(t(x) %*% x) %*% t(x) %*% y
}
2.線性回歸應(yīng)用示例
預(yù)測醫(yī)療費(fèi)用:利用病人的數(shù)據(jù)來預(yù)測他們的平均醫(yī)療費(fèi)用,進(jìn)而創(chuàng)建一個精算表來設(shè)定年度保費(fèi)的價格狰右。
1)收集數(shù)據(jù)
1338個案例胀茵,包括保險受益者,病人特點(diǎn)(年齡挟阻、性別、BMI峭弟、區(qū)域等)和歷年計劃計入的總醫(yī)療費(fèi)用的特征附鸽。
數(shù)據(jù)下載:
鏈接: https://pan.baidu.com/s/1Hgn5jad2O1HCgNSJrzT9MA 提取碼: vjr9
2)探索和準(zhǔn)備數(shù)據(jù)
## Example: Predicting Medical Expenses ----
## Step 2: Exploring and preparing the data ----
insurance <- read.csv("insurance.csv", stringsAsFactors = TRUE)
str(insurance)
# summarize the charges variable
summary(insurance$expenses)
# histogram of insurance charges
hist(insurance$expenses)
# table of region
table(insurance$region)
# exploring relationships among features: correlation matrix
cor(insurance[c("age", "bmi", "children", "expenses")])
# visualing relationships among features: scatterplot matrix
pairs(insurance[c("age", "bmi", "children", "expenses")])
# more informative scatterplot matrix
library(psych)
pairs.panels(insurance[c("age", "bmi", "children", "expenses")])
兩個變量相關(guān)性由橢圓形狀表示:越拉伸相關(guān)性越強(qiáng)。每個變量的局部回歸平滑曲線表示x軸和y軸變量之間的一般關(guān)系瞒瘸。倒U形(如age和bmi)
3)訓(xùn)練數(shù)據(jù)
## Step 3: Training a model on the data ----
ins_model <- lm(expenses ~ age + children + bmi + sex + smoker + region,
data = insurance)
ins_model <- lm(expenses ~ ., data = insurance) # this is equivalent to above
# see the estimated beta coefficients
ins_model
截距很難解釋坷备,沒有內(nèi)在意義,在實(shí)際中常常被忽略情臭。
指定6個變量省撑,但輸出了10個系數(shù):因?yàn)閘m函數(shù)將虛擬編碼自動應(yīng)用于因子類型的變量中。
估計的系數(shù)是相對于參照類別解釋的俯在。
4)評估模型
## Step 4: Evaluating model performance ----
# see more detail about the estimated beta coefficients
summary(ins_model)
5)提高模型性能
①添加非線性關(guān)系
如添加一個高階項(xiàng)到回歸模型中竟秫,把模型當(dāng)成多項(xiàng)式處理。比如年齡對醫(yī)療費(fèi)用的影響可能不是恒定的跷乐,越老的人肥败,治療費(fèi)越高,考慮將age創(chuàng)建一個新的非線性變量age^2
②將一個數(shù)值型變量轉(zhuǎn)換為二進(jìn)制指標(biāo)
當(dāng)一個特征的影響不是累積的愕提,而是當(dāng)特征的取值達(dá)到一個給定的閾值后才產(chǎn)生影響馒稍。比如BMI只有大于30時才有影響。
③加入相互作用的影響
當(dāng)兩個特征存在共同影響時浅侨,可考慮相互作用纽谒,如肥胖指標(biāo)bmi30和吸煙指標(biāo)smoker可能存在相互作用。
④綜合以上三點(diǎn)一起改進(jìn)
## Step 5: Improving model performance ----
# add a higher-order "age" term
insurance$age2 <- insurance$age^2
# add an indicator for BMI >= 30
insurance$bmi30 <- ifelse(insurance$bmi >= 30, 1, 0)
# create final model
ins_model2 <- lm(expenses ~ age + age2 + children + bmi + sex +
bmi30*smoker + region, data = insurance)
summary(ins_model2)
R方從0.75提高到了0.87如输,即模型現(xiàn)在能解釋醫(yī)療費(fèi)用變化的87%鼓黔。