Page 1 of 2

Issue with Interactions in Formula

Posted: Mon Sep 11, 2017 2:54 pm
by jpcmu5002
runMLwiN is inconsistent accepting interactions in my formulas and I don't see a pattern. For example, interactions that work when in separate models create an error when included in the same formula. Here's an example. (Unfortunately, I haven't been able to recreate the problem with MLwiN's example data sets so far.)

#set up formula for three models
form.raceInteract1 <- formula(z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d1BlackNonHisp_scs:d0locIndeg + (1|scid) + (1|commID) + (1|aid) )

form.raceInteract2 <- formula(z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d1BlackNonHisp_scs:d0extIndeg + (1|scid) + (1|commID) + (1|aid) )

form.raceInteract3 <- formula(z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d1BlackNonHisp_scs:d0locIndeg + d1BlackNonHisp_scs:d0extIndeg + (1|scid) + (1|commID) + (1|aid) )

#fit models
fit1 <- runMLwiN(form.raceInteract1, data=DF, D="Normal")
fit2 <- runMLwiN(form.raceInteract2, data=DF, D="Normal")
fit3 <- runMLwiN(form.raceInteract1, data=DF, D="Normal")


The first two models run as they should but the third returns.
Error in `[.data.frame`(indata, , outvars) : undefined columns selected

All three models include the same variables. The only difference is that both interaction terms are included in the third. The variables with the d prefix are dummy variables. All the fixed parameters are at level 1.

I've also tried turning the dummies to factors but with the same result.

Can anyone offer advice as to what may be going on?

Thank you in advance!

Re: Issue with Interactions in Formula

Posted: Mon Sep 11, 2017 3:12 pm
by ChrisCharlton
I'm not sure whether it is a typo copying the code into your message but your third fit line:

Code: Select all

fit3 <- runMLwiN(form.raceInteract1, data=DF, D="Normal")
appears to refer to the first formula (form.raceInteract1) instead of the third (form.raceInteract3). If you are using different data between the fits then this might explain the error. If not then can you try the fixed part of the model with other R fit functions (e.g. lm() or lmer()) to see whether you see similar behaviour there?

Re: Issue with Interactions in Formula

Posted: Mon Sep 11, 2017 6:36 pm
by jpcmu5002
Thanks for the prompt reply.

The form.raceInteract1 for the third model was just a typo copying to the post. The fitted model was correct.
It's the same data set for all models and the formula works fine with lmer.

Re: Issue with Interactions in Formula

Posted: Tue Sep 12, 2017 8:34 am
by ChrisCharlton
Could you please provide further information about the variables in the data, e.g. number of factor levels with their labels and number of unique values for each variable?

Re: Issue with Interactions in Formula

Posted: Tue Sep 12, 2017 8:51 am
by ChrisCharlton
I think that I can see a possible cause. If I run the following:

Code: Select all

> form.raceInteract3 <- formula(z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d1BlackNonHisp_scs:d0locIndeg + d1BlackNonHisp_scs:d0extIndeg + (1|scid) + (1|commID) + (1|aid) )
> terms.formula(form.raceInteract3)
z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + 
    zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d1BlackNonHisp_scs:d0locIndeg + 
    d1BlackNonHisp_scs:d0extIndeg + (1 | scid) + (1 | commID) + 
    (1 | aid)
attr(,"variables")
list(z_pcm3partr, d0locIndeg, d0extIndeg, zN0mapDDpopBXisol, 
    zN0mapDDextpopBXisol, d1BlackNonHisp_scs, 1 | scid, 1 | commID, 
    1 | aid)
attr(,"factors")
                     d0locIndeg d0extIndeg zN0mapDDpopBXisol zN0mapDDextpopBXisol d1BlackNonHisp_scs 1 | scid 1 | commID 1 | aid d0locIndeg:d1BlackNonHisp_scs d0extIndeg:d1BlackNonHisp_scs
z_pcm3partr                   0          0                 0                    0                  0        0          0       0                             0                             0
d0locIndeg                    1          0                 0                    0                  0        0          0       0                             1                             0
d0extIndeg                    0          1                 0                    0                  0        0          0       0                             0                             1
zN0mapDDpopBXisol             0          0                 1                    0                  0        0          0       0                             0                             0
zN0mapDDextpopBXisol          0          0                 0                    1                  0        0          0       0                             0                             0
d1BlackNonHisp_scs            0          0                 0                    0                  1        0          0       0                             1                             1
1 | scid                      0          0                 0                    0                  0        1          0       0                             0                             0
1 | commID                    0          0                 0                    0                  0        0          1       0                             0                             0
1 | aid                       0          0                 0                    0                  0        0          0       1                             0                             0
attr(,"term.labels")
 [1] "d0locIndeg"                    "d0extIndeg"                    "zN0mapDDpopBXisol"             "zN0mapDDextpopBXisol"          "d1BlackNonHisp_scs"            "1 | scid"                     
 [7] "1 | commID"                    "1 | aid"                       "d0locIndeg:d1BlackNonHisp_scs" "d0extIndeg:d1BlackNonHisp_scs"
attr(,"order")
 [1] 1 1 1 1 1 1 1 1 2 2
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
You will see that the term order of the interaction d1BlackNonHisp_scs:d0locIndeg has been flipped around to d0locIndeg:d1BlackNonHisp_scs. The extra data variables are generated from this, however R2MLwN will be looking for them as they are in the original formula. Could you try adjusting your third formula to match the variables as above to see if that fixes it?

Re: Issue with Interactions in Formula

Posted: Wed Sep 13, 2017 8:38 pm
by jpcmu5002
Apologies for the delayed response. I tried that solution. Unless I misunderstood the suggestion, the same error comes up,

Code: Select all

>
> form.raceInteract3B <- formula(z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d0locIndeg:d1BlackNonHisp_scs + d0extIndeg:d1BlackNonHisp_scs  + (1|scid) + (1|commID) + (1|aid) )

> attributes(terms.formula(form.raceInteract3B))
$variables
list(z_pcm3partr, d0locIndeg, d0extIndeg, zN0mapDDpopBXisol, 
    zN0mapDDextpopBXisol, d1BlackNonHisp_scs, 1 | scid, 1 | commID, 
    1 | aid)

$factors
                     d0locIndeg d0extIndeg zN0mapDDpopBXisol
z_pcm3partr                   0          0                 0
d0locIndeg                    1          0                 0
d0extIndeg                    0          1                 0
zN0mapDDpopBXisol             0          0                 1
zN0mapDDextpopBXisol          0          0                 0
d1BlackNonHisp_scs            0          0                 0
1 | scid                      0          0                 0
1 | commID                    0          0                 0
1 | aid                       0          0                 0
                     zN0mapDDextpopBXisol d1BlackNonHisp_scs 1 | scid
z_pcm3partr                             0                  0        0
d0locIndeg                              0                  0        0
d0extIndeg                              0                  0        0
zN0mapDDpopBXisol                       0                  0        0
zN0mapDDextpopBXisol                    1                  0        0
d1BlackNonHisp_scs                      0                  1        0
1 | scid                                0                  0        1
1 | commID                              0                  0        0
1 | aid                                 0                  0        0
                     1 | commID 1 | aid d0locIndeg:d1BlackNonHisp_scs
z_pcm3partr                   0       0                             0
d0locIndeg                    0       0                             1
d0extIndeg                    0       0                             0
zN0mapDDpopBXisol             0       0                             0
zN0mapDDextpopBXisol          0       0                             0
d1BlackNonHisp_scs            0       0                             1
1 | scid                      0       0                             0
1 | commID                    1       0                             0
1 | aid                       0       1                             0
                     d0extIndeg:d1BlackNonHisp_scs
z_pcm3partr                                      0
d0locIndeg                                       0
d0extIndeg                                       1
zN0mapDDpopBXisol                                0
zN0mapDDextpopBXisol                             0
d1BlackNonHisp_scs                               1
1 | scid                                         0
1 | commID                                       0
1 | aid                                          0

$term.labels
 [1] "d0locIndeg"                    "d0extIndeg"                   
 [3] "zN0mapDDpopBXisol"             "zN0mapDDextpopBXisol"         
 [5] "d1BlackNonHisp_scs"            "1 | scid"                     
 [7] "1 | commID"                    "1 | aid"                      
 [9] "d0locIndeg:d1BlackNonHisp_scs" "d0extIndeg:d1BlackNonHisp_scs"

$order
 [1] 1 1 1 1 1 1 1 1 2 2

$intercept
[1] 1

$response
[1] 1

$class
[1] "terms"   "formula"

$.Environment
<environment: R_GlobalEnv>

> fit.raceInteract3B <- runMLwiN(form.raceInteract3B, data=DF, D="Normal")
Error in `[.data.frame`(indata, , outvars) : undefined columns selected
Calls: runMLwiN -> [ -> [.data.frame
Execution halted

Re: Issue with Interactions in Formula

Posted: Wed Sep 13, 2017 8:47 pm
by jpcmu5002
I tried to bypass the problem entirely by using a character string instead of a formula object, which the manual says can be done. I can't find examples of this approach, so I'm confused why I'm getting the following error. I think I'm following the syntax rules on page 22 of the manual correctly.

Code: Select all

> charform.raceInteract3B <- 'z_pcm3partr ~ 1 + d0locIndeg + d0extIndeg + zN0mapDDpopBXisol + zN0mapDDextpopBXisol + d1BlackNonHisp_scs + d0locIndeg:d1BlackNonHisp_scs + d0extIndeg:d1BlackNonHisp_scs  + (1|scid) + (1|commID) + (1|aid)'

> fitchar.raceInteract3B <- runMLwiN(charform.raceInteract3B, data=DF)
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:0: unexpected end of input
1: ~

Re: Issue with Interactions in Formula

Posted: Thu Sep 14, 2017 9:09 am
by ChrisCharlton
I don't think that specifying the formula as a string is likely to make a difference as it will be converted back to a formula object internally anyway. Could you try adding the option drop.data=FALSE to your estoptions list in case one of the variables is being inadvertently removed and see if that makes a difference?

Re: Issue with Interactions in Formula

Posted: Fri Sep 15, 2017 3:03 pm
by ChrisCharlton
I believe that we have now managed to track down the cause of this bug. Could you please let us know whether the attached test version of the package fixes the problem for you?

Re: Issue with Interactions in Formula

Posted: Mon Sep 18, 2017 3:22 pm
by jpcmu5002
Hmmm... Well with this version, the model that was producing the error before (form.raceInteract3 in the original post) now runs.
Unfortunately, the two models that worked before (form.raceInteract1 and form.raceInteract2) no longer work. They now produce

Code: Select all

Error in '[.data.frame'(indata, ,outvars):undefined columns selected
I also tried including drop.data=FALSE in estoptions, but to no avail.