Section 4: Bayesian Regression
Previously, we addressed the question: "is my chat response time effected by who I'm talking to?". We have estimated model parameters for each individual I've had conversations with. But sometimes we want to understand the effect of more factors such as "day of week," "time of day," etc. We can use GLM (generalized linear models) to better understand the effects of these factors.
Linear regression reminder
When we have a response that is continuous from to , we can consider using a linear regression represented by:
We read this as: our response is normally distributed around with a standard deviation of . The value of is described by a linear function of explanatory variables with a baseline intercept .
Link functions
In the event you're not modeling a continuous response variable from to , you may need to use a link function to transform your response range. For a Poisson distribution, the canonical link function used is the log link. This can be formally described as:
This is considered to be a fixed effects model. The coefficients are estimated across the entire population as opposed to estimating separate parameters for each person (like in the pooled and partially pooled model in Section 3).
Fixed effects Poisson regression
To construct a Poisson regression in PyMC3, we need to apply the log link function to . The underlying data model in PyMC3 uses theano and hence we need to use the theano tensor method theano.tensor.exp()
as shown below.
As you can see from the above results, the baseline intercept has an estimated value of between 2.5 and 2.9. So what does this mean?
Unfortunately, interpreting the parameters of a Poisson regression is more involved than a simple linear regression (y = x). In this linear regression, we could say for every unit increase in x, increases by . However, in the Poisson regression we need to consider the link function. The following cross validated post explains in great detail how we arrive at the below formulation.
For a Poisson model, given a unit change in , the fitted changes by
The main takeaway from this is that the effect of changing x depends on the current value of y. Unlike the simple linear regression, a unit change in x does not cause a consistent change in y.
Marginal and pairwise density plots
The below plot shows the marginal densities (across the diagonal) and the pairwise densities (lower and upper panes). This plot is very useful for understanding how covariates interact with one another. In the above example, we can see that as the number of participants increases, the baseline intercept decreases.
Mixed effects poisson regression
We can make a small extension to the above model by including a random intercept parameter. This will allow us to estimate a baseline parameter value for each person I communicate with. For all other parameters I will estimate a parameter across the entire population. For each person i and each message j, this is formally represented as:
By introducing this random effects parameter for each person i, it allows the model to establish a different baseline for each person responded to - whilst estimating the effects of the covariates on the response for the entire population.
The interpretation of the above results are interesting:
Each person has a different baseline response rate (as shown in the pooled and partially pooled model in Section 3)
Longer messages take marginally longer to respond to
You are more likely to get a slow response if you message me on the weekend
I tend to reply more quickly to conversations that have multiple people added to it (group chat)
And after accounting for the effect of each covariate on the response, the model estimates the below parameters.