tidymodels / broom

Convert statistical analysis objects from R into tidy format
https://broom.tidymodels.org
Other
1.45k stars 303 forks source link

`boot.tidy` conf.method "norm" fails for anything that does not have exactly 2 results #1212

Open yjml opened 2 weeks ago

yjml commented 2 weeks ago

As per title, tidy.boot(..., conf.method = "norm") will error on any boot object that does not have exactly two results, since apparently that was hardcoded https://github.com/tidymodels/broom/blob/7f3d795c8e52229edba0776993c3cdea5d687b0b/R/boot-tidiers.R#L119

Additionally, the rows/columns of the 2x2 confidence interval matrix are also incorrectly transposed, i.e. in the example below, the result should be (-0.0901, 0.2716) and (-0.294, 0.0794). Should be a rbind and not a cbind I believe?

library(boot)
library(broom)

set.seed(123)
d = data.frame(v1 = rnorm(100), v2=rnorm(100))
boot1 = function(d, i) {
  d = d[i,]
  c(mean(d$v1), mean(d$v2), cor(d$v1, d$v2))
}
boot2 = function(d, i) {
  d = d[i,]
  c(mean(d$v1), mean(d$v2))
}
boot3 = function(d, i) {
  d = d[i,]
  c(mean(d$v1))
}

r1 = boot(d, boot1, 1000)
r2 = boot(d, boot2, 1000)
r3 = boot(d, boot3, 1000)

tidy(r1, conf.int = T, conf.method = "norm")
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 3, 2
tidy(r2, conf.int = T, conf.method = "norm")
#> # A tibble: 2 x 5
#>   statistic      bias std.error conf.low conf.high
#>       <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
#> 1    0.0904 -0.000358    0.0923  -0.0901   -0.294 
#> 2   -0.108  -0.000230    0.0953   0.272     0.0794
tidy(r3, conf.int = T, conf.method = "norm")
#> Error in ci.list[[2]]: subscript out of bounds

### Other methods work correctly ###
tidy(r1, conf.int = T, conf.method = "basic")
#> # A tibble: 3 x 5
#>   statistic      bias std.error conf.low conf.high
#>       <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
#> 1    0.0904  0.00188     0.0889  -0.0906    0.264 
#> 2   -0.108  -0.000687    0.0983  -0.304     0.0864
#> 3   -0.0495  0.000840    0.0964  -0.248     0.137
tidy(r2, conf.int = T, conf.method = "basic")
#> # A tibble: 2 x 5
#>   statistic      bias std.error conf.low conf.high
#>       <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
#> 1    0.0904 -0.000358    0.0923  -0.0932    0.282 
#> 2   -0.108  -0.000230    0.0953  -0.290     0.0826
tidy(r3, conf.int = T, conf.method = "basic")
#> # A tibble: 1 x 5
#>   statistic    bias std.error conf.low conf.high
#>       <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
#> 1    0.0904 0.00119    0.0913  -0.0951     0.269
simonpcouch commented 2 weeks ago

Just noting that I've seen this! Related to #655. Will revisit next time I'm making a sweep through broom issues. :)