I am trying to modify some code that I found in one of the answers on this post:
Extract Formula From lm with Coefficients (R)
AlexB provided these wonderful lines of code:
get_formula <- function(model) {
broom::tidy(model)[, 1:2] %>%
mutate(sign = ifelse(sign(estimate) == 1, ' + ', ' - ')) %>% #coeff signs
mutate_if(is.numeric, ~ abs(round(., 2))) %>% #for improving formatting
mutate(a = ifelse(term == '(Intercept)', paste0('y ~ ', estimate), paste0(sign, estimate, ' * ', term))) %>%
summarise(formula = paste(a, collapse = '')) %>%
as.character
}
Though this works for some of my code, I am having issues adapting it to print formulas from gl.net models using RIDGE, LASSO, and Net Elastic Regression.
library(caret)
library(glmnet)
library(mlbench)
library(psych)
data("BostonHousing")
data <- BostonHousing
set.seed(23)
ind <- sample(2, nrow(data), replace = T, prob = c(0.7, 0.3))
train <- data[ind==1,]
test <- data[ind==2,]
custom <- trainControl(method = "repeatedcv",number = 10,repeats = 5,verboseIter = T)
set.seed(23)
ridge <- train(medv~., train,method = "glmnet",tuneGrid = expand.grid(alpha = 0,lambda = seq(0.0001,1,length = 5)),trControl = custom)
ridge
coef(ridge$finalModel, ridge$bestTune$lambda) # the coefficient estimates
get_formula <- function(model) {
broom::tidy(model)[, 1:2] %>%
mutate(sign = ifelse(sign(estimate) == 1, ' + ', ' - ')) %>% #coeff signs
mutate_if(is.numeric, ~ abs(round(., 2))) %>% #for improving formatting
mutate(a = ifelse(term == '(Intercept)', paste0('y ~ ', estimate), paste0(sign, estimate, ' * ', term))) %>%
summarise(formula = paste(a, collapse = '')) %>%
as.character
}
get_formula(ridge$finalModel)
However, given that it is not in the same format as in the previous post, I am having issues modifying the function so that it can print out the equations I am looking for.
Error: Problem with `mutate()` input `sign`.
x object 'estimate' not found
i Input `sign` is `ifelse(sign(estimate) == 1, " + ", " - ")`.
Run `rlang::last_error()` to see where the error occurred.
Thank kindly for the help.
The broom
package has a tidy
variant for gl.net
- you don't need to index into the tidied data with [, 1:2]
.
Just use tidy(model)
and the rest of the pipe will work just fine.
Here's the key part of the function, taken out for demonstration:
broom::tidy(ridge$finalModel) %>%
mutate(sign = ifelse(sign(estimate) == 1, ' + ', ' - ')) %>% #coeff signs
mutate_if(is.numeric, ~ abs(round(., 2))) %>% #for improving formatting
mutate(a = ifelse(term == '(Intercept)', paste0('y ~ ', estimate), paste0(sign, estimate, ' * ', term)))
# A tibble: 1,400 x 7
term step estimate lambda dev.ratio sign a
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 (Intercept) 1 21.7 6655. 0 " + " y ~ 21.68
2 (Intercept) 2 21.7 6064. 0.01 " + " y ~ 21.73
3 (Intercept) 3 21.7 5525. 0.01 " + " y ~ 21.73
4 (Intercept) 4 21.7 5034. 0.01 " + " y ~ 21.74
5 (Intercept) 5 21.7 4587. 0.01 " + " y ~ 21.74
6 (Intercept) 6 21.8 4180. 0.01 " + " y ~ 21.75
7 (Intercept) 7 21.8 3808. 0.01 " + " y ~ 21.75
8 (Intercept) 8 21.8 3470. 0.01 " + " y ~ 21.76
9 (Intercept) 9 21.8 3162. 0.01 " + " y ~ 21.77
10 (Intercept) 10 21.8 2881. 0.02 " + " y ~ 21.78
# … with 1,390 more rows
Minor note: across
can now replace mutate_if
, eg
mutate(across(where(is.numeric), ~abs(round(., 2))))
With minor updates, you can get the equation of a ridge regression, as follows:
as.matrix(coef(ridge$finalModel, ridge$bestTune$lambda)) %>%
as.data.frame() %>%
tibble::rownames_to_column('term') %>%
rename(estimate = 2) %>%
mutate(sign = ifelse(sign(estimate) == 1, ' + ', ' - ')) %>% #coeff signs
mutate(across(where(is.numeric), ~abs(round(., 2)))) %>% #for improving formatting
mutate(a = ifelse(term == '(Intercept)', paste0('y ~ ', estimate), paste0(sign, estimate, ' * ', term))) %>%
summarise(formula = paste(a, collapse = ''))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.