mxFactor command

4 replies [Last post]
CarolKan's picture
Offline
Joined: 04/17/2014

Dear all

I am trying to run a saturated model but there is something wrong in the way I have have ordered the variables, as I received this error message:
# Error: In model 'Sat' column 'dm1' is not an ordered factor. Use mxFactor() on this column.

So, I did the following after the reshape command:
TwDEP_DM <- reshape(DEP_DM, idvar = c("PAIRID", "ZYG"), timevar = "TVAB", direction = "wide")
colnames(TwDEP_DM) <- c('PAIRID', 'ZYG', 'dm1', 'dep1', 'age1','dm2', 'dep2', 'age2')
TwDEP_DM$dm1 <- mxFactor(TwDEP_DM$dm1, levels=c(0:2), ordered = TRUE)
TwDEP_DM$dep1 <- mxFactor(TwDEP_DM$dep1, levels=c(0:2), ordered = TRUE)
TwDEP_DM$dm2 <- mxFactor(TwDEP_DM$dm2, levels=c(0:2), ordered = TRUE)
TwDEP_DM$dep2 <- mxFactor(TwDEP_DM$dep2, levels=c(0:2), ordered = TRUE)

But:
> SatFit <- mxRun(SatModel, intervals=F)
Running Sat
# Error: In model 'Sat' I was expecting 1 thresholds in column 'dm1' of matrix/algebra 'MZ.expThresMZ' but I hit NA values after only 0 thresholds. You need to increase the number of free thresholds for 'dm1' and give them values other than NA

Is there anything I can do differently?
I think there is something in the mxFactor () as all of the "0" has been replaced as "1"; "1" with "2" ...

As before mxFactor, the data is liked this:
> str(TwDEP_DM)
'data.frame': 43565 obs. of 8 variables:
$ PAIRID: num 21 22 23 24 26 29 31 33 34 35 ...
$ ZYG : num 1 4 2 2 4 1 4 4 4 1 ...
$ dm1 : num 0 0 NA 0 NA 0 NA 0 NA 0 ...
$ dep1 : num 0 0 NA 1 NA 0 NA 0 NA 0 ...
$ age1 : num 73 74 NA 74 NA 74 NA NA NA NA ...
$ dm2 : num 0 NA 0 0 0 0 0 0 0 0 ...
$ dep2 : num 0 NA 1 0 0 0 0 0 0 0 ...
$ age2 : num 73 NA 73 74 74 74 NA NA NA NA ...
- attr(*, "reshapeWide")=List of 5
..$ v.names: NULL
..$ timevar: chr "TVAB"
..$ idvar : chr "PAIRID" "ZYG"
..$ times : num 1 2
..$ varying: chr [1:3, 1:2] "DMDx.1" "DepDx.1" "Age.1" "DMDx.2" .

after mxFactor
> str(TwDEP_DM)
'data.frame': 43565 obs. of 8 variables:
$ PAIRID: num 21 22 23 24 26 29 31 33 34 35 ...
$ ZYG : num 1 4 2 2 4 1 4 4 4 1 ...
$ dm1 : Ord.factor w/ 3 levels "0"<"1"<"2": 1 1 NA 1 NA 1 NA 1 NA 1 ...
$ dep1 : Ord.factor w/ 3 levels "0"<"1"<"2": 1 1 NA 2 NA 1 NA 1 NA 1 ...
$ age1 : num 73 74 NA 74 NA 74 NA NA NA NA ...
$ dm2 : Ord.factor w/ 3 levels "0"<"1"<"2": 1 NA 1 1 1 1 1 1 1 1 ...
$ dep2 : Ord.factor w/ 3 levels "0"<"1"<"2": 1 NA 2 1 1 1 1 1 1 1 ...
$ age2 : num 73 NA 73 74 74 74 NA NA NA NA ...
- attr(*, "reshapeWide")=List of 5
..$ v.names: NULL
..$ timevar: chr "TVAB"
..$ idvar : chr "PAIRID" "ZYG"
..$ times : num 1 2
..$ varying: chr [1:3, 1:2] "DMDx.1" "DepDx.1" "Age.1" "DMDx.2" ...

I have tried:
TwDEP_DM$dm1 <- mxFactor(TwDEP_DM$dm1, levels=c(0:2), exclude NA, ordered = TRUE)
TwDEP_DM$dm1 <- mxFactor(TwDEP_DM$dm1, levels=c(0:2), labels (2, NA), ordered = TRUE)

Any help will be much appreciated.

Kind regards
Carol

tbates's picture
Offline
Joined: 07/31/2009
what is in MxModel?

OpenMx is saying that the thresholds matrix is set for zero thresholds for variable dm1.
But as Rob said, with just

SatFit <- mxRun(SatModel)

It's hard to say why... need to see the rest of what is in SatModel, in particular how you are setting the mxFIMLObjective and what matrix you are passing the thresholds argument to this function.

RobK's picture
Offline
Joined: 04/19/2011
I think it's a problem with your model-expected thresholds

After mxFactor(), try entering TwDEP_DM$dm1[1:10] into the R console. Do you see zeroes and NAs, or ones and NAs?

I don't think it's a problem with mxFactor(), though. The more important question is, what are the initial values of your model's expected thresholds? The second error message you included in your post suggests that they are somehow started at NA, and if that's the case, OpenMx won't be able to do anything with them. Another possibility is that the number of thresholds is too small for the number of levels in the ordinal variables.

BTW, the reason you got the second error after using mxFactor() isn't because mxFactor() caused the second error. Instead, mxFactor() enabled your variables to pass the check of being ordinal variables where necessary, but your model then fails a subsequent check because of the problem with the thresholds. At least, this is what I think is happening; without seeing your syntax, I can't be completely sure.

CarolKan's picture
Offline
Joined: 04/17/2014
Thank you. This is what I got

Thank you.

This is what I got from TwDEP_DM$dm1[1:10]
[1] 0 0 0 0 0 0
Levels: 0 < 1

The data looks like this. It is a twin dataset, so there are a lot of missing twin with all missing variables.

PAIRID ZYG dm1 dep1 age1 dm2 dep2 age2
1 21 1 0 0 73 0 0 73
2 22 4 0 0 74 NA NA NA
3 23 2 NA NA NA 0 1 73
4 24 2 0 1 74 0 0 74
5 26 4 NA NA NA 0 0 74
6 29 1 0 0 74 0 0 74
7 31 4 NA NA NA 0 0 NA
8 33 4 0 0 NA 0 0 NA
9 34 4 NA NA NA 0 0 NA
10 35 1 0 0 NA 0 0 NA

Kind regards
Carol

mhunter's picture
Offline
Joined: 07/31/2009
No problem with mxFactor

There is no problem with mxFactor here. There is just some confusion about how R represents factors.

 
> require(OpenMx)
> 
> set.seed(46) # Make repeatable
> 
> data <- data.frame(x1=sample(0:2, 50, replace=TRUE))
> data$x2 <- data$x1+1
> 
> data[1:10,]
   x1 x2
1   0  1
2   0  1
3   1  2
4   1  2
5   0  1
6   1  2
7   2  3
8   1  2
9   2  3
10  2  3
> 
> data$x1F <- mxFactor(data$x1, levels=0:2)
> data$x2F <- mxFactor(data$x2, levels=1:3)
> data[1:10,]
   x1 x2 x1F x2F
1   0  1   0   1
2   0  1   0   1
3   1  2   1   2
4   1  2   1   2
5   0  1   0   1
6   1  2   1   2
7   2  3   2   3
8   1  2   1   2
9   2  3   2   3
10  2  3   2   3
> str(data)
'data.frame':   50 obs. of  4 variables:
 $ x1 : int  0 0 1 1 0 1 2 1 2 2 ...
 $ x2 : num  1 1 2 2 1 2 3 2 3 3 ...
 $ x1F: Ord.factor w/ 3 levels "0"<"1"<"2": 1 1 2 2 1 2 3 2 3 3 ...
  ..- attr(*, "mxFactor")= logi TRUE
 $ x2F: Ord.factor w/ 3 levels "1"<"2"<"3": 1 1 2 2 1 2 3 2 3 3 ...
  ..- attr(*, "mxFactor")= logi TRUE

I create two variables. The first has values of 0, 1, and 2. The second variable is identical to the first plus 1. I then apply mxFactor to both variables. Notice that when I just look at the data, the mxFactor'ed variables are still printed like their non-mxFactor'ed relatives. However, when I look at str(data) you can see the underlying representation of factors in R. The 1 1 2 2 1 2 3 2 3 3 refers to which level of the factor occurs, not the value of the level. So, levels 1 1 2 2 1 2 ... refers to the values "0" "0" "1" "1" "0" "1" in x1F, but it refers to "1" "1" "2" "2" "1" "2" in x2F.

Does that help to clarify the situation?