Adding Quadratic Term in a Path Model?

1,113 views
Skip to first unread message

Tim Duclos

unread,
May 3, 2016, 12:57:11 PM5/3/16
to lavaan
Hello Lavaan Experts,

I am trying to figure out if I can add quadratic and/or polynomial terms within a path model in lavaan.   Below is my model code for my path model.  Please let me know if and how I can add a quadratic term between bird_N and, say, t.mean (or any other variable serving as a predictor of bird_N).  I wish to add this quadratic term in order to take into account a unimodal response (lack of linearity) between bird_N and associated predictor/mediating variables related to bird_N. 

Help!



SEM.1<-
'
###
#direct effect of mean temp
  bird_N ~ m*t.mean

#direct effect of count of 5th quantile of t
  bird_N ~ n*t.count.05

#direct effect of count of 95th quantile of t
  bird_N ~ o*t.count.95

#direct effect of t. range
  bird_N ~ p*t.range

#direct effect of precip
  bird_N ~ q*precip

###
#indirect effect of mean temp, via pc1.both
pc1.both ~ a*t.mean
bird_N ~ k*pc1.both

#indirect effect of count of 5th quantile of t, via pc1.both
pc1.both ~ b*t.count.05

#indirect effect of count of 95th quantile of t, via pc1.both
pc1.both ~ c*t.count.95

#indirect effect of t. range, via pc1.both
pc1.both ~ d*t.range

#indirect effect of precip, via pc1.both
pc1.both ~ e*precip


###
#indirect effect of mean temp, via pc2.both
pc2.both ~ f*t.mean
bird_N ~ l*pc2.both

#indirect effect of count of 5th quantile of t, via pc2.both
pc2.both ~ g*t.count.05

#indirect effect of count of 5th quantile of t, via pc2.both
pc2.both ~ h*t.count.95

#indirect effect of t. range, via pc2.both
pc2.both ~ i*t.range

#indirect effect of precip, via pc2.both
pc2.both ~ j*precip


### 
#direct effect of mean t on bird
DE_t.mean:= m

#direct effect of t.count.05
DE_t.count.05:= n

#direct effect of t.count.95
DE_t.count.95:= o

#direct effect of range
DE_t.range:= p

#total direct effect of t
TDE_t:= m + n + o + p

#direct effect of precip (which is the total direct effect of precip)
TDE_precip:= q

#total direct effect of climate
TDE_climate:=  m + n + o + p + q


###
#indirect effect of mean t, via pc1.both
IE_t.mean_pc1:= a*k

#indirect effect of t.count.05, via pc1.both
IE_t.count.05_pc1:= b*k

#indirect effect of t.count.95, via pc1.both
IE_t.count.95_pc1:= c*k

#indirect effect of t.range, via pc1.both
IE_t.range_pc1:= d*k

#total indirect effect of t via pc1.both
TIE_t_pc1:= (a*k)+(b*k)+(c*k)+(d*k)

#indirect effect of precp, via pc1.both
IE_precip_pc1:= e*k

###
#total indirect effect of climate via pc1.both
TIE_t_climate:= (a*k)+(b*k)+(c*k)+(d*k)+(e*k)

###
#indirect effect of mean t via pc2.both
IE_t.mean_pc2:= f*l

#indirect effect of t.count.05 via pc2.both
IE_t.count.05_pc2:= g*l

#indirect effect of t.count.95 via pc2.both
IE_t.count.95_pc2:= h*l

#indirect effect of t.range, via pc2.both
IE_t.range_pc2:= i*l

#total indirect effect of t via pc2.both
TIE_t_pc2:= (f*l)+(g*l)+(h*l)+(i*l)

#indirect effect of precip via pc2.both
IE_precip_pc2:= j*l

#total indirect effect of climate via pc2.both
TIE_t_climate:= (f*l)+(g*l)+(h*l)+(i*l)+(j*l)

####
#total effect of mean t on bird via all pathways
TE_t.mean:= m + (a*k) + (f*l)

#total effect of t.count.05 on bird via all pathways
TE_t.count.05:= n + (b*k) + (g*l)

#total effect of t.count.95 on bird via all pathways
TE_t.count.95:= o + (c*k) + (h*l)

#total effect of t.range on bird via all pathways
TE_t.range:= p + (d*k) + (i*l)

#total effect of precip via all pathways
TE_precip:= q + (e*k) + (j*l)

#total effect of t via all pathways
TE_t:= (m + (a*k) + (f*l)) + (n + (b*k) + (g*l)) + (o + (c*k) + (h*l)) + (p + (d*k) + (i*l)) 

#total effect of climate via all pathways
TE_climate:= (m + (a*k) + (f*l)) + (n + (b*k) + (g*l)) + (o + (c*k) + (h*l)) + (p + (d*k) + (i*l)) +(q + (e*k) + (j*l)) 

#####

#total indirect effect of t via veg (both pc1 and pc2)
TIE_t:= (a*k)+(b*k)+(c*k)+(d*k)  +  (f*l)+(g*l)+(h*l)+(i*l)

#total indirect effect of climate via veg (both pc1 and pc2)
TIE_t:= (a*k)+(b*k)+(c*k)+(d*k)+(e*k)  +  (f*l)+(g*l)+(h*l)+(i*l)+(j*l)

###
#specify any (residual) covariances 
#t_mean  ~~ t_min #as an example. 

  '



Message has been deleted

Tim Duclos

unread,
May 3, 2016, 1:44:17 PM5/3/16
to lavaan
Also, while I'm at it, can anyone tell me why the above model only has a single degree of freedom calculated for it. 


On Tuesday, May 3, 2016 at 12:57:11 PM UTC-4, Tim Duclos wrote:
Hello Lavaan Experts,

Terrence Jorgensen

unread,
May 4, 2016, 4:23:54 AM5/4/16
to lavaan
I am trying to figure out if I can add quadratic and/or polynomial terms within a path model in lavaan.

If you want to include nonlinearity in variables (e.g., interaction terms or polynomials), then you need to calculate those nonlinear variables manually and add them to your data.frame, then use add those variables as predictors in your lavaan syntax.  For example:

myData$x.squ <- myData$x^2
myData$x.cub <- myData$x^3

syntax <- ' y ~ x + x.squ + x.cub '


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

Terrence Jorgensen

unread,
May 4, 2016, 4:32:41 AM5/4/16
to lavaan
Also, while I'm at it, can anyone tell me why the above model only has a single degree of freedom calculated for it. 

You didn't explain why you are suprised by df == 1, but you could inspect which parameters are being estimated to see if that matches your expectation:

lavInspect(sem(SEM.1, ...), "free")

Tim Duclos

unread,
May 4, 2016, 10:37:32 AM5/4/16
to lavaan
Thank you Terrence.  

I am interested how lavaan calculates degrees of freedom and what the implications are for only having one degree of freedom in this model.  Thanks!  

Terrence Jorgensen

unread,
May 4, 2016, 5:18:05 PM5/4/16
to lavaan
I am interested how lavaan calculates degrees of freedom and what the implications are for only having one degree of freedom in this model.  

As with other modeling frameworks, df are calculated as the difference between the observed and estimated pieces of information, but in covariance structure analysis the observed pieces of information are not the rows of data.  Rather, they are the observed sample  moments (variances, covariances, and optionally means).  When you add the polynomial terms, those variables will be separate rows/columns in your covariance matrix and mean vector, too.  Unless you set "fixed.x = FALSE", then the submatrix of exogenous variables is treated as fixed rather than estimated, so don't count those cells when calculating df.

Tim Duclos

unread,
Jul 27, 2016, 2:52:58 PM7/27/16
to lavaan
Terrance,  I know this was awhile ago now, forgive me,  but I want to make sure that I have specified the polynomials correctly in the lavaan syntax.  So, following from your example, say I want to take into account a quadratic relationship between pc2.both and its predictors t.mean and precip, how do I then actually specify the pathway in the lavaan syntax?

As you instructed, I would first calculate and add the quadratic of precip and t.mean to my data frame:

myData$precip.sq<- myData$precip^2
myData$t.mean.sq<- myData$t.mean^2

Then, my question is:  in the SEM model syntax, do I apply the path parameter to both the original variable and the squared form?

pc2.both ~ f*t.mean + f*t.mean.sq
pc2.both ~ j*precip + j*precip.sq 

Or do I do this:

pc2.both ~ f*t.mean + t.mean.sq
pc2.both ~ j*precip + precip.sq 

Or do I do this:

pc2.both ~ f*(t.mean + t.mean.sq)
pc2.both ~ j*(precip + precip.sq) 


FYI, if I wasn't trying to take into a quadratic relationship, then my normal model syntax would simply be:
pc2.both ~ f*t.mean
pc2.both ~ j*precip 


Thank you in advance for any help you can provide!

Cheers,
Tim

Terrence Jorgensen

unread,
Jul 29, 2016, 4:27:47 AM7/29/16
to lavaan
Make sure you do not constrain the linear and quadratic slopes to equality.  So use the second one:

pc2.both ~ f*t.mean + t.mean.sq
pc2.both ~ j*precip + precip.sq 

You can still label the quadratic slopes as well, but make sure to give them different labels (e.g., "f.q" and "j.q").

Tim Duclos

unread,
Jul 29, 2016, 9:42:00 AM7/29/16
to lavaan
Thank you for your help Terrence! 
Reply all
Reply to author
Forward
0 new messages