I am trying to model some data using the glmnet package in R. Let's say I have the following data

training_x <- data.frame(variable1 = c(1, 2, 3, 2, 3),
                         variable2 = c(1, 2, 3, 4, 5))
y <- c(1, 2, 3, 4, 5)

(This is a simplification; my data are much more complicated.) Then I used the following code to create the glmnet model.

x <- as.matrix(training_x)
GLMnet_model_1 <- glmnet(x, y, family="gaussian", alpha=0.755,
                         nlambda=1000, standardize=FALSE, maxit=100000)

I am using standardize=FALSE because my real life data are already standardized. Then I want to make prediction over a new set of data. Let's say my new data are:

newdata <- as.matrix(data.frame(variable1 = c(2, 2, 1, 3), 
                                variable2 = c(6, 2, 1, 3)))
results <- predict(object=GLMnet_model_1, newx, type="response")

I would expect results to contain 4 elements (predictions of the newdata), but instead it gives me a 4x398 matrix. What am I doing wrong?


1 Answer 1


You need to specify for which value of lambda you want to predict the response. All you need to do is to call like like e.g.:

results <-predict(GLMnet_model_1, s=0.01, newx, type="response")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.