getCov.RdConvenience functions to deal with covariance and correlation matrices.
getCov(x, lower = TRUE, diagonal = TRUE, sds = NULL,
names = paste("V", 1:nvar, sep=""))
char2num(s)
cor2cov(R, sds, names = NULL)The elements of the covariance matrix. Either inside a character
string or as a numeric vector. In the former case, the function
char2num is used to convert the numbers (inside the character
string) to numeric values.
Logical. If TRUE, the numeric values in x are
the lower-triangular elements of the (symmetric) covariance matrix only.
If FALSE, x contains the upper triangular elements only.
Note we always assumed the elements are provided row-wise!
Logical. If TRUE, the numeric values in x
include the diagonal elements. If FALSE, a unit diagonal is assumed.
A numeric vector containing the standard deviations to be
used to scale the elements in x or the correlation matrix R
into a covariance matrix.
The variable names of the observed variables.
Character string containing numeric values; comma's and semi-colons are ignored.
A correlation matrix, to be scaled into a covariance matrix.
The getCov function is typically used to input the lower
(or upper) triangular elements of a (symmetric) covariance matrix. In many
examples found in handbooks, only those elements are shown. However, lavaan
needs a full matrix to proceed.
The cor2cov function is the inverse of the cov2cor
function, and scales a correlation matrix into a covariance matrix given
the standard deviations of the variables. Optionally, variable names can
be given.
# The classic Wheaton et. al. (1977) model
# panel data on he stability of alienation
lower <- '
11.834,
6.947, 9.364,
6.819, 5.091, 12.532,
4.783, 5.028, 7.495, 9.986,
-3.839, -3.889, -3.841, -3.625, 9.610,
-21.899, -18.831, -21.748, -18.775, 35.522, 450.288 '
# convert to a full symmetric covariance matrix with names
wheaton.cov <- getCov(lower, names=c("anomia67","powerless67", "anomia71",
"powerless71","education","sei"))
# the model
wheaton.model <- '
# measurement model
ses =~ education + sei
alien67 =~ anomia67 + powerless67
alien71 =~ anomia71 + powerless71
# equations
alien71 ~ alien67 + ses
alien67 ~ ses
# correlated residuals
anomia67 ~~ anomia71
powerless67 ~~ powerless71
'
# fitting the model
fit <- sem(wheaton.model, sample.cov=wheaton.cov, sample.nobs=932)
#> Warning: lavaan->lav_model_vcov():
#> The variance-covariance matrix of the estimated parameters (vcov) does not
#> appear to be positive definite! The smallest eigenvalue (= -2.305106e+01)
#> is smaller than zero. This may be a symptom that the model is not
#> identified.
# showing the results
summary(fit, standardized=TRUE)
#> lavaan 0.6-20 ended normally after 84 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 17
#>
#> Number of observations 932
#>
#> Model Test User Model:
#>
#> Test statistic 4.735
#> Degrees of freedom 4
#> P-value (Chi-square) 0.316
#>
#> Parameter Estimates:
#>
#> Standard errors Standard
#> Information Expected
#> Information saturated (h1) model Structured
#>
#> Latent Variables:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> ses =~
#> education 1.000 2.607 0.842
#> sei 5.219 0.295 17.697 0.000 13.609 0.642
#> alien67 =~
#> anomia67 1.000 2.663 0.774
#> powerless67 0.979 0.046 21.461 0.000 2.606 0.852
#> alien71 =~
#> anomia71 1.000 2.850 0.805
#> powerless71 0.922 0.056 16.473 0.000 2.628 0.832
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> alien71 ~
#> alien67 0.607 0.050 12.085 0.000 0.567 0.567
#> ses -0.227 0.052 -4.369 0.000 -0.207 -0.207
#> alien67 ~
#> ses -0.575 0.054 -10.659 0.000 -0.563 -0.563
#>
#> Covariances:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> .anomia67 ~~
#> .anomia71 1.623 0.275 5.898 0.000 1.623 0.356
#> .powerless67 ~~
#> .powerless71 0.339 0.234 1.450 0.147 0.339 0.121
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> .education 2.801 0.323 8.676 0.000 2.801 0.292
#> .sei 264.597 8.597 30.777 0.000 264.597 0.588
#> .anomia67 4.731 0.317 14.906 0.000 4.731 0.400
#> .powerless67 2.563 0.266 9.644 0.000 2.563 0.274
#> .anomia71 4.399 0.499 8.819 0.000 4.399 0.351
#> .powerless71 3.070 0.416 7.374 0.000 3.070 0.308
#> ses 6.798 0.489 13.897 0.000 1.000 1.000
#> .alien67 4.841 0.407 11.905 0.000 0.683 0.683
#> .alien71 4.083 0.385 10.606 0.000 0.503 0.503
#>