Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active February 13, 2024 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brshallo/7eed49c743ac165ced2294a70e73e65e to your computer and use it in GitHub Desktop.
Save brshallo/7eed49c743ac165ced2294a70e73e65e to your computer and use it in GitHub Desktop.
The RMSE intervall method is based on the solution suggested on cross validated: https://stats.stackexchange.com/a/78318/193123
library(palmerpenguins)
library(dplyr)

#' @param rmse Root mean squared error on your sample
#' @param df Degrees of Freedom in your model. In this case it should be the
#'   same as the number of observations in your sample.
rmse_interval <- function(rmse, deg_free, p_lower = 0.025, p_upper = 0.975){
  tibble(.pred_lower = sqrt(deg_free / qchisq(p_upper, df = deg_free)) * rmse,
         .pred_upper = sqrt(deg_free / qchisq(p_lower, df = deg_free)) * rmse)
}

penguins <- palmerpenguins::penguins %>% 
  na.omit()

split <- rsample::initial_split(penguins)
train <- rsample::training(split)
holdout <- rsample::testing(split)

mod <- lm(body_mass_g ~ bill_length_mm + bill_depth_mm + sex + species, data = train)

preds <- bind_cols(
  tibble(.pred = predict(mod, holdout)),
  relocate(holdout, body_mass_g)
)

# rmse example
preds %>% 
  summarise(rmse = yardstick::rmse_vec(body_mass_g, .pred), 
            n = n()) %>% 
  mutate(rmse_interval(rmse, n))
#> # A tibble: 1 x 4
#>    rmse     n .pred_lower .pred_upper
#>   <dbl> <int>       <dbl>       <dbl>
#> 1  327.    83        284.        386.

# example just adding column for standard error#
preds %>% 
  summarise(n = n(),
            mae = yardstick::mae_vec(body_mass_g, .pred)) %>% 
  mutate(mae_se = mae / sqrt(n),
         mae_lower = mae - 1.96 * mae_se,
         mae_upper = mae + 1.96 * mae_se)
#> # A tibble: 1 x 5
#>       n   mae mae_se mae_lower mae_upper
#>   <int> <dbl>  <dbl>     <dbl>     <dbl>
#> 1    83  244.   26.8      192.      297.

Created on 2021-03-17 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment