# SEM, Binary and Categorical Data in independent variables

Attachment | Size |
---|---|

example1.txt | 17.99 KB |

SEM.R | 2.95 KB |

example.pdf | 98.36 KB |

The data set has (please see example diagram)

+ 3 continuous variables(X1, X2, X3) for a latent variable (intercept)

+ 3 mediators (one binary X8 and two categorical variables X6, X7)

+ 1 dependent variable (X4).

This coding was based on the post about Categorical Data in both independent and dependent variables http://openmx.psyc.virginia.edu/thread/3883

When I run SEM.R model I have following error message.

Error in mxRAMObjective(A = "A", S = "S", M = "M", thresholds = "Threshold") :

argument 'F' is not a string (the name of the 'F' matrix)

When I delete #Threshold matrix from the SEM.R, I have the following error message.

Error: The following error occurred while evaluating the subexpression '`Linear Growth Curve Model Path Specification.I` - `Linear Growth Curve Model Path Specification.A`' during the evaluation of 'expVars' in model 'Linear Growth Curve Model Path Specification' : non-conformable arrays

#Threshold matrix

mxMatrix(type = "Full", nrow = 1, ncol = 7, free = c(T, T, T, T, T, T, T),

values = c(-0.18173563, -1.27430357,-1.75717640, 0.37933827,-0.01542988, -0.82819033, -0.12312234), byrow = T, dimnames = list(c(), c("X1","X2","X3","X4","X6","X7","X8")),

name = "Threshold"),

#Means and covaraince matrix

mxRAMObjective(A="A", S="S", M="M", thresholds = "Threshold")

I know you are busy! But I will return you back! I appreciate your time!

Hyoshin,

University of Maryland

## The first error you mention

`mxRAMObjective()`

needs a character string value for its argument`F`

, which has no default. The character string you provide is supposed to be the name of the 'F' (filter) matrix in the MxModel. I think what you want to do instead ismxRAMObjective(A="A", S="S", M="M", F="F", thresholds = "Threshold")

The second error you mention is occurring because your identity matrix

`"I"`

is 7x7, but your asymmetric-paths matrix`"A"`

ends up being 8x8, since you have 7 manifest variables and 1 latent variable. Redefine`"I"`

asmxMatrix("Iden",8,8,name="I")

Further, I notice the 7th line of your script is

dataset$X7 <- mxFactor(dataset$X8, levels = c(0, 1), ordered = T);

I suspect you want to do

dataset$X8 <- mxFactor(dataset$X8, levels = c(0, 1), ordered = T);

You'll also need to provide

`X8`

with thresholds, for instance,mxThreshold(vars='X8', nThresh=1, values=1)

Finally, if you're using

`mxThreshold()`

in a RAM-type model, I'm not sure you need the thresholds matrix. I think you can simply adjust your objective tomxRAMObjective(A="A", S="S", M="M", F="F", thresholds = "Thresholds")

Log in or register to post comments

In reply to The first error you mention by RobK

## Try this

vars <- c("X1","X2","X3","X4","X6","X7","X8");

dataset$X6 <- mxFactor(dataset$X6, levels = c(1, 2, 3), ordered = T);

dataset$X7 <- mxFactor(dataset$X7, levels = c(1, 2, 3), ordered = T);

dataset$X8 <- mxFactor(dataset$X8, levels = c(0, 1), ordered = T);

fmat <- diag(7)[c(4,5,7),]

growthCurveModel <- mxModel(

"Linear Growth Curve Model Path Specification",

type="RAM",

mxThreshold(vars='X6', nThresh=2, values=c(1, 3)),

mxThreshold(vars='X7', nThresh=2, values=c(1, 3)),

mxThreshold(vars='X8', nThresh=1, values=1),

mxData(dataset, type="raw"),

manifestVars=c("X1","X2","X3","X4","X6","X7","X8"),

latentVars=c("intercept"),

mxMatrix("Iden",8,8,name="I"), mxMatrix("Unit",3,1,name="u3"),

mxMatrix("Full",3,7,values=fmat,name="fmat"),

mxAlgebra(diag2vec(fmat%&%(F%&%(solve(I-A)%&%S))), name="expVars"),

mxConstraint(expVars==u3,name="Varcon"),

# latent variances and covariance

mxPath(from = "intercept", to = "X6", arrows = 2, values = 0, free = T, labels = "cov6"),

mxPath(from = "intercept", to = "X7", arrows = 2, values = 0, free = T, labels = "cov7"),

mxPath(from = "intercept", to = "X8", arrows = 2, values = 0, free = T, labels = "cov8"),

mxPath(from = c("X1","X2","X3","X4","X6","X7","X8"), arrows = 2, free = c(F, F, F, T, T, T, T), values = c(1,1,1,1,1,1,1), lbound=.01),

mxPath(from = "intercept", arrows = 2, free = T, values = 1, lbound=.01),

# intercept loadings

mxPath(from = "intercept", to=c("X1","X2","X3"), arrows=1, free=FALSE, values = 0, labels= c("a1","a2","a3")),

mxPath(from = c("intercept", "X6"), to = "X4", arrows = 1, values = 0, free = c(T,T), labels = c("i678", "b6")),

mxPath(from = c("intercept", "X7"), to = "X4", arrows = 1, values = 0, free = c(T,T), labels = c("i678", "b7")),

mxPath(from = c("intercept", "X8"), to = "X4", arrows = 1, values = 0, free = c(T,T), labels = c("i678", "b8")),

# manifest means

mxPath(

from="one",

to=c("X1","X2","X3","X4","X6","X7","X8"),

arrows=1,

free=FALSE,

values=c(0, 0, 0, 0, 0, 0, 0)

),

# latent means

mxPath(

from="one",

to=c("intercept"),

arrows=1,

free=TRUE,

values=c(0),

labels=c("meani")

),

#Means and covaraince matrix

mxRAMObjective(A="A", S="S", M="M", F="F", thresholds = "Thresholds")

) # close model

`run <- mxRun(growthCurveModel, intervals=F)`

You will want to adjust starting values, though.

Edit: In particular, note the extra parentheses in

`mxAlgebra(diag2vec(fmat%&%(F%&%(solve(I-A)%&%S))), name="expVars")`

. One needs to be careful about order-of-operations when using the quadratic product operator,`%&%`

.Log in or register to post comments

In reply to Try this by AdminRobK

## Thank you so much!

I really appreciate both of you spending time for this problem. Hope you can get rewards and good lucks in your work!

Log in or register to post comments