Full equation from a stacked model

I'm wondering how to get the full equation from a stacked model, including the intercept? I see one in the last part of section 20.2 (right above the start of 20.3) in tidymodeling with R, but I can't seem to figure out where that might be buried in the results. Any help is appreciated. Thanks!

In case you're curious why I want these, I'm trying to make sure I fully understand where the scores come from when there is a binary response since the "stacked" scores are MUCH lower than the scores from the individual models.

Sure, you can get this pretty easily. Once the stack is blended, there is an equation sub-object that has the generalized linear model prediction equation.

Here's a regression example from the docs:

library(tidymodels)
library(stacks)
library(rlang)
#> 
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, flatten, flatten_chr, flatten_dbl, flatten_int, flatten_lgl,
#>     flatten_raw, invoke, splice

tidymodels_prefer()

# from ?blend_predictions

reg_st <- 
  stacks() %>%
  add_candidates(reg_res_lr) %>%
  add_candidates(reg_res_svm) %>%
  add_candidates(reg_res_sp)

# evaluate the data stack
reg_st <- 
  reg_st %>%
  blend_predictions()

reg_st$equations
#> $numeric
#> $.pred
#> -8.84601130389494 + (reg_res_svm_1_3 * 0.63844261429) + (reg_res_sp_10_1 * 
#>     0.0481870055572203) + (reg_res_sp_03_1 * 0.485948459397137)
#> 
#> attr(,"class")
#> [1] "elnet_numeric"

# An R expression: 
reg_st$equations$numeric$.pred
#> -8.84601130389494 + (reg_res_svm_1_3 * 0.63844261429) + (reg_res_sp_10_1 * 
#>     0.0481870055572203) + (reg_res_sp_03_1 * 0.485948459397137)

# Make into text: 
expr_deparse(reg_st$equations$numeric$.pred, width = 200)
#> [1] "-8.84601130389494 + (reg_res_svm_1_3 * 0.63844261429) + (reg_res_sp_10_1 * 0.0481870055572203) + (reg_res_sp_03_1 * 0.485948459397137)"

Created on 2023-09-16 with reprex v2.0.2

Thanks for the quick response. I looked in the equations output and somehow missed that :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.