-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support of brms regression models #89
Conversation
I have added basic support of brmrs model. It still needs to be tested with different cases. I had to deal with the fact that there is no Please have a look in particular to the folowing files:
In addition, library(broom.helpers)
library(broom.mixed)
#> Registered S3 method overwritten by 'broom.mixed':
#> method from
#> tidy.gamlss broom
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
load(system.file("extdata", "brms_example.rda", package="broom.mixed"))
brms_crossedRE %>% tidy_plus_plus(intercept = T) %>% knitr::kable()
library(brms)
#> Le chargement a nécessité le package : Rcpp
#> Loading 'brms' package (version 2.14.4). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#>
#> Attachement du package : 'brms'
#> The following object is masked from 'package:stats':
#>
#> ar
mtcars <- mtcars %>% mutate(gear = gear %>% factor, vs = vs %>% factor, carb = carb %>% factor)
contrasts(mtcars$vs) <- "contr.sum"
model_brms_2 <- brm(mpg ~ (1 | gear) + hp + vs + carb, data = mtcars, backend = "cmdstanr", refresh = 0)
#> Compiling Stan program...
#> Start sampling
#> Running MCMC with 4 sequential chains...
#>
#> Chain 1 finished in 0.9 seconds.
#> Chain 2 finished in 0.9 seconds.
#> Chain 3 finished in 0.9 seconds.
#> Chain 4 finished in 0.9 seconds.
#>
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.9 seconds.
#> Total execution time: 4.0 seconds.
#>
#> Warning: 4 of 4000 (0.0%) transitions ended with a divergence.
#> This may indicate insufficient exploration of the posterior distribution.
#> Possible remedies include:
#> * Increasing adapt_delta closer to 1 (default is 0.8)
#> * Reparameterizing the model (e.g. using a non-centered parameterization)
#> * Using informative or weakly informative prior distributions
model_brms_2 %>% tidy_plus_plus(intercept = T) %>% knitr::kable()
Created on 2021-01-27 by the reprex package (v0.3.0) |
Regarding:
Family: gaussian
Links: mu = identity; sigma = identity
Formula: mpg ~ wt + (1 | cyl) + (1 + wt | gear)
Data: mtcars (Number of observations: 32)
Samples: 2 chains, each with iter = 100; warmup = 50; thin = 1;
total post-warmup samples = 100
Group-Level Effects:
~cyl (Number of levels: 3)
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sd(Intercept) 4.57 3.64 0.35 14.63 1.12 13 50
~gear (Number of levels: 3)
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sd(Intercept) 4.57 3.21 0.06 12.84 1.00 43 48
sd(wt) 1.71 1.32 0.12 4.92 1.03 27 77
cor(Intercept,wt) -0.29 0.58 -0.96 0.89 1.00 45 61
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 33.89 3.81 26.28 40.47 1.04 54 31
wt -3.91 1.39 -6.27 -1.17 1.03 27 60
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 2.59 0.41 2.03 3.63 1.01 118 77
Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think those files are OK.
@larmarange thank you kindly for updating to accommodate brms models. I see that it took some re-structuring and adding new S3 functions. |
Hey guys, I don't know if you are aware but library(brms)
fit <- brm(bf(mpg ~ 1 + (1 + hp | cyl), decomp = "QR"), data = mtcars, refresh = 0)
broom.mixed::tidy(fit, conf.int = TRUE) r$> broom.mixed::tidy(fit, conf.int = TRUE)
Registered S3 method overwritten by 'broom.mixed':
method from
tidy.gamlss broom
# A tibble: 5 x 8
effect component group term estimate std.error conf.low conf.high
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 fixed cond NA (Intercept) 20.8 3.52 13.9 28.0
2 ran_pars cond cyl sd__(Intercept) 7.09 4.12 1.21 17.5
3 ran_pars cond cyl sd__hp 0.0440 0.0360 0.00184 0.143
4 ran_pars cond cyl cor__(Intercept).hp -0.221 0.554 -0.976 0.881
5 ran_pars cond Residual sd__Observation 3.27 0.480 2.48 4.33 PS: Sorry |
Dear @storopoli See also #90 about better management of mixed models in general. |
new var_type "ran_pars"
@ddsjoberg R CMD check fails for R3.5 and R3.4. It says that "brms" is not available, although installed through historical version of CRAN. Any idea? |
i'll take a look tomorrow. I may post a bunch of commits to test FYI |
Maybe an option could be to not force suggested packages for R 3.4 and 3.5? |
Do you mean not do checks on 3.5 and 3.4, or is there a way to skip the suggested packages? I looked at 3.4 and it seems to be an issue installing rstan. Either of the options above seem reasonable |
I think you may find that R Cmd check will fail if every package in Suggests cannot be installed (even if the checks using that package are skipped when it's not installed). |
OK with the change all checks passed. Please note that for R 3.4, some tests are skipped due to the fact that brms is not available. However, I guess that it is acceptable for R 3.4 and R 3.5 to skip corresponding tests if some packages are not available. For R release, oldrel and devel, we still force all suggested packages to pass the tests. |
That is amazing! |
I have separated checks in 2 workflows. For R 3.4 and R 3.5 I have added
|
Codecov Report
@@ Coverage Diff @@
## master #89 +/- ##
==========================================
- Coverage 99.85% 99.71% -0.14%
==========================================
Files 33 34 +1
Lines 1375 1414 +39
==========================================
+ Hits 1373 1410 +37
- Misses 2 4 +2
|
fix #87
and basic support for crr() models
fix #91