目錄
3.2.1 盒形圖的離羣點分析,需要根據四分位距去掉部分數據點。
與單元線性迴歸的模型相比,誤差減小了,下面繼續看看殘差分佈。
線性迴歸是一種基本的機器學習算法,用於基於一個或多個自變量預測數字因變量,而因變量(Y)應該是連續的。這裏將描述如何在Julia中構建線性迴歸,構建模型後如何診斷。
1、線性迴歸概述
線性迴歸是一種最基本的機器學習算法,用於基於一個或多個自變量預測因變量。因變量(Y)應該是連續的。線性迴歸找到最能描述Y變量作爲X變量(特徵)函數的數學方程。方程一旦形成,就可以用來預測只有X已知時Y的值。 這一數學方程可概括如下:
𝑌=𝛽1+𝛽2𝑋+𝜖
其中,𝛽1爲截距,𝛽2爲斜率。如果只有一個X變量,則稱爲“簡單線性迴歸”。如果涉及多個預測因子(X),則稱爲“多元線性迴歸”。不管怎樣,它們的構建過程基本是相同。 𝛽i 稱爲迴歸係數,𝜖爲誤差項 且是 迴歸模型最不好解釋的部分。
2、數據準備
這將使用預期壽命的數據來做迴歸分析。目標是根據不同的特徵和人口統計數據預測各國人民的預期壽命。
數據集下載地址:預期壽命數據。
using DataFrames
using CSV
using Plots
using Lathe
using GLM
using Statistics
using StatsPlots
using MLBase
ENV["COLUMNS"] = 1000
df = DataFrame(CSV.File("D:/Life-Expectancy-Data.csv"))
3、數據探索
在建模之前,首先要分析下數據,看下數據的矩陣大小,檢查下是否有missing 值或者異常值。
3.1簡單的數據清洗
去掉missing值,如果是線性迴歸,使用中值或者均值替代missing,會影響分析結果。
#數據框的規格檢查
println(size(df))
#(2938, 22)
#規範化列名,很多列名會帶有空格或者其他特殊字符
colnames = Symbol[]
for i in string.(names(df))
push!(colnames,Symbol(replace(replace(replace(strip(i)," " => "_"),"-" => "_"), "/" => "_")))
end
rename!(df, colnames);
#數據概要統計分析,比如mean,min,max,median,
#當然還有幾個非常重要的信息 ,nunique:w唯一值統計數,nmissing,missing值統計數
ds = describe(df)
names(df)
gd = groupby(df, [:Life_expectancy,],sort= false,skipmissing=true)
fh = DataFrame(gd)
# dropmissing!(fh)
數據概要分析
│ Row │ variable │ mean │ min │ median │ max │ nunique │ nmissing │ eltype │
│ │ Symbol │ Union… │ Any │ Union… │ Any │ Union… │ Union… │ Type │
├─────┼─────────────────────────────────┼───────────┼─────────────┼───────────┼────────────┼─────────┼──────────┼─────────────────────────┤
│ 1 │ Country │ │ Afghanistan │ │ Zimbabwe │ 183 │ │ String │
│ 2 │ Year │ 2007.52 │ 2000 │ 2008.0 │ 2015 │ │ │ Int64 │
│ 3 │ Status │ │ Developed │ │ Developing │ 2 │ │ String │
│ 4 │ Life_expectancy │ 69.319 │ 44.3 │ 72.1 │ 89.0 │ │ 0 │ Union{Missing, Float64} │
│ 5 │ Adult_Mortality │ 163.921 │ 1 │ 144.0 │ 723 │ │ 0 │ Union{Missing, Int64} │
│ 6 │ infant_deaths │ 30.3879 │ 0 │ 3.0 │ 1800 │ │ │ Int64 │
│ 7 │ Alcohol │ 4.62009 │ 0.01 │ 3.77 │ 17.87 │ │ 193 │ Union{Missing, Float64} │
│ 8 │ percentage_expenditure │ 742.76 │ 0.0 │ 66.5539 │ 19479.9 │ │ │ Float64 │
│ 9 │ Hepatitis_B │ 80.968 │ 1 │ 92.0 │ 99 │ │ 544 │ Union{Missing, Int64} │
│ 10 │ Measles │ 2423.73 │ 0 │ 17.0 │ 212183 │ │ │ Int64 │
│ 11 │ BMI │ 38.3002 │ 1.0 │ 43.5 │ 77.6 │ │ 32 │ Union{Missing, Float64} │
│ 12 │ under_five_deaths │ 42.1162 │ 0 │ 4.0 │ 2500 │ │ │ Int64 │
│ 13 │ Polio │ 82.6002 │ 3 │ 93.0 │ 99 │ │ 19 │ Union{Missing, Int64} │
│ 14 │ Total_expenditure │ 5.91715 │ 0.37 │ 5.73 │ 17.6 │ │ 226 │ Union{Missing, Float64} │
│ 15 │ Diphtheria │ 82.4039 │ 2 │ 93.0 │ 99 │ │ 19 │ Union{Missing, Int64} │
│ 16 │ HIV_AIDS │ 1.71779 │ 0.1 │ 0.1 │ 50.6 │ │ │ Float64 │
│ 17 │ GDP │ 7523.47 │ 1.68135 │ 1779.5 │ 119173.0 │ │ 443 │ Union{Missing, Float64} │
│ 18 │ Population │ 1.27937e7 │ 34.0 │ 1.37887e6 │ 1.29386e9 │ │ 644 │ Union{Missing, Float64} │
│ 19 │ thinness__1_19_years │ 4.84456 │ 0.1 │ 3.3 │ 27.7 │ │ 32 │ Union{Missing, Float64} │
│ 20 │ thinness_5_9_years │ 4.8755 │ 0.1 │ 3.3 │ 28.6 │ │ 32 │ Union{Missing, Float64} │
│ 21 │ Income_composition_of_resources │ 0.628373 │ 0.0 │ 0.678 │ 0.948 │ │ 160 │ Union{Missing, Float64} │
│ 22 │ Schooling │ 12.012 │ 0.0 │ 12.4 │ 20.7 │ │ 160 │ Union{Missing, Float64} │
3.2 圖形分析數據
3.2.1 盒形圖的離羣點分析,需要根據四分位距去掉部分數據點。
想來看下概念解析:
四分位距(interquartile range, IQR),又稱四分差,四分位距通常是用來構建箱形圖,以及對概率分佈的簡要圖表概述。是描述統計學中的一種方法,以確定第三四分位數和第一四分位數的區別。與方差、標準差一樣,表示統計資料中各變量分散情形,但四分差更多爲一種穩健統計(robust statistic)。
分位數是將總體的全部數據按大小順序排列後,處於各等分位置的變量值。如果將全部數據分成相等的兩部分,它就是中位數;如果分成四等分,就是四分位數;八等分就是八分位數等。四分位數也稱爲四分位點,它是將全部數據分成相等的四部分,其中每部分包括25%的數據,處在各分位點的數值就是四分位數。四分位數有三個,第一個四分位數就是通常所說的四分位數,稱爲下四分位數,第二個四分位數就是中位數,第三個四分位數稱爲上四分位數,分別用Q1、Q2、Q3表示 [2] 。
第一四分位數 (Q1),又稱“較小四分位數”,等於該樣本中所有數值由小到大排列後第25%的數字。
第二四分位數 (Q2),又稱“中位數”,等於該樣本中所有數值由小到大排列後第50%的數字。
第三四分位數 (Q3),又稱“較大四分位數”,等於該樣本中所有數值由小到大排列後第75%的數字。
第三四分位數與第一四分位數的差距又稱四分位距(InterQuartile Range,IQR)。公式:IQR = Q3 − Q1,
#####基於盒形圖的離羣點分析
# Box Plot
boxplot(fh.Life_expectancy, title = "Box Plot - Life Expectancy", ylabel = "Life Expectancy (years)", legend = false)
#很明顯的看到有一些 異常值,需要刪除這些數據點。
#第25百分位數又稱第一個四分位數
first_percentile = percentile(fh.Life_expectancy, 25)
#公式:IQR = Q3 − Q1,四分位距(interquartile range, IQR),又稱四分差,四分位距通常是用來構建箱形圖,以及對概率分佈的簡要圖表概述。是描述統計學中的一種方法,以確定第三四分位數和第一四分位數的區別。與方差、標準差一樣,表示統計資料中各變量分散情形,但四分差更多爲一種穩健統計(robust statistic)。
iqr_value = iqr(fh.Life_expectancy)
df = fh[fh.Life_expectancy .> (first_percentile - 1.5*iqr_value),:];
3.2.2 密度圖進行分佈分析
density(df.Life_expectancy , title = "Density Plot - Life Expectancy", ylabel = "Frequency", xlabel = "Life Expectancy", legend = false)
當y變量爲正態分佈或接近正態分佈時,線性迴歸效果良好。下面是y變量的分佈,即預期壽命。分佈確實有幾個轉折點,這說明分佈是混合的。然而,總體分佈確實像正太分佈的曲線, 爲了更好的理解,繼續看下一個分析。
3.2.3 散點圖相關分析
當y變量與x變量線性相關時,線性迴歸效果比較好。下面有幾個概念需要先理解下,然後再看相關係數和散點圖,
VAR(方差) 、COR(相關係數) 、COV(協方差)
方差:體現的是一組數據的波動情況,值越小波動越小。詳細參閱:方差
協方差:兩種不同數據的方差,體現兩組數據的變化趨勢如何,正值變化趨勢一致,負值變化趨勢相反,0不相關。詳細參閱:協方差
相關係數:從概率論的角度,分析兩組不同數據的相關程度,取值範圍[-1,1],越接近與0越不相關,0時卻不意味着兩組數據獨立,相關係數是兩個變量之間的線性關聯的一個度量,不一定有因果關係的含義。詳細參閱:相關係數
println("Correlation of Life Expectancy with Adult Mortality Rate is ", cor(df.Adult_Mortality,df.Life_expectancy), "\n\n")
# Scatter plot
train_plot = scatter(df.Adult_Mortality,df.Life_expectancy, title = "Scatter Plot Life Expectancy vs Adult Mortality Rate", ylabel = "Life Expectancy", xlabel = "Adult Mortality Rate",legend = false)
這兩個特徵看起來是線性相關的,但仍然有一些點分佈在其他位置。在後面我們構建具有多個特性的迴歸模型時,也許其他一些特徵能解釋這種關係。
4、數據預處理
數據預處理是建模的重要步驟之一,在前面的步驟中,已經處理了部分異常數據。這裏主要針對帶有分類的列進行分析。
4.1、獨熱編碼(One Hot Encoding)
獨熱編碼是將類別變量轉換爲多個數字列的過程,因爲有類別。這樣,變量就可以被輸入到ML算法中,從而更好地進行預測。對於每一個特徵,如果它有m個可能值,那麼經過獨熱編碼後,就變成了m個二元特徵(如成績這個特徵有好,中,差變成one-hot就是100, 010, 001)。數據集中存在兩個分類列需要處理。Country列有183個獨特的值,分類太多了,只能放棄。相反,對Status列進行獨熱編碼。
scaled_feature = Lathe.preprocess.OneHotEncode(df,:Status)
select!(df, Not([:Status,:Country]))
first(df,5)
4.2、按照比例分割爲“測試集”和“訓練集”
#安裝比例拆分爲“測試集”和“訓練集”
using Lathe.preprocess: TrainTestSplit
train, test = TrainTestSplit(df,.75)
5、建立模型
這裏使用GLM軟件包建立線性迴歸模型,它與R中的“GLM”包非常相似。先從單個變量開始。要使用GLM訓練線性迴歸模型,需要使用lm()函數,它接受公式對象作爲第一個參數,使用@formula創建所需的公式對象。
fm = @formula(Life_expectancy ~ Adult_Mortality)
linearRegressor = lm(fm, train)
# StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}
#
# Life_expectancy ~ 1 + Adult_Mortality
#
# Coefficients: 一些評價指標,重點看P值,也就是Pr(>|t|)
# ──────────────────────────────────────────────────────────────────────────────────
# Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
# ──────────────────────────────────────────────────────────────────────────────────
# (Intercept) 78.1938 0.245709 318.237 <1e-99 77.7119 78.6757
# Adult_Mortality -0.053974 0.00119445 -45.1874 <1e-99 -0.0563164 -0.0516316
# ──────────────────────────────────────────────────────────────────────────────────
6 檢驗標準(模型診斷)
這裏給出一些概念:
P值 ,P值越小,表明結果越顯著,詳細參考:P值
T 值:是t檢驗bai的統計量值,t檢驗,亦稱student t檢驗(duStudent's t test),主要用於樣本含量較小(例如n < 30),總體標準差σ未知的正態分佈。 t檢驗是用t分佈理論來推論差異發生的概率,從而比較兩個平均數的差異是否顯著。t值是通過將β係數(X變量的權重)除以其標準誤差來計算的統計度量。這裏的標準誤差是對β係數偏差的估計。t值越大說明β係數等於零的可能性越大,所以,t值越高越好。 參考:T值
R平方:
R平方是一種統計測度,它告訴我們在這個模型中,因變量(y)的變化比例是由不同的特徵(自變量)解釋的。R平方值越高越好,最大值爲1。 參考:R平方
# R平方值爲,
r2(linearRegressor)
#> 0.4548557168286279
可以看到上面的擬合出的模型已經給出了一些評價指標的值:
# “estimate”是指已獲得相關的數據,用某個確定的樣本值而得到的一個確切地估計值。
#Std. Error 標準誤差,均方根誤差是用來衡量觀測值同真值之間的偏差,當樣本容量n足夠大時,標準差趨於穩定。標準誤差隨着n的增大而減小,甚至趨近於0。
# t value,t值,指的是T檢驗,主要用於樣本含量較小(例如n<30),總體標準差σ未知的正態分佈資料。T檢驗是用t分佈理論來推論差異發生的概率,從而比較兩個平均數的差異是否顯著。
# Pr(>|t|) P值,雙側檢驗p值
# Lower 95% Upper 95% 置信區間
額外給出一些其他的檢驗標準:
STATISTIC |
CRITERION |
R-Squared |
Higher the better |
Adj R-Squared |
Higher the better |
F-Statistic |
Higher the better |
Std. Error |
Closer to zero the better |
t-statistic |
Should be greater 1.96 for p-value to be less than 0.05 |
AIC |
Lower the better |
BIC |
Lower the better |
Mallows cp |
Should be close to the number of predictors in model |
MAPE (Mean absolute percentage error) |
Lower the better |
MSE (Mean squared error) |
Lower the better |
Min_Max Accuracy => mean(min(actual, predicted)/max(actual, predicted)) |
Higher the better |
7、 模型預測、評價、交叉驗證
涉及到的概念: 參考: 常用的評價誤差指標有以下幾種,RMSE、MSE、MAE、MAPE、SMAPE
(1)平均誤差(MAE)
(2)平均絕對百分比誤差(MAPE)
(3)均方根誤差(RMSE)
(4)均方誤差(MSE)
(5)誤差分佈,是否符合正太分佈
在獲取性能度量數據時,將研究訓練集數據和測試集數據的評價指標。現在模型已經準備好了,讓我們對訓練集數據和測試數據以及cmopute平方誤差進行預測。
7.1 模型的預測與評價
7.1.1預測
訓練集和測試集都要預測
# 預測 Prediction
ypredicted_test = predict(linearRegressor, test)
ypredicted_train = predict(linearRegressor, train)
7.1.2 計算誤差
先是平方誤差
# 測試數據集評估(計算平方誤差)
performance_testdf = DataFrame(y_actual = test[!,:Life_expectancy], y_predicted = ypredicted_test)
performance_testdf.error = performance_testdf[!,:y_actual] - performance_testdf[!,:y_predicted]
performance_testdf.error_sq = performance_testdf.error.*performance_testdf.error
# 訓練數據集(計算平方誤差)
performance_traindf = DataFrame(y_actual = train[!,:Life_expectancy], y_predicted = ypredicted_train)
performance_traindf.error = performance_traindf[!,:y_actual] - performance_traindf[!,:y_predicted]
performance_traindf.error_sq = performance_traindf.error.*performance_traindf.error ;
誤差函數
#MAPE
function mape(performance_df)
mape = mean(abs.(performance_df.error./performance_df.y_actual))
return mape
end
# RMSE
function rmse(performance_df)
rmse = sqrt(mean(performance_df.error.*performance_df.error))
return rmse
end
根據上面的誤差函數 計算誤差
# 測試數據集 誤差
println("Mean Absolute test error: ",mean(abs.(performance_testdf.error)), "\n")
println("Mean Aboslute Percentage test error: ",mape(performance_testdf), "\n")
println("Root mean square test error: ",rmse(performance_testdf), "\n")
println("Mean square test error: ",mean(performance_testdf.error_sq), "\n")
# Mean Absolute test error: 4.871609770224901
# Mean Aboslute Percentage test error: 0.07455633832726753
# Root mean square test error: 6.89521671872878
# Mean square test error: 47.54401359823688
# 訓練數據集 誤差
println("Mean train error: ",mean(abs.(performance_traindf.error)), "\n")
println("Mean Absolute Percentage train error: ",mape(performance_traindf), "\n")
println("Root mean square train error: ",rmse(performance_traindf), "\n")
println("Mean square train error: ",mean(performance_traindf.error_sq), "\n")
# Mean train error: 4.726124200320006
# Mean Absolute Percentage train error: 0.07258363410210457
# Root mean square train error: 6.716975968260544
# Mean square train error: 45.117766158189674
誤差分佈
錯誤不應該有任何模式,它應該遵循正態分佈。這裏使用直方圖對訓練和測試數據集進行殘差分析
# 誤差柱狀圖,看它是否在測試數據集上正態分佈, Histogram of error to see if it's normally distributed on test dataset
histogram(performance_testdf.error, bins = 50, title = "Test Error Analysis", ylabel = "Frequency", xlabel = "Error",legend = false)
# 誤差柱狀圖,看它是否在訓練數據集上正態分佈, Histogram of error to see if it's normally distributed on train dataset
histogram(performance_traindf.error, bins = 50, title = "Training Error Analysis", ylabel = "Frequency", xlabel = "Error",legend = false)
通過上面的柱狀圖 可以看到誤差是正態分佈的,但仍然有一些異常值。下面使用散點圖查看實際值和預測值。分析誤差是沒有特定的形式的
# 測試數據集
test_plot = scatter(performance_testdf[!,:y_actual],performance_testdf[!,:y_predicted], title = "Predicted value vs Actual value on Test Data", ylabel = "Predicted value", xlabel = "Actual value", legend = false)
# 訓練數據集
train_plot = scatter(performance_traindf[!,:y_actual],performance_traindf[!,:y_predicted], title = "Predicted value vs Actual value on Train Data", ylabel = "Predicted value", xlabel = "Actual value",legend = false)
可以看到有一些奇怪的點,這裏很難數據來解釋上面的問題,稍後將在下一節稍後解釋這一點。
7.2 交叉驗證 Cross Validation
如果擬合出的模型在25%的分割率(測試數據)時表現良好,但並不足以說明模型在任何時候都會表現得同樣出色。
這是什麼意思呢?一個好的模型在新數據(X vars)上的表現應該和在訓練數據上的表現一樣好。如果在新數據集上(測試數據和任何未來數據)的預測出的數據和實際數據差異過大,則表明該模型可能過度擬合。也就是說,它對訓練數據的解釋過於複雜,不夠籠統。
因此,儘可能嚴格地交叉驗證模型的準確性是非常重要的。 進行這種嚴格測試的一種方法是在不同的互斥數據塊上進行訓練和測試,並觀察模型方程的準確性是否一樣好。 這就是交叉驗證的目的。下面是具體的實現。
# 交叉驗證函數,可以使用下面函數 RMSE、MSE、MAE、MAPE、SMAPE
function cross_validation(train,k, fm = @formula(Life_expectancy ~ Adult_Mortality))
a = collect(Kfold(size(train)[1], k))
for i in 1:k
row = a[i]
temp_train = train[row,:]
temp_test = train[setdiff(1:end, row),:]
linearRegressor = lm(fm, temp_train)
performance_testdf = DataFrame(y_actual = temp_test[!,:Life_expectancy], y_predicted = predict(linearRegressor, temp_test))
performance_testdf.error = performance_testdf[!,:y_actual] - performance_testdf[!,:y_predicted]
println("Mean Aboslute Percentage test error: ",mape(performance_testdf), "\n")
end
end
cross_validation(train,10)
驗證結果如下:
# Mean Aboslute Percentage test error: 0.07138211558037776
# Mean Aboslute Percentage test error: 0.07010161078533023
# Mean Aboslute Percentage test error: 0.08265067731693981
# Mean Aboslute Percentage test error: 0.06696041707392626
# Mean Aboslute Percentage test error: 0.08010228310894138
# Mean Aboslute Percentage test error: 0.06245662468846822
8、Julia多元線性迴歸
到目前爲止,只使用一個自變量來實現線性迴歸。但在實際情況下,會有多個自變量,無論是單變量還是多變量,它們的核心概念是一樣的,下面是具體的實現。這裏使用7個特性來構建迴歸模型。
8.1 、去掉missing 值,並重新劃分數據集
#數據清洗
gd = groupby(df, [:Life_expectancy,:Adult_Mortality,:infant_deaths,:Developing,:BMI,:Total_expenditure,:HIV_AIDS,:Income_composition_of_resources],sort= false,skipmissing=true)
fh = DataFrame(gd)
fh = fh[:,[:Life_expectancy,:Adult_Mortality,:infant_deaths,:Developing,:BMI,:Total_expenditure,:HIV_AIDS,:Income_composition_of_resources]]
disallowmissing(fh)
train, test = TrainTestSplit(fh,.75)
# 迴歸模型
fm2 = @formula(Life_expectancy ~ Adult_Mortality + infant_deaths + Developing + BMI + Total_expenditure + HIV_AIDS + Income_composition_of_resources)
linearRegressor = lm(fm2, train)
#R 平方
r2(linearRegressor)
與單元線性迴歸的模型相比,誤差減小了,下面繼續看看殘差分佈。
8.2、模型診斷
計算誤差
function predict2(linearRegressor::RegressionModel,test::DataFrame,train::DataFrame)
# 診斷
ypredicted_test = predict(linearRegressor, test)
ypredicted_train = predict(linearRegressor, train)
# 測試數據集
performance_testdf = DataFrame(y_actual = test[!,:Life_expectancy], y_predicted = ypredicted_test)
performance_testdf.error = performance_testdf[!,:y_actual] - performance_testdf[!,:y_predicted]
performance_testdf.error_sq = performance_testdf.error.*performance_testdf.error
# 訓練數據集
performance_traindf = DataFrame(y_actual = train[!,:Life_expectancy], y_predicted = ypredicted_train)
performance_traindf.error = performance_traindf[!,:y_actual] - performance_traindf[!,:y_predicted]
performance_traindf.error_sq = performance_traindf.error.*performance_traindf.error ;
# 測試數據集誤差
println("Mean Absolute test error: ",mean(abs.(performance_testdf.error)), "\n")
println("Mean Aboslute Percentage test error: ",mape(performance_testdf), "\n")
println("Root mean square test error: ",rmse(performance_testdf), "\n")
println("Mean square test error: ",mean(performance_testdf.error_sq), "\n")
# 訓練數據集誤差
println("Mean train error: ",mean(abs.(performance_traindf.error)), "\n")
println("Mean Aboslute Percentage train error: ",mape(performance_traindf), "\n")
println("Root mean square train error: ",rmse(performance_traindf), "\n")
println("Mean square train error: ",mean(performance_traindf.error_sq), "\n")
end
predict2(linearRegressor,test,train)
# Mean Absolute test error: 3.19108586769138
# Mean Aboslute Percentage test error: 0.04889948491362247
# Root mean square test error: 4.474131472086835
# Mean square test error: 20.01785242951791
# Mean train error: 3.265500683664497
# Mean Aboslute Percentage train error: 0.050029246118041305
# Root mean square train error: 4.4325406761905635
# Mean square train error: 19.647416846083896
所有特徵的p值、t值和R平方值均具有統計學意義。從診斷學的角度來看,這是一個很好的模型。比前一個好。下面看看這個模型在預測中的準確性。與單元線性迴歸的模型相比,誤差減小了,下面繼續看看殘差分佈。
殘差分佈
# 測試數據集殘差分佈
histogram(performance_testdf.error, bins = 50, title = "Test Error Analysis", ylabel = "Frequency", xlabel = "Error",legend = false)
# 訓練數據集的殘差分佈
histogram(performance_traindf.error, bins = 50, title = "Training Error Analysis", ylabel = "Frequency", xlabel = "Error",legend = false)
這幾乎是正態分佈的,說明誤差分佈也比以前的模型好。下面用散點圖看看預測值
預測值與實際值非常接近。之前看到的一些怪異的數據點 現在也能通過自變量解釋了。
8.3、交叉驗證
cross_validation(train,10, fm)
# Mean Aboslute Percentage test error: 0.052744242203262945
# Mean Aboslute Percentage test error: 0.04686934760263142
# Mean Aboslute Percentage test error: 0.04728274263246261
# Mean Aboslute Percentage test error: 0.052746085003455706
# Mean Aboslute Percentage test error: 0.047812645635019635
# Mean Aboslute Percentage test error: 0.051726285166176805
# Mean Aboslute Percentage test error: 0.048795829636545066
# Mean Aboslute Percentage test error: 0.0491212004243017
# Mean Aboslute Percentage test error: 0.049672209036192565
# Mean Aboslute Percentage test error: 0.05783432064238129
明顯要比單變量的線性迴歸誤差小很多。
。。。。。待續