0

I am doing ridge regression model using cv.glmnet(), but the knit (to HTML) outputs are very different than console outputs. I already used set.seed() function but it doesn't work. Here's the code I wrote:

set.seed(90)
lambdas <- 10^seq(2, -3, by = -.1) # list of lambdas to find out the best one for the model
fit <- cv.glmnet(training_data_X, training_data_Y, alpha = 0, lambda = lambdas) # fit the model
lambda_optimal <- min(fit$lambda) # get the optimal lambda according to the fitted model
fit_optimal <- glmnet(training_data_X, training_data_Y, alpha = 0, lambda = lambda_optimal) # fit a model again with optimal lambda

test_data_Y$pred <- exp(predict(fit_optimal, s = lambda_optimal, newx = test_data_X))
sst_test <- sum((test_data_Y$truth_values - mean(test_data_Y$truth_values))^2)
sse_test <- sum((test_data_Y$truth_values - test_data_Y$pred)^2)
r_square_test <- 1 - sse_test / sst_test
r_square_test # R-squared value of the test set

The R-Squared value is very much different from the console output. And when I checked the test_data_Y table, I see that my predictions and truth values are also different from the console values.

How can I solve this issue?

Thank you in advance.

tr1umph
  • 5
  • 4
  • Try setting the seed on the line just before running cv.glmnet(). Also, it'd help if you share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Desmond May 23 '22 at 01:49

0 Answers0