Bootstrap for a variable after group by (getting confidence intervals for each group)

28 Views Asked by At

I want to perform the bootstrap function boot() to get the confidence intervals of the variable 'R_CO_GC' after grouping by the column 'BARRIER_POS' which has 3 values (after the filter).

First I defined my median function to enter the boot function:

mediana_boot <- function(x,i){
  return(median(x[i]))
}

After that, I filtered the value I was not interested in from 'BARRIER_POS' and then grouped the data frame by the same variable. Later I used mutate to perform the bootstrap and calculate the confidence intervals. Then I used summarise to get the median, extract the lower and upper intervals, and keep them, alongside the median, in a data frame as a final output:

lr2_analysis_CO_FIL %>%
  filter(BARRIER_POS != 'no_street') %>%
  group_by(BARRIER_POS) %>% 
  mutate(boot_CO = boot.ci(boot(R_CO_GC, mediana_boot, R = 100), conf = 0.95, type = "bca")) %>% 
  summarise(mediana = median(R_CO_GC),
            CI.L = boot_CO[4],
            CI.U = boot_CO[5]) %>%
  data.frame(mediana, CI.L, CI.U)

But sadly I got this error:

Error in cnd_bullet_header(action) : promise already under evaluation: recursive default argument reference or earlier problems?

I don't know how to solve it. Any ideas?

I would also like to add a column referring to the 3 groups (BARRIER_POS) from which I got the median and confidence intervals.

1

There are 1 best solutions below

1
Rui Barradas On

Write a function returning the confidence intervals as a tibble and apply summarise directly to it.

library(boot)
library(tidyverse)

mediana_boot <- function(x,i){
  median(x[i])
}
boot_ci_bca <- function(x) {
  b <- boot(x, mediana_boot, R = 1000L)
  ci <- boot.ci(b, conf = 0.95, type = "bca")$bca[4:5]
  names(ci) <- c("lwr", "upr")
  as_tibble(as.list(ci))
}

iris %>%
  group_by(Species) %>%
  summarise(
    mediana = median(Sepal.Length),
    boot_ci_bca(Sepal.Length)
  )
#> Warning: There was 1 warning in `summarise()`.
#> ℹ In argument: `boot_ci_bca(Sepal.Length)`.
#> ℹ In group 1: `Species = setosa`.
#> Caused by warning in `norm.inter()`:
#> ! extreme order statistics used as endpoints
#> # A tibble: 3 × 4
#>   Species    mediana   lwr   upr
#>   <fct>        <dbl> <dbl> <dbl>
#> 1 setosa         5     4.8  5   
#> 2 versicolor     5.9   5.7  6.05
#> 3 virginica      6.5   6.3  6.7

Created on 2024-03-01 with reprex v2.0.2