load libraries.
library(psych)
library(lavaan)
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
##
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
##
## cor2cov
library(semTools)
##
## ###############################################################################
## This is semTools 0.5-6
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
##
## Attaching package: 'semTools'
## The following objects are masked from 'package:psych':
##
## reliability, skew
library(semPlot)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ stringr 1.4.1
## ✔ tidyr 1.2.1 ✔ forcats 0.5.2
## ✔ readr 2.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%() masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ readr::clipboard() masks semTools::clipboard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
load data.
data <- read.csv("~/okara004@ucr.edu - Google Drive/My Drive/classes/3 -- spring '23/PSYC 214 -- stats/homework/HW #1/HW1_Data/WLS_HW1_CFA_rev23.csv")
get descriptives.
# gender
gender <- factor(data$sexrsp, levels = 1:2, labels = c("Male", "Female"))
summary(gender)
## Male Female
## 4637 5051
4637 + 5051 ### = 9688
## [1] 9688
5051/9688 ### = 0.5213666, 52.14% female
## [1] 0.5213666
# age
data <- mutate(data, Age = 103-(data$byear))
psych::describe(data$Age) ### mean age: 64.16yrs; SD: 0.51yrs
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 9688 64.16 0.51 64 64.1 0 63 66 3 1.1 2.71 0.01
# education
data$educ92 <- as.numeric(data$educ92)
psych::describe(data$educ92) ### mean: 13.61 (about 1yr of college); SD: 2.26yrs
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 8452 13.61 2.26 12 13.21 0 12 21 9 1.14 0.04 0.02
# nationality on father's side
glimpse(data$ge096fa)
## int [1:9688] NA 14 4 NA 2 14 14 NA 14 14 ...
data$ge096fa <- as.factor(data$ge096fa)
summary(data$ge096fa) ### primarily european
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 633 108 52 463 549 166 128 182 6 82 81 231 4 3417 474 202
## 17 18 19 20 22 23 24 25 26 27 28 29 30 31 32 33
## 43 21 47 4 20 10 91 3 8 21 55 2 1 1 1 1
## 34 35 36 37 38 39 40 41 42 NA's
## 1 5 16 11 20 9 16 11 4 2488
# nationality on mother's side
glimpse(data$ge095ma)
## int [1:9688] NA 4 1 NA 41 14 4 NA 14 14 ...
data$ge095ma <- as.factor(data$ge095ma)
summary(data$ge095ma) ### also primarily european
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 622 91 38 495 554 151 104 168 28 75 81 218 14 3106 527 192
## 17 18 19 20 22 23 24 25 26 27 28 30 32 33 35 36
## 48 31 59 3 50 7 80 5 8 32 36 1 1 1 2 16
## 37 38 39 40 41 42 43 44 46 47 NA's
## 8 58 26 27 32 11 3 1 1 1 2676
observed corr & cov/var matrices.
# take a look at the observed correlation matrix.
cor_matrix <- cor(data[, c("in501rer", "in502rer", "in503rer", "in504rer", "in505rer", "in506rer", "in507rer")], use = "complete", method = "pearson")
print(cor_matrix)
## in501rer in502rer in503rer in504rer in505rer in506rer in507rer
## in501rer 1.0000000 0.2792627 0.3178501 0.3187617 0.3245953 0.2274303 0.2614723
## in502rer 0.2792627 1.0000000 0.3957867 0.4068193 0.3168857 0.2216130 0.2816966
## in503rer 0.3178501 0.3957867 1.0000000 0.3038079 0.2239655 0.1543646 0.2044126
## in504rer 0.3187617 0.4068193 0.3038079 1.0000000 0.6495276 0.3410393 0.3231119
## in505rer 0.3245953 0.3168857 0.2239655 0.6495276 1.0000000 0.4479959 0.3972244
## in506rer 0.2274303 0.2216130 0.1543646 0.3410393 0.4479959 1.0000000 0.5039401
## in507rer 0.2614723 0.2816966 0.2044126 0.3231119 0.3972244 0.5039401 1.0000000
### correlation between volunteer identity and group/organization identity is pretty high compared to the others. correlations among factor 1 variables are not all that much higher than the correlations between factor 1 variables and factor 2 variables, but correlations among the factor 2 variables do appear to be a bit higher than correlations between the factor 2 and factor 1 variables.
# covariance matrix
cov_matrix <- cov(data[, c("in501rer", "in502rer", "in503rer", "in504rer", "in505rer", "in506rer", "in507rer")], use = "complete")
print(cov_matrix)
## in501rer in502rer in503rer in504rer in505rer in506rer in507rer
## in501rer 3.4519497 1.0836549 0.8668438 1.0220569 1.0973510 0.7619236 0.9173476
## in502rer 1.0836549 4.3620590 1.2133700 1.4663040 1.2042580 0.8345877 1.1109728
## in503rer 0.8668438 1.2133700 2.1546295 0.7695947 0.5981891 0.4085685 0.5665912
## in504rer 1.0220569 1.4663040 0.7695947 2.9781975 2.0396021 1.0612363 1.0529450
## in505rer 1.0973510 1.2042580 0.5981891 2.0396021 3.3108706 1.4698602 1.3648441
## in506rer 0.7619236 0.8345877 0.4085685 1.0612363 1.4698602 3.2513359 1.7158757
## in507rer 0.9173476 1.1109728 0.5665912 1.0529450 1.3648441 1.7158757 3.5657587
two-factor model.
# start by fitting a two-factor model.
model_2f <- "
F1 = ~ in501rer + in502rer + in503rer
F2 = ~ in504rer + in505rer + in506rer + in507rer"
# run the CFA and take a look at the fit indices.
model_2f_fit <- cfa(model_2f, data = data)
summary(model_2f_fit, standard = TRUE, fit.measures = TRUE)
## lavaan 0.6.15 ended normally after 37 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 15
##
## Used Total
## Number of observations 6312 9688
##
## Model Test User Model:
##
## Test statistic 1283.951
## Degrees of freedom 13
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 11105.204
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.885
## Tucker-Lewis Index (TLI) 0.815
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -83717.758
## Loglikelihood unrestricted model (H1) -83075.783
##
## Akaike (AIC) 167465.517
## Bayesian (BIC) 167566.770
## Sample-size adjusted Bayesian (SABIC) 167519.104
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.124
## 90 Percent confidence interval - lower 0.119
## 90 Percent confidence interval - upper 0.130
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.058
##
## 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
## F1 =~
## in501rer 1.000 0.984 0.530
## in502rer 1.352 0.047 28.747 0.000 1.330 0.637
## in503rer 0.839 0.030 27.522 0.000 0.825 0.562
## F2 =~
## in504rer 1.000 1.315 0.762
## in505rer 1.122 0.021 52.958 0.000 1.475 0.811
## in506rer 0.754 0.019 39.452 0.000 0.991 0.550
## in507rer 0.755 0.020 37.750 0.000 0.992 0.526
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## F1 ~~
## F2 0.896 0.035 25.726 0.000 0.693 0.693
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .in501rer 2.484 0.054 45.658 0.000 2.484 0.720
## .in502rer 2.594 0.069 37.437 0.000 2.594 0.595
## .in503rer 1.474 0.034 43.616 0.000 1.474 0.684
## .in504rer 1.249 0.034 36.875 0.000 1.249 0.419
## .in505rer 1.134 0.037 30.436 0.000 1.134 0.342
## .in506rer 2.269 0.045 50.640 0.000 2.269 0.698
## .in507rer 2.580 0.050 51.314 0.000 2.580 0.724
## F1 0.968 0.053 18.210 0.000 1.000 1.000
## F2 1.729 0.054 31.734 0.000 1.000 1.000
### none of the fit indices are looking too good...chi-square is very large for only 13 degrees of freedom, CFI & TLI do not reach the 0.95 threshold, and the RMSEA does not fall below the .06 threshold
# visualization of the model.
semPaths(model_2f_fit)

# take a look at the expected cov matrix and the residual cov matrix.
exp_matrix_2f <- fitted(model_2f_fit)
exp_matrix_2f
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 3.451
## in502rer 1.308 4.361
## in503rer 0.812 1.097 2.154
## in504rer 0.896 1.211 0.751 2.978
## in505rer 1.005 1.359 0.843 1.940 3.310
## in506rer 0.675 0.913 0.566 1.303 1.462 3.251
## in507rer 0.676 0.914 0.567 1.305 1.464 0.984 3.565
resid_matrix_2f <- resid(model_2f_fit)
resid_matrix_2f
## $type
## [1] "raw"
##
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 0.000
## in502rer -0.225 0.000
## in503rer 0.055 0.116 0.000
## in504rer 0.126 0.255 0.018 0.000
## in505rer 0.092 -0.155 -0.245 0.099 0.000
## in506rer 0.086 -0.078 -0.158 -0.242 0.007 0.000
## in507rer 0.241 0.197 -0.001 -0.252 -0.100 0.732 0.000
one-factor model.
# let's see if the one-factor model fits better than the two-factor model.
model_1f <- "
F1 = ~ in501rer + in502rer + in503rer + in504rer + in505rer + in506rer + in507rer"
# run the CFA and take a look at the fit indices.
model_1f_fit <- cfa(model_1f, data = data)
summary(model_1f_fit, standard = TRUE, fit.measures = TRUE)
## lavaan 0.6.15 ended normally after 33 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 14
##
## Used Total
## Number of observations 6312 9688
##
## Model Test User Model:
##
## Test statistic 1858.735
## Degrees of freedom 14
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 11105.204
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.834
## Tucker-Lewis Index (TLI) 0.750
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -84005.150
## Loglikelihood unrestricted model (H1) -83075.783
##
## Akaike (AIC) 168038.301
## Bayesian (BIC) 168132.804
## Sample-size adjusted Bayesian (SABIC) 168088.315
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.144
## 90 Percent confidence interval - lower 0.139
## 90 Percent confidence interval - upper 0.150
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.072
##
## 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
## F1 =~
## in501rer 1.000 0.840 0.452
## in502rer 1.241 0.046 26.990 0.000 1.042 0.499
## in503rer 0.689 0.030 23.317 0.000 0.579 0.394
## in504rer 1.563 0.048 32.499 0.000 1.312 0.760
## in505rer 1.693 0.052 32.716 0.000 1.421 0.781
## in506rer 1.170 0.041 28.303 0.000 0.982 0.545
## in507rer 1.198 0.043 27.973 0.000 1.006 0.533
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .in501rer 2.746 0.052 52.954 0.000 2.746 0.796
## .in502rer 3.276 0.063 51.999 0.000 3.276 0.751
## .in503rer 1.820 0.034 53.875 0.000 1.820 0.845
## .in504rer 1.256 0.033 38.064 0.000 1.256 0.422
## .in505rer 1.291 0.036 35.663 0.000 1.291 0.390
## .in506rer 2.286 0.045 50.833 0.000 2.286 0.703
## .in507rer 2.554 0.050 51.167 0.000 2.554 0.716
## F1 0.705 0.041 17.178 0.000 1.000 1.000
### chi-squared is larger than the two-factor model, CFI and TLI are both smaller than the two-factor model, AIC and BIC are both larger than the two-factor model, and RMSEA is larger than the two-factor model. so we gained one degree of freedom, but fit worsened.
# visualization of the model.
semPaths(model_1f_fit)

# take a look at the expected cov matrix and the residual cov matrix.
exp_matrix_1f <- fitted(model_1f_fit)
exp_matrix_1f
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 3.451
## in502rer 0.875 4.361
## in503rer 0.486 0.603 2.154
## in504rer 1.102 1.367 0.759 2.978
## in505rer 1.193 1.481 0.822 1.865 3.310
## in506rer 0.825 1.023 0.568 1.289 1.396 3.251
## in507rer 0.845 1.048 0.582 1.320 1.429 0.988 3.565
resid_matrix_1f <- resid(model_1f_fit)
resid_matrix_1f
## $type
## [1] "raw"
##
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 0.000
## in502rer 0.209 0.000
## in503rer 0.381 0.610 0.000
## in504rer -0.080 0.099 0.010 0.000
## in505rer -0.096 -0.277 -0.224 0.175 0.000
## in506rer -0.063 -0.189 -0.160 -0.228 0.074 0.000
## in507rer 0.073 0.063 -0.015 -0.267 -0.065 0.728 0.000
two-factor model – vol & org equated.
# since volunteer identity and group/organization identity had the highest correlation on in the observed correlation matrix, i'm going to try equating them in the model so see if that improves fit.
model_2fEQ <- "
F1 = ~ in501rer + in502rer + in503rer
F2 = ~ a*in504rer + a*in505rer + in506rer + in507rer"
# run the CFA and take a look at the fit indices.
model_2fEQ_fit <- cfa(model_2fEQ, data = data)
summary(model_2fEQ_fit, standard = TRUE, fit.measures = TRUE)
## lavaan 0.6.15 ended normally after 35 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 14
##
## Used Total
## Number of observations 6312 9688
##
## Model Test User Model:
##
## Test statistic 1325.712
## Degrees of freedom 14
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 11105.204
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.882
## Tucker-Lewis Index (TLI) 0.822
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -83738.639
## Loglikelihood unrestricted model (H1) -83075.783
##
## Akaike (AIC) 167505.277
## Bayesian (BIC) 167599.780
## Sample-size adjusted Bayesian (SABIC) 167555.292
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.122
## 90 Percent confidence interval - lower 0.116
## 90 Percent confidence interval - upper 0.127
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.060
##
## 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
## F1 =~
## in501rer 1.000 0.980 0.527
## in502rer 1.362 0.047 28.878 0.000 1.334 0.639
## in503rer 0.841 0.031 27.584 0.000 0.825 0.562
## F2 =~
## in504rer (a) 1.000 1.392 0.789
## in505rer (a) 1.000 1.392 0.782
## in506rer 0.705 0.017 42.327 0.000 0.982 0.544
## in507rer 0.710 0.018 40.436 0.000 0.988 0.523
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## F1 ~~
## F2 0.959 0.036 26.914 0.000 0.703 0.703
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .in501rer 2.491 0.054 45.918 0.000 2.491 0.722
## .in502rer 2.581 0.069 37.432 0.000 2.581 0.592
## .in503rer 1.474 0.034 43.806 0.000 1.474 0.684
## .in504rer 1.175 0.031 37.714 0.000 1.175 0.378
## .in505rer 1.233 0.032 38.626 0.000 1.233 0.389
## .in506rer 2.287 0.045 50.713 0.000 2.287 0.704
## .in507rer 2.589 0.050 51.303 0.000 2.589 0.726
## F1 0.960 0.053 18.193 0.000 1.000 1.000
## F2 1.937 0.046 41.957 0.000 1.000 1.000
### the chi-square is a little higher than the original two-factor model, the AIC and BIC are a little higher than the original two-factor model, the CFI is a little lower than the original two-factor model but the BIC is a tad higher than the original two-factor model, and the RMSEA is a tad lower. overall, wouldn't say this is a better fit than the original two-factor model, despite gaining one degree of freedom.
# visualization of the model.
semPaths(model_2fEQ_fit)

# take a look at the expected cov matrix and the residual cov matrix.
exp_matrix_2fs <- fitted(model_2fEQ_fit)
exp_matrix_2fs
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 3.451
## in502rer 1.308 4.361
## in503rer 0.808 1.100 2.154
## in504rer 0.959 1.306 0.807 3.112
## in505rer 0.959 1.306 0.807 1.937 3.170
## in506rer 0.676 0.921 0.569 1.366 1.366 3.251
## in507rer 0.681 0.927 0.573 1.375 1.375 0.970 3.565
resid_matrix_2fs <- resid(model_2fEQ_fit)
resid_matrix_2fs
## $type
## [1] "raw"
##
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 0.000
## in502rer -0.224 0.000
## in503rer 0.059 0.113 0.000
## in504rer 0.063 0.160 -0.037 -0.134
## in505rer 0.138 -0.102 -0.209 0.103 0.141
## in506rer 0.085 -0.087 -0.161 -0.305 0.103 0.000
## in507rer 0.236 0.184 -0.006 -0.322 -0.010 0.746 0.000
two-factor model – org dropped.
# instead of equating volunteer identity and group/organization identity, i'm going to try dropping group/organization identity from the model to see if that improves fit.
model_2fD <- "
F1 = ~ in501rer + in502rer + in503rer
F2 = ~ in504rer + in506rer + in507rer"
# run the CFA and take a look at the fit indices.
model_2fD_fit <- cfa(model_2fD, data = data)
summary(model_2fD_fit, standard = TRUE, fit.measures = TRUE)
## lavaan 0.6.15 ended normally after 40 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 13
##
## Used Total
## Number of observations 6362 9688
##
## Model Test User Model:
##
## Test statistic 743.305
## Degrees of freedom 8
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 6819.818
## Degrees of freedom 15
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.892
## Tucker-Lewis Index (TLI) 0.797
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -73474.999
## Loglikelihood unrestricted model (H1) -73103.347
##
## Akaike (AIC) 146975.999
## Bayesian (BIC) 147063.854
## Sample-size adjusted Bayesian (SABIC) 147022.544
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.120
## 90 Percent confidence interval - lower 0.113
## 90 Percent confidence interval - upper 0.128
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.059
##
## 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
## F1 =~
## in501rer 1.000 0.962 0.518
## in502rer 1.389 0.050 27.765 0.000 1.336 0.640
## in503rer 0.875 0.032 26.975 0.000 0.842 0.574
## F2 =~
## in504rer 1.000 1.003 0.581
## in506rer 1.153 0.036 32.210 0.000 1.157 0.640
## in507rer 1.258 0.039 32.549 0.000 1.261 0.667
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## F1 ~~
## F2 0.681 0.030 22.625 0.000 0.706 0.706
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .in501rer 2.528 0.055 45.914 0.000 2.528 0.732
## .in502rer 2.574 0.071 36.238 0.000 2.574 0.590
## .in503rer 1.444 0.034 42.163 0.000 1.444 0.671
## .in504rer 1.971 0.045 44.057 0.000 1.971 0.662
## .in506rer 1.926 0.049 39.367 0.000 1.926 0.590
## .in507rer 1.985 0.054 36.791 0.000 1.985 0.555
## F1 0.926 0.053 17.612 0.000 1.000 1.000
## F2 1.005 0.048 20.764 0.000 1.000 1.000
### the chi-squared is smaller than the original two-factor model, although we lost 5 degrees of freedom. the AIC and BIC are lower than the original two-factor model, the CFI is a tad higher but the TLI is a bit lower than the original two-factor model; however, the CFI still does not reach the 0.95 threshold. the RMSEA is a tad lower than the original two-factor model, but still does not fall below the 0.06 threshold. this model fits a teeny bit better, but not really by all that much.
# visualization of the model.
semPaths(model_2fD_fit)

# take a look at the expected cov matrix and the residual cov matrix.
exp_matrix_2fd <- fitted(model_2fD_fit)
exp_matrix_2fd
## $cov
## in501r in502r in503r in504r in506r in507r
## in501rer 3.453
## in502rer 1.285 4.359
## in503rer 0.810 1.125 2.153
## in504rer 0.681 0.946 0.596 2.977
## in506rer 0.786 1.091 0.687 1.160 3.264
## in507rer 0.857 1.190 0.749 1.264 1.459 3.576
resid_matrix_2fd <- resid(model_2fD_fit)
resid_matrix_2fd
## $type
## [1] "raw"
##
## $cov
## in501r in502r in503r in504r in506r in507r
## in501rer 0.000
## in502rer -0.202 0.000
## in503rer 0.058 0.089 0.000
## in504rer 0.336 0.521 0.170 0.000
## in506rer -0.017 -0.247 -0.275 -0.099 0.000
## in507rer 0.064 -0.073 -0.182 -0.218 0.263 0.000
null model.
# lastly, just gonna take a look at the null model to cover all bases.
null_model <- "
in501rer ~~ in501rer
in502rer ~~ in502rer
in503rer ~~ in503rer
in504rer ~~ in504rer
in505rer ~~ in505rer
in506rer ~~ in506rer
in507rer ~~ in507rer"
# run the CFA and take a look at the fit indices.
null_model_fit <- cfa(null_model, data = data)
summary(null_model_fit, standard = TRUE, fit.measures = TRUE)
## lavaan 0.6.15 ended normally after 9 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 7
##
## Used Total
## Number of observations 6312 9688
##
## Model Test User Model:
##
## Test statistic 11105.204
## Degrees of freedom 21
## P-value (Chi-square) 0.000
##
## Model Test Baseline Model:
##
## Test statistic 11105.204
## Degrees of freedom 21
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 0.000
## Tucker-Lewis Index (TLI) 0.000
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -88628.385
## Loglikelihood unrestricted model (H1) -83075.783
##
## Akaike (AIC) 177270.770
## Bayesian (BIC) 177318.022
## Sample-size adjusted Bayesian (SABIC) 177295.778
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.289
## 90 Percent confidence interval - lower 0.285
## 90 Percent confidence interval - upper 0.294
## P-value H_0: RMSEA <= 0.050 0.000
## P-value H_0: RMSEA >= 0.080 1.000
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.300
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## in501rer 3.451 0.061 56.178 0.000 3.451 1.000
## in502rer 4.361 0.078 56.178 0.000 4.361 1.000
## in503rer 2.154 0.038 56.178 0.000 2.154 1.000
## in504rer 2.978 0.053 56.178 0.000 2.978 1.000
## in505rer 3.310 0.059 56.178 0.000 3.310 1.000
## in506rer 3.251 0.058 56.178 0.000 3.251 1.000
## in507rer 3.565 0.063 56.178 0.000 3.565 1.000
### chi-square is much higher than the original two-factor model and we lost 6 degrees of freedom. AIC and BIC are higher than the original two-factor model, CFI and TLI are 0, and RMSEA is much higher than the original two-factor model.
# visualization of the model.
semPaths(null_model_fit)

# take a look at the expected cov matrix and the residual cov matrix.
exp_matrix_null <- fitted(null_model_fit)
exp_matrix_null
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 3.451
## in502rer 0.000 4.361
## in503rer 0.000 0.000 2.154
## in504rer 0.000 0.000 0.000 2.978
## in505rer 0.000 0.000 0.000 0.000 3.310
## in506rer 0.000 0.000 0.000 0.000 0.000 3.251
## in507rer 0.000 0.000 0.000 0.000 0.000 0.000 3.565
resid_matrix_null <- resid(null_model_fit)
resid_matrix_null
## $type
## [1] "raw"
##
## $cov
## in501r in502r in503r in504r in505r in506r in507r
## in501rer 0.000
## in502rer 1.083 0.000
## in503rer 0.867 1.213 0.000
## in504rer 1.022 1.466 0.769 0.000
## in505rer 1.097 1.204 0.598 2.039 0.000
## in506rer 0.762 0.834 0.409 1.061 1.470 0.000
## in507rer 0.917 1.111 0.567 1.053 1.365 1.716 0.000