chjackson / msm

The msm R package for continuous-time multi-state modelling of panel data
https://chjackson.github.io/msm/
57 stars 17 forks source link

Pearson goodness of fit tests fail for models with covariate interactions #95

Closed pkirwan closed 8 months ago

pkirwan commented 8 months ago

Running pearson.msm for a fitted model with covariate interactions results in an error.

Reproducible example:

psor.q <- rbind(c(0,0.1,0,0),c(0,0,0.1,0),c(0,0,0,0.1),c(0,0,0,0))
psor.msm <- msm(state ~ months, subject=ptnum, data=psor,
                qmatrix = psor.q, covariates = ~ollwsdrt*hieffusn,
                constraint = list(hieffusn=c(1,1,1),ollwsdrt=c(1,1,2)))
pearson.msm(psor.msm, timegroups=2, intervalgroups=2, covgroups=2)
Error in `[.data.frame`(dat, , attr(dat, "covnames.q"), drop = FALSE) : 
undefined columns selected

The error occurs on this line of the function: https://github.com/chjackson/msm/blob/4a131f07bff9f978bd5660fcfedeb91ef722543e/R/pearson.R#L262

Unless there is a reason not to consider multi-state models with covariate interactions, then a fix for this could be to exclude interaction terms from the list of covnames, e.g. by replacing: attr(dat,"covnames.q") with: attr(dat, "covnames.q")[!grepl(":", attr(dat, "covnames.q"))]

chjackson commented 8 months ago

Thanks - this is now fixed in https://github.com/chjackson/msm/commit/1ee610e1a8ec95928ad97187ebe4f053bcb8296c