glmnet

glmnet training throws error on x,y dataframe arguments: am I using it wrong?

坚强是说给别人听的谎言 提交于 2019-12-13 02:16:07
问题 I'm trying to learn a penalized logistic regression method with glmnet. I'm trying to predict if a car from the mtcars example data will have an automatic transmission or manual. I think my code is pretty straightforward, but I seem to be getting an error: This first block simply splits mtcars into an 80% train set and a 20% test set library(glmnet) attach(mtcars) smp_size <- floor(0.8 * nrow(mtcars)) set.seed(123) train_ind <- sample(seq_len(nrow(mtcars)), size=smp_size) train <- mtcars

How to generate all first-order interaction terms for Lasso Logistic Regression?

风格不统一 提交于 2019-12-12 14:26:21
问题 Is there a way in glmnet to do first order interactions? For instance, if my X matrix was: V1 V2 V3 0 1 0 1 0 1 1 0 0 ... Is there a way to specify that it do something along the lines of `y~ V1 + V2 + V3 + V1*V2 + V2 *V3 + V1*V3' without manually creating the columns? My actual matrix is larger and would be a pain to create all first order cross products by hand. 回答1: The proper R syntax for such a formula is y~(V1+V2+V3)^2 For example set.seed(15) dd <- data.frame(V1=runif(50), V2=runif(50)

“Error in drop(y %*% rep(1, nc))” error for cv.glmnet in glmnet R package

岁酱吖の 提交于 2019-12-12 14:03:58
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . I have a function to return the auc value for a cv.glmnet model and it often, although not the majority of the time, returns the following error when executing the cv.glmnet function: Error in drop(y % % rep(1, nc)) : error in evaluating the argument 'x' in selecting a method for function 'drop': Error in y % % rep(1, nc) : non-conformable arguments I've read a little bit about

Extracting non-zero coefficients in glmnet in R

北城以北 提交于 2019-12-11 18:38:52
问题 I'm doing a lasso logistic regression. I've used cv.glmnet to get the non-zero coefficients. And it seems to work i.e. I do get some non-zero coefficients and the rest go to zero. However, when I use coef function to print all coefficients it gives me a list of all coefficients. Is there a way to extract coefficients and their names that are not zero. The code of what I've done is: cv.lasso = cv.glmnet(x_train,y_train, alpha = 0.6, family = "binomial") coef(cv.lasso, s=cv.lasso$lambda.1se)

glmnet not converging for lambda.min from cv.glmnet

雨燕双飞 提交于 2019-12-11 08:35:14
问题 I ran a 20-fold cv.glmnet lasso model to obtain the "optimal" value for lambda. However, when I attempt to reproduce the results from glmnet() , the I get an error that reads: Warning messages: 1: from glmnet Fortran code (error code -1); Convergence for 1th lambda value not reached after maxit=100000 iterations; solutions for larger lambdas returned 2: In getcoef(fit, nvars, nx, vnames) : an empty model has been returned; probably a convergence issue My code reads as such: set.seed(5) cv.out

plot.glmnet increase size of variable labels

此生再无相见时 提交于 2019-12-11 03:08:59
问题 Currently I am using the glmnet package to run a lasso regression (which in the example below is being saved to the "fits" variable. Then when I plot the fits variable it comes up properly but the coefficient labels are very small. Any ideas how I can increase the size of these? Reproducible example below... require(glmnet) #setup sample DF with 5 variables set.seed(123) sampleDF <- data.frame("V1"=rnorm(100,mean=0,sd=.10),"V2"=rnorm(100,mean=0,sd=.10),"V3"=rnorm(100,mean=0,sd=.10),"V4"=rnorm

Ordinary least squares with glmnet and lm

£可爱£侵袭症+ 提交于 2019-12-11 02:38:10
问题 This question was asked in stackoverflow.com/q/38378118 but there was no satisfactory answer. LASSO with λ = 0 is equivalent to ordinary least squares, but this does not seem to be the case for glmnet() and lm() in R. Why? library(glmnet) options(scipen = 999) X = model.matrix(mpg ~ 0 + ., data = mtcars) y = as.matrix(mtcars["mpg"]) coef(glmnet(X, y, lambda = 0)) lm(y ~ X) Their regression coefficients agree by at most 2 significant figures, perhaps due to slightly different termination

Why can't pass only 1 coulmn to glmnet when it is possible in glm function in R?

三世轮回 提交于 2019-12-10 20:45:56
问题 Why there is no possibility to pass only 1 explanatory variable to model in glmnet function from glmnet package when it is possible in glm function from base? Code and error are below: > modelX<-glm( ifelse(train$cliks <1,0,1)~(sparseYY[,40]), family="binomial") > summary(modelX) Call: glm(formula = ifelse(train$cliks < 1, 0, 1) ~ (sparseYY[, 40]), family = "binomial") Deviance Residuals: Min 1Q Median 3Q Max -0.2076 -0.2076 -0.2076 -0.2076 2.8641 Coefficients: Estimate Std. Error z value Pr(

Legend label errors with glmnet plot in R

那年仲夏 提交于 2019-12-10 18:44:50
问题 I modified the function from this post ( Adding labels on curves in glmnet plot in R ) to add legend to the plot as follows: library(glmnet) fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1]) lbs_fun <- function(fit, ...) { L <- length(fit$lambda) x <- log(fit$lambda[L]) y <- fit$beta[, L] labs <- names(y) text(x, y, labels=labs, ...) legend('topright', legend=labs, col=1:length(labs), lty=1) # <<< ADDED BY ME } plot(fit, xvar="lambda") lbs_fun(fit) However, I am getting mismatch between text

Is cv.glmnet overfitting the the data by using the full lambda sequence?

江枫思渺然 提交于 2019-12-09 13:51:30
问题 cv.glmnet has been used by most research papers and companies. While building a similar function like cv.glmnet for glmnet.cr (a similar package that implements the lasso for continuation ratio ordinal regression) I came across this problem in cv.glmnet . `cv.glmnet` first fits the model: glmnet.object = glmnet(x, y, weights = weights, offset = offset, lambda = lambda, ...) After the glmnet object is created with the complete data, the next step goes as follows: The lambda from the complete