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

433 views
Skip to first unread message

Giuseppe Mignemi

unread,
Mar 26, 2021, 8:30:00 AM3/26/21
to lavaan
Hi There, 

I am working on a moderated mediation with SEM, I'm using the lavaan package.
The model works, but i'm having problem with probe2WayRC of the semTools package. 

The error message is the following: "Error in varEst[targetcol, targetcol] : subscript out of bounds".

The model is this: 

Model_sem<-" F1=~x1+x2+x3+x4
                     F2=~w1+w2
                     Med=~m1+m2+m3+m4
                     Y=~y1
                     Interaction=~x1.w1+x1. w2  +x2.w1+x2. w2  +x3.w1+x3. w2+M_x4.w1+x4.w2

                    
                     F1~NA*1
                     F2~NA*1
                      Med  ~NA*1
                     Interaction~NA*1
                     
                     
                     x1~~x1.w1
                     x1~~x1.w2
                     

                     x3~~x3.w1
                     x3~~x3.w2
                     
                     x4y~~x4.w1
                     x4~~x4.w2
                     
                     x2~~x2.w1
                     x2~~x2.w2
                     
                     F2~~0*Interaction
                     F1~~0*Interaction
                     
                     
                     Med  ~a1*F1+a2*Interaction+a3*F2
                      Y  ~c1*F1+b1*Med
                     
                     a1b1:=a1*b1
                     a3b1:=a3*b1
                     
                    Total_effec_F1=c1+a1*b1"

After getting the fit of the model above:
Fit_Model_sem<-sem( Model_sem  ,mydata,estimator="DWLS", se= "bootstrap", bootstrap=1000)

I run the following function: 
probe2WayRC(Fit_ Model_sem ,nameX= c("F1","F2","Interaction"), nameY= "Med", modVar = "F1",valProbe = c(-1, 0, 1))

But this message appears: 

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


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

Thank you all!
Giuseppe

Alex Schoemann

unread,
Mar 26, 2021, 9:02:09 AM3/26/21
to lavaan
Hi Guiseppe,

How did you form your product indicators? probe2WayRC is designed for residual centered (orthogonalized) product indicators. If you used another method it won't function correctly, that's probably the reason for the error. If you used mean centering for product indicators (or double mean centering) then the probe2WayMC function is a better choice. In either case you shouldn't need to include residual covariances between the original items and the product terms in your model.

Alex

Giuseppe Mignemi

unread,
Mar 26, 2021, 9:12:54 AM3/26/21
to lavaan
Thanks Alex!

I used this method for product indicattors: 

indProd(df, var1 = 1:4, var2 = 5:6,match = FALSE, meanC = TRUE, doubleMC = TRUE) 

At any rate, I' ve already used probe2WayMC and the same error message appears: " Error in varEst[targetcol, targetcol] : subscript out of bounds ". 

Thank you so much for the prompt answer!

Best regards, 
Giuseppe

Alex Schoemann

unread,
Mar 28, 2021, 3:20:00 PM3/28/21
to lavaan
You've found a bug! The current version of the probe* functions will give that error when the paths from the predictors to the outcome use a label. We'll work on fixing it, for now if you want simple slopes you can remove the labels from your code and you should be able to estimate simple slopes.

Alex

Giuseppe Mignemi

unread,
Mar 28, 2021, 4:26:24 PM3/28/21
to lavaan
Removing the labels it actually works. 

Thank you so much Alex!

Giuseppe

Terrence Jorgensen

unread,
Mar 30, 2021, 8:22:13 PM3/30/21
to lavaan
Resolved:

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

Harmanjit Singh

unread,
Jun 18, 2021, 1:54:07 AM6/18/21
to lavaan
Hi Terrence,

This bug seems to exist in the currrent development version of semTools (ì.e. version 0.5-4.915).

While running probe2WayMC (); it is showing  " Error in varEst[targetcol, targetcol] : subscript out of bounds ".  I didn't use any labels either within the model.

However, when I uninstalled the development version and installed the regular version of semTools (version 0.5-4), probe2WayMC () ran smoothly without any errors. 

Could you please fix this in the development version?

Harmanjit Singh

unread,
Jun 20, 2021, 11:52:48 PM6/20/21
to lavaan
Gentle reminder.

Terrence Jorgensen

unread,
Jun 21, 2021, 10:55:57 PM6/21/21
to lavaan
Could you please fix this in the development version?

Not unless you can post a reproducible example with data, so I can track it down (probably sometime in late July).

Harmanjit Singh

unread,
Jun 24, 2021, 6:35:38 AM6/24/21
to lav...@googlegroups.com
That will be difficult for me.

However, here is the sample code which ran successfully in semTools (version 0.5-4) but displayed error (and failed to run) in  semTools (version 0.5-4.915):

library(semTools)
ModerationData <- indProd(Mydata, var1 = c('Item1','Item2','Item3','Item4'),
                          var2 = c('Item5','Item6','Item7','Item8'),
                          match = FALSE, meanC = TRUE, residualC = FALSE, doubleMC = TRUE)

ModerationModel <-
  '
# Independent variable
Indv  =~ Item1 + Item2 + Item3 + Item4 

# Moderting variable
Modv =~ Item5 + Item6 + Item7 + Item8

# Dependent variables
DV1 =~ Item9 + Item10
DV2 =~ Item11 + Item12 + Item13

# Interaction term
IndvXModv =~ Item1.Item5 + Item1.Item6 + Item1.Item7 + Item1.Item8 + Item2.Item5 + Item2.Item6 + Item2.Item7 + Item2.Item8 + Item3.Item5 + Item3.Item6 + Item3.Item7 + Item3.Item8 + Item4.Item5 + Item4.Item6 + Item4.Item7 + Item4.Item8

# Regressions
DV1 ~ Indv + Modv + IndvXModv
DV2 ~ Indv + Modv + IndvXModv  
 '
fit <- sem(ModerationModel, estimator="MLM", data = ModerationData, std.lv = T, meanstructure = T)
fit

#Check means of latent variables
lavInspect(fit, "mean.lv")

#Moderation Slope
result2WayMC_DV1 <- probe2WayMC(fit, nameX = c("Indv","Modv","IndvXModv"), nameY = "DV1", modVar = "Modv", valProbe = c(-1,0,1))
result2WayMC_DV2 <- probe2WayMC(fit, nameX = c("Indv","Modv","IndvXModv"), nameY = "DV2", modVar = "Modv", valProbe = c(-1,0,1))
result2WayMC_DV1
result2WayMC_DV2
plotProbe(result2WayMC_DV1, xlim = c(-2,2), xlab = "Indv", ylab = "DV1", legend = TRUE, legendArgs = list())
plotProbe(result2WayMC_DV2, xlim = c(-2,2), xlab = "Indv", ylab = "DV2", legend = TRUE, legendArgs = list())

--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/Vs7d59p-Czc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/4bf37c85-0a49-4e2a-b464-f81f632295f8n%40googlegroups.com.

Terrence Jorgensen

unread,
Jul 8, 2021, 4:01:43 AM7/8/21
to lavaan
You could use your script to generate example data by providing population parameters.  There is no way to include an interaction effect that way, but you can get data on the main-effect variables so that an example can be run.

pop <- ' # Independent variable
Indv  =~ .7*Item1 + .7*Item2 + .7*Item3 + .7*Item4 
# Moderting variable
Modv =~ .7*Item5 + .7*Item6 + .7*Item7 + .7*Item8

Indv ~~ 1*Indv + .3*Modv
Modv ~~ 1*Modv

# Dependent variables
DV1 =~ .7*Item9 + .7*Item10
DV2 =~ .7*Item11 +.7*Item12 + .7*Item13

# Regressions
DV1 ~ .5*Indv + .5*Modv
DV2 ~ .5*Indv + .5*Modv
DV1 ~~ .35*DV1 + .1*DV2
DV2 ~~ .35*DV2

Item1  ~~ .51*Item1 
Item2  ~~ .51*Item2 
Item3  ~~ .51*Item3 
Item4  ~~ .51*Item4 
Item5  ~~ .51*Item5 
Item6  ~~ .51*Item6 
Item7  ~~ .51*Item7 
Item8  ~~ .51*Item8 
Item9  ~~ .51*Item9 
Item10 ~~ .51*Item10
Item11 ~~ .51*Item11
Item12 ~~ .51*Item12
Item13 ~~ .51*Item13
'
fitted(sem(pop))
Mydata <- simulateData(pop, sample.nobs = 500, seed = 123)

When I did this, I ran your script without a problem, using semTools 0.5-4.915 and lavaan 0.6-9

Harmanjit Singh

unread,
Jul 9, 2021, 4:08:23 AM7/9/21
to lavaan
Thanks Terrence.

There were no issues with running main effects or mediation effects. The error occurred only during moderation analysis. 

Terrence Jorgensen

unread,
Jul 10, 2021, 7:40:34 AM7/10/21
to lavaan
Okay, I found the problem.  When there weren't user-supplied labels, I was making labels with paste() instead of paste0().  fixed in the development version.

devtools::install_github("simsem/semTools/semTools")

Harmanjit Singh

unread,
Jul 17, 2021, 8:38:37 AM7/17/21
to lavaan
Hi Terrence, 

Thanks for fixing this. I ran the entire model by reinstalling semTools development version. 
This time moderation analysis ran smoothly.
Thanks a lot! :)
Reply all
Reply to author
Forward
0 new messages