stacks::predict(member=T) .When multiplying members by coeff, the result does not match the .pred.

I decided to combine the models using ranger and glmnet.

From several models, only two had coefficients.

> collect_parameters(stack=fit_stack, 
+                    candidates="ranger_model")
# A tibble: 10 x 3
   member            min_n  coef
   <chr>             <int> <dbl>
 1 ranger_model_1_01    34 0    
 2 ranger_model_1_02    19 0    
 3 ranger_model_1_03    24 0    
 4 ranger_model_1_04     4 0.798
 5 ranger_model_1_05    27 0    
 6 ranger_model_1_06    16 0    
 7 ranger_model_1_07    39 0    
 8 ranger_model_1_08     9 0    
 9 ranger_model_1_09    30 0    
10 ranger_model_1_10    11 0    
> collect_parameters(stack=fit_stack, 
+                    candidates="glmnet_model")
# A tibble: 1 x 2
  member            coef
  <chr>            <dbl>
1 glmnet_model_1_1 0.285

I used coefficient to see if it matched .pred, and it did not.

predict(fit_stack, test, members = TRUE) %>% 
  mutate(makepred =(glmnet_model_1_1*0.285) + (ranger_model_1_04*0.798))

# A tibble: 733 x 5
   .pred glmnet_model_1_1 ranger_model_1_04 makepred `.pred - makepred`
   <dbl>            <dbl>             <dbl>    <dbl>              <dbl>
 1  5.13             5.03              5.18     5.57             -0.434
 2  5.29             5.29              5.29     5.73             -0.434
 3  5.29             5.29              5.28     5.72             -0.434

How do I calculate it to match?
Where do I check the coefficients of the bias term?

I think those are only candidate coefficients. I think fit_members should be used.

There is an intercept term to add in.

stacks actually makes the predictions from an R expression with embedded coefficients (as opposed to using predict.glmnet()). You can find that in the equations sub-object.

Here's an example from ?blend_predictions:

library(stacks)

reg_st <- 
 stacks() %>%
 add_candidates(reg_res_lr) %>%
 add_candidates(reg_res_svm) %>%
 add_candidates(reg_res_sp) %>%
 blend_predictions()
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip

reg_st$equations$numeric$.pred
#> -198.86340517531 + (reg_res_svm_1_5 * 2.63664094173829) + (reg_res_svm_1_3 * 
#>     0.674976091964984) + (reg_res_svm_1_1 * 0.302283813193429) + 
#>     (reg_res_sp_2_1 * 0.235986731464408)

Created on 2021-10-28 by the reprex package (v2.0.0)

2 Likes

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.