ML2 - Regression

目录

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的目的是对一个数值变量进行预测。预测的方法有很多种,对应不同的model,可以分为Linear Model和Non-linear Model。

1 Linear Model

广义的linear model的公式如下,也就是,我们想要估计的函数f(X)是变量X们的线性组合,此处的X其实可以是X^2ln(X)等:
f(X)=\beta_{0} + \sum_{j=1}^{p}X_{j}\beta_{j}
但是由于对参数\betaestimation method的不同,linear model又可以分成好几类。

1.1 OLS(Ordinary Least Squares)

估计方法

OLS是比较常用的一种linear model。估计方法是最小化residual sum of squares(RSS)来得到参数的估计值。

\begin {align} \min_{\beta} RSS(\beta)&=\sum_{i=1}^{N}\left(y_i - f(x_i)\right)^2 \\ &=\sum_{i=1}^{N}\left(y_i - \beta_0 -\sum_{j=1}^{p}x_{ij}\beta_{j}\right)^2 \\ \end {align}

如果要使得OLS estimator是unbiased,需要满足几个假设

  1. E[f(X)|X]X的线性组合
  2. \epsilon_i是白噪声即\epsilon_i \sim N(0,\sigma^2),相互之间独立并和X独立

OLS的统计意义上的好处在于,Gauss-Markov Theorem告诉我们,他算出来的参数estimator是所有unbiased linear estimator中最好的。

几何意义

OLS存在几何直觉:要找到躺在由X向量张成的hyperplane上的\hat{y},其实就是y在这个hyperplane上的投影。一般来说,如果你想在子空间中找到某个点来表示高维空间中的某个点,我们能做到最好的就是把那个点投影到子空间中。

数据解释

  • p-value
    p-value的背后是对变量coefficient的hypothesis testing:
    H_0 : \beta_j= 0
    H_a : \beta_j \ne 0

    根据维基百科,p-value是the probability of obtaining results at least as extreme as the observed results。我们带入观测值,p-value为发生比观测值还极端的事件几率。 p-value越小,我们有更强的证据不接受原假设,即接受\beta_j \ne 0
  • confidence interval
    OLS reult table 会给出变量coefficient的95%置信区间。因为noise的存在,所以每一个变量coefficient都是有自己的standard error。
  • R squared/ adjuested R squared

    R2取值介于0和1,趋向于0表示model几乎无法解释Y的变化,趋向于1表示model能比较好地解释Y的变化。

    为了通过剔除变量个数的影响而较好地比较不同ols model,我们引入adjuested R squared,它惩罚了对不重要的解释变量地引入。其数值比R2小。
  • F-statistic
    H_0 : \beta_1=\beta_2=\dots= 0
    H_a : \exists j,\beta_j \ne 0

    F-statistic一般大于10就说明model是对Y有解释力度的。

变量选择

为了不让模型过于复杂导致over-fitting,从一堆变量中选择合适的几个变量是一个难点。

“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, ...

此处等我有更多经验会继续填坑。

  1. One step by one step
    Forward stepwise selection
    Backward stepwise selection
  2. Business sence
  3. Shrinkage method

一些实操的问题

  1. Qualitative Predictors类别变量
    如果变量只有2个取值(0/1),就在data processing的时候把它转换成dummy variable。
    如果变量有n个取值,就generate n-1个dummy。
  2. Interaction term交乘项
    变量之间会互相影响,导致系数估计会偏差,所以我们必要时要引入交乘项。比如如下bmi30对expense的影响,是会因为身份不同而不同的,是smoker的人,bmi30每增加一单位,expense增加\beta_{11}+\beta_{12}
  3. Multicollinearity多重共线性
    多重共线性会导致参数的估计有偏,例如有一些变量参数不显著,所以在回归之前最好看一下correlation matrix,剔除相关系数较大的一些变量。
  4. Non-constant variance of error terms (heteroscedasticity)异方差
    异方差会导致参数的估计有偏。异方差主要出现在time series。为了解决这个问题,可以对被解释变量Y做transformation,比如取log(Y),\sqrt{ Y}来稳定Y的方差。
  5. Dependence of the error terms\epsilon残差互不独立
    残差互不独立会影响参数估计,特别是置信区间估计。在time series中,解决办法为ARIMA。在cross-section数据中,除了考虑我们是不是遗漏了什么变量之外,也可以注意experiment design。
  6. Outliers极端值
    极端值主要会影响置信区间估计。在确定没有什么重要的遗漏变量导致极端值之后,我们更应该注意这类极端值为什么发生,它背后可能会有重要的商业因素。
  7. High leverage points高杠杆点
    高杠杆点就是在“杠杆两边”,对estimated line产生重大影响的点,是X取值比较unusual的那类点。解决它,第一,可以用rubust regression;第二,在考虑到它的确不影响商业问题的基础上drop它。
    image.png

1.2 Regression Shrinkage Models

\begin{equation*}MSE = Bias^2 + Variance\end{equation*}

为什么我们需要Shrinkage Models?OLS虽然常用,但是可能会出现over-fitting的问题,针对这个问题shrinkage models通过引入penalty term来优化out-of-sample performance。Regression Shrinkage Models在牺牲了bias的时候减少了variance。同时,其中的LASSO还可以“自动”筛选出比较重要的变量,尤其适合n<p的小样本。

1.2.2 Ridge Regression

估计方法

Ridge最小化的目标方程是RSS加上一个penalty term(regularization term),用来降低\beta的值的大小从而simplify model:
\begin{equation*} \min_{\beta} \sum_{i=1}^n (y_i - \sum_{j=1}^p x_{ij}\beta_j)^2 + \lambda \sum_{j=1}^p \beta_j^2 \end{equation*}
其中\lambda是tuning parameter,可以通过cross validation寻找最优的值。注意:\beta并不完全是0(not sparse)。

直觉上来说,如果coeff被限制得越小,那么Y就会对feature的微小变动不那么敏感,也就是说model的variance会变小。

1.2.1 LASSO Regression

估计方法

Ridge的一个劣势是不能让参数完全为0,这样使得model还是有些复杂。LASSO通过把L2约束改成L1约束,可以解决这个问题。
\begin{equation*} \min_{\beta} \sum_{i=1}^n(Y_i-\sum_{j=1}^p X_{ij}\beta_j)^2 + \lambda \sum_{j=1}^p|\beta_j| \end{equation*}

实操问题

在跑ridge和LASSO之前,由于penalty term的存在,predictor的scale会很大程度上影响参数的估计值\hat{\beta},所以跑回归之前一定要standardize predictors

\lambda的选择

先选出a grid of potential values,然后对每一个value算出cross validation的error rate,最后选择error rate最小的那一个带入model。

1.2.3 Elastic Net

Elastic Net是Ridge和LASSO的合体,L1和L2 form都用上了。


2 Non-linear Model

2.1 广义上为linear model的non-linear ones

经过转化,一些含有多次项和指数项的model可以看成linear model,用OLS进行参数估计。

  • 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

    1. 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.
    1. The procedure is repeated k times so that we obtain k
      models and performance estimates.
    1. 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())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容