Skip to content

Commit

Permalink
lasso code chunk edit, Alan comments carpentries-incubator#115
Browse files Browse the repository at this point in the history
  • Loading branch information
mallewellyn authored Mar 1, 2024
1 parent 2c8f3c7 commit 8ba4898
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _episodes_rmd/03-regression-regularisation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,11 @@ We can use this new idea to choose a lambda value by finding the lambda
that minimises the error across each of the test and training splits. In R:

```{r lasso-cv, fig.cap="Cross-validated mean squared error for different values of lambda under a LASSO penalty.", fig.alt="Alt"}
##Fit lasso
lasso <- cv.glmnet(methyl_mat[, -1], age, alpha = 1)
plot(lasso)
##Extract the coefficients and return those that are non-zero
coefl <- coef(lasso, lasso$lambda.min)
selected_coefs <- as.matrix(coefl)[which(coefl != 0), 1] ; selected_coefs
```
Expand Down

0 comments on commit 8ba4898

Please sign in to comment.