Something I’ve been trying to understand better over the pandemic has been the algorithms and mathematics that underpin commonly used statistical models. I’ve been reading a lot of great books. At the moment, I’m reading Wood’s book on Generalized Additive Models. He has a great chapter on mixed models. I see these models used a lot, but in many contexts there isn’t much discussion of what’s going on under the hood of whatever software package computed the model. So in this post, I’ll describe the logic of fitting these models with maximum likelihood estimation and write a function that does so in Python.