Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constraint statement does not work for factor variables. #39

Closed
ngwali opened this issue Oct 19, 2021 · 2 comments
Closed

constraint statement does not work for factor variables. #39

ngwali opened this issue Oct 19, 2021 · 2 comments

Comments

@ngwali
Copy link

ngwali commented Oct 19, 2021

Hi Chris,

Thanks you so much for assisting me with setting up the constraint matrix. But I have a new problem on this matrix.

sex is a covariate in my mode.
I have 8 levels in the Q-matrix, some tranisitions not allowed are 2-8, 7-1, 8-1, 8-2, 8-3.

Option 1: Sex is a factor variable coded "Female", "Male"

sex.msm <- msm(state ~ Obstime, subject = subj, data = DF,
obstype = 2,
gen.inits=init,
qmatrix = Q,
method = "BFGS",
opt.method = "optim",
covariates = ~ sex,
constraint=list(sex=c( 1, 1, 1, 1, 1, 1, 1,
-1, 2, 2, 2, 2, 2,
-1, -2, 3, 3, 3, 3, 3,
-1, -2, -3, 4, 4, 4, 4,
-1, -2, -3, -4, 5, 5, 5,
-1, -2, -3, -4, -5, 6, 6,
-2, -3, -4, -5, -6, 7,
-4, -5, -6, -7)),
control = list (fnscale=95161, trace = 2, REPORT = 1,maxit=15000, reltol=1e-8))
Error in msm.check.constraint(constraint, mm) :
Covariate "sex" in constraint statement not in model.
In addition: Warning message:
In if (gen.inits) { :
the condition has length > 1 and only the first element will be used

**Option 2 : Sex is a numeric variable having values 0, 1. Sex is not Not a factor No errors whatsoever.

DF$sex <-as.numeric(DF$sex)
DF$sex[DF$sex==1]=0
DF$sex[DF$sex==2]=1

sex.msm <- msm(state ~ Obstime, subject = subj, data = DF,
obstype = 2,
gen.inits=init,
qmatrix = Q,
method = "BFGS",
opt.method = "optim",
covariates = ~ sex,
constraint=list(sex=c( 1, 1, 1, 1, 1, 1, 1,
-1, 2, 2, 2, 2, 2,
-1, -2, 3, 3, 3, 3, 3,
-1, -2, -3, 4, 4, 4, 4,
-1, -2, -3, -4, 5, 5, 5,
-1, -2, -3, -4, -5, 6, 6,
-2, -3, -4, -5, -6, 7,
-4, -5, -6, -7)),
control = list (fnscale=95161, trace = 2, REPORT = 1,maxit=15000, reltol=1e-8))
initial value 1.000000
final value 0.999298
converged

Used 33 function and 31 gradient evaluations

NB: Option 2 does work with no errors. My worries are as follows:

I have a factor variable called DMT with foour levels "DMT cat 1", "DMT cat 2", "DMT cat 3", and "DMT cat 4", where "DMT cat 4" is the reference level. This becomes an issue converting it to a numeric variables with values 1,2,3, 4, because the you cannot get separate hazards for level 1, 2, and 3 when using the constraint statement. However, using DMT as factor with 4 levels without the constraint statement produces hazards for 3 levels.

Can you sove this problem for me please? would be very much happy.

Thanks.

@chjackson
Copy link
Owner

See help(msm) section constraint:

          For categorical covariates, defined as factors, specify
          constraints as follows:
          'list(..., covnameVALUE1 = c(...), covnameVALUE2 = c(...),
          ...)'
          where 'covname' is the name of the factor, and 'VALUE1',
          'VALUE2', ... are the labels of the factor levels (usually
          excluding the baseline, if using the default contrasts).

There was actually a bug in the error handling here - there was supposed to be a warning message like the following, but this was not being triggered. I've fixed that in the development version.

Error in msm.check.constraint(constraint, mm) : 
  Covariate "sex" in constraint statement not in model.
	For factor covariates, specify constraints using covnameCOVVALUE = c(...)

@ngwali
Copy link
Author

ngwali commented Oct 20, 2021

That works now.

Many thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants