RMS Semiparametric Ordinal Longitudinal Model

We’ve been working on an R package for Markov ordinal transition models over the past year and it’s finally in a state that’s pretty usable.

I called it mostr (MOST was already taken) and you can find it here on github.

It’s not quite ready yet for CRAN…

My main goal was to lower the barrier for people to use MOST models, by using very similar workflow as the marginaleffects package. I focused mainly on marginal effects but mostr also supports predictions, contrasts, and variance estimation for particular patient profiles. Most of the functionality is shown in the vignettes.

Analytic variance estimation for second-order models, partial-PO models, and estimands like time benefit is still missing (it gets complicated very quickly), but optimized resampling approaches support any type of estimand.

NB: Especially for making our code faster (porting to C++), I relied quite a lot on Claude.

6 Likes

Much needed! Thank you

It’s great to see this Johannes.

An about-to-be-released major update of the orm function adds two types of random effects that are pertinent for MOST models. The first is basic random intercepts which I think of as principled GEE to make treat effect standard errors more accurate. The second is an adjustment for an increasing-over-time variance of Y problem caused by conditioning on Y previous rather than on residuals from Y previous. I am thinking that the first type of random effect, at least, should be standard in MOST to make standard errors more robust.
For estimation I condition on random effect = 0 so that no code changes are needed for prediction.

Random effects are implemented so that scaling to large numbers of distinct Y values is efficient. For example, the ordinal package cannot efficiently handle 100 distinct Y values when there are random effects, but orm can efficiently handle thousands.

Making the Hmisc package function for computing state occupancy probabilities easy to use and making bootstrap confidence intervals for such derived quantities easy to get would be nice extensions of your package if you don’t already have those. Keep up the great work.

2 Likes

Thanks for the nice words, Frank!

We have implemented our own bootstrapping approaches (see here and here), either NPM or fractional weighted bootstrap. We wrote our own implementation because we wanted to process all patients at once and as efficiently as possible (IIRC your Hmisc function only allows processing of one patient profile at a time).

I saw that you were working on this! We have adressed the intra-patient correlation using robust (sandwich) variance estimation, which controls frequentist type I error very well in all kinds of scenarios in our simulations.

I’m not yet sure how to handle random effects when calculating the variance for the ATE analytically, because I’m quite sure that just setting them to 0 will lead to undercoverage for derived estimands like difference in time in state 1 between groups, when actually randomly sampling from a superopulation. (Ofc we don’t randomly sample in reality, but one might still want to show coverage for that case)

I remember that when using Predict() using a blrm() model, RE are also set to 0. We built our own prediction function so the user can include or exclude the random effects, but we don’t support completely integrating them out.

I’m afraid that I don’t follow.

I highly recommend against using ATE.

Nice work on the bootstrap.

The sandwich estimator will give fairly valid standard errors but will not correctly estimate the (remaining) Markov structure (dependence on Yprev) when the random effect variance is large. The sandwich estimator will also not be able to make the tailored dual random effects variance adjustment. The link I provided above explains this second form.
The sandwich estimator may also have trouble when cluster size variation is huge.

1 Like

Very cool @Johannes_Schwenke !
Any chance for Python version? What about performance metrics? Mainly Calibration.

For another topic:

@f2harrell

You wrote in Longitudinal Ordinal Models as a General Framework for Medical Outcomes that it’s trivial to implement ML with markov models. Can you ellaborate on that?

Tbh, I would expect Claude Opus 5.5 to pretty much one shot a Python version of this package.

I haven’t thought much about performance metrics yet. What did you have in mind for calibration. Currently more interested in MSE when some assumptions are violated / we borrow accross the scale when we shouldn’t.

This is a good reference:

https://onlinelibrary.wiley.com/doi/10.1002/sim.10094

Mainly, Calibration for state-occupancy or transition probabilities

1 Like

The beauty of first order Markov models is that the previous Y value comes an ordinary covariate. So you can model transitions with any software or even ML. The only trick is to convert transition probabilities into state occupancy probabilities, to handle certain random effects patterns, and to allow the exposure variable to have different effects over time (e.g., one state occurs early, another state occurs late; non-proportional odds in time).

1 Like

i feel like it’s the usual story, we should be using this model but instead i see people dichotomise an ordinal QoL scale and apply logistic regression. Why? For the sas user the nlmixed code is a bit complicated and the run time is long (can be many hours). But these are not excuses, even when there are a number of outcomes to analyse. Is there a paper in a medical journal where they used this repeated measures model with random intercepts for patients and have plotted the exceedance probabilites over time (conditioning on rand effect=0 and eg the median baseline score)? It would be fun to confirm that someone got it into a medical journal

My colleagues at Imperial College London have published such articles. Look for ones authored by Matthew Shun-Shin and myself.

1 Like

We’ve been working on correctly handling missing states for our mostr package.

A colleague pointed out that with missingness, we are not maximizing the correct likelihood. We should average over the paths the patient could have taken. Currently, one can do a hybrid solution by including the row with the missing Y using Ocens(). But for the next row yprev has to be imputed or carried forward, which is not elegant (and does not respect the Markov structure of the model).

With the help of Fable 5.2, I was able to implement this in a very, very efficient way, so model fitting doesn’t take much longer than with complete data, even if averaging over thousands of paths in situations of high missingness. Use of Ocens() is also supported. Essentially, we fit the model first on the rows with an observed state, and then optimize the correct likelihood using Newton-Raphson, while always averaging over the paths the patients could have taken.

The result is removal of bias and often higher efficiency because we use all information available.

However, the resulting code is so difficult for me/us to parse that it will probably still take some time until this becomes a part of the R package.

If anybody wants to contribute, they are of course very welcome.

Small example where 70% of non-absorbing states are randomly missing.

library(mostr)

# 10,000 patients in two arms, ACTT-2 preset, daily states for 28 days,
# treatment odds ratio 0.8 for being in a worse state.
complete <- sim_markov(
  "actt2",
  n_patients = 10000,
  times = 1:28,
  treatment_effect = log(0.8),
  seed = 20261117
)

# One row per patient and day with the state `y` and the previous day's
# state `yprev`; state 8 (death) is absorbing.
complete <- prepare_markov_data(complete, absorb = 8L)

# Delete each non-absorbing state with probability 0.7, independently of
# everything else (missing completely at random). Deaths are always recorded.
set.seed(1)
observed <- complete
y <- as.integer(as.character(complete$y))
missed <- y != 8 & stats::runif(nrow(complete)) < 0.7

# On the row after a deleted state, the previous state is unknown as well.
same_patient <- c(FALSE, complete$id[-1] == complete$id[-nrow(complete)])
after_missed <- c(FALSE, missed[-length(missed)]) & same_patient

observed$y[missed] <- NA
observed$yprev[after_missed] <- NA

# fit model and drop any rows with missing data
fit_drop <- mostm(
  y ~ yprev + rms::rcs(time, c(2, 6, 11, 16, 21, 27)) + tx,
  data = observed, id_var = "id", absorb = "8",
  missing_states = "drop"
)

# fit and average over the paths the patients could have taken
fit_average <- mostm(
  y ~ yprev + rms::rcs(time, c(2, 6, 11, 16, 21, 27)) + tx,
  data = observed, id_var = "id", absorb = "8",
  missing_states = "average"
)

Time it takes to fit the models

Fit Time per mostm() call Iterations
Rows dropped 1.8–2.2 s 9 (rms::orm())
Averaged over unobserved states 15.7–16.1 s 12 Newton iterations
Complete data, for reference 5.8–6.1 s not read out

Dropping rows with missing data → mortality overestimated

Coefficient Table
Complete data Rows dropped Averaged Simulation
y ≥ 2 −0.665 (0.028) −0.832 (0.073) −0.701 (0.038) −0.6465
y ≥ 3 −4.867 (0.054) −4.909 (0.161) −4.823 (0.083) −4.9314
y ≥ 4 −5.148 (0.054) −5.224 (0.161) −5.120 (0.084) −5.2230
y ≥ 5 −7.338 (0.056) −7.383 (0.164) −7.319 (0.087) −7.4169
y ≥ 6 −11.461 (0.058) −11.625 (0.170) −11.457 (0.090) −11.5321
y ≥ 7 −15.171 (0.062) −15.282 (0.183) −15.179 (0.095) −15.2262
y ≥ 8 (death) −21.976 (0.078) −21.112 (0.211) −21.933 (0.107) −22.0693
yprev = 2 −0.004 (0.022) 0.058 (0.073) 0.048 (0.069) 0
yprev = 3 6.195 (0.065) 6.158 (0.216) 6.200 (0.127) 6.2218
yprev = 4 7.081 (0.050) 7.111 (0.153) 7.043 (0.081) 7.1412
yprev = 5 9.877 (0.051) 9.937 (0.159) 9.850 (0.083) 9.9809
yprev = 6 14.102 (0.057) 14.199 (0.173) 14.098 (0.091) 14.1645
yprev = 7 19.309 (0.065) 19.471 (0.199) 19.313 (0.099) 19.3517
time −0.306 (0.006) −0.289 (0.017) −0.302 (0.009) −0.3084
time′ 2.473 (0.083) 2.338 (0.247) 2.414 (0.118) 2.5186
time″ −4.929 (0.214) −4.600 (0.658) −4.765 (0.311) −5.0276
time‴ 3.107 (0.262) 2.796 (0.842) 2.887 (0.394) 3.1714
time⁗ −1.039 (0.244) −1.006 (0.814) −0.820 (0.384) −1.0672
tx −0.206 (0.010) −0.232 (0.032) −0.210 (0.012) −0.2231
Treatment odds ratio 0.814 0.793 0.810 0.800

Contact Thomas Stewart of the University of Virginia School of Data Science who has done a lot of work on this and has figured out the complex state occupancy probability calculations from deconditioning partial conditioning. It’s pretty numerically taxing. @thomas.stewart