[Paper Review] merlin - a unified modelling framework for data analysis and methods development in Stata
This paper introduces merlin, a unified modeling framework in Stata that enables flexible fitting of complex statistical models, including multilevel mixed-effects models, joint models of longitudinal and survival data, and user-defined distributions. It leverages a hierarchical syntax for specifying models with arbitrary link functions, random effects, and time-dependent covariates, achieving high flexibility at the cost of computational speed.
merlin can do a lot of things. From simple stuff, like fitting a linear regression or a Weibull survival model, to a three-level logistic mixed effects model, or a multivariate joint model of multiple longitudinal outcomes (of different types) and a recurrent event and survival with non-linear effects...the list is rather endless. merlin can do things I haven't even thought of yet. I'll take a single dataset, and attempt to show you the full range of capabilities of merlin, and discuss some future directions for the implementation in Stata.
Motivation & Objective
- To develop a general-purpose modeling framework in Stata capable of handling a wide array of statistical models beyond standard implementations.
- To unify previous specialized commands (e.g., stjm, stmixed, stgenreg) into a single, extensible framework.
- To enable users to specify complex models with multiple outcome types, random effects, and non-linear effects using a consistent syntax.
- To support methodological innovation by allowing users to define custom distributions, hazard functions, and link functions via Mata programming.
- To provide a foundation for future extensions, including analytic derivatives, new random effects distributions, and dynamic prediction tools.
Proposed method
- merlin uses a hierarchical model specification syntax: `merlin (model1) (model2) ...`, where each model is defined by a dependent variable, components, and options.
- Each component is built from elements such as covariates, restricted cubic splines (rcs), time-dependent functions, and random effects, combined via multiplication and addition.
- The framework supports multiple families (e.g., Gaussian, Bernoulli, Weibull, Royston-Parmar) and allows user-defined log-likelihoods through Mata functions.
- Random effects are integrated using adaptive or non-adaptive Gaussian quadrature, or Monte Carlo integration, with support for multivariate normal or t-distributed random effects.
- The model is estimated via maximum likelihood, with support for time-varying coefficients through the `timevar()` option and `fp()` or `rcs()` functions.
- The architecture allows linking submodels through shared random effects or structural equations, enabling joint modeling of diverse outcome types.
Experimental results
Research questions
- RQ1Can a single, extensible Stata command support a broad range of statistical models, from simple linear regression to complex multivariate joint models?
- RQ2How can a unified framework be designed to handle mixed-effects models with arbitrary distributions, link functions, and random effects structures?
- RQ3What are the trade-offs between flexibility and computational efficiency in a general-purpose modeling engine?
- RQ4To what extent can user-defined distributions and hazard functions be seamlessly integrated into a maximum-likelihood estimation framework?
- RQ5How can shell commands be used to simplify complex model specifications while retaining full access to the underlying merlin engine?
Key findings
- merlin successfully fits a four-outcome model combining survival, continuous longitudinal, binary, and time-to-event data with two random effects, converging in approximately 16 minutes on a standard laptop.
- The framework supports complex models such as multivariate joint models with non-linear effects, time-dependent covariates, and multiple random effects structures.
- User-defined distributions and hazard functions can be implemented via Mata functions, enabling custom likelihoods and flexible modeling of complex survival processes.
- The use of restricted cubic splines (rcs) and fractional polynomial (fp) terms allows flexible modeling of non-linear covariate effects and time trends.
- Despite its flexibility, merlin’s current implementation using finite-difference score and Hessian calculations results in slower performance compared to specialized commands.
- Shell commands are proposed as a solution to simplify syntax and improve convergence by enabling tailored starting values and model-specific logic while calling merlin under the hood.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.