Functions for model fitting and inference