# Create random x (predictors) and y (binary)
x = matrix(rnorm(25000), ncol = 50)
y = 1 * (plogis(apply(x[,1:5], 1, sum) + rnorm(500, 0, 0.1)) > 0.5)
# Predict y via cross-validation
fit_fun = function (x_training, y_training) {
list(
lasso = glmnet_fit(x_training, y_training, family = "binomial")
)
}
predict_fun = function (m, x_test) {
glmnet_predict(m$lasso, x_test)
}
# Only 2 folds to ensure the example runs quickly
res = cv(x, y, family = "binomial", fit_fun = fit_fun, predict_fun = predict_fun, nfolds = 2)
# Show the main model
lasso = glmnet_get.main.model(res$models, "lasso")
cat(
"Model: ~plogis(", round(lasso$a0, 2), "+",
paste0(round(lasso$beta, 2), "*", names(lasso$beta), collapse = " + "),
")\n"
)
Run the code above in your browser using DataLab