When i use the predict glmnet function, i get the error mentioned below the code.
mydata <- read.csv("data.csv")
x <- mydata[,1:4]
y <- mydata[,5]
data <- cbind(x,y)
model <- model.matrix(y~., data=data)
ridgedata <- model[,-1]
train <- sample(1:dim(ridgedata)[1], round(0.8*dim(ridgedata)[1]))
test <- setdiff(1:dim(ridgedata)[1],train)
x_train <- data[train, ]
y_train <- data$y[train]
x_test <- data[test, ]
y_test <- data$y[test]
k=5
grid =10^seq(10,-2, length =100)
fit <- cv.glmnet(model,y,k=k,lambda = grid)
lambda_min <- fit$lambda.min
fit_test <- predict(fit, newx=x_test,s=lambda_min)
The error is as follows:
Error in
as.matrix(cbind2(1, newx) %*% nbeta)
: error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error incbind2(1, newx) %*% nbeta
: not-yet-implemented method for<data.frame> %*% <dgCMatrix>
I tried debugging, but i am not sure where the
as.matrix(cbind2(1, newx) %*% nbeta)
code is being used and what is causing this error.
x_test <- as.matrix(data[test, ])
? – Ben Bolker