# Get the difference between two conditions in the raw scale from a model with a lognormal likelihood

**URL:** <https://discourse.mc-stan.org/t/get-the-difference-between-two-conditions-in-the-raw-scale-from-a-model-with-a-lognormal-likelihood/24440>\
**Category:** brms\
**Tags:** cognitive-science\
**Created:** [September 16, 2021, 7:28am UTC](https://discourse.mc-stan.org/t/get-the-difference-between-two-conditions-in-the-raw-scale-from-a-model-with-a-lognormal-likelihood/24440 "2021-09-16T07:28:08Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Joshua\_Wiley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/joshua_wiley/32/12625_2.png) [@Joshua\_Wiley](https://discourse.mc-stan.org/u/Joshua_Wiley)\
**Post date:** [September 17, 2021, 7:03am UTC](https://discourse.mc-stan.org/t/get-the-difference-between-two-conditions-in-the-raw-scale-from-a-model-with-a-lognormal-likelihood/24440/6 "2021-09-17T07:03:04Z")

</div>

Incidentally a somewhat related conversation over here:

> [@Calculating average marginal effects in models with random slopes](https://discourse.mc-stan.org/t/calculating-average-marginal-effects-in-models-with-random-slopes/24172/7):
>
> @jackbailey Thanks, I took a look and like your approach of doing the calculations and weighting by unique predictor combos to save time. brmsmargins matches your code in cases where they both work. Although the original uncertainty around whether these are accurate remains for me. dummy \<- mtcars dummy$Time \<- mtcars$am m3 \<- brm(mpg ~ Time + (1 + Time | cyl), data = dummy, cores = 4) summary(m3) which gives: Family: gaussian Links: mu = identity; sigma = identity Formula: mpg ~ Time +…

led to creation of an early draft `brmsmargins` package on github. Once installed, you could get what you want using:

```no-highlight
library(brmsmargins)

## AME for two specific trials (0, 1)
marg <- brmsmargins(
  fit_press_trial,
  at = data.frame(c_trial = c(0, 1)),
  contrasts = cbind(
    "Estimate c_trial = 0" = c(1, 0),
    "Estimate c_trial = 1" = c(0, 1),
    "Estimate c_trial 1 - 0" = c(-1, 1)),
  CI = 0.95, CIType = "HDI")
marg$ContrastSummary

## M Mdn LL UL PercentROPE PercentMID
##1: 168.26971114 168.26542263 166.14610932 170.4999232 NA NA
##2: 168.35775319 168.35455232 166.15603648 170.5090509 NA NA
##3: 0.08804205 0.08793096 0.06797247 0.1081785 NA NA
## CI CIType ROPE MID Label
##1: 0.95 HDI <NA> <NA> Estimate c_trial = 0
##2: 0.95 HDI <NA> <NA> Estimate c_trial = 1
##3: 0.95 HDI <NA> <NA> Estimate c_trial 1 - 0

```

This gives you the contrast, on the original metric between c\_trial 0 and 1.  
Or if you want more of an average marginal effect of the slope, you can use numerical derivatives as below. In this instance, they are fairly trivially different, but may be of use.

```no-highlight
## AME for the numerical derivative
marg2 <- brmsmargins(
  fit_press_trial,
  add = data.frame(c_trial = c(0, 1e-5)),
  contrasts = cbind(
    "Estimate AME for slope of c_trial " = c(-1/1e-5, 1/1e-5)),
  CI = 0.95, CIType = "HDI")

marg2$ContrastSummary
## M Mdn LL UL PercentROPE PercentMID CI
##1: 0.08815504 0.08803763 0.06801887 0.1083883 NA NA 0.95
## CIType ROPE MID Label
##1: HDI <NA> <NA> Estimate AME for slope of c_trial 

```

Note that both these solutions would marginalize over the sample distribution of covariates, if you had any covariates.

---

_[View the full topic](https://discourse.mc-stan.org/t/get-the-difference-between-two-conditions-in-the-raw-scale-from-a-model-with-a-lognormal-likelihood/24440)._
