Skip to contents

This function extracts residuals from a fast regression model table (fast_regression()).

Usage

extract_regression_residuals(.model_tbl, .pivot_long = FALSE)

Arguments

.model_tbl

A fast regression model specification table (fst_reg_spec_tbl).

.pivot_long

A logical value indicating if the output should be pivoted. The default is FALSE.

Value

The function returns a list of data frames, each containing residuals, actual values, and predicted values for a specific model.

Details

The function checks if the input model specification table inherits the class 'fst_reg_spec_tbl' and if it contains the column 'pred_wflw'. It then manipulates the data, grouping it by model, and extracts residuals for each model. The result is a list of data frames, each containing residuals, actual values, and predicted values for a specific model.

Author

Steven P. Sanderson II, MPH

Examples

library(recipes, quietly = TRUE)

rec_obj <- recipe(mpg ~ ., data = mtcars)

fr_tbl <- fast_regression(mtcars, rec_obj, .parsnip_eng = c("lm","glm"),
.parsnip_fns = "linear_reg")

extract_regression_residuals(fr_tbl)
#> [[1]]
#> # A tibble: 32 × 4
#>    .model_type     .actual .predicted   .resid
#>    <chr>             <dbl>      <dbl>    <dbl>
#>  1 lm - linear_reg    15         14.1  0.872  
#>  2 lm - linear_reg    21         22.0 -0.999  
#>  3 lm - linear_reg    33.9       30.3  3.57   
#>  4 lm - linear_reg    22.8       28.3 -5.46   
#>  5 lm - linear_reg    15.8       17.6 -1.76   
#>  6 lm - linear_reg    15.5       15.7 -0.193  
#>  7 lm - linear_reg    21.4       21.2  0.205  
#>  8 lm - linear_reg    19.7       19.7  0.00718
#>  9 lm - linear_reg    15.2       16.4 -1.24   
#> 10 lm - linear_reg    10.4       11.5 -1.05   
#> # ℹ 22 more rows
#> 
#> [[2]]
#> # A tibble: 32 × 4
#>    .model_type      .actual .predicted   .resid
#>    <chr>              <dbl>      <dbl>    <dbl>
#>  1 glm - linear_reg    15         14.1  0.872  
#>  2 glm - linear_reg    21         22.0 -0.999  
#>  3 glm - linear_reg    33.9       30.3  3.57   
#>  4 glm - linear_reg    22.8       28.3 -5.46   
#>  5 glm - linear_reg    15.8       17.6 -1.76   
#>  6 glm - linear_reg    15.5       15.7 -0.193  
#>  7 glm - linear_reg    21.4       21.2  0.205  
#>  8 glm - linear_reg    19.7       19.7  0.00718
#>  9 glm - linear_reg    15.2       16.4 -1.24   
#> 10 glm - linear_reg    10.4       11.5 -1.05   
#> # ℹ 22 more rows
#> 
extract_regression_residuals(fr_tbl, .pivot_long = TRUE)
#> [[1]]
#> # A tibble: 96 × 3
#>    .model_type     name        value
#>    <chr>           <chr>       <dbl>
#>  1 lm - linear_reg .actual    15    
#>  2 lm - linear_reg .predicted 14.1  
#>  3 lm - linear_reg .resid      0.872
#>  4 lm - linear_reg .actual    21    
#>  5 lm - linear_reg .predicted 22.0  
#>  6 lm - linear_reg .resid     -0.999
#>  7 lm - linear_reg .actual    33.9  
#>  8 lm - linear_reg .predicted 30.3  
#>  9 lm - linear_reg .resid      3.57 
#> 10 lm - linear_reg .actual    22.8  
#> # ℹ 86 more rows
#> 
#> [[2]]
#> # A tibble: 96 × 3
#>    .model_type      name        value
#>    <chr>            <chr>       <dbl>
#>  1 glm - linear_reg .actual    15    
#>  2 glm - linear_reg .predicted 14.1  
#>  3 glm - linear_reg .resid      0.872
#>  4 glm - linear_reg .actual    21    
#>  5 glm - linear_reg .predicted 22.0  
#>  6 glm - linear_reg .resid     -0.999
#>  7 glm - linear_reg .actual    33.9  
#>  8 glm - linear_reg .predicted 30.3  
#>  9 glm - linear_reg .resid      3.57 
#> 10 glm - linear_reg .actual    22.8  
#> # ℹ 86 more rows
#>