I am somewhat new to R and trying to polish my logistic regression. I am testing if my risk factors(cruise, age, sex, and year) have a significant effect on my dependent variable, MPS infection (named MPS_BINARY). I have a total of four cruises (5, 7, 9, 11), three years, thirteen ages, and two sexes (1 or 0).

I am able to run the full model with the following command:

>mylogit<-glm(MPS_BINARY~ AGE * SEX * CRUISE * YEAR, data=mps,family="binomial")

From this I have my p-values, I can now identify significant risk factors and interactions. My data shows a significant p-value for the interaction between cruise 7 and year. Following backwards selection, I now need to run the model again with my original risk factors and significant terms. I am having increasing difficulty isolating cruise 7 from my cruise data to run as an interaction with year. I have tried using the command:


But this, of course, does not recognize cruise 7 and I receive the error message: Error in model.frame.default(formula = MPS_BINARY ~ AGE + SEX + CRUISE + : invalid type (NULL) for variable 'mps$CRUISE7'.

My question is how can I run my logistic regression with all of my risk factors, and the significant interaction between year and cruise 7? I cannot figure out how to isolate only cruise 7 for the interaction with year. Please let me know if you need more information, thank you!

  • 4
    $\begingroup$ note: when you get a significant pvalue for the interaction between cruise 7 and year, this is relative to the reference levels. A better approach perhaps is to model the main effects (~AGE+SEX+YEAR+CRUISE) and then allow an interaction between cruise anf year (~AGE+SEX+CRUISE7*YEAR) and examine the if this results in a chnage in the deviance (use anova(mod1, mod2) $\endgroup$
    – user20650
    Commented Jul 7, 2015 at 16:27
  • $\begingroup$ typo in model 2: should be ~AGE+SEX+CRUISE*YEAR $\endgroup$
    – user20650
    Commented Jul 8, 2015 at 16:22

1 Answer 1


You probably shouldn't do this. A general rule of thumb advises against thinking of categorical variables as their individual levels during model selection.

Stepwise model selection is also frowned upon, it's especially problematic when you use p-values / significance as the criteria instead of a measure that penalizes more complicated models like AIC or BIC.

If you really want to break out the 7th cruise as its own variable the way is to create a dummy variable:

mps$CRUISE7 = ifelse(as.numeric(mps$CRUISE) == 7, 1, 0)

Then you can use it like any other variable. But at this point you'll have trouble with the non-broken-out CRUISE variable, you'll probably need to break it out into each of their individual levels (see tidyr::spread) and then treat them all individually... well, all but one level which will be your reference and gets lumped in with the intercept (thanks to commenters!); inadvisable but not impossible. The standard approach would be to not break out the individual cruises.

  • 1
    $\begingroup$ also with the caveat that all levels cant be broken out $\endgroup$
    – user20650
    Commented Jul 7, 2015 at 16:31
  • 1
    $\begingroup$ And with the caveat that you've selected the 7th cruise based on looking at the data, so p-values can't be interpreted easily. Agreed (+1) that stepwise selection is not a good idea. $\endgroup$
    – EdM
    Commented Jul 8, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.