LARS-LASSO回归的应用
2022-06-09 本文已影响0人
azhsrdj
R 中 lars包的介绍
- lars()
lars(x, y, type = c("lasso", "lar", "forward.stagewise", "stepwise"), trace = FALSE, normalize = TRUE, intercept = TRUE, Gram, eps = 1e-12, max.steps, use.Gram = TRUE)
x matrix of predictors
y response
type One of "lasso", "lar", "forward.stagewise" or "stepwise". The names can be
abbreviated to any unique substring. Default is "lasso".
//type : 表示所使用的回归方法,包括(lasso, lar, forward.stagewise, stepwise),选择不同的回归方法将得到不同的解路径;默认是Lasso
trace If TRUE, lars prints out its progress
normalize If TRUE, each variable is standardized to have unit L2 norm, otherwise it is left alone. Default is TRUE.
// normalize : 表示是否对变量进行归一化,当为TRUE时,程序将对x和y进行L2正则化;默认是TRUE
intercept if TRUE, an intercept is included in the model (and not penalized), otherwise no
intercept is included. Default is TRUE.
// intercept ---- 表示是否对变量进行中心化,当为TRUE时,程序将对x和y分别减去其均值。默认是TRUE
Gram The X’X matrix; useful for repeated runs (bootstrap) where a large X’X stays
the same.
eps An effective zero, with default 1e-12. If lars() stops and reports NAs, consider
increasing this slightly.
max.steps Limit the number of steps taken; the default is 8 * min(m,n-intercept), with
m the number of variables, and n the number of samples. For type="lar"
or type="stepwise", the maximum number of steps is min(m,n-intercept).
For type="lasso" and especially type="forward.stagewise", there can be
many more terms, because although no more than min(m,n-intercept) variables can be active during any step, variables are frequently droppped and added
as the algorithm proceeds. Although the default usually guarantees that the algorithm has proceeded to the saturated fit, users should check.
use.Gram When the number m of variables is very large, i.e. larger than N, then you may
not want LARS to precompute the Gram matrix. Default is use.Gram=TRUE.
- cv.lars()
cv.lars(x, y, K = 10, index, trace = FALSE, plot.it = TRUE, se = TRUE, type = c("lasso", "lar", "forward.stagewise", "stepwise"), mode=c("fraction", "step"), ...)
Arguments
x Input to lars
y Input to lars
K Number of folds
index Abscissa values at which CV curve should be computed. If mode="fraction"
this is the fraction of the saturated |beta|. The default value in this case is
index=seq(from = 0,to = 1,length =100). If mode="step", this is the number of steps in lars procedure. The default is complex in this case, and depends
on whether N>p or not. In principal it is index=1:p. Users can supply their own
values of index (with care).
// K :表示在进行交叉验证时,将数据随机分为K份,每次使用其中K-1份作为训练数据,用剩下的一份进行验证,最后计算这K次验证的均方误差;
trace Show computations?
plot.it Plot it?
se Include standard error bands?
type type of lars fit, with default "lasso"
mode This refers to the index that is used for cross-validation. The default is "fraction"
for type="lasso" or type="forward.stagewise". For type="lar" or type="stepwise"
the default is "step"
// mode : 表示用到的参数指标, step即按步数step去选择所需的参数,fraction即按照path中的横坐标|beta|/max|beta|去选择所需变量,fraction默认为0:100:1即 index=seq(from=0,to=1,length=100).
... Additional arguments to lars
- predict.lars()
predict(object, newx, s, type = c("fit", "coefficients"), mode = c("step", "fraction", "norm", "lambda"), ...)
object A fitted lars object
newx If type="fit", then newx should be the x values at which the fit is required. If
type="coefficients", then newx can be omitted.
s a value, or vector of values, indexing the path. Its values depends on the mode=
argument. By default (mode="step"), s should take on values between 0 and p
(e.g., a step of 1.3 means .3 of the way between step 1 and 2.)
type If type="fit", predict returns the fitted values. If type="coefficients", predict
returns the coefficients. Abbreviations allowed.
mode
//type ---- 当type为"fit"时, 可以给定一个新的样本newx,则该函数返回通过lars回归模型得到的预测值; 当type为"coefficient"时,则不需要输入newx, 该函数返回模型的回归系数;
Mode="step" means the s= argument indexes the lars step number, and the coefficients will be returned corresponding to the values corresponding to step s. If
mode="fraction", then s should be a number between 0 and 1, and it refers to the
ratio of the L1 norm of the coefficient vector, relative to the norm at the full LS
solution. Mode="norm" means s refers to the L1 norm of the coefficient vector.
Mode="lambda" uses the lasso regularization parameter for s; for other models
it is the maximal correlation (does not make sense for lars/stepwise models).
// 当mode选定以后, 就可以在 s 中输入给定的参数值.(可以用向量的形式输入多个参数)
Abbreviations allowed.
... Any arguments for predict.lars should work for coef.lars
- summary.lars() 和 plot.lars()
演示数据集 diabetes 介绍
library(lars)
data(diabetes)
attach(diabetes)
diabetes 有三个向量 x, y, x2 三个和442个观测值。
其中x包含了age、sex等10个变量,y是结果变量;x2包含了64个变量,x2.age, x2.sex, x2.age^2, x2.age:sex等,变量自身的平方和两两交互项。
操作过程
# blog中用 type=lar 为例
library(lars)
data(diabetes)
attach(diabetes)
1. 交叉验证
作者分别用step和fraction方式分别作交叉验证
data(diabetes)
attach(diabetes)
# 作者用的是
cvsol1<-cv.lars(x2,y,type="lar",mode="step")
cvsol1<-cv.lars(x2,y, index=1:65, type="lar",mode="step")
detach(diabetes)
cvsol1$index[which.min(cvsol1$cv)]
# 作者的结果是15,我的结果是17
data(diabetes)
attach(diabetes)
cvsol2 <- cv.lars(x2,y,type="lar", index=seq(from = 0,to = 1,length =100), mode="fraction" )
detach(diabetes)
cvsol2$index[which.min(cvsol2$cv)]
结果是 0.03030303
2. 计算求解路径
object <- lars(x,y,type="lar")
# 作者的求解参数很简单。
object <- lars(x2,y,type="lar")
object$beta[15,]
sum(abs(object$beta[15,])/sum(abs(object$beta[65,])))
结果是0.3183121
用step求得的
3. 利用回归模型进行预测
predict(object,newx=x2[1:10,], s=17, type="fit", mode= "step")
# result:
# $s
# [1] 18
# $fraction
# [1] 0.265625
# $mode
# [1] "step"
# $fit
# 1 2 3 4 5 6 7 8
# 202.70457 81.19421 179.09791 168.50446 120.41225 124.65107 78.91868 144.84991
# 9 10
# 158.30079 206.70851
参照https://blog.csdn.net/nya0731/article/details/79895307
在此感谢博主nya0731