Non-linear Logistic Regression with brms

I would like to explore a non-linear transformation \frac{Emax*X}{EC50+X} of my predictor X in a logistic regression problem. I tried the below but something seems off when I plot the model. Happy for any assistance.

 set.seed(666)
 pk = seq(0,100, length.out = 50)

data <- tibble('pk' = pk,"pd" = response) %>% 
  mutate(response = ifelse(pk >50, rbinom(n(), c(0,1), c(0.1,0.9)),
                           rbinom(n(), c(0,1), c(0.7,0.3))))

inv_logit <- function(x) 1 / (1 + exp(-x))

fit_1 <- brm(
  bf(response ~ inv_logit(int + Emax*pk/(EC50 + pk)) , 
   int~1, Emax ~ 1,EC50 ~1, nl = TRUE),
  data = data, family = bernoulli("identity"), 
  prior = c(
    prior(beta(1, 1), nlpar = "int", lb = 0, ub = 1),
    prior(normal(0.8, 0.1), nlpar = "Emax"),
    prior(normal(50, 10), nlpar = "EC50")
  )
)

Now the output of this model looks reasonable

 Family: bernoulli 
  Links: mu = identity 
Formula: response ~ inv_logit(int + Emax * pk/(EC50 + pk)) 
         int ~ 1
         Emax ~ 1
         EC50 ~ 1
   Data: data (Number of observations: 50) 
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup samples = 4000

Population-Level Effects: 
               Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
int_Intercept      0.07      0.07     0.00     0.25 1.00     3024     1746
Emax_Intercept     0.74      0.10     0.55     0.94 1.00     3493     2716
EC50_Intercept    54.30      9.44    36.45    73.02 1.00     3514     2758

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).

But when I plot the model

plot(conditional_effects(fit_1), points = TRUE)

It looks off. Am I missing something?

1 Like

Hi Logan, welcome to the forums!

The reason your plot looks off is because you’re plotting the samples on the probability scale, while the observed points are on the categorical scale.

Also, you don’t need to define the non-linear function inv_logit for your model, since the bernoulli defaults to a logit link.

4 Likes