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), V3=runif(50), y=runif(50)) lm(y~(V1+V2+V3)^2, dd) Call: lm(formula = y ~ (V1 + V2 + V3)^2, data = dd) Coefficients: (Intercept) V1 V2 V3 V1:V2 V1:V3 V2:V3 0.54169 -0.10030 -0.01226 -0.10150 0.38521 -0.03159 0.01200 Or,...

r,sparse-matrix,dummy-data,glmnet,lasso

I don't think you need a sparse.model.matrix, as all that it really gives you above a regular matrix is expansion of factor terms, and if you're binary already that won't give you anything. You certainly don't need to code as factors, I frequently use glmnet on a regular (non-model) sparse...

Check if sum(y<=0). The code in the function says it will return that error if y's equal 0 as well. Code is here, error you're referencing is on line 5: https://github.com/jeffwong/glmnet/blob/master/R/coxnet.R...

As it looks as if the label size is hard coded (and global changes to cex will change the other plot features) you can change plot.glmnet # copy the plot function myPlot <- plotCoef # replace relevant part body(myPlot)[[14]] <- quote(if (label) { nnz = length(which) xpos = max(index) pos...

Yes, there is a convenient way for that. Two steps in it are important. library(glmnet) # Sample data data <- data.frame(matrix(rnorm(9 * 10), ncol = 9)) names(data) <- c(paste0("x", 1:8), "y") # First step: using .*. for all interactions f <- as.formula(y ~ .*.) y <- data$y # Second step:...

matlab,regression,cross-validation,glmnet,lasso

You can use a leave-one-out approach regardless of your training method. As explained here, you can use crossvalind to split the data into training and test sets. [Train, Test] = crossvalind('LeaveMOut', N, M) ...

Since glmnet doesn't accept the full data frame with a formula (and thus no na.omit), but uses separate response and predictor matrices, you will have to find which values in b are missing, and then subset your predictor matrix to exclude those rows. library(glmnet) set.seed(123) a <- matrix(rnorm(100*20),100,20) b <-...

r,classification,logistic-regression,glmnet

Well no you cannot do that in the function glmnet but you can do that very easily just before you run the function using model.matrix: a <- factor( rep(c("cat1", "cat2", "cat3", "no-cat"),50) ) #make a factor levels(a) <- c("no-cat", "cat1", "cat2", "cat3") #change the order of the levels because #the...

As the labels are hard coded it is perhaps easier to write a quick function. This is just a quick shot, so can be changed to be more thorough. I would also note that when using the lasso there are normally a lot of variables so there will be a...

Try this: fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1], lambda=cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.min) coef(fit) Or you can specify a specify a lambda value in coef: fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1]) coef(fit, s = cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.min) You need to pick a "best" lambda. In this case, I used lambda.min, but you could use cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.1se or any...

r,regression,linear-regression,glmnet

This can be achieved by providing a penalty.factor vector, as described in ?glmnet. A penalty factor of 0 indicates that the "variable is always included in the model", while 1 is the default. glmntfit <- cv.glmnet(mydata[,-1], mydata[, 1], penalty.factor=c(0, rep(1, ncol(mydata) - 2))) ...