how to "switch off" the covariance between two exogenous variables (not latent variables) in lavaan?

3,192 views
Skip to first unread message

Bin Chen

unread,
May 11, 2014, 4:30:40 AM5/11/14
to lav...@googlegroups.com
Hi all,
I am a bit confused about how to tell lavaan "don't include the covariance between two exogenous variables" ...
Because the two exogenous variables were manipulated by me, they should not, and they cannot have any correlation...

I tried fixed.x = T or F, they give me the same results. the degree of freedom didn't change...
It seems a very simple model C ~ A + B cannot be tested in lavaan.
Please see the coding sample, if I made any mistake there.


> A = runif(100, min = 1, max = 100)
> B = runif(100, min = 1, max = 100)
> error = runif(100, min = 1, max = 10)
> C = A + B + error
> test <- data.frame(A, B, C)
> mod <- 'C ~ A + B'
> mod.fit <- sem(mod, data = test, fixed.x = T)
> summary(mod.fit, standardized = T, rsq = T)
lavaan (0.5-16) converged normally after   1 iterations

  Number of observations                           100

  Estimator                                         ML
  Minimum Function Test Statistic                0.000
  Degrees of freedom                                 0
  P-value (Chi-square)                           1.000

Parameter estimates:

  Information                                 Expected
  Standard Errors                             Standard

                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Regressions:
  C ~
    A                 1.011    0.010  100.961    0.000    1.011    0.637
    B                 1.022    0.009  111.062    0.000    1.022    0.701

Variances:
    C                 7.126    1.008                              7.126    0.004

R-Square:

    C                 0.996

> mod.fit <- sem(mod, data = test, fixed.x = F)
> summary(mod.fit, standardized = T, rsq = T)
lavaan (0.5-16) converged normally after  53 iterations

  Number of observations                           100

  Estimator                                         ML
  Minimum Function Test Statistic                0.000
  Degrees of freedom                                 0
  P-value (Chi-square)                           0.000

Parameter estimates:

  Information                                 Expected
  Standard Errors                             Standard

                   Estimate  Std.err  Z-value  P(>|z|)   Std.lv  Std.all
Regressions:
  C ~
    A                 1.011    0.010  100.961    0.000    1.011    0.637
    B                 1.022    0.009  111.062    0.000    1.022    0.701

Covariances:
  A ~~
    B                86.296   78.777    1.095    0.273   86.296    0.110

Variances:
    C                 7.126    1.008                              7.126    0.004
    A               719.041  101.688                       719.041    1.000
    B               852.701  120.590                       852.701    1.000

R-Square:

    C                 0.996

Edward Rigdon

unread,
May 11, 2014, 5:56:31 AM5/11/14
to lav...@googlegroups.com
Add the line
A~~0*B
to your model.  This line fixes the A,B covariance to 0.
--Ed Rigdon

Sent from my iPad
--
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 post to this group, send email to lav...@googlegroups.com.
Visit this group at http://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

yrosseel

unread,
May 11, 2014, 2:32:31 PM5/11/14
to lav...@googlegroups.com
On 05/11/2014 10:30 AM, Bin Chen wrote:
> Hi all,
> I am a bit confused about how to tell lavaan "don't include the
> covariance between two exogenous variables"
> Because the two exogenous variables were manipulated by me, they should
> not, and they cannot have any correlation...

By default, lavaan uses fixed.x = TRUE, which implies that lavaan will
NOT model/estimate the variances and covariances of the exogenous
variables in the model. The variances/covariances will simply be fixed
at their sample values. If the sample covariance happens to be zero, so
be it.

> I tried fixed.x = T or F, they give me the same results. the degree of
> freedom didn't change.

They shouldn't change, unless you restrict some variances/covariances.

> It seems a very simple model C ~ A + B cannot be tested in lavaan.

mod <- 'C ~ A + B' with fixed.x = TRUE is what you should do (and this
is the default). This is exactly as in regression. Do you 'force' a
correlation between predictors to be zero in a regression context?
Usually not. You just leave them as there are, and this is the default
behaviour of fixed.x = TRUE.

If you *really* must force a covariance to be zero, then you should:

1) use fixed.x = FALSE (since you will model them)
2) add 'A ~~ 0*B' to the model syntax.

Yves.

Bin Chen

unread,
May 12, 2014, 4:54:11 AM5/12/14
to lav...@googlegroups.com
Great thanks to Edward and Yves!

Marcos Angelini

unread,
Jan 28, 2016, 4:56:49 AM1/28/16
to lavaan
Dear Yves,

I think there is an error when you set fixed.x = T because it does not free the parameters that it suppose. Here a proof:

x <- rnorm(1000,1,1)
y
<- rnorm(1000,1,1)
z
<- x*0.6+y*0.5+rnorm(1000,0,sqrt(1-2*0.5^2))
d
<- data.frame(x=x,y=y,z=z)
my.mod <- 'z ~ x + y'
my.fit <- sem(my.mod, data = d, meanstructure = T, fixed.x = T)
summary
(my.fit)

# lavaan (0.5-20) converged normally after  15 iterations
#
# Number of observations                          1000
#
# Estimator                                         ML
# Minimum Function Test Statistic                0.000
# Degrees of freedom                                 0
# Minimum Function Value               0.0000000000000

partable(my.fit) # IT SHOWS THAT THERE ARE FIVE FREE PARAMETERS

#   id lhs op rhs user group free ustart exo label plabel start    est    se
# 1  1   z  ~   x    1     1    1     NA   0         .p1. 0.000  0.597 0.022
# 2  2   z  ~   y    1     1    2     NA   0         .p2. 0.000  0.523 0.022
# 3  3   z ~~   z    0     1    3     NA   0         .p3. 0.558  0.478 0.021
# 4  4   x ~~   x    0     1    0     NA   1         .p4. 1.007  1.007 0.000
# 5  5   x ~~   y    0     1    0     NA   1         .p5. 0.013  0.013 0.000
# 6  6   y ~~   y    0     1    0     NA   1         .p6. 0.988  0.988 0.000
# 7  7   z ~1        0     1    4     NA   0         .p7. 1.043 -0.063 0.037
# 8  8   x ~1        0     1    0     NA   1         .p8. 1.016  1.016 0.000
# 9  9   y ~1        0     1    0     NA   1         .p9. 0.956  0.956 0.000


But, if you set the model in this way it works.

my.mod <- '
z ~ x + y
x ~~ 0*y
x ~1*1
y ~1*1
x ~~ 1*x
y ~~ 1*y
'
my.fit <- sem(my.mod, data = d, meanstructure = T, fixed.x = F)
summary(my.fit)

# lavaan (0.5-20) converged normally after  17 iterations
#
# Number of observations                          1000
#
# Estimator                                         ML
# Minimum Function Test Statistic                2.422
# Degrees of freedom                                 5
# P-value (Chi-square)                           0.788

partable(my.fit)
#   id lhs op rhs user group free ustart exo label plabel start    est    se
# 1  1   z  ~   x    1     1    1     NA   0         .p1. 0.000  0.597 0.022
# 2  2   z  ~   y    1     1    2     NA   0         .p2. 0.000  0.523 0.022
# 3  3   x ~~   y    1     1    0      0   0         .p3. 0.000  0.000 0.000
# 4  4   x ~1        1     1    0      1   0         .p4. 1.000  1.000 0.000
# 5  5   y ~1        1     1    0      1   0         .p5. 1.000  1.000 0.000
# 6  6   x ~~   x    1     1    0      1   0         .p6. 1.000  1.000 0.000
# 7  7   y ~~   y    1     1    0      1   0         .p7. 1.000  1.000 0.000
# 8  8   z ~~   z    0     1    3     NA   0         .p8. 0.558  0.478 0.021
# 9  9   z ~1        0     1    4     NA   0         .p9. 1.043 -0.063 0.038



Marcos

yrosseel

unread,
Jan 28, 2016, 5:27:09 AM1/28/16
to lav...@googlegroups.com
On 01/28/2016 10:56 AM, Marcos Angelini wrote:
> Dear Yves,
>
> I think there is an error when you set fixed.x = T because it does not
> free the parameters that it suppose. Here a proof:

It is not supposed to. When fixed.x = TRUE, we just take the 'x'-part of
the model for what it is: a set of correlated exogenous variables. This
means that we simply copy the 'x' part of the observed covariance
matrix, and plug it in the implied covariance matrix.

Why would we need to fix those covariances to zero?

Only when fixed.x = FALSE, the covariances between the exogenous
covariates become model parameters, and you can (if you believe this is
a good idea) fixed them to zero in the syntax.

Yves.

Marcos Angelini

unread,
Jan 28, 2016, 5:51:01 AM1/28/16
to lavaan
Thank you for your reply Yves. But then I have a very basic question.

What I do not understand is that if I fix the model with the sampling data -(co)variance and mean- in the syntax lavaan reports that I have 5 free degree of freedom. But if I set fixed.x = TRUE lavaan reports zero degree of freedom. What does "degree of freedom" mean then?

In this case I set x ~~ 0*y because this is the sampling variance.

Marcos

yrosseel

unread,
Jan 28, 2016, 6:16:02 AM1/28/16
to lav...@googlegroups.com
On 01/28/2016 11:51 AM, Marcos Angelini wrote:
> Thank you for your reply Yves. But then I have a very basic question.
>
> What I do not understand is that if I fix the model with the sampling
> data -(co)variance and mean- in the syntax lavaan reports that I have 5
> free degree of freedom.

If you add them in the syntax, they become model parameters. If you fix
those model parameters, you gain degrees of freedom.

> But if I set fixed.x = TRUE lavaan reports zero
> degree of freedom.

In this case, the mean/var/cov of the exogenous covariates are not
considered as part of the model. We do not 'fix' anything, we just leave
them alone. Therefore, they are not counted as 'data points', and they
are not included as model parameters.

> What does "degree of freedom" mean then?

See various older posts, eg:
https://groups.google.com/forum/#!searchin/lavaan/degrees$20of$20freedom$20fixed.x/lavaan/d1f1bTZxg4A/MdGt_vMJYQkJ

Yves.

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages