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. Some results are even statistically significant.
Am I using the function wrong?
This is probably because your
sexvariable is coded as acharacterinstead of afactor.Just converting
sexinto factor before to run the model should do the trick here:panel_data$sex <- factor(panel_data$sex)Here is a reproducible example to illustrate this tricky
msmpackage behaviour:Let's first create a sex variable on
cavdataset ascharacter(sex.chr) and asfactor(sex.fct)we have to define the initial
qmatrixtoo:If you use the
characterversion ofsexas covariate you will get tthe same results for both covariates (the covariates are basicaly ignored)But using the factorial version of
sexwill give you the expected results