美文网首页
岭回归解决多重共线

岭回归解决多重共线

作者: Jason数据分析生信教室 | 来源:发表于2023-11-29 14:25 被阅读0次

    概括

    岭回归是一种简约模型,执行L2正则化。L2正则化添加的惩罚相当于回归系数的平方,并试图将它们最小化。岭回归的方程如下所示:

    LS Obj + λ (sum of the square of coefficients)
    

    这里的目标如下:

    1. 如果 λ = 0,则输出类似于简单线性回归。
    2. 如果 λ = 非常大,回归系数将变为零。

    训练岭回归模型

    要在R中构建岭回归,会用到glmnet包中的glmnet函数。使用mtcars数据集来进行对里程的预测。

    # Loaging the library
    library(glmnet)
    # Getting the independent variable
    x_var <- data.matrix(mtcars[, c("hp", "wt", "drat")])
    # Getting the dependent variable
    y_var <- mtcars[, "mpg"]
    
    # Setting the range of lambda values
    lambda_seq <- 10^seq(2, -2, by = -.1)
    # Using glmnet function to build the ridge regression in r
    fit <- glmnet(x_var, y_var, alpha = 0, lambda  = lambda_seq)
    # Checking the model
    summary(fit)
    
    # Output
              Length Class     Mode
    a0         41    -none-    numeric
    beta      123    dgCMatrix S4
    df         41    -none-    numeric
    dim         2    -none-    numeric
    lambda     41    -none-    numeric
    dev.ratio  41    -none-    numeric
    nulldev     1    -none-    numeric
    npasses     1    -none-    numeric
    jerr        1    -none-    numeric
    offset      1    -none-    logical
    call        5    -none-    call
    nobs        1    -none-    numeric
    

    选择最佳Lambda值

    glmnet 函数会针对所有不同的 lambda 值多次训练模型,我们将这些值作为向量序列传递给 glmnet 函数的 lambda 参数。接下来的任务是自动使用 cv.glmnet() 函数来识别能够导致最小误差的最优 lambda 值。这可以通过交叉验证来实现,交叉验证有助于评估模型在新的、未见过的数据上的泛化能力。

    以下是使用 cv.glmnet() 进行岭回归交叉验证的示例:

    # Using cross validation glmnet
    ridge_cv <- cv.glmnet(x_var, y_var, alpha = 0, lambda = lambdas)
    # Best lambda value
    best_lambda <- ridge_cv$lambda.min
    best_lambda
    
    # Output
    [1] 79.43000
    

    使用K折交叉验证决定最佳模型

    最佳模型可以通过从交叉验证对象中调用 glmnet.fit 来提取。根据Dev值来决定最佳模型,这里可以通过将 lambda 设置为 79.43000 来重新构建模型。

    best_fit <- ridge_cv$glmnet.fit
    head(best_fit)
    
    # Output
          Df   %Dev    Lambda
     [1,]  3 0.1798 100.00000
     [2,]  3 0.2167  79.43000
     [3,]  3 0.2589  63.10000
     [4,]  3 0.3060  50.12000
     [5,]  3 0.3574  39.81000
     [6,]  3 0.4120  31.62000
    

    建立最后的模型

    # Rebuilding the model with optimal lambda value
    best_ridge <- glmnet(x_var, y_var, alpha = 0, lambda = 79.43000)
    

    确认回归系数

    coef(best_ridge)
    
    # Output
    4 x 1 sparse Matrix of class "dgCMatrix"
                          s0
    (Intercept) 20.099502946
    hp          -0.004398609
    wt          -0.344175261
    drat         0.484807607
    

    如果事先有划分训练集和验证集的话也可以通过R2值来检查模型拟合度

    # here x is the test dataset
    pred <- predict(best_ridge, s = best_lambda, newx = x)
    
    # R squared formula
    actual <- test$Price
    preds <- test$PreditedPrice
    rss <- sum((preds - actual) ^ 2)
    tss <- sum((actual - mean(actual)) ^ 2)
    rsq <- 1 - rss/tss
    rsq
    

    相关文章

      网友评论

          本文标题:岭回归解决多重共线

          本文链接:https://www.haomeiwen.com/subject/zmkfgdtx.html