You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In brms 2.19, monotonic effects ignore ordered factor levels that are not present in the data (but defined as factor levels) - even with drop_unused_levels = FALSE. The monotonic term vignette can be modified as follows to provoke the issue (by only having 2 of the 4 factor levels present in the data):
library(brms)
#> Loading required package: Rcpp#> Warning: package 'Rcpp' was built under R version 4.3.1#> Loading 'brms' package (version 2.19.0). Useful instructions#> can be found by typing help('brms'). A more detailed introduction#> to the package is available through vignette('brms_overview').#> #> Attaching package: 'brms'#> The following object is masked from 'package:stats':#> #> arincome_options<- c("below_20", "20_to_40", "40_to_100", "greater_100")
income<-factor(
income_options[1:2], # income_options[1:4] workslevels=income_options,
ordered=TRUE
)
mean_ls<- c(30, 60, 70, 75)
ls<-mean_ls[income] + rnorm(length(income), sd=7)
dat<-data.frame(income, ls)
income_model<- bf(ls~1+ mo(income))
get_prior(income_model, data=dat)
#> prior class coef group resp dpar nlpar lb ub#> (flat) b #> (flat) b moincome #> student_t(3, 38.6, 16.1) Intercept #> student_t(3, 0, 16.1) sigma 0 #> dirichlet(1) simo moincome1 #> source#> default#> (vectorized)#> default#> default#> default# This runs but only creates a 1-dimensional dirichlet distribution for the # monotone term if the data frame has only 2 rows (but 4 defined income levels)fit1<- brm(income_model, data=dat, drop_unused_levels=FALSE, refresh=0)
#> Compiling Stan program...#> Start sampling#> Warning: There were 271 divergent transitions after warmup. See#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup#> to find out why this is a problem and how to eliminate them.#> Warning: Examine the pairs() plot to diagnose sampling problems#> Warning: The largest R-hat is NA, indicating chains have not mixed.#> Running the chains for more iterations may help. See#> https://mc-stan.org/misc/warnings.html#r-hat#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.#> Running the chains for more iterations may help. See#> https://mc-stan.org/misc/warnings.html#bulk-ess#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.#> Running the chains for more iterations may help. See#> https://mc-stan.org/misc/warnings.html#tail-ess
summary(fit1)
#> Warning: Parts of the model have not converged (some Rhats are > 1.05). Be#> careful when analysing the results! We recommend running more iterations and/or#> setting stronger priors.#> Warning: There were 271 divergent transitions after warmup. Increasing#> adapt_delta above 0.8 may help. See#> http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup#> Family: gaussian #> Links: mu = identity; sigma = identity #> Formula: ls ~ 1 + mo(income) #> Data: dat (Number of observations: 2) #> Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;#> total post-warmup draws = 4000#> #> Population-Level Effects: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS#> Intercept 30.97 10.16 11.29 54.52 1.01 1045 1366#> moincome 18.76 20.62 -26.64 59.40 1.02 1061 935#> #> Simplex Parameters: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS#> moincome1[1] 1.00 0.00 1.00 1.00 NA NA NA#> #> Family Specific Parameters: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS#> sigma 13.73 13.62 0.92 46.72 1.07 43 22#> #> Draws were sampled 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).income_model_prior<- prior(normal(30, 1), class="Intercept") +
prior(normal(45, 22.5), class="b", coef="moincome") +
prior(dirichlet(c(2, 1, 1)), class="simo", coef="moincome1")
# This fails if the data frame has only 2 rows (but 4 defined income levels)# with "Error: Invalid Dirichlet prior. Expected input of length 1."fit2<- brm(income_model, data=dat, prior=income_model_prior, drop_unused_levels=FALSE, refresh=0)
#> Error: Invalid Dirichlet prior. Expected input of length 1.
summary(fit2)
#> Error in eval(expr, envir, enclos): object 'fit2' not found
The text was updated successfully, but these errors were encountered:
luwidmer
changed the title
Monotonic effect ignores ordered factor levels that are not present in the data (but defined as factor levels)
Bug? Monotonic effect ignores ordered factor levels that are not present in the data (but defined as factor levels)
Aug 9, 2023
In brms 2.19, monotonic effects ignore ordered factor levels that are not present in the data (but defined as factor levels) - even with
drop_unused_levels = FALSE
. The monotonic term vignette can be modified as follows to provoke the issue (by only having 2 of the 4 factor levels present in the data):Created on 2023-08-09 with reprex v2.0.2
Session info
The text was updated successfully, but these errors were encountered: