【问题标题】:How to automate variable selection in glmnet and cross validation如何在 glmnet 和交叉验证中自动选择变量
【发布时间】:2013-08-24 04:32:43
【问题描述】:

我正在学习使用glmnetbrnn 包。考虑以下代码:

library(RODBC)
library(brnn)
library(glmnet)
memory.limit(size = 4000)
z <-odbcConnect("mydb") # database with Access queries and tables

# import the data
f5 <- sqlFetch(z,"my_qry")

# head(f5)

# check for 'NA'
sum(is.na(f5))

# choose a 'locn', up to 16 of variable 'locn' are present
f6 <- subset(f5, locn == "mm")
# dim(f6)

# use glmnet to identify possible iv's

training_xnm <- f6[,1:52] # training data
xnm <- as.matrix(training_xnm)
y <- f6[,54] # response

fit.nm <- glmnet(xnm,y, family="binomial", alpha=0.6, nlambda=1000,standardize=TRUE,maxit=100000)
# print(fit.nm)

# cross validation for glmnet to determine a good lambda value
cv.fit.nm <- cv.glmnet(xnm, y)

# have a look at the 'min' and '1se' lambda values
cv.fit.nm$lambda.min
cv.fit.nm$lambda.1se
# returned $lambda.min of 0.002906279, $lambda.1se of 2.587214

# for testing purposes I choose a value between 'min' and '1se'
mid.lambda.nm = (cv.fit.nm$lambda.min + cv.fit.nm$lambda.1se)/2

print(coef(fit.nm, s = mid.lambda.nm)) # 8 iv's retained

# I then manually inspect the data frame and enter the column index for each of the iv's
# these iv's will be the input to my 'brnn' neural nets

cols <- c(1, 3, 6, 8, 11, 20, 25, 38) # column indices of useful iv's

# brnn creation: only one shown but this step will be repeated
# take a 85% sample from data frame
ridxs <- sample(1:nrow(f6), floor(0.85*nrow(f6)) ) # row id's
f6train <- f6[ridxs,] # the resultant data frame of 85%
f6train <-f6train[,cols] # 'cols' as chosen above

# For the 'brnn' phase response is a binary value, 'fin'
# and predictors are the 8 iv's found earlier
out = brnn( fin ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8, data=f6train, neurons=3,normalize=TRUE, epochs=500, verbose=FALSE)
#summary(out)

# see how well the net predicts the training cases
pred <- predict(out)

以上脚本运行正常。

我的问题是:如何自动化上述脚本以针对locn 的不同值运行,这基本上就是我如何概括获得步骤:cols &lt;- c(1, 3, 6, 8, 11, 20, 25, 38) # column indices of useful iv's。目前我可以手动执行此操作,但看不到如何针对locn 的不同值以一般方式执行此操作,例如

locn.list <- c("am", "bm", "cm", "dm", "em")  
for(j in 1:5) {
this.locn <- locn.list[j]
# run the above script
}

【问题讨论】:

  • 看起来不可能对您的数据进行任何测试,但您应该立即了解,在标记后使用“(”会使 R 查找具有该名称的函数。可能想要locn.list[j] .j&lt;-1 行看起来完全是多余的。
  • 感谢 DWin 的评论:我的错,错字,是的,我同意 j
  • 感谢您的评论 DWin:我的错,错字,是的,我同意 j
  • 当您同意代码中存在错误时,您应该编辑您的问题。如果您能清楚地了解如何使数据集可用,我会对这个问题感兴趣。
  • 感谢 DWin,我已按照您的建议编辑了我的帖子。

标签: r glmnet


【解决方案1】:

自从发布我的问题后,我发现了 Simon、Friedman、Hastie 和 Tibshirani 的一篇论文:Coxnet:Regularized Cox Regression,其中介绍了如何提取我想要的内容。

本文中的一些相关细节并适用于我的数据(lambda 的符号除外!): 我们可以检查我们的模型选择了哪些协变量处于活动状态,并查看这些协变量的系数。

coef(fit.nm, s = cv.fit.nm$lambda.min) # returns the p length coefficient vector

对应的解的lambda =cv.fit$lambda.min.

Coefficients <- coef(fit.nm, s = cv.fit.nm$lambda.min)
Active.Index <- which(Coefficients != 0)
Active.Coefficients <- Coefficients[Active.Index]

Active.Index # identifies the covariates that are active in the model and
Active.Coefficients # shows the coefficients of those covariates

希望这对其他人有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 2013-11-08
    • 2017-12-03
    • 2016-05-16
    • 2017-07-15
    • 2018-03-20
    • 2016-09-01
    • 2019-09-07
    相关资源
    最近更新 更多