一、一元線性回歸
以R中自帶的trees數據集為例進【微軟visual studio2017中R相關數據科學模塊】
> head(trees) Girth Height Volume#包含樹齡、樹高、體積 1 8.3 70 10.3 2 8.6 65 10.3 3 8.8 63 10.2 4 10.5 72 16.4 5 10.7 81 18.8 6 10.8 83 19.7
先繪制一下散點圖,看看變量之間是否存在線性關系:體積、樹齡
> library(ggplot2) > qplot(x = log(Girth),y = log(Volume),data = trees,main = 'Volume and Girth Relation', colour = I('skyblue'),size = 10)
有圖得知,存在線性關系,進行建模
> model<-lm(Volume~Girth,data = trees) > summary(model) Call: lm(formula = Volume ~ Girth, data = trees) Residuals:#殘差較大 Min 1Q Median 3Q Max -8.065 -3.107 0.152 3.495 9.587 Coefficients:#系數的假設檢驗,P值都較小,在模型中呈顯著狀態*** Estimate Std. Error t value Pr(>|t|) (Intercept) -36.9435 3.3651 -10.98 7.62e-12 *** Girth 5.0659 0.2474 20.48 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.252 on 29 degrees of freedom Multiple R-squared: 0.9353, Adjusted R-squared: 0.9331 F-statistic: 419.4 on 1 and 29 DF, p-value: < 2.2e-16 #F統計量,也是顯著的
將擬合直線繪制到原圖上,查看擬合情況
> plot(Volume ~ Girth, data = trees,main = 'Volume and Girth Relation', col = 'skyblue') + abline(model, col = 'red', lty = 3)
但截距項不應該為負數(無論樹齡再小體積也不應該為負數,就算只是一顆種子它也是有體積的),所以也可以用下面方法將截距強制為0:
> model <- lm(Volume ~ Girth - 1, data = trees) + summary(model) Call: lm(formula = Volume ~ Girth - 1, data = trees) Residuals:#殘差有所增大 Min 1Q Median 3Q Max -11.104 -8.470 -6.199 1.883 27.129 Coefficients: Estimate Std. Error t value Pr(>|t|) Girth 2.4209 0.1253 19.32 <2e-16 *** #截距項已經去除只剩樹齡 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 9.493 on 30 degrees of freedom Multiple R-squared: 0.9256, Adjusted R-squared: 0.9231 F-statistic: 373.1 on 1 and 30 DF, p-value: < 2.2e-16
將現在的模型與之前的模型進行比較
也可以plot模型輸出結果,評價模型效果
par(mfrow=c(2,2)) plot(model)
左上圖是殘差對擬合值作圖,整體呈現出一種先下降后下升的模式,顯示殘差中可能還存在未提煉出來的影響因素。
右上圖殘差QQ圖,用以觀察殘差是否符合正態分布。
左下圖是標准化殘差對擬合值,用於判斷模型殘差是否等方差。
右下圖是標准化殘差對杠桿值,虛線表示的cooks距離等高線。我們發現31號樣本有較大的影響,需要進行變換。
plot(sqrt(Volume)~Girth,data=trees,pch=16,col='skyblue')#對整體進行開根號變換,線性趨勢仍然明顯
plot(log(Volume) ~ Girth, data = trees, pch = 16, col = 'red')#對整體進行log變換,
與開根號趨勢差異不大,采用開根號后變換重新進行建模
> model <- lm(sqrt(Volume) ~ Girth, data = trees) + summary(model) Call: lm(formula = sqrt(Volume) ~ Girth, data = trees) Residuals:#整體殘差明顯變小 Min 1Q Median 3Q Max -0.56640 -0.19429 -0.01169 0.20934 0.65575 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.55183 0.23719 -2.327 0.0272 * Girth 0.44262 0.01744 25.385 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.2997 on 29 degrees of freedom Multiple R-squared: 0.9569, Adjusted R-squared: 0.9555 F-statistic: 644.4 on 1 and 29 DF, p-value: < 2.2e-16
> plot(sqrt(Volume) ~ Girth, data = trees, pch = 16, col = 'red') + abline(model, lty = 2)
其實跟最初擬合的效果差別不大
par(mfrow=c(2,2)) plot(model)
上圖可知,原先的31號點已被削減,基本上沒有太明顯的異常點
預測使用時只需要輸入建立模型名稱以及數據框(包含X屬性的列名,本列中即樹齡),預測值為體積
> head(predict(model, trees)) 1 2 3 4 5 6 3.121955 3.254743 3.343268 4.095729 4.184254 4.228516
二、多元線性回歸
與一元線性回歸不同的是采用多個自變量來預測或者估計因變量
步驟與一元類似:
1、相關性檢驗,共線變量需要進行一定取舍,可以通過corrplot(cor(數據框名稱))查看相關性
2、model <- lm(因變量~自變量1+自變量2+自變量3..., data = 數據框名稱)進行建模,summary進行查看模型效果
3、同樣可以用par(mfrow=c(2,2))、plot(model)查看效果及異常點情況
4、根據結果進行調整,重新建模,直到獲得較為滿意的結果為止。
三、廣義線性回歸
基本語法
邏輯回歸,再根據結果進行
model <- glm(因變量~自變量1 +自變量2 + 自變量3..., data = 數據框, family = binomial)
泊松回歸:修改family
老師原文地址:https://notebooks.azure.com/YukWang/libraries/rDataAnalysis