LARS-LASSO回归的应用


R 中 lars包的介绍

  1. 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.
  1. 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
  1. 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
  1. 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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,179评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,229评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,032评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,533评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,531评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,539评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,916评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,813评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,568评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,654评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,354评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,937评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,918评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,152评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,852评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,378评论 2 342

推荐阅读更多精彩内容