CS-7863-Sci-Stat-Proj-6/Schrick-Noah_glmnet.R

20 lines
941 B
R

if (!require("glmnet")) install.packages("glmnet")
library(glmnet)
glm_fcn <- function(train.X, train.y, alpha_p){
glmnet.class.model<-cv.glmnet(as.matrix(train.X), train.y, alpha=alpha_p,
family="binomial", type.measure="class")
glmnet.class.model$lambda.1se
glmnet.class.model$lambda.min
plot(glmnet.class.model)
glmnet.class.coeffs<-predict(glmnet.class.model,type="coefficients")
#glmnet.cc.coeffs # maybe 3 is most important, Excess kurtosis
model.class.terms <- colnames(train.X) # glmnet includes an intercept but we are going to ignore
#nonzero.glmnet.qtrait.coeffs <- model.qtrait.terms[glmnet.qtrait.coeffs@i[which(glmnet.qtrait.coeffs@i!=0)]] # skip intercept if there, 0-based counting
glmnet.df <- data.frame(as.matrix(glmnet.class.coeffs))
glmnet.df$abs_scores <- abs(glmnet.df$lambda.1se)
#dplyr::slice_max(glmnet.df,order_by=abs_scores,n=21)
return(glmnet.df)
}