diff --git a/vignettes/comp-outcome.Rmd b/vignettes/comp-outcome.Rmd index 43dbd29..de24b10 100644 --- a/vignettes/comp-outcome.Rmd +++ b/vignettes/comp-outcome.Rmd @@ -67,8 +67,13 @@ Our `brms` model can be then fitted using the `brmcoda()` function. ```r mv <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ STRESS + (1 | ID), - cores = 8, seed = 123) -#> Error in unserialize(socklist[[n]]): error reading from connection + cores = 8, seed = 123, backend = "cmdstanr") +#> Warning: In the future, 'rescor' will be set to FALSE by default for all models. It is thus +#> recommended to explicitely set 'rescor' via 'set_rescor' instead of using the default. +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. ``` Here is a `summary()` of the model. @@ -77,7 +82,57 @@ We can see that stress significantly predicted `ilr1` and `ilr2`. ```r summary(mv$Model) -#> Error in eval(expr, envir, enclos): object 'mv' not found +#> Family: MV(gaussian, gaussian, gaussian, gaussian) +#> Links: mu = identity; sigma = identity +#> mu = identity; sigma = identity +#> mu = identity; sigma = identity +#> mu = identity; sigma = identity +#> Formula: ilr1 ~ STRESS + (1 | ID) +#> ilr2 ~ STRESS + (1 | ID) +#> ilr3 ~ STRESS + (1 | ID) +#> ilr4 ~ STRESS + (1 | ID) +#> Data: tmp (Number of observations: 3540) +#> Draws: 8 chains, each with iter = 6000; warmup = 1000; thin = 1; +#> total post-warmup draws = 40000 +#> +#> Group-Level Effects: +#> ~ID (Number of levels: 266) +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS +#> sd(ilr1_Intercept) 0.33 0.02 0.30 0.37 1.00 13574 21989 +#> sd(ilr2_Intercept) 0.30 0.02 0.28 0.34 1.00 11200 19827 +#> sd(ilr3_Intercept) 0.39 0.02 0.35 0.43 1.00 17013 24936 +#> sd(ilr4_Intercept) 0.30 0.02 0.27 0.33 1.00 16596 25482 +#> +#> Population-Level Effects: +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS +#> ilr1_Intercept -0.43 0.02 -0.48 -0.39 1.00 11069 19841 +#> ilr2_Intercept 1.47 0.02 1.42 1.51 1.00 9583 18405 +#> ilr3_Intercept -0.87 0.03 -0.93 -0.81 1.00 17975 27392 +#> ilr4_Intercept 0.65 0.02 0.60 0.69 1.00 18563 26992 +#> ilr1_STRESS -0.01 0.00 -0.02 -0.00 1.00 64970 35109 +#> ilr2_STRESS 0.01 0.00 0.00 0.01 1.00 56689 36287 +#> ilr3_STRESS 0.00 0.01 -0.01 0.01 1.00 67676 33111 +#> ilr4_STRESS 0.01 0.00 -0.00 0.01 1.00 62003 35093 +#> +#> Family Specific Parameters: +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS +#> sigma_ilr1 0.44 0.01 0.43 0.45 1.00 57828 33519 +#> sigma_ilr2 0.38 0.00 0.37 0.39 1.00 59436 34195 +#> sigma_ilr3 0.70 0.01 0.68 0.71 1.00 52722 34899 +#> sigma_ilr4 0.53 0.01 0.51 0.54 1.00 52690 33648 +#> +#> Residual Correlations: +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS +#> rescor(ilr1,ilr2) -0.54 0.01 -0.57 -0.52 1.00 57259 33739 +#> rescor(ilr1,ilr3) -0.18 0.02 -0.21 -0.14 1.00 56781 34166 +#> rescor(ilr2,ilr3) -0.05 0.02 -0.09 -0.02 1.00 56093 32472 +#> rescor(ilr1,ilr4) 0.11 0.02 0.07 0.14 1.00 56993 31275 +#> rescor(ilr2,ilr4) -0.05 0.02 -0.08 -0.01 1.00 54469 33148 +#> rescor(ilr3,ilr4) 0.56 0.01 0.54 0.58 1.00 53173 34399 +#> +#> Draws were sampled using sample(hmc). 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). ``` # Bayes Factor for compositional multilevel modelling @@ -104,14 +159,24 @@ Let's examine whether stress predicts the overall sleep-wake composition. mv0 <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ 1 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: In the future, 'rescor' will be set to FALSE by default for all models. It is thus +#> recommended to explicitely set 'rescor' via 'set_rescor' instead of using the default. +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. # full model mv <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ STRESS + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: In the future, 'rescor' will be set to FALSE by default for all models. It is thus recommended to explicitely set 'rescor' via 'set_rescor' instead of using the default. + +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. ``` We can now compare these models with the `bayesfactor_models()` function @@ -119,14 +184,19 @@ We can now compare these models with the `bayesfactor_models()` function ```r comparison <- bayesfactor_models(mv$Model, denominator = mv0$Model) -#> Error in eval(expr, envir, enclos): object 'mv' not found ``` ```r comparison -#> Error in eval(expr, envir, enclos): object 'comparison' not found +#> Bayes Factors for Model Comparison +#> +#> Model BF +#> [1] 2.90e-05 +#> +#> * Against Denominator: [2] +#> * Bayes Factor Type: marginal likelihoods (bridgesampling) ``` With a $BF$ < 1, our data favours the intercept only model, showing that there is diff --git a/vignettes/comp-outcome.Rmd.orig b/vignettes/comp-outcome.Rmd.orig index 24d8177..15ac5a0 100644 --- a/vignettes/comp-outcome.Rmd.orig +++ b/vignettes/comp-outcome.Rmd.orig @@ -60,10 +60,10 @@ head(cilr$TotalILR) A model with multilevel compositional outcomes is multivariate, as it has multiple ILR coordinate outcomes,each of which is predicted by a set of predictors. Our `brms` model can be then fitted using the `brmcoda()` function. -```{r brmcoda, results = "hide", message=FALSE, warning=FALSE} +```{r brmcoda, results = "hide", message=FALSE} mv <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ STRESS + (1 | ID), - cores = 8, seed = 123) + cores = 8, seed = 123, backend = "cmdstanr") ``` Here is a `summary()` of the model. @@ -91,17 +91,17 @@ Let's examine whether stress predicts the overall sleep-wake composition. *Note*: To use Bayes factors, `brmsfit` models must be fitted with an additional non-default argument `save_pars = save_pars(all = TRUE)`. -```{r bfmodel, results = "hide", message=FALSE, warning=FALSE} +```{r bfmodel, results = "hide", message=FALSE} # intercept only mv0 <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ 1 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) # full model mv <- brmcoda(compilr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ STRESS + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) ``` We can now compare these models with the `bayesfactor_models()` function diff --git a/vignettes/comp-predictor.Rmd b/vignettes/comp-predictor.Rmd index 3c6b4f0..ad3eb77 100644 --- a/vignettes/comp-predictor.Rmd +++ b/vignettes/comp-predictor.Rmd @@ -39,19 +39,8 @@ purpose. ```r library(multilevelcoda) library(brms) -#> Loading required package: Rcpp -#> 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': -#> -#> ar library(bayestestR) library(doFuture) -#> Loading required package: foreach -#> Loading required package: future options(digits = 3) # reduce number of digits shown ``` @@ -74,7 +63,6 @@ data("psub") The following table shows a few rows of data from `mcompd`. - | TST| WAKE| MVPA| LPA| SB| ID| Age| Female| STRESS| |---:|----:|-----:|---:|-----:|---:|----:|------:|------:| | 542| 99.0| 297.4| 460| 41.4| 185| 29.7| 0| 3.67| @@ -84,29 +72,23 @@ The following table shows a few rows of data from `mcompd`. | 281| 18.8| 403.0| 611| 126.3| 184| 22.3| 1| 1.18| | 397| 26.5| 39.9| 587| 389.8| 184| 22.3| 1| 0.00| - - The following table shows the sequential binary partition being used in `sbp`. Columns correspond to the composition variables (TST, WAKE, MVPA, LPA, SB). Rows correspond to distinct ILR coordinates. - -| TST| WAKE| MVPA| LPA| SB| -|---:|----:|----:|---:|--:| -| 1| 1| -1| -1| -1| -| 1| -1| 0| 0| 0| -| 0| 0| 1| -1| -1| -| 0| 0| 0| 1| -1| - - +| | | | | | +|--:|--:|--:|--:|--:| +| 1| 1| -1| -1| -1| +| 1| -1| 0| 0| 0| +| 0| 0| 1| -1| -1| +| 0| 0| 0| 1| -1| The following table shows how all the possible binary substitutions contrasts are setup. Time substitutions work by taking time from the -1 variable and adding time to the +1 variable. - | TST| WAKE| MVPA| LPA| SB| |---:|----:|----:|---:|--:| | 1| -1| 0| 0| 0| @@ -130,8 +112,6 @@ contrasts are setup. Time substitutions work by taking time from the | 0| 0| -1| 0| 1| | 0| 0| 0| -1| 1| - - # Multilevel model with compositional predictors ## Compositions and isometric log ratio (ILR) coordinates. @@ -174,8 +154,11 @@ from between- and within-person sleep-wake behaviours (expressed as ILR coordina m <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), - cores = 8, seed = 123) -#> Error in unserialize(socklist[[n]]): error reading from connection + cores = 8, seed = 123, backend = "cmdstanr") +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. ``` Here is a `summary()` of the model results. @@ -183,34 +166,35 @@ Here is a `summary()` of the model results. ```r summary(m$Model) +#> 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. #> Family: gaussian #> Links: mu = identity; sigma = identity -#> Formula: STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID) +#> Formula: STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID) #> Data: tmp (Number of observations: 3540) -#> Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; -#> total post-warmup draws = 4000 +#> Draws: 1 chains, each with iter = 500; warmup = 250; thin = 1; +#> total post-warmup draws = 250 #> #> Group-Level Effects: #> ~ID (Number of levels: 266) #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS -#> sd(Intercept) 0.99 0.06 0.87 1.11 1.00 1179 2176 +#> sd(Intercept) 0.99 0.07 0.87 1.12 1.00 112 183 #> #> Population-Level Effects: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS -#> Intercept 2.64 0.47 1.68 3.57 1.00 1429 2263 -#> bilr1 0.12 0.33 -0.54 0.77 1.00 971 1409 -#> bilr2 0.53 0.35 -0.16 1.21 1.00 1124 1679 -#> bilr3 0.13 0.21 -0.29 0.55 1.00 1052 1809 -#> bilr4 -0.01 0.29 -0.57 0.55 1.00 1021 1941 -#> wilr1 -0.34 0.12 -0.58 -0.10 1.00 2990 3206 -#> wilr2 0.05 0.13 -0.21 0.31 1.00 3396 3077 -#> wilr3 -0.10 0.08 -0.25 0.04 1.00 2900 3218 -#> wilr4 0.23 0.10 0.04 0.43 1.00 2894 3220 -#> Female -0.39 0.17 -0.74 -0.08 1.00 1405 1697 +#> Intercept 2.51 0.45 1.70 3.42 1.06 49 125 +#> bilr1 0.20 0.30 -0.45 0.75 1.11 10 71 +#> bilr2 0.44 0.30 -0.28 0.89 1.12 8 99 +#> bilr3 0.12 0.22 -0.29 0.51 1.01 81 175 +#> bilr4 -0.02 0.32 -0.58 0.63 1.00 85 118 +#> wilr1 -0.36 0.12 -0.59 -0.12 1.01 162 155 +#> wilr2 0.04 0.13 -0.22 0.27 1.00 100 154 +#> wilr3 -0.11 0.08 -0.26 0.04 1.02 96 114 +#> wilr4 0.25 0.09 0.07 0.41 1.01 133 113 #> #> Family Specific Parameters: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS -#> sigma 2.37 0.03 2.31 2.43 1.00 4462 2814 +#> sigma 2.36 0.03 2.30 2.42 1.01 393 187 #> #> Draws were sampled using sample(hmc). For each parameter, Bulk_ESS #> and Tail_ESS are effective sample size measures, and Rhat is the potential @@ -274,30 +258,42 @@ For precise Bayes factors, we will use 40,000 posterior draws for each model. m0 <- brmcoda(compilr = cilr, formula = STRESS ~ 1 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. # between-person composition only model m1 <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. # within-person composition only model m2 <- brmcoda(compilr = cilr, formula = STRESS ~ wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. # full model m <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) -#> Error in unserialize(socklist[[n]]): error reading from connection + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) +#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt. +#> If your model failed to compile please run rebuild_cmdstan(). +#> If the issue persists please open a bug report. +#> Error: An error occured during compilation! See the message above for more information. ``` We can now compare these models with the `bayesfactor_models()` function, using the intercept-only @@ -306,13 +302,21 @@ model as reference. ```r comparison <- bayesfactor_models(m$Model, m1$Model, m2$Model, denominator = m0$Model) -#> Error in eval(expr, envir, enclos): object 'm1' not found +#> Warning: Bayes factors might not be precise. +#> For precise Bayes factors, sampling at least 40,000 posterior samples is recommended. +#> Error: Bridgesampling failed. Perhaps you did not set 'save_pars = save_pars(all = TRUE)' when fitting your model? If you are running bridge sampling on another machine than the one used to fit the model, you may need to set recompile = TRUE. ``` ```r comparison -#> Error in eval(expr, envir, enclos): object 'comparison' not found +#> Bayes Factors for Model Comparison +#> +#> Model BF +#> [1] 2.90e-05 +#> +#> * Against Denominator: [2] +#> * Bayes Factor Type: marginal likelihoods (bridgesampling) ``` We can see that model with only within-person composition is the best model - with $BF$ = 11.86 compared to the null (intercept only). @@ -322,7 +326,13 @@ Let's compare these models against the full model. ```r update(comparison, reference = 1) -#> Error in eval(expr, envir, enclos): object 'comparison' not found +#> Bayes Factors for Model Comparison +#> +#> Model BF +#> [2] 3.45e+04 +#> +#> * Against Denominator: [1] +#> * Bayes Factor Type: marginal likelihoods (bridgesampling) ``` Again, our data favours the within-person composition only model over the full model, giving 2.93 times more support. @@ -356,7 +366,6 @@ The below example examines the changes in stress for different pairwise substitu ```r bsubm <- substitution(object = m, delta = 5, level = "between", ref = "grandmean") -#> Warning: executing %dopar% sequentially: no parallel backend registered ``` The output contains multiple data sets of results for all compositional components. @@ -371,16 +380,14 @@ knitr::kable(bsubm$BetweenSub$TST) | Mean| CI_low| CI_high| Delta|From |To |Level |Reference | |------:|------:|-------:|-----:|:----|:---|:-------|:---------| -| 0.031| -0.002| 0.064| 5|WAKE |TST |between |grandmean | -| 0.003| -0.014| 0.019| 5|MVPA |TST |between |grandmean | -| 0.006| -0.007| 0.018| 5|LPA |TST |between |grandmean | -| 0.007| -0.008| 0.021| 5|SB |TST |between |grandmean | -| -0.029| -0.060| 0.002| -5|WAKE |TST |between |grandmean | -| -0.003| -0.019| 0.014| -5|MVPA |TST |between |grandmean | -| -0.006| -0.018| 0.007| -5|LPA |TST |between |grandmean | -| -0.007| -0.021| 0.008| -5|SB |TST |between |grandmean | - - +| 0.022| -0.009| 0.051| 5|WAKE |TST |between |grandmean | +| 0.004| -0.013| 0.019| 5|MVPA |TST |between |grandmean | +| 0.006| -0.007| 0.017| 5|LPA |TST |between |grandmean | +| 0.007| -0.006| 0.018| 5|SB |TST |between |grandmean | +| -0.021| -0.047| 0.008| -5|WAKE |TST |between |grandmean | +| -0.004| -0.018| 0.013| -5|MVPA |TST |between |grandmean | +| -0.006| -0.017| 0.007| -5|LPA |TST |between |grandmean | +| -0.007| -0.018| 0.006| -5|SB |TST |between |grandmean | None of the results are significant, given that the credible intervals did not cross 0, showing that increasing sleep (TST) at the expense of any other behaviours was not associated in changes in stress. @@ -408,16 +415,14 @@ knitr::kable(wsubm$WithinSub$TST) | Mean| CI_low| CI_high| Delta|From |To |Level |Reference | |------:|------:|-------:|-----:|:----|:---|:------|:---------| -| 0.017| 0.002| 0.032| 5|WAKE |TST |within |grandmean | +| 0.017| 0.002| 0.031| 5|WAKE |TST |within |grandmean | | -0.003| -0.009| 0.003| 5|MVPA |TST |within |grandmean | -| -0.005| -0.009| 0.000| 5|LPA |TST |within |grandmean | -| -0.002| -0.007| 0.003| 5|SB |TST |within |grandmean | -| -0.016| -0.030| -0.002| -5|WAKE |TST |within |grandmean | +| -0.005| -0.009| -0.001| 5|LPA |TST |within |grandmean | +| -0.002| -0.006| 0.003| 5|SB |TST |within |grandmean | +| -0.016| -0.029| -0.002| -5|WAKE |TST |within |grandmean | | 0.003| -0.003| 0.009| -5|MVPA |TST |within |grandmean | -| 0.005| 0.000| 0.009| -5|LPA |TST |within |grandmean | -| 0.002| -0.003| 0.007| -5|SB |TST |within |grandmean | - - +| 0.005| 0.001| 0.009| -5|LPA |TST |within |grandmean | +| 0.002| -0.003| 0.006| -5|SB |TST |within |grandmean | At within-person level, there were significant results for substitution of sleep (TST) and time awake in bed (WAKE) for 5 minutes, but not other behaviours. diff --git a/vignettes/comp-predictor.Rmd.orig b/vignettes/comp-predictor.Rmd.orig index 99a7808..125aa4e 100644 --- a/vignettes/comp-predictor.Rmd.orig +++ b/vignettes/comp-predictor.Rmd.orig @@ -126,11 +126,11 @@ from between- and within-person sleep-wake behaviours (expressed as ILR coordina *Notes: make sure you pass the correct names of the ILR coordinates to `brms` model.* -```{r brmcoda, results = "hide", message=FALSE, warning=FALSE} +```{r brmcoda, results = "hide", message=FALSE} m <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), - cores = 8, seed = 123) + cores = 8, seed = 123, backend = "cmdstanr") ``` Here is a `summary()` of the model results. @@ -190,37 +190,37 @@ For precise Bayes factors, we will use 40,000 posterior draws for each model. *Notes* : To use Bayes factors, `brmsfit` models must be fitted with an additional non-default argument `save_pars = save_pars(all = TRUE)`. -```{r bfmodel, results = "hide", message=FALSE, warning=FALSE} +```{r bfmodel, results = "hide", message=FALSE} # intercept only model m0 <- brmcoda(compilr = cilr, formula = STRESS ~ 1 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) # between-person composition only model m1 <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) # within-person composition only model m2 <- brmcoda(compilr = cilr, formula = STRESS ~ wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) # full model m <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000, - save_pars = save_pars(all = TRUE)) + backend = "cmdstanr", save_pars = save_pars(all = TRUE)) ``` We can now compare these models with the `bayesfactor_models()` function, using the intercept-only model as reference. -```{r bf1, results='hide', message=FALSE, warning=FALSE} +```{r bf1, results='hide', message=FALSE} comparison <- bayesfactor_models(m$Model, m1$Model, m2$Model, denominator = m0$Model) ``` diff --git a/vignettes/mlmcoda-mlmcoda-plotbsubm1-1.png b/vignettes/mlmcoda-mlmcoda-plotbsubm1-1.png index 4719536..5a55e9b 100644 Binary files a/vignettes/mlmcoda-mlmcoda-plotbsubm1-1.png and b/vignettes/mlmcoda-mlmcoda-plotbsubm1-1.png differ diff --git a/vignettes/substitution-model.Rmd b/vignettes/substitution-model.Rmd index b48fec3..91cad0a 100644 --- a/vignettes/substitution-model.Rmd +++ b/vignettes/substitution-model.Rmd @@ -60,8 +60,7 @@ cilr <- compilr(data = mcompd, sbp = sbp, m <- brmcoda(compilr = cilr, formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID), - cores = 8, seed = 123) -#> Error in unserialize(socklist[[n]]): error reading from connection + cores = 8, seed = 123, backend = "cmdstanr") ``` A `summary()` of the model results. @@ -78,21 +77,34 @@ summary(m$Model) #> #> Group-Level Effects: #> ~ID (Number of levels: 266) -#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS -#> sd(Intercept) 0.99 0.06 0.87 1.11 1.00 1179 2176 +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS +#> sd(Intercept) 0.99 0.06 0.87 1.11 1.00 1179 +#> Tail_ESS +#> sd(Intercept) 2176 #> #> Population-Level Effects: -#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS -#> Intercept 2.64 0.47 1.68 3.57 1.00 1429 2263 -#> bilr1 0.12 0.33 -0.54 0.77 1.00 971 1409 -#> bilr2 0.53 0.35 -0.16 1.21 1.00 1124 1679 -#> bilr3 0.13 0.21 -0.29 0.55 1.00 1052 1809 -#> bilr4 -0.01 0.29 -0.57 0.55 1.00 1021 1941 -#> wilr1 -0.34 0.12 -0.58 -0.10 1.00 2990 3206 -#> wilr2 0.05 0.13 -0.21 0.31 1.00 3396 3077 -#> wilr3 -0.10 0.08 -0.25 0.04 1.00 2900 3218 -#> wilr4 0.23 0.10 0.04 0.43 1.00 2894 3220 -#> Female -0.39 0.17 -0.74 -0.08 1.00 1405 1697 +#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS +#> Intercept 2.64 0.47 1.68 3.57 1.00 1429 +#> bilr1 0.12 0.33 -0.54 0.77 1.00 971 +#> bilr2 0.53 0.35 -0.16 1.21 1.00 1124 +#> bilr3 0.13 0.21 -0.29 0.55 1.00 1052 +#> bilr4 -0.01 0.29 -0.57 0.55 1.00 1021 +#> wilr1 -0.34 0.12 -0.58 -0.10 1.00 2990 +#> wilr2 0.05 0.13 -0.21 0.31 1.00 3396 +#> wilr3 -0.10 0.08 -0.25 0.04 1.00 2900 +#> wilr4 0.23 0.10 0.04 0.43 1.00 2894 +#> Female -0.39 0.17 -0.74 -0.08 1.00 1405 +#> Tail_ESS +#> Intercept 2263 +#> bilr1 1409 +#> bilr2 1679 +#> bilr3 1809 +#> bilr4 1941 +#> wilr1 3206 +#> wilr2 3077 +#> wilr3 3218 +#> wilr4 3220 +#> Female 1697 #> #> Family Specific Parameters: #> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS @@ -125,19 +137,19 @@ post-hoc analyses using the substitution models from our `multilevel` package. `multilevelcoda` package provides `2` different methods to compute substitution models, via the `substitution()` function. -Simple substitution models: +Basic substitution models: - *Between-person* substitution - *Within-person* substitution -Average substitution models: +Average marginal substitution models: -- Average *between-person* substitution -- Average *within-person* substitution +- Average marginal *between-person* substitution +- Average marginal *within-person* substitution *Tips: Substitution models are often computationally demanding tasks. You can speed up the models using parallel execution, for example, using `doFuture` package.* -## Simple Substitution model +## Basic Substitution model The below example examines the changes in stress for different pairwise substitution of sleep-wake behaviours for a period of 1 to 5 minutes, at between-person level. We specify `level = between` to indicate substitutional change would be at the between-person level, and `ref = "grandmean"` to indicate substitution model using the grand compositional mean as reference composition. @@ -181,10 +193,10 @@ plotsub(data = subm1$BetweenSub$TST, ``` - -``` -#> Error in plotsub(data = subm1$BetweenSub$TST, x = "sleep", y = "stress"): could not find function "plotsub" -``` +