# Effects coding method in CFA

$$ \frac{\lambda_1 + \lambda_2 + \lambda_3 + \lambda_4}{4} = 1 $$

Below is a code example. I tried adding an `mxConstraint()` function in `mxModel()` but the syntax is not correct. How can I apply the constraints properly?

library(OpenMx)

data <- mxData(observed = mydata, type="raw")

# residual variances

res_var <- mxPath(

from = paste0("x", 1:4),

arrows = 2,

free = TRUE,

values = 1,

labels = paste0("e_x", 1:4)

)

# latent variance

lat_var <- mxPath(

from = "L",

arrows = 2,

free = TRUE,

values = 1,

labels = c("var_L")

)

# factor loadings

fac_loads <- mxPath(

from = "L",

to = paste0("x", 1:4),

free = c(TRUE, TRUE, TRUE, TRUE),

values = 1,

arrows = 1,

label = paste0("L__x", 1:4)

)

# means

means <- mxPath(

from = "one",

to = c(L, paste0("x", 1:4),

arrows = 1,

free = FALSE,

values = 0,

labels = c(paste0("mean_", student_sci_ach), "mean_stSCI")

)

model <- mxModel(

"My Model",

type = "RAM",

manifestVars = paste0("x", 1:4),

latentVars = "L",

data,

var, lat_var,

fac_loads,

means,

# I tried defining a constraint here, but this obviously doesn't work.

mxConstraint(mean(paste0("L__x", 1:4)) == 1)

)

results <- mxRun(model)

summary(results)

<\blockcode>

## try something like this

`=`

in this case) have to be valid MxAlgebra-like expressions, evaluable in the MxModel's namespace; mere character strings aren't accepted. Try something like this:model <- mxModel(

"My Model",

type = "RAM",

manifestVars = paste0("x", 1:4),

latentVars = "L",

data,

var, lat_var,

fac_loads,

means,

mxMatrix(type="Full",nrow=4,ncol=1,free=T,labels=paste0("L__x", 1:4),name="Lmat"),

mxConstraint(mean(Lmat) == 1)

)

Log in or register to post comments

In reply to try something like this by AdminRobK

## Thanks, that did the trick!

Log in or register to post comments

In reply to Thanks, that did the trick! by pehkawn

## Yep

mxMatrix(type="Full",nrow=4,ncol=1,free=T, values=1, labels=paste0("L__x", 1:4),name="Lmat"),

Use of omxAssignFirstParameters() means that you don't actually know which starting value was used. Sometimes that's ok, but, (e.g., to debug bad starting values) it's not always ideal.

Log in or register to post comments