Markov Process – Explaining Unexpected Identical Transition Rate Matrices for Different Covariate Values in R’s MSM

markov-processrtransition matrix

I am fitting a continuous-time Markov model to a panel dataset using the R package MSM. Because I am interested in sex-differences in transition rates, I fit the model with covariate sex ("M" or "F") by running

model_object <- msm(
  formula = state ~ nr_years, 
  subject = id_var,
  qmatrix = M, # matrix encoding allowed transitions between states
  data = panel_data,
  covariates = ~ sex,
  control = list(fnscale = 40000, maxit = 1e6) # got these from the help pages
)

After fitting the model I obtain the transition rate matrix using

qmatrix.msm(model_object, covariates = list(sex = "M"))
qmatrix.msm(model_object, covariates = list(sex = "F"))

These lines the exact same transition rate matrix. This is a bit unexpected to be, because when I use the hazard.msm function to extract hazard ratios, there are some differences between sexes ( are even statistically significant).

Does this make sense statistically?

Best Answer

I have learned that the cause of this problem is that I didn't transform sex (which is a character vector) to a factor. In this case, msm() will silently ignore this covariate, which is why the Q matrices were the same. I had tried to specify covariates = ~ factor(sex) but this didn't help.

TL;DR: categorical variables should be coded as factors before using these as covariates with the msm() function.