moderation analysis - error

254 views
Skip to first unread message

Su Lee

unread,
Aug 17, 2020, 5:06:56 PM8/17/20
to lavaan

Hello all,

I am new to R.- I have been trying to do moderation analysis using structural equation modeling. 

I have been reading and watching to learn more about it; but I got stuck here for days ( the error was highlighted in pink)…. If someone can kindly help me through this, it will be so great.


I copied and pasted my entire codes to help you understand better. 





library(lavaan)

x<-c("gender","AGE", “a” , “b”, “c” , “PS” , “ d”, “e”, “f”, “g”)

#multiple latent variables

SUMMERMODEL<-' risk =~a+b+c                                   

                                  MH =~d+e                                                  

                                  PH =~f+g '

fit2<-cfa(SUMMERMODEL, data = SUMMER_final, missing= "FIML")

summary(fit2, standardized=TRUE, fit.measures=TRUE)


#structural model

SUMMERMODEL<-'#Latent variable definitions

SUMMERMODEL<-' risk =~a+b+c                                   

                                  MH =~d+e                                                  

                                  PH =~f+g

  #regressions

  MH~ risk+gender +age

  PH ~risk +gender+age

 

#residual correlations

MH ~~ PH'


fit<-sem(SUMMERMODEL, data=SUMMER_final, missing= "FIML")

summary(fit,standardized=TRUE)


# moderation analysis

semdata<-data.frame(predict(fit))

-centering (IV and moderator)

riskc<-scale(semdata$risk,center=TRUE,scale=FALSE)

PSc<-scale(SUMMER_final$PS,center = TRUE,scale = FALSE)

xz<-riskc%*%c(PSc)


#REGRESSIONS

regression1<-lm(MH~ riskc + PSc+xz+gender+age, data=SUMMER_final, missing= “FIML”)   

summary(regression1) : When I run this part (pink highlight), I got this error - Error in h(simpleerror(msg,call)) : error in evaluating the argument ‘object’ in selecting a method for function ‘ summary: object ‘regression1’ not found. I have been searching for how to fix this error, I have not been successful . 

regression2=lm(PH~riskc+PSc+xz+gender+age, data=SUMMER_final, missing= “FIML”)

summary(regression2)


I am glad to find this group !!! Thank you very much !!!

Terrence Jorgensen

unread,
Aug 17, 2020, 5:12:50 PM8/17/20
to lavaan

riskc<-scale(semdata$risk,center=TRUE,scale=FALSE)

PSc<-scale(SUMMER_final$PS,center = TRUE,scale = FALSE)


You are storing the data in new objects, rather than as new columns in SUMMER, which is the data.frame you are telling lm() to look in. 

xz<-riskc%*%c(PSc)


These are univariate, don't use matrix multiplication, just the regular multiplication operator: riskc * PSc 
 

regression1<-lm(MH~ riskc + PSc+xz+gender+age, data=SUMMER_final, missing= “FIML”)   


lm() does not have a missing="FIML" option; that is a lavaan option.
 
Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Su Lee

unread,
Aug 17, 2020, 6:20:34 PM8/17/20
to lavaan

Hello Dr. Jorgensen

Thank you for getting back to me.
I think I don't quite understand what to do with centering.

Initially, I was trying to do 

riskc<-scale(SUMMER_final$risk,center=TRUE,scale=FALSE)
This did not work - I was wondering if risk is a latent variable.
So I added semdata<-data.frame(predict(fit)) before I did centering.
and I used the following code.

riskc<-scale(semdata$risk,center=TRUE,scale=FALSE).

This worked.

speaking of xz,
xz<- riskc * PSc does not work.... how can I fix this?  I am so sorry - I have been searching but I could not quite get how to fix these parts.

Fawad Latif

unread,
Aug 18, 2020, 8:07:42 AM8/18/20
to lav...@googlegroups.com
Just a question with respect to moderation here. Cant we use IndProd for moderation in Lavaan.

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/4f9ed443-5808-4794-be9d-07ca9eb143e5o%40googlegroups.com.

Terrence Jorgensen

unread,
Aug 19, 2020, 4:33:19 AM8/19/20
to lavaan
riskc<-scale(semdata$risk,center=TRUE,scale=FALSE).

My point is that for lm() to find it in data=SUMMER_final, you should put it in there:

SUMMER_final$riskc <- scale(semdata$risk,center=TRUE,scale=FALSE)[ , 1]


speaking of xz,
xz<- riskc * PSc does not work.... how can I fix this? 

The problem is that scale() always returns a matrix, so in the syntax above, I added the brackets to extract the first/only column, making it a vector.  Do the same for PSc, and elementwise multiplication should work fine.  Alternatively, you can just subtract the mean

SUMMER_final$riskc <- semdata$risk - mean(semdata$risk, na.rm=TRUE)

FYI, if your latent means were already fixed to zero, then the factor score estimates should have means that are effectively zero.  This might all be unnecessary extra work.


Can't we use semTools::indProd() for moderation in lavaan

Yes, to model a latent interaction directly via product indicators.  See the references on that help page for details.  Fawad's suggestion is probably preferable to using factor scores for numerous reasons:
  • Treating factor score estimates as known values in a subsequent analysis will give you deflated SEs and inflated Type I error rates.
  • Since the model for the factors did not include nonlinear effects, it will not preserve them in the factor-score estimates.  So if there is an interaction, your power will be deflated.

Su Lee

unread,
Aug 19, 2020, 1:07:24 PM8/19/20
to lavaan


Hello Dr. Jorgensen,

Thank you so much for your guidance. It is so helpful. 
When I tried to run SUMMER_final$riskc <- scale(semdata$risk,center=TRUE,scale=FALSE)[ , 1]

I got the following error. Summer_final has 295 observations and semdata has 287 observations.

Error: Assigned data '...[]'  must be compatible with existing data.
x Existing data has 295 rows.
x assigned data has 287 rows.
i only vectors of size 1 are recycled.


Could you help me figure out this error ?

Patrick (Malone Quantitative)

unread,
Aug 19, 2020, 1:36:09 PM8/19/20
to lav...@googlegroups.com
Su,

It's telling you what the problem is: SUMMER_final has a different number of rows than  semdata.

Is there any particular reason you're putting the centered version in a new dataframe, instead of a new variable in semdata?

Pat

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.


--
Patrick S. Malone, Ph.D., Malone Quantitative
NEW Service Models: http://malonequantitative.com

He/Him/His

Su Lee

unread,
Aug 19, 2020, 2:04:59 PM8/19/20
to lavaan
Hello Pat,

If I don't save the variable in Summer_final, xz<- riskc * PSc does not work... (PSc is in summer_final ).
Does this make sense ?

Su



On Monday, August 17, 2020 at 2:06:56 PM UTC-7, Su Lee wrote:

Patrick (Malone Quantitative)

unread,
Aug 19, 2020, 2:15:50 PM8/19/20
to lav...@googlegroups.com
Does risk exist in Summer_final? If so, try using that in scale().

Or center it in semdata and then merge it by an ID vector. The fact that you have cases in Summer_final that are not in semdata is the problem. It's not a lavaan issue.

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.

Su Lee

unread,
Aug 19, 2020, 2:20:45 PM8/19/20
to lav...@googlegroups.com
no , risk is not in Summer_final. It is a latent variable . 

I also tried to do center it in semdata and then merge it by an ID vector. It did not work .....


Patrick (Malone Quantitative)

unread,
Aug 19, 2020, 5:55:59 PM8/19/20
to lav...@googlegroups.com
Got it. I suspect you lost some cases for missing exogenous variables in the sem() step. Did you get a warning to that effect? If so, see if you can locate and remove those same cases from Summer_final.

Su Lee

unread,
Aug 19, 2020, 6:05:28 PM8/19/20
to lav...@googlegroups.com
I saw the message - 8 cases were deleted due to missing values
                                  in exogenous variable(s), while fixed. x=TRUE.- 
 what exactly should I do ? I am so new and your guidance is really helpful .

Su


Patrick (Malone Quantitative)

unread,
Aug 19, 2020, 6:09:01 PM8/19/20
to lav...@googlegroups.com
Basically you need to omit cases where either gender or age is missing from Summer_final (suggesting you put in a new df so you're not trimming your original). The syntax for that is pretty easy to find on a websearch.

Su Lee

unread,
Aug 19, 2020, 6:32:11 PM8/19/20
to lav...@googlegroups.com
Can you please tell me more about this ? where should I put this new code ? 

Terrence Jorgensen

unread,
Aug 20, 2020, 2:25:31 AM8/20/20
to lavaan
I saw the message - 8 cases were deleted due to missing values
                                  in exogenous variable(s), while fixed. x=TRUE.- 
 what exactly should I do ? 

If you set missing = "fiml.x", they will be retained.

Alternatively, if they remain listwise deleted, then look at the ?lavPredict help page for an example of how to merge your factor scores into the original data, even when the rows mismatch.

Su Lee

unread,
Aug 20, 2020, 2:35:59 AM8/20/20
to lav...@googlegroups.com
Thank you so much !!

semdata<-data.frame(predict(fit), missing = "fiml.x", ) like this ??



--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.

Su Lee

unread,
Aug 20, 2020, 2:55:46 AM8/20/20
to lav...@googlegroups.com
Thank you and sorry !! - I figured out that missing = "fiml.x" is not working.
As you advised, I dug into ?lavPredict.
I could use the following codes.
semdata<-data.frame(predict(fit), type= "ov")
riskc<-scale(semdata$risk,center=TRUE, scale=FALSE)
PSc<-scale(SUMMER_final$PS,center = TRUE,scale = FALSE)
Everything  worked .
xz<-riskc*PSc is not working though..... I got an error in riskc*PSc : non-conformable arrays.

I am so sorry for many questions but I ran into walls. 






On Wed, Aug 19, 2020 at 11:25 PM Terrence Jorgensen <tjorge...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.

Su Lee

unread,
Aug 20, 2020, 3:13:44 AM8/20/20
to lav...@googlegroups.com
so how can I fix this error ??

Su Lee

unread,
Aug 20, 2020, 3:49:45 PM8/20/20
to lav...@googlegroups.com
Hi Dr. Jorgensen
I figured out how to fix my errors related to centering- thank you so much for your help !!!!!!!!
I used the following codes- see the parts highlighted in pink ( I just wanted to share these just in case someone experienced the same issues as me ). I got another error related to regressions at the very end. could you please look at it ?


library(lavaan)

x<-c("gender","AGE", “a” , “b”, “c” , “PS” , “ d”, “e”, “f”, “g”)

#multiple latent variables

SUMMERMODEL<-' risk =~a+b+c                                   

                                  MH =~d+e                                                  

                                  PH =~f+g '

fit2<-cfa(SUMMERMODEL, data = SUMMER_final, missing= "FIML")

summary(fit2, standardized=TRUE, fit.measures=TRUE)

 

#structural model

SUMMERMODEL<-'#Latent variable definitions

SUMMERMODEL<-' risk=~a+b+c                                   

                                  MH=~d+e                                                  

                                  PH=~f+g

  #regressions

  MH~ risk+gender +age

  PH ~risk +gender+age

 

#residual correlations

MH ~~ PH'

 

fit<-sem(SUMMERMODEL, data=SUMMER_final, missing= "FIML.X")

summary(fit,standardized=TRUE)

 

# moderation analysis

semdata<-data.frame(predict(fit))

-centering (IV and moderator)

SUMMER_final$riskc<-scale(semdata$risk,center=TRUE,scale=FALSE)

SUMMER_final$PSc<-scale(SUMMER_final$PS,center= TRUE,scale = FALSE)

SUMMER_final$xz<- SUMMER_final$riskc*SUMMER_final$PSc

 

#REGRESSIONS

regression1<-lm(MH~SUMMER_final$riskc + SUMMER_final$PSc +xz+gender+age, data=SUMMER_final)

summary(regression1) 

: The error says that object -regression 1- not found

regression2=lm(PH~ SUMMER_final$riskc + SUMMER_final$PSc +xz+gender+age, data=SUMMER_final)

summary(regression2)


Would you please give me some guidance to navigate this error ?







Patrick (Malone Quantitative)

unread,
Aug 20, 2020, 4:38:54 PM8/20/20
to lav...@googlegroups.com
What messages follow the lm() call? It sounds like it's failing.

Su Lee

unread,
Aug 20, 2020, 4:41:56 PM8/20/20
to lav...@googlegroups.com
Hi Patrick, 
The error says that object -regression 1- not found


Patrick (Malone Quantitative)

unread,
Aug 20, 2020, 5:58:48 PM8/20/20
to lav...@googlegroups.com
That's the error you said you got when you ran summary(). I'm asking what messages you got before that, when you ran lm().

Su Lee

unread,
Aug 20, 2020, 6:18:27 PM8/20/20
to lav...@googlegroups.com
Oh I see. before that I did not get any messages. 





Terrence Jorgensen

unread,
Aug 21, 2020, 8:53:23 AM8/21/20
to lavaan
semdata<-data.frame(predict(fit), missing = "fiml.x", ) like this ??

No, it is a lavaan() option, not a data.frame() argument.  You specify it when you fit the model (assuming you use a ML estimator).

?lavOptions

Su Lee

unread,
Aug 21, 2020, 11:17:18 AM8/21/20
to lav...@googlegroups.com
Thank you , Dr. Terrence !! I figured it out . Now I got this error . How can I fix this problem ? I have been trying to fix this but not been successful.

#REGRESSIONS

regression1<-lm(MH~SUMMER_final$riskc + SUMMER_final$PSc +xz+gender+age, data=SUMMER_final)

summary(regression1) 

: The error says that object -regression 1- not found

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.

Su Lee

unread,
Aug 21, 2020, 12:18:31 PM8/21/20
to lav...@googlegroups.com
Hi Dr Jorgensen and Patrick, 
Everything has worked out - I am so happy now !!! you really helped the most challenging part . 
I just want to say thank you so much and have a peaceful  weekend !!

Happy weekend , everyone !!!




On Fri, Aug 21, 2020 at 5:53 AM Terrence Jorgensen <tjorge...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages