mydf3를 예제 자료로 사용하도록 하겠다. 아래는 지난 시간에 작성한, 자료를 읽어들여 전처리를 하고 하위 척도 점수를 계산하는 명령이다.library(readxl)
library(dplyr)
mydf = as.data.frame(read_excel(path = '/cloud/project/mydata.xlsx'))
names(mydf) = c('agree', 'age', 'sex', 'edu', 'marital',
paste0('bpns', 1:18),
paste0('ctq', 1:10))
mydf3 = mydf %>% filter(bpns14<=5 & ctq7<=4 & ctq9<=4 & ctq10<=4) %>%
filter(rowSums(is.na(.))==0) %>%
mutate(bpns1r = 6 - bpns1,
bpns2r = 6 - bpns2,
bpns3r = 6 - bpns3,
bpns6r = 6 - bpns6,
bpns14r = 6 - bpns14,
ctq6r = 5 - ctq6,
ctq7r = 5 - ctq7,
ctq8r = 5 - ctq8,
ctq9r = 5 - ctq9,
ctq10r = 5 - ctq10) %>%
mutate(autonomy = rowSums(select(.,bpns1r,bpns2r,bpns3r,bpns4,bpns5,bpns6r)),
competence = rowSums(select(.,bpns7:bpns12)),
related = rowSums(select(.,bpns13,bpns14r,bpns15:bpns18)),
abuse = rowSums(select(.,ctq1:ctq5)),
neglect = rowSums(select(.,ctq6r:ctq10r)))
lavaan 패키지를 사용해서 실행할 수 있다.lavaan 패키지 사용법에 대한 자세한 설명이 제시되어 있다: https://lavaan.ugent.be/tutorial/index.htmlmydf3 사용하여, 기본심리욕구 척도에 대해 아래와 같은 확인적 요인분석을 실시한다고 해보자.

확인적 요인분석은 lavaan 패키지의 cfa() 함수를 사용해서 다음과 같이 실시할 수 있다.
install.packages('lavaan')
library(lavaan)
model.cfa = 'autonomy =~ bpns1r + bpns2r + bpns3r + bpns4 + bpns5 + bpns6r
competence =~ bpns7 + bpns8 + bpns9 + bpns10 + bpns11 + bpns12
relatedness =~ bpns13 + bpns14r + bpns15 + bpns16 + bpns17 + bpns18'
fit.cfa = cfa(model = model.cfa, data = mydf3)
summary(fit.cfa, fit.measures = T, standardized = T)
# lavaan 0.6.16 ended normally after 37 iterations
#
# Estimator ML
# Optimization method NLMINB
# Number of model parameters 39
#
# Number of observations 307
#
# Model Test User Model:
#
# Test statistic 386.839
# Degrees of freedom 132
# P-value (Chi-square) 0.000
#
# Model Test Baseline Model:
#
# Test statistic 2005.130
# Degrees of freedom 153
# P-value 0.000
#
# User Model versus Baseline Model:
#
# Comparative Fit Index (CFI) 0.862
# Tucker-Lewis Index (TLI) 0.841
#
# Loglikelihood and Information Criteria:
#
# Loglikelihood user model (H0) -6166.063
# Loglikelihood unrestricted model (H1) -5972.643
#
# Akaike (AIC) 12410.126
# Bayesian (BIC) 12555.473
# Sample-size adjusted Bayesian (SABIC) 12431.782
#
# Root Mean Square Error of Approximation:
#
# RMSEA 0.079
# 90 Percent confidence interval - lower 0.070
# 90 Percent confidence interval - upper 0.089
# P-value H_0: RMSEA <= 0.050 0.000
# P-value H_0: RMSEA >= 0.080 0.460
#
# Standardized Root Mean Square Residual:
#
# SRMR 0.078
#
# 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
# Autonomy =~
# bpns1r 1.000 0.699 0.689
# bpns2r 0.947 0.089 10.592 0.000 0.662 0.715
# bpns3r 1.032 0.096 10.805 0.000 0.722 0.733
# bpns4 0.701 0.082 8.512 0.000 0.490 0.555
# bpns5 0.718 0.080 8.963 0.000 0.502 0.588
# bpns6r 0.794 0.089 8.968 0.000 0.556 0.588
# Competence =~
# bpns7 1.000 0.630 0.646
# bpns8 0.859 0.090 9.565 0.000 0.541 0.673
# bpns9 0.770 0.086 8.952 0.000 0.485 0.618
# bpns10 1.007 0.097 10.403 0.000 0.634 0.758
# bpns11 0.829 0.094 8.818 0.000 0.522 0.607
# bpns12 0.808 0.098 8.207 0.000 0.508 0.556
# Relatedness =~
# bpns13 1.000 0.526 0.627
# bpns14r 0.979 0.121 8.118 0.000 0.515 0.565
# bpns15 0.839 0.091 9.195 0.000 0.441 0.665
# bpns16 0.972 0.103 9.451 0.000 0.511 0.691
# bpns17 0.916 0.101 9.083 0.000 0.481 0.654
# bpns18 0.990 0.110 8.962 0.000 0.520 0.642
#
# Covariances:
# Estimate Std.Err z-value P(>|z|) Std.lv Std.all
# Autonomy ~~
# Competence 0.211 0.038 5.497 0.000 0.479 0.479
# Relatedness 0.197 0.034 5.817 0.000 0.536 0.536
# Competence ~~
# Relatedness 0.187 0.032 5.870 0.000 0.565 0.565
#
# Variances:
# Estimate Std.Err z-value P(>|z|) Std.lv Std.all
# .bpns1r 0.541 0.054 10.115 0.000 0.541 0.525
# .bpns2r 0.420 0.043 9.759 0.000 0.420 0.489
# .bpns3r 0.448 0.047 9.457 0.000 0.448 0.462
# .bpns4 0.539 0.048 11.278 0.000 0.539 0.692
# .bpns5 0.477 0.043 11.070 0.000 0.477 0.654
# .bpns6r 0.583 0.053 11.068 0.000 0.583 0.654
# .bpns7 0.553 0.052 10.584 0.000 0.553 0.582
# .bpns8 0.354 0.034 10.302 0.000 0.354 0.547
# .bpns9 0.380 0.035 10.835 0.000 0.380 0.618
# .bpns10 0.297 0.033 8.969 0.000 0.297 0.425
# .bpns11 0.467 0.043 10.926 0.000 0.467 0.632
# .bpns12 0.577 0.051 11.267 0.000 0.577 0.690
# .bpns13 0.426 0.040 10.702 0.000 0.426 0.606
# .bpns14r 0.565 0.051 11.175 0.000 0.565 0.681
# .bpns15 0.246 0.024 10.328 0.000 0.246 0.558
# .bpns16 0.286 0.029 10.008 0.000 0.286 0.523
# .bpns17 0.311 0.030 10.448 0.000 0.311 0.573
# .bpns18 0.387 0.037 10.567 0.000 0.387 0.588
# Autonomy 0.489 0.077 6.334 0.000 1.000 1.000
# Competence 0.396 0.068 5.834 0.000 1.000 1.000
# Relatedness 0.276 0.049 5.603 0.000 1.000 1.000
cfa() 함수의 인자는 다음과 같다.
model인자에는 측정 모형을 명세한다. 모형은 반드시 '' 안에 입력해야 한다. 모형을 입력할 때, =~ 기호는 measured by를 나타낸다. 즉, =~ 기호 앞에는 요인(factor) 혹은 잠재변수(latent variable)의 이름을, =~ 기호 뒤에는 측정 변수명을 입력한다. 동일한 요인을 측정하는 측정 변수들은 + 기호로 연결하여 변수명을 입력한다. 요인의 이름은 원하는대로 만들어서 사용하면 된다.data 인자에는 측정 변수를 포함하고 있는 데이터프레임을 입력한다.summary() 함수에 cfa()에서 반환된 결과를 입력하면 보다 자세한 결과를 볼 수 있다.
fit.measures = T와 같이 입력하면, CFI, TLI, AIC, BIC, SABIC, RMSEA, SRMR 등과 같은 다양한 적합도 지수를 출력한다.standardized = T와 같이 입력하면, 표준화된 결과를 출력한다. 출력된 결과 중 Std.lv는 잠재 변수만 표준화되었을 때의 결과를 나타내고, Std.all은 잠재 변수와 측정 변수 모두 표준화되었을 때의 결과를 나타낸다.<aside> 📎
적합도 지수
mydf3를 사용하여, 아래와 같은 구조방정식 모형을 사용한 분석을 실시한다고 해보자.

일반적으로 구조방정식은 lavaan 패키지의 sem() 함수를 사용해서 구현한다. 위의 구조방정식 모형을 사용해서 분석할 때 사용할 수 있는 명령은 다음과 같다.