Skip to content

Commit

Permalink
comparisons manual computation
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Aug 22, 2024
1 parent e18052a commit 77a0c21
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions vignettes/brms.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,45 @@ avg_slopes(mod, dpar = "sigma")
```



# Manual computation: Counterfactual comparisons

Here is an example which replicates `comparisons()` output manually. Hopefully this will help some readers understand what is going on under the hood:

```{r, eval = FALSE}
library(marginaleffects)
data("ChickWeight")
mod = brm(data = ChickWeight,
weight ~ Time * Diet + (Time|Chick),
seed = 123,
backend = "cmdstanr")
# NA
comparisons(mod,
variables = "Time",
by = "Diet",
re_formula = NA)
d0 <- ChickWeight
d1 <- transform(d0, Time = Time + 1)
p0 <- posterior_epred(mod, newdata = d0, re_formula = NA)
p1 <- posterior_epred(mod, newdata = d1, re_formula = NA)
p <- p1 - p0
cmp <- apply(p, 1, function(x) tapply(x, ChickWeight$Diet, mean))
apply(cmp, 1, quantile, prob = .025)
# NULL
comparisons(mod,
variables = "Time",
by = "Diet",
re_formula = NULL)
d0 <- ChickWeight
d1 <- transform(d0, Time = Time + 1)
p0 <- posterior_epred(mod, newdata = d0, re_formula = NULL)
p1 <- posterior_epred(mod, newdata = d1, re_formula = NULL)
p <- p1 - p0
cmp <- apply(p, 1, function(x) tapply(x, ChickWeight$Diet, mean))
apply(cmp, 1, quantile, prob = .025)
```

0 comments on commit 77a0c21

Please sign in to comment.