0
$\begingroup$

I've got a problem with my code, whilst making predictions using glmnet() Here is my code plus an error which occurs:

X_train <- model.matrix(price ~., train)[, -1] 
y_train <- train$price 
#nrow(X_train) ==  length(y_train)

## 10-fold cross-validation in order to find the best labda parameter
set.seed(12345678)
lambda_set <- 10^seq(-3, 5, length.out = 100)
ridge_cv <- cv.glmnet(X_train, 
                      y_train,
                      alpha = 0,
                      lambda = lambda_set,
                      standarize = TRUE,
                      nfolds = 10)
plot(ridge_cv)
lambda_ridge <- ridge_cv$lambda.min # 0.001

## ridge regression model fit for lambda_ridge
ridge_train <- glmnet(X_train,
                      y_train,
                      alpha = 0,
                      lambda = lambda_ridge,
                      standarize = TRUE)

coef(ridge_train) 
y_predict_ridge_train <- predict(ridge_train, X_train)
rmse(y_train, y_predict_ridge_train) # RMSE: 377.8191
sqrt(mean((y_train - y_predict_ridge_train)^2)) # RMSE manual

## ridge regression model validation on validation dataset
X_valid <- model.matrix(price ~ ., validation)[, -1]
validation$price
y_predict_ridge_valid <- predict(ridge_train, X_valid) %>% as.vector()

The error:

Error in cbind2(1, newx) %*% nbeta : 
  Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90

I've checked answers from similar questions posed here, but they didn't solve my issue.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.