Computing fitted values for Weibull using posterior samples?

I am trying to understand regression using the Weibull family using brms. I would like to be able to compute the fitted values (mean from regression line) manually using the posterior samples.

The mean should be equal to scale*gamma(1 + 1/shape).

In brms, a log link for mu is used and an identity link for shape is used. For the simplest model with only an Intercept, the exp(Intercept) should recover the scale parameter. The shape parameter is identity.
Now, I would have thought to get the mean, I would do:
exp(samples$b_Intercept)*gamma(1 + 1/samples$shape)

But this does not seem to work.
So I have tried these two:
mean(exp(samples$b_Intercept * (gamma(1 + 1/exp(samples$shape)))))
mean(exp(samples$b_Intercept * (gamma(1 + 1/(samples$shape)))))

And depending on the values used in the simulation for shape and scale, one is sometimes closer than the other.
But neither recover the values of doing fitted(model)

Simulation code is attached.weibull question code.txt (1.1 KB)

What am I doing wrong? Thanks for the help!

I think @jonah’s post clarifies the common ways to do posterior draws in brms, does this help you?

Thanks for the response!
Actually, I was trying to figure out how to use the parameters from the model and the formula for the mean for a weibull regression model to obtain the same answers as using the functions in Stan.
For example, using my example code above, it looks like I should be able to do:

mean((s1$b_Intercept) * (gamma(1 + 1/(s1$shape))))

and obtain the same answer as doing,

a<-posterior_linpred(m)
a<-data.frame(a)
mean(a$X1)

But I don’t get the same answer.

@mike-lawrence loves Weibull, I’m sure he can help out :)

1 Like

BTW, the shape is also \log linked right? And why do you have a gamma in the formula, where’s that from?

I believe the shape is identity linked in brms.
Gamma function is part of the formula for the mean in Weibull. Weibull distribution - Wikipedia
Parameterization of Response Distributions in brms

According to ?brmsfamily there’s a \log link for shape:

weibull(link = "log", link_shape = "log")

however, that shouldn’t matter since you want to mimic posterior_linpred(), in which case one shouldn’t transform anything, right?

If you run the model in the sim, it says “shape = identity”

Right, to mimic posterior_linpred() you shouldn’t use any inverse link function.
I would also like to know how to mimic, fitted() though, in which case it would be helpful to know where to apply exp() in the formula.

@paul.buerkner any thoughts on what I am doing wrong here?