semTools probe2WayRC

397 views
Skip to first unread message

Stephan Volpers

unread,
Nov 4, 2015, 10:32:50 AM11/4/15
to lavaan
Hallo,

I am facing the following error estimating simple slopes analysis in R using the semTools package an lavaan fitted model:

Error in varEst[targetcol, targetcol] : subscript out of bounds

The printed value of targetcol is:

[1] "per~mlc"          "per~hyb"    "per~m_mlc_hyb"


It seems to be a problem with loop funktion in varEst (http://stackoverflow.com/questions/15031338/subscript-out-of-bounds-general-definition-and-solution), but I do not know how and where to adress the issue. Overall, the model itself fits well and there seems to be no problem with that. May the problem arise because hyb is no latent variable but an exogenous one?

I would be really appy for any suggestions to solve the challenge!

Thank you!
Stephan

Terrence Jorgensen

unread,
Nov 5, 2015, 5:06:33 AM11/5/15
to lavaan
May the problem arise because hyb is no latent variable but an exogenous one?

I thought that might be the problem, but after toying with the example on the help page, I couldn't reproduce your problem:

dat2wayRC <- orthogonalize(dat2way, 1:3, 4, match = FALSE)
head(dat2way)
head(dat2wayRC)
 
## Try it as a single-indicator latent variable
model1 <- '
## measurement model
f1 =~ x1 + x2 + x3
f2 =~ x4
f12 =~ x1.x4 + x2.x4 + x3.x4
f3 =~ x7 + x8 + x9
## structural model (latent interaction is orthogonal to factors)
f3 ~ f1 + f2 + f12
f12 ~~ 0*f1
f12 ~~ 0*f2
## fix intercepts to identify latent locations
x1 ~ 0*1
x4 ~ 0*1
x1.x4 ~ 0*1
x7 ~ 0*1
## estimate latent means
f1 ~ NA*1
f2 ~ NA*1
f12 ~ NA*1
f3 ~ NA*1
'
fitRC2way <- sem(model1, data = dat2wayRC)
probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1))
 
## Try it as a fixed exogenous predictor
model2 <- '
## measurement model
f1 =~ x1 + x2 + x3
f12 =~ x1.x4 + x2.x4 + x3.x4
f3 =~ x7 + x8 + x9
## structural model (latent interaction is orthogonal to factor)
f3 ~ f1 + x4 + f12
f12 ~~ 0*f1
## fix intercepts to identify latent locations
x1 ~ 0*1
x1.x4 ~ 0*1
x7 ~ 0*1
## estimate latent means
f1 ~ NA*1
f12 ~ NA*1
f3 ~ NA*1
'
fitRCex <- sem(model2, data = dat2wayRC, fixed.x=TRUE)
probe2WayRC(fitRCex, c("f1", "x4", "f12"), "f3", "x4", c(-1, 0, 1))

Notice that the results are slightly different between the single-indicator latent variable method and the exogenous method, probably because the covariance between the latent "main effects" factors is estimated in the first approach but not the latter.  My guess is that you should create a latent variable from your exogenous predictor anyway, for this reason.  But if you still get an error, you might need to provide enough data and your full script for anyone to be able to figure out what the problem is.

Terry

Stephan Volpers

unread,
Nov 5, 2015, 8:38:58 AM11/5/15
to lavaan
Hi Terry,

thank you for your help. You are right, if i take your modified example syntax and my data it works. The problem occurs when I add another latent variable.

For Example for problem (1):
## Try it as a single-indicator latent variable
model1 <- '
## measurement model
f1 =~ x1 + x2 + x3
f2 =~ x4
f12 =~ x1.x4 + x2.x4 + x3.x4
f3 =~ x7 + x8 + x9

f4
=~ x5+x6+x7
## structural model (latent interaction is orthogonal to factors)
f3 ~ f1 + f2 + f12 + f4
f12 ~~ 0*f1
f12 ~~ 0*f2
## fix intercepts to identify latent locations
x1 ~ 0*1
x4 ~ 0*1
x1.x4 ~ 0*1
x7 ~ 0*1
x5
~ 0*1
## estimate latent means
f1 ~ NA*1
f2 ~ NA*1
f12 ~ NA*1
f3 ~ NA*1
f4 ~ NA*1
'
fitRC2way <- sem(model1, data = dat2wayRC)
probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1))

I tested the model with three different latent vars of my data individually.

Furthermore, I added another interaction term to the example model. Then the following Warning appears:

Warning messages:
1: In sqrt(varIntcept) : NaNs produced
2: In sqrt(varIntcept) : NaNs produced


Terrence Jorgensen

unread,
Nov 6, 2015, 3:30:30 AM11/6/15
to lavaan
The problem occurs when I add another latent variable.

Could you be more specific?  Adding another latent variable (f4) in the syntax you posted, which only had a main effect (no interactions) did not yield an error for me, although the results differed (probably because x7 now has cross-loadings on f3 and f4, changing the nature of the factors).
 
Furthermore, I added another interaction term to the example model. Then the following Warning appears:

Warning messages:
1: In sqrt(varIntcept) : NaNs produced
2: In sqrt(varIntcept) : NaNs produced

Perhaps if you are probing a moderated effect of a variable that interacts with 2 moderators, you would need to choose levels for both moderators, and the function might not yet be equipped to handle that.  Please post the R syntax necessary to reproduce the problem, so we can figure it out.

Terry

Stephan Volpers

unread,
Nov 6, 2015, 6:15:07 AM11/6/15
to lavaan


Am Freitag, 6. November 2015 09:30:30 UTC+1 schrieb Terrence Jorgensen:
The problem occurs when I add another latent variable.

Could you be more specific?  Adding another latent variable (f4) in the syntax you posted, which only had a main effect (no interactions) did not yield an error for me, although the results differed (probably because x7 now has cross-loadings on f3 and f4, changing the nature of the factors).

Yes, you are right. I need the additional main effects for theoretical and cross-loding reasons). The model fits, but i could replicate the error occurs as soon as I add any main effect to the example model. At least this happened when I used my dataset.
 
 
Furthermore, I added another interaction term to the example model. Then the following Warning appears:

Warning messages:
1: In sqrt(varIntcept) : NaNs produced
2: In sqrt(varIntcept) : NaNs produced

Perhaps if you are probing a moderated effect of a variable that interacts with 2 moderators, you would need to choose levels for both moderators, and the function might not yet be equipped to handle that.  Please post the R syntax necessary to reproduce the problem, so we can figure it out.


Here you find my syntax:

dat<-indProd(dat, var1=c("pbkf1", "pbkf2"), var2=c("hybr"), match=FALSE, meanC=TRUE, residualC=TRUE, doubleMC=FALSE)
dat
<-indProd(dat, var1=c("pmlc1", "pmlc2"), var2=c("hybr"), match=FALSE, meanC=TRUE, residualC=TRUE, doubleMC=FALSE)
dat
<-indProd(dat, var1=c("pckf1", "pckf2"), var2=c("hybr"), match=FALSE, meanC=TRUE, residualC=TRUE, doubleMC=FALSE)
#mean centring before orthogonalization to improve model fit, problem does still occur

modell
<- '
#Structural Model
perf ~ mlc + bkf + ckf + erf_ber + hyb + a*m_mlc_hyb + c*m_ckf_hyb + b*m_bkf_hyb + d*adpt + e*per + g*vbs

mlc ~ k*adpt + l*per + m*vbs + erf_ber
bkf ~ n*adpt + o*per + p*vbs + erf_ber
ckf ~ q*adpt + r*per + s*vbs + erf_ber
erf_ber ~ hyb

#i=indirect effect & t = total effects
i_mlchyb_adpt := a*k
i_mlchyb_per := a*l
i_mlchyb_vbs := a*m

i_bkfhyb_adpt := b*k
i_bkfhyb_per := b*l
i_bkfhyb_vbs := b*m

i_ckfhyb_adpt := c*k
i_ckfhyb_per := c*l
i_ckfhyb_vbs := c*m

#Measurement Model
perf =~ p4 + p1 + p3
mlc =~ pmlc1 + pmlc2
bkf =~ pbkf2 + pbkf1
ckf =~ pckf2 + pckf1
adpt =~ as4+as1+as2+as3
vbs =~ vbs1+vbs3+vbs5+vbs4+vbs7
per =~ per4+per2+per5
hyb =~ hybr
m_bkf_hyb =~ pbkf2.hybr+pbkf1.hybr
m_mlc_hyb =~ pmlc1.hybr+pmlc2.hybr
m_ckf_hyb =~ pckf2.hybr+pckf1.hybr

#Correlations
ckf ~~ hyb
bkf ~~ hyb
mlc ~~ hyb
mlc ~~ 0*m_mlc_hyb
ckf ~~ 0*m_ckf_hyb
bkf ~~ 0*m_bkf_hyb
hyb ~~ 0*m_mlc_hyb
hyb ~~ 0*m_ckf_hyb
hyb ~~ 0*m_bkf_hyb


## fix intercepts to identify latent locations
p4 ~ 0*1
pmlc1 ~ 0*1
pbkf2 ~ 0*1
pckf2 ~ 0*1
as4 ~ 0*1
vbs1 ~ 0*1
per4 ~ 0*1
hybr ~ 0*1
pbkf2.hybr ~ 0*1
pmlc1.hybr ~ 0*1
pckf2.hybr ~ 0*1

## estimate latent means
perf ~ NA*1
mlc ~ NA*1
bkf ~ NA*1
ckf ~ NA*1
adpt ~ NA*1
vbs ~ NA*1
per ~ NA*1
hyb ~ NA*1
m_bkf_hyb ~ NA*1
m_mlc_hyb ~ NA*1
m_ckf_hyb ~ NA*1
'


fit
<- sem(model=sor.modell, data=sor.dat2, missing = "fiml", se = "robust")
probe2WayRC
(fit, c("mlc", "hybr", "m_mlc_hyb"), "perf", "hybr", c(-1,0,1))

Thank you!

Stephan
data.csv

Alex Schoemann

unread,
Nov 6, 2015, 10:11:00 AM11/6/15
to lavaan
Hi Stephen,

Your code helped clear things up. The issue is the use of labels for regression paths involved in the moderation portion of the model. This has to do with how probe2wayRC picks up on the relationships of interest. Here's an example (based on Terry's code above):

This works:

model1 <- '
## measurement model
f1 =~ x1 + x2 + x3
f2 =~ x4
f12 =~ x1.x4 + x2.x4 + x3.x4
f3 =~ x7 + x8 + x9
## structural model (latent interaction is orthogonal to factors)
f3 ~ f1 + f2 + f12
f12 ~~ 0*f1
f12 ~~ 0*f2
## fix intercepts to identify latent locations
x1 ~ 0*1
x4 ~ 0*1
x1.x4 ~ 0*1
x7 ~ 0*1
## estimate latent means
f1 ~ NA*1
f2 ~ NA*1
f12 ~ NA*1
f3 ~ NA*1

'
fitRC2way <- sem(model1, data = dat2wayRC)
probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1))

But this (adding labels) causes an error:

model1 <- '
## measurement model
f1 =~ x1 + x2 + x3
f2 =~ x4
f12 =~ x1.x4 + x2.x4 + x3.x4
f3 =~ x7 + x8 + x9
## structural model (latent interaction is orthogonal to factors)
f3 ~ a*f1 + b*f2 + f12
f12 ~~ 0*f1
f12 ~~ 0*f2
## fix intercepts to identify latent locations
x1 ~ 0*1
x4 ~ 0*1
x1.x4 ~ 0*1
x7 ~ 0*1
## estimate latent means
f1 ~ NA*1
f2 ~ NA*1
f12 ~ NA*1
f3 ~ NA*1
'
fitRC2way <- sem(model1, data = dat2wayRC)
probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1))

Hope this helps,

Alex

Stephan Volpers

unread,
Nov 6, 2015, 10:40:31 AM11/6/15
to lavaan
Alex, Terry,

thank you so much. This was the problem I was not able to come up with.

Stephan
Message has been deleted

şule selçuk

unread,
Dec 22, 2018, 8:51:25 AM12/22/18
to lavaan
Hi, 

I would like to ask that  what do values of -1, 0, and 1 (i.e., c(-1, 0, 1)) stand for? Do they represent 1 sd below and the mean, mean of latent moderating factor?  


result2wayRC <- probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1)). 

result2wayRC


Şule

Terrence Jorgensen

unread,
Jan 3, 2019, 2:36:08 PM1/3/19
to lavaan
what do values of -1, 0, and 1 (i.e., c(-1, 0, 1)) stand for? Do they represent 1 sd below and the mean, mean of latent moderating factor?  

result2wayRC <- probe2WayRC(fitRC2way, c("f1", "f2", "f12"), "f3", "f2", c(-1, 0, 1)). 

Yes, if your moderating factor's mean = zero and variances = 1.  

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Doris

unread,
Aug 8, 2019, 8:35:15 AM8/8/19
to lavaan
Hi all,

do I get it right that as the probe2wayRC function cannot deal with labels for regression paths, I am not able to estimate a moderated mediation, where I need the labels for the indirect effect? If not, is there any other possibility to estimate a moderated mediation using lavaan?

Thank you for clarification.
Doris

Terrence Jorgensen

unread,
Aug 8, 2019, 9:17:10 AM8/8/19
to lavaan
do I get it right

No, the inability of probe2wayRC() to work with moderated mediation has no bearing on lavaan's ability to estimate such models, and for you to define simple indirect effects in the syntax to probe the interaction.  You can find examples with observed variables in this forum, which generalize to working with latent variables using product indicators.
Reply all
Reply to author
Forward
0 new messages