目錄
0 前言
1 Linear Model
1.1 OLS(Ordinary Least Squares)
1.2 Regression Shrinkage Models
? 1.2.1 Ridge
? 1.2.2 LASSO
? 1.2.3 Elastic Net
2 Non-linear Model
2.1 廣義上為linear model的non-linear ones
2.2 Tree model
3 Model Evaluation
3.1 Metrics
3.2 Cross Validation
0 前言
Regression的目的是對一個(gè)數(shù)值變量進(jìn)行預(yù)測。預(yù)測的方法有很多種,對應(yīng)不同的model,可以分為Linear Model和Non-linear Model。
1 Linear Model
廣義的linear model的公式如下,也就是,我們想要估計(jì)的函數(shù)是變量
們的線性組合,此處的
其實(shí)可以是
、
等:
但是由于對參數(shù)的estimation method的不同,linear model又可以分成好幾類。
1.1 OLS(Ordinary Least Squares)
估計(jì)方法
OLS是比較常用的一種linear model。估計(jì)方法是最小化residual sum of squares(RSS)來得到參數(shù)的估計(jì)值。
如果要使得OLS estimator是unbiased,需要滿足幾個(gè)假設(shè):
-
是
的線性組合
-
是白噪聲即
,相互之間獨(dú)立并和
獨(dú)立
OLS的統(tǒng)計(jì)意義上的好處在于,Gauss-Markov Theorem告訴我們,他算出來的參數(shù)estimator是所有unbiased linear estimator中最好的。
幾何意義
OLS存在幾何直覺:要找到躺在由X向量張成的hyperplane上的,其實(shí)就是
在這個(gè)hyperplane上的投影。一般來說,如果你想在子空間中找到某個(gè)點(diǎn)來表示高維空間中的某個(gè)點(diǎn),我們能做到最好的就是把那個(gè)點(diǎn)投影到子空間中。

數(shù)據(jù)解釋
-
p-value
p-value的背后是對變量coefficient的hypothesis testing:
根據(jù)維基百科,p-value是the probability of obtaining results at least as extreme as the observed results。我們帶入觀測值,p-value為發(fā)生比觀測值還極端的事件幾率。 p-value越小,我們有更強(qiáng)的證據(jù)不接受原假設(shè),即接受。
-
confidence interval
OLS reult table 會給出變量coefficient的95%置信區(qū)間。因?yàn)閚oise的存在,所以每一個(gè)變量coefficient都是有自己的standard error。 -
R squared/ adjuested R squared
R2取值介于0和1,趨向于0表示model幾乎無法解釋Y的變化,趨向于1表示model能比較好地解釋Y的變化。
為了通過剔除變量個(gè)數(shù)的影響而較好地比較不同ols model,我們引入adjuested R squared,它懲罰了對不重要的解釋變量地引入。其數(shù)值比R2小。 -
F-statistic
F-statistic一般大于10就說明model是對Y有解釋力度的。
變量選擇
為了不讓模型過于復(fù)雜導(dǎo)致over-fitting,從一堆變量中選擇合適的幾個(gè)變量是一個(gè)難點(diǎn)。
“Selecting variables in regression models is a complicated problem, and there are many conflicting views on which type of variable selection procedure is best, e.g. LRT, F-test, AIC, and BIC.”
Scott Zeger on 'how to pick the wrong model': Turn your scientific problem over to a computer that, knowing nothing about your science or your question, is very good at optimizing AIC, BIC, ...
此處等我有更多經(jīng)驗(yàn)會繼續(xù)填坑。
-
One step by one step
Forward stepwise selection
Backward stepwise selection - Business sence
- Shrinkage method
一些實(shí)操的問題
-
Qualitative Predictors類別變量
如果變量只有2個(gè)取值(0/1),就在data processing的時(shí)候把它轉(zhuǎn)換成dummy variable。
如果變量有n個(gè)取值,就generate n-1個(gè)dummy。 -
Interaction term交乘項(xiàng)
變量之間會互相影響,導(dǎo)致系數(shù)估計(jì)會偏差,所以我們必要時(shí)要引入交乘項(xiàng)。比如如下bmi30對expense的影響,是會因?yàn)樯矸莶煌煌模莝moker的人,bmi30每增加一單位,expense增加:
-
Multicollinearity多重共線性
多重共線性會導(dǎo)致參數(shù)的估計(jì)有偏,例如有一些變量參數(shù)不顯著,所以在回歸之前最好看一下correlation matrix,剔除相關(guān)系數(shù)較大的一些變量。 -
Non-constant variance of error terms (heteroscedasticity)異方差
異方差會導(dǎo)致參數(shù)的估計(jì)有偏。異方差主要出現(xiàn)在time series。為了解決這個(gè)問題,可以對被解釋變量Y做transformation,比如取,
來穩(wěn)定Y的方差。
-
Dependence of the error terms
殘差互不獨(dú)立
殘差互不獨(dú)立會影響參數(shù)估計(jì),特別是置信區(qū)間估計(jì)。在time series中,解決辦法為ARIMA。在cross-section數(shù)據(jù)中,除了考慮我們是不是遺漏了什么變量之外,也可以注意experiment design。 -
Outliers極端值
極端值主要會影響置信區(qū)間估計(jì)。在確定沒有什么重要的遺漏變量導(dǎo)致極端值之后,我們更應(yīng)該注意這類極端值為什么發(fā)生,它背后可能會有重要的商業(yè)因素。 -
High leverage points高杠桿點(diǎn)
高杠桿點(diǎn)就是在“杠桿兩邊”,對estimated line產(chǎn)生重大影響的點(diǎn),是X取值比較unusual的那類點(diǎn)。解決它,第一,可以用rubust regression;第二,在考慮到它的確不影響商業(yè)問題的基礎(chǔ)上drop它。
image.png
1.2 Regression Shrinkage Models
為什么我們需要Shrinkage Models?OLS雖然常用,但是可能會出現(xiàn)over-fitting的問題,針對這個(gè)問題shrinkage models通過引入penalty term來優(yōu)化out-of-sample performance。Regression Shrinkage Models在犧牲了bias的時(shí)候減少了variance。同時(shí),其中的LASSO還可以“自動(dòng)”篩選出比較重要的變量,尤其適合n<p的小樣本。
1.2.2 Ridge Regression
估計(jì)方法
Ridge最小化的目標(biāo)方程是RSS加上一個(gè)penalty term(regularization term),用來降低的值的大小從而simplify model:
其中是tuning parameter,可以通過cross validation尋找最優(yōu)的值。注意:
并不完全是0(not sparse)。
直覺上來說,如果coeff被限制得越小,那么Y就會對feature的微小變動(dòng)不那么敏感,也就是說model的variance會變小。
1.2.1 LASSO Regression
估計(jì)方法
Ridge的一個(gè)劣勢是不能讓參數(shù)完全為0,這樣使得model還是有些復(fù)雜。LASSO通過把L2約束改成L1約束,可以解決這個(gè)問題。
實(shí)操問題
在跑ridge和LASSO之前,由于penalty term的存在,predictor的scale會很大程度上影響參數(shù)的估計(jì)值,所以跑回歸之前一定要standardize predictors!

的選擇
先選出a grid of potential values,然后對每一個(gè)value算出cross validation的error rate,最后選擇error rate最小的那一個(gè)帶入model。
1.2.3 Elastic Net
Elastic Net是Ridge和LASSO的合體,L1和L2 form都用上了。

2 Non-linear Model
2.1 廣義上為linear model的non-linear ones
經(jīng)過轉(zhuǎn)化,一些含有多次項(xiàng)和指數(shù)項(xiàng)的model可以看成linear model,用OLS進(jìn)行參數(shù)估計(jì)。
- Polynomial regression
-
Log-transformation
2.2 Tree model
3 Model Evaluation
3.1 Metrics
R-Squared
? The value designates the total proportion of variance in the dependent variable explained by the independent variable.
? Between 0 and 1; the value toward 1 indicates a better model fit.
? For example, 0.97 means 97% of variability in the dependent variable can be explained by the independent variable.
Root Mean Squared Error (RMSE)

The square root of the mean of the squared errors.
? RMSE indicates how close the predicted values are to the actual values; hence a lower RMSE value signifies that the model performance is good.
? One of the key properties of RMSE is that the unit will be the same as the target variable.
Mean Absolute Error (MAE)

? The mean or average of absolute value of the errors, i.e. the predicted minus actual.
3.2 Cross Validation

- Splits the training dataset into k-folds without replacement, i.e. any given data point will only be part of one of the subset, where k-1 (i.e., k minus one) folds are used for the model training and one fold is used for testing.
- The procedure is repeated k times so that we obtain k
models and performance estimates.
- The procedure is repeated k times so that we obtain k
- Calculate the average performance of the models based on the individual folds to obtain a performance estimate that is less sensitive to the sub-partitioning of the training data compared to the holdout or single fold method.
Python code
# Build a decision tree classifier
clf = tree.DecisionTreeClassifier(random_state=2017)
# Evaluate the model using 10-fold cross-validation
train_scores = cross_val_score(clf, X_train, y_train, scoring='accuracy', cv=5)
print ("Train Fold AUC Scores: ", train_scores)
print ("Train CV AUC Score: ", train_scores.mean())






