Specifies correlated errors among predictors
e1 %~~% e2
first variable involved in correlated error
second variable involved in correlated error
For use in psem
to identify correlated sets of variables.
# Generate example data
dat <- data.frame(x1 = runif(50),
x2 = runif(50), y1 = runif(50),
y2 = runif(50))
# Create list of structural equations
sem <- psem(
lm(y1 ~ x1 + x2, dat),
lm(y2 ~ y1 + x1, dat)
)
# Look at correlated error between x1 and x2
# (exogenous)
cerror(x1 %~~% x2, sem, dat)
#> Response Predictor Estimate Std.Error DF Crit.Value P.Value
#> df ~~x1 ~~x2 -0.1163599 NA 48 -0.8116788 0.4209829
# Same as cor.test
with(dat, cor.test(x1, x2))
#>
#> Pearson's product-moment correlation
#>
#> data: x1 and x2
#> t = -0.81168, df = 48, p-value = 0.421
#> alternative hypothesis: true correlation is not equal to 0
#> 95 percent confidence interval:
#> -0.3823247 0.1674100
#> sample estimates:
#> cor
#> -0.1163599
#>
# Look at correlatde error between x1 and y1
# (endogenous)
cerror(y1 %~~% x1, sem, dat)
#> Error in is.data.frame(data): object 'dat' not found
# Not the same as cor.test
# (accounts for influence of x1 and x2 on y1)
with(dat, cor.test(y1, x1))
#>
#> Pearson's product-moment correlation
#>
#> data: y1 and x1
#> t = -1.2395, df = 48, p-value = 0.2212
#> alternative hypothesis: true correlation is not equal to 0
#> 95 percent confidence interval:
#> -0.4332189 0.1075106
#> sample estimates:
#> cor
#> -0.1761072
#>
# Specify in psem
sem <- update(sem, x1 %~~% y1)
coefs(sem)
#> Error in is.data.frame(data): object 'dat' not found