我正在研究使用套索作为选择特征并将预测模型与二值目标拟合的方法。以下是我正在尝试使用正则化逻辑回归的方法的一些代码。
我的问题是我得到一组“重要”变量,但是我能够对这些变量进行排序以估计每个变量的相对重要性吗?为此,可以通过绝对值对系数进行标准化(我知道它们通过coef
函数以原始可变比例显示)?如果是这样,如何进行(使用x和y的标准偏差)标准化回归系数。
样本代码:
library(glmnet)
#data comes from
#http://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)
datasetTest <- read.csv('C:/Documents and Settings/E997608/Desktop/wdbc.data.txt',head=FALSE)
#appears to use the first level as the target success
datasetTest$V2<-as.factor(ifelse(as.character(datasetTest$V2)=="M","0","1"))
#cross validation to find optimal lambda
#using the lasso because alpha=1
cv.result<-cv.glmnet(
x=as.matrix(dataset[,3:ncol(datasetTest)]),
y=datasetTest[,2],
family="binomial",
nfolds=10,
type.measure="deviance",
alpha=1
)
#values of lambda used
histogram(cv.result$lambda)
#plot of the error measure (here was deviance)
#as a CI from each of the 10 folds
#for each value of lambda (log actually)
plot(cv.result)
#the mean cross validation error (one for each of the
#100 values of lambda
cv.result$cvm
#the value of lambda that minimzes the error measure
#result: 0.001909601
cv.result$lambda.min
log(cv.result$lambda.min)
#the value of lambda that minimzes the error measure
#within 1 SE of the minimum
#result: 0.007024236
cv.result$lambda.1se
#the full sequence was fit in the object called cv.result$glmnet.fit
#this is same as a call to it directly.
#here are the coefficients from the min lambda
coef(cv.result$glmnet.fit,s=cv.result$lambda.1se)