model building 一. variable choice 二. residual analysis 三. form of model 四. build a model...

27
Model building 壹. Variable choice 壹. Residual analysis 壹. Form of model 壹. Build a model with qualitative variables

Post on 22-Dec-2015

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

Model building

.壹 Variable choice

.貳 Residual analysis

.參 Form of model

.肆 Build a model with qualitative variables

Page 2: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

H0: βk=0 ( 其它 βi≠0) , H1: βk≠0.

方法一 : t-test 參考報表 mreg_fat

1.-p-nd.f. ,}.{.

* k

k

bes

bt

方法二 : Partial F-test

註 : F-test 與 t-test 為等價的方法 .

1).-p-n,1(d.f. , )1/(),...,(

)other |(*

11

pnXXSSE

XXSSRF

p

ik

一、 Variable Choice

檢定 Xk 是否可自模式中剔除的方法

若檢定結果為不顯著,意指當模式含有其它 Xi 時, Xk 對 Y 的影響不顯著

Page 3: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

Pearson Correlation Coefficients, N = 20X1 X2 X3 Y

X1表皮厚度 1.00000 0.92384 0.45778 0.84327

X2大腿周長 0.92384 1.00000 0.08467 0.87809

X3上臂粗 0.45778 0.08467 1.00000 0.14244

Y脂肪量 0.84327 0.87809 0.14244 1.00000

例:尋求解釋人體脂肪成因之迴歸式Correlation Matrix :

注意 : X1 與 X2 高度相關

Multicollinearity

當預測變數間高度相關時,對迴歸式的估計可能造成誤判,則稱此現象為多元共線性現象。

Page 4: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

模式中含 MSE b1( p值) (s.e.) b2( p值) (s.e.) b3( p值) (s.e.)X1 7.95 .86(.0001) (0.13)X2 .86(.0001) (0.11)X3 .20(.55)(0.33)X1,X2 6.47 .22(.47) .65(.04)X1,X3 1.0(.0001) -0.43(.02)X2,X3 .85(.0001) .10(.56)X1,X2,X3 6.15 4.3(.17) (1.60) -2.9(.28) (3.02) -2.2(0.19) (2.58)

注意: 1. 加入 X1 至 X2 模式,顯著 p 值 明顯改變 2. 加入 X1 至 (X2 ,X3) 模式,係數與 p 值有明顯改變 3. 加入 X2 至 (X1, X3) 模式,係數與 p 值有明顯改變

各模式的係數及顯著性表皮厚度 大腿周長 上臂粗

Page 5: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

多元共線性存在的影響 :

1. 增加或刪除一預測變數,原係數將有明顯改變。 2. 導致各係數不顯著。 3. 單一係數對反應變數的意義不正確。 4. 係數估計之標準誤較大。

選擇合適的預測變數,才能得到合適的迴歸式

Page 6: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

評估迴歸模式的測值 : Ra2, Cp, AIC, SBC, PRESS 。

目的:假設有 P 個與 Y 相關的潛在預測變數,希望由其中選取最少量個預測變數,得到一足以解釋 Y 變量的迴歸式

選擇預測變數

AIC, SBC criterion

AICp = n ln(SSEp) – n ln(n) + 2p

SBCp = n ln(SSEp) – n ln(n) + ln(n) p

﹡ AIC: Akaike’s information criterion SBC: Schwarz infromation criterion AIC, SBC 常用於時間序列建模,值較低者,模式預測能力較佳

Page 7: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

逐步迴歸 (Stepwise regession)

依據 partial F-test 加入潛在預測變數中對 Y 有顯著影響者,在加入的同時,以 partial F-test 剔除已加入者對 Y 影響不顯著者;直到模式中每一預測變數對 Y 的邊際影響顯著,模式外每一預測變數對 Y邊際影響不顯著為止。

自動搜尋程序:前進選擇法,後退選擇法,逐步迴歸法 (stepwise regression)

前進選擇法只考慮增加 X 變數,不考慮剔除 X 變數。

後退選擇法只考慮剔除 X 變數,不考慮增加 X 變數。

Page 8: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

Summary of Stepwise Procedure for Dependent Variable Y

Variable Number Partial Model

Step Entered Removed In R**2 R**2 C(p) F Prob>F

1 X4 1 0.5274 0.5274 787.9471 58.0214 0.0001

2 X3 2 0.1591 0.6865 507.8069 25.8888 0.0001

3 X2 3 0.1964 0.8829 161.6520 83.8313 0.0001

4 X1 4 0.0895 0.9724 5.0000 158.652 0.0001

5 X4 3 0.0000 0.9723 3.0390 0.039 0.8442

SAS/EG 使用:在 model 項中的 model selection 鉤選屬意的方法,詳細步驟參考 SAS 報表 out,以下為 stepwise regression 步驟之一範例。

Page 9: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

此模式含有四項假設 :

線性、同變異性、獨立性、及常態性。 由差殘可檢視模式是否違背上列假設檢視方法分為圖形觀察及統計檢定。

以考慮的模式做推論之前,應先檢查模式對資料的適當性,通常以殘差為工具,稱為殘差分析。

模式 Yi = β0 + β1 Xi + εi , εi ~ NID( 0, σ2)

二、 Residual analysis

Page 10: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

殘差圖種類 :

• t 化殘差的順序圖 , 盒形圖 , 及常態機率圖 。• 對 Y 、對 X 的殘差圖。

以殘差或 t 化殘差為縱軸的分散圖,或分佈圖稱為的殘差圖。

典型 t 化殘差圖 :

2

0

-2

殘差圖

MSE

ee ii *t 化殘差:

(student residual ,以 MSE 為標準差 , 將 ei 標準化得到的值 )

ˆe iii yy 殘差:

Page 11: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

社區相館例之殘差常態機率圖 :

Page 12: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

殘差圖 :

Page 13: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

1. 非直線模式 X 殘差圖呈曲線

2. 變異數非固定值 X 殘差圖呈梯形

3. 離群值存在 X 殘差圖 , 及殘差盒 形圖出現離群值

殘差圖分析

偏離情況 殘差圖形狀 範例

5. 誤差項非常態性 殘差的常態機率圖 偏離直線

4. 誤差項的不獨立 殘差順序圖分群呈現 ( 如:時間序列資料 ) fig4

fig3

fig2

fig1

Page 14: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

fig1

fig2

Page 15: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

fig4

fig3

Page 16: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

使用迴歸來分析時間序列資料時,誤差項可能依時間先後有相關性,此稱為自相關現象 (autocorrelation) ,此種資料違背獨立性的情況,會表現在殘差圖上,需修正模式。 ( 使用 chap 6 模式 )

【例】 X :產品年銷售量, Y :某公司的年銷售量

X-Y

分散圖 :

R2=0.999

Page 17: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

殘差圖

( 殘差明顯違背獨立性假設 )

Page 18: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

資料的自相關現象對迴歸分析結果產生下列現象:1. 係數的估計量仍為不偏,但無法達到最小變異數。2. MSE 低估真實的誤差變異數。3. s.e.{bk} 低估係數之標準差。

4. t-test , F-test ,及 confidence interval 無法再直接應用。

Page 19: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

一些特殊曲線模式

• 多項式迴歸 ; 如 : E(Y) =β0+β1X1+ β2 X12 。

• 轉換變數 ; 如 : E(log(Y)) =β0+β1X1+β2 X2 。• 含交互作用項 ; 如 : E(Y) =β0+β1X1+β2 X2 + β3X 1 X2 。• 二次完全迴歸式 ;

如 : E(Y)=β0+β1X1+ β2 X12 +β3 X2 + β4 X2

2 + β5X 1 X2

• 質性預測變數 ; 如 : E(Y) =β0+β1X1+β2 X2 , X1 = 0 或 1 。

三、 Form of model

Page 20: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

1 、 Linear Trend model : Yt =β0+β1 t+ εt

時間序列模式

3 、 Exponential model :

可利用 ln 轉換為線性

2 、 Quadratic Trend model : Yt =β0+β1 t + β2 t2 + εt

bt taY ˆ

)(lnlnˆln tbaYt

( 將資料 Y, t 轉換為 ln(Y), ln(t) ,再執行迴歸分析 )

4 、 S-Shaped Model

logistic function limitupper a is L ,))( exp(1 1 t

t t

LY

tt

tY

L )()1ln( 1

Page 21: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

例: The data below gives per capita consumption of malt beverages in the United States in gallons over the period 1950 - 1980. Fit a polynomial model for the data. Which one is a better fit?

Year 1950 1951 …….. 1980

Consump. 17.2 16.8 …….. 23.2

Page 22: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

先後執行直線,二次,三次及四次迴歸式直線,二次,三次的殘差圖:

Parameter Estimates

Variable LabelDF

ParameterEstimate

StandardError t Value Pr > |t| Type I SS

Intercept Intercept 1 17.4 0.308 56.53 <.0001 9743

time 時間 1 -0.157 0.129 -1.22 0.2339 136

tsq 平方 1 -0.0338 0.016 -2.11 0.0448 67.1

tcu 三次方 1 0.00327 0.000747 4.37 0.0002 2.40

t4 四次方 1 -0.00005882 0.00001159 -5.07 <.0001 2.01

四次的參數估計及顯著性檢定:

Page 23: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

以二次式預估時,對 t 的殘差圖,明顯可見仍殘留曲線關係。若以四次式預估,得對一次的 F檢定是不顯著的,且三次式的殘差圖並無曲線趨勢,由二次到四次, R2 的增加率都不明顯,故以三次式預估年需求量是較適當的。

直線,二次,三次及四次迴歸的判定係數

多項式 一次 二次 三次 四次R2 .6496 .9693 .9807 .9903

Ra2 .6376 .9671 .9786 .9888

Page 24: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

Parameter Estimates

Variable LabelDF

ParameterEstimate

StandardError t Value Pr > |t|

Intercept Intercept 1 18.5 0.315 58.67 <.0001

time 時間 1 -0.735 0.0840 -8.76 <.0001

tsq 平方 1 0.0445 0.00605 7.36 <.0001

tcu 三次方 1 -0.000497 0.000124 -4.00 0.0004

迴歸式為:

Y =18.5 – 0.735X + 0.0445 X2 – 0.000497 X3 , R2 = 0.9807

配適圖:

Page 25: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

在迴歸分析中,必需將質性變數量化,以 0或 1的指標變數表示之。

註 : 指標變數 ( Indicator variable ) 是以 0 或 1 識別一質性變數的各組,又稱為虛變數 ( Dummy variable)

數量變數 Quantitative :所得、年齡、溫度、資產、…等。質性變數 Qualitative :性別、購買狀態、職業分類、…等。

一個具 c 項分組的質性變數可用 c-1 個指標變數表示,每一指標變數均取值 0 或 1 。

四、 Qualitative variable model

Page 26: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

例 : 季別銷貨量 (Y) 對季別廣告支出 (X1) 建立一迴歸模式。 需要三個指標變數,定義 :

X2 = 1 ,若為第一季, X2 = 0 ,其他。 X3 = 1 ,若為第二季, X3 = 0 ,其他。 X4 = 1 ,若為第三季, X4 = 0 ,其他。

:

443322110 iiiiii XXXXY 模式

季別時間序列應用

Page 27: Model building 一. Variable choice 二. Residual analysis 三. Form of model 四. Build a model with qualitative variables

模式為四平行線, β1 為斜率,代表廣告的影響,以下的基準線是第四季:

X2 X3 X5

第一季 1 0 0

第二季 0 1 0

第三季 0 0 1

第四季 0 0 0

110 iii XY

相對於第四季, β2 為第一季的影響, β3 為第二季的影響,

β4 為第三季的影響。