简体   繁体   English

提取由r中的glmnet或lars软件包选择的变量

[英]Extracting variables chosen by glmnet or lars packages in r

I'm dealing with a large p small n problem (p>1000, n=150) and have decided to use glmnet and lars to select possible explanatory variables (as I understand is their functionality) rather than do this manually. 我正在处理一个大的p小n问题(p> 1000,n = 150),并决定使用glmnet和lars选择可能的解释变量(据我所知是它们的功能),而不是手动执行。

I have gotten out both a lars, and a glmnet object from the functions, but now can't figure out how to extract from those objects, the names of the coefficients / the optimal model, that the functions have chosen. 我已经从函数中获得了lars和glmnet对象,但是现在无法弄清楚如何从这些对象中提取函数选择的系数名称/最佳模型。

Any help would be greatly appreciated! 任何帮助将不胜感激!

That's what the coef function is for. 这就是coef函数的作用。 See ?coef.glmnet for more details. 有关更多详细信息,请参见?coef.glmnet

# fit a glmnet object
obj <- glmnet(x, y)

# matrix of coefficients
# each row is a variable, each column is one step in the glmnet path
coef(obj)

# coefficients for a specific model in the glmnet sequence
coef(obj, s=obj$lambda[1])
coef(obj, s=0.01)

I also think this should be easier, but here is one way assuming you know which spot one the regularization path you want to use, ie you know the Lambda of interest. 我也认为这应该更容易一些,但是这是一种假设您知道要使用正则化路径的位置的一种方法,即您知道感兴趣的Lambda。

mod <- glmnet(x, y)
LambdaIndex <- 50 # just randomly picking one of the 100 Lambas tried
nonZerosIndices <- mod$beta[, LambdaIndex]!=0 
nonZeroVars <- names(mod$beta[nonZerosIndices, LambdaIndex])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM