tidymodels / broom

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

Possible bug with `tidy() ` function on `lm.beta` object #1190

Closed AlvaroTorresMartos closed 4 months ago

AlvaroTorresMartos commented 4 months ago

When using the tidy()function from the broom package with the argument conf.int = TRUEon an object created with the lm() function, the result is correct. Below is the code and output for both cases.

This is not true if you use tidy() with conf.int=TRUE on an object created with lm.beta. In principle, significant beta coefficients must be non-zero, and therefore the confidence interval must not contain zero.

Correct result: Code

library(dplyr) library(lm.beta) library(broom) lm.beta(lm(y ~ x, data = data)) %>% broom::tidy(conf.int = TRUE)

Output

term estimate std.error statistic p.value conf.low conf.high

1 (Intercept) 1.89 0.119 15.8 8.77e-33 1.66 2.13 2 x 1.06 0.292 3.63 4.01e- 4 **0.482 1.64**

Wrong result: Code

lm.beta(lm(y ~ x, data = prepubertal)) %>% broom::tidy(conf.int = TRUE)

Output

term estimate std_estimate std.error statistic p.value conf.low conf.high

1 (Intercept) 1.89 NA 0.119 15.8 8.77e-33 NA NA 2 x 1.06 0.296 0.292 3.63 4.01e- 4 **-0.282 0.874**

I think that calculating the confidence intervals of the standardized coefficients can be simplified by merging the two results and running the following code:

merged_results %>% dplyr::mutate(std.conf.low = (conf.low*std_estimate)/estimate, std.conf.high= (conf.high*std_estimate)/estimate)

simonpcouch commented 4 months ago

Thanks for the issue, @AlvaroTorresMartos!

tidy.lm.beta() simply reports the confidence internals that lm.beta() does:

# load libraries for models and data
library(broom)
library(lm.beta)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# fit models
mod <- stats::lm(speed ~ ., data = cars)
std <- lm.beta(mod)

# summarize model fit with tidiers
tidy(std, conf.int = TRUE) %>% select(conf.low, conf.high)
#> # A tibble: 2 × 2
#>   conf.low conf.high
#>      <dbl>     <dbl>
#> 1   NA        NA    
#> 2    0.772     0.842

# use lm.beta's own method
confint(std)
#>                 2.5 %    97.5 %
#> (Intercept)        NA        NA
#> dist        0.7717199 0.8420699

Created on 2024-03-07 with reprex v2.1.0

If you're seeing an exception to this in your problem context, please feel free to reopen with a minimal reprex (reproducible example). :)

AlvaroTorresMartos commented 4 months ago

Thank you very much for responding, if there is a bug I understand it is up to the author of the lm.beta package to fix it. Thank you for developing broom package, it is an incredibly useful tool.

Best regards, Álvaro

El jue, 7 mar 2024 a las 17:54, Simon P. Couch @.***>) escribió:

Thanks for the issue, @AlvaroTorresMartos https://github.com/AlvaroTorresMartos!

tidy.lm.beta() simply reports the confidence internals that lm.beta() does:

load libraries for models and data

library(broom) library(lm.beta) library(dplyr)#> #> Attaching package: 'dplyr'#> The following objects are masked from 'package:stats':#> #> filter, lag#> The following objects are masked from 'package:base':#> #> intersect, setdiff, setequal, union

fit modelsmod <- stats::lm(speed ~ ., data = cars)std <- lm.beta(mod)

summarize model fit with tidiers

tidy(std, conf.int = TRUE) %>% select(conf.low, conf.high)#> # A tibble: 2 × 2#> conf.low conf.high#> #> 1 NA NA #> 2 0.772 0.842

use lm.beta's own method

confint(std)#> 2.5 % 97.5 %#> (Intercept) NA NA#> dist 0.7717199 0.8420699

Created on 2024-03-07 with reprex v2.1.0 https://reprex.tidyverse.org

If you're seeing an exception to this in your problem context, please feel free to reopen with a minimal reprex https://github.com/tidyverse/reprex#what-is-a-reprex (reproducible example). :)

— Reply to this email directly, view it on GitHub https://github.com/tidymodels/broom/issues/1190#issuecomment-1983996893, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASSFJNNPNA3DLEBABOHGY3LYXCLT7AVCNFSM6AAAAABEK5G3CSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBTHE4TMOBZGM . You are receiving this because you were mentioned.Message ID: @.***>

github-actions[bot] commented 3 months ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.