步驟一: 整理資料

讀取A區資料檔案

#讀資料
data <- read.csv(file = "C:/Users/John/Documents/IRT整理檔案.csv", 
                  header = TRUE, stringsAsFactors = TRUE)
head(data)
##   A1a A1b A2a A2b A2c A2d A2e A2f A2g A2h A3a A3b A3c A3d A3e A3f A3g A3h A3i
## 1   2   5   1   1   1   2   1   1   3   1   6   7   6   5   4   3   3   4   1
## 2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## 3   6   6   3   3   3   4   2   4   4   4   7   5   7   4   5   4   2   5   7
## 4   7   7   7   7   4   7   1   7   7  NA   7   7   7   6   7   7   7   7   7
## 5   7   7   5   7   5   7   1   5   7   6   6   6   6   4   4   4   3   4   6
## 6   1   3   2   1   2   5   2   3   2   1   3   2   2   2   4   2   3   1   1
##   A3j A3k A3l A3m
## 1   3   3   3   4
## 2   1   1   1   1
## 3   7   6   6   7
## 4   7   7   7   7
## 5   5   6   7   7
## 6   3   3   2   2
print(nrow(data))
## [1] 3881

檢查是否有NA值

data |> apply(2, is.na) |> apply(2, sum)
## A1a A1b A2a A2b A2c A2d A2e A2f A2g A2h A3a A3b A3c A3d A3e A3f A3g A3h A3i A3j 
##  25  87  13  15  26  19  13  26  30  16  11  17  48  34  36  31  33  18  21  22 
## A3k A3l A3m 
##  30  24  15

#因為資料數有3881筆,資料數龐大,因此決定將有NA值得觀察值(受試者)都剔除

data2 <- na.omit(data)
head(data2)
##   A1a A1b A2a A2b A2c A2d A2e A2f A2g A2h A3a A3b A3c A3d A3e A3f A3g A3h A3i
## 1   2   5   1   1   1   2   1   1   3   1   6   7   6   5   4   3   3   4   1
## 2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## 3   6   6   3   3   3   4   2   4   4   4   7   5   7   4   5   4   2   5   7
## 5   7   7   5   7   5   7   1   5   7   6   6   6   6   4   4   4   3   4   6
## 6   1   3   2   1   2   5   2   3   2   1   3   2   2   2   4   2   3   1   1
## 7   1   1   2   2   1   1   1   1   1   1   2   2   1   2   2   1   1   2   1
##   A3j A3k A3l A3m
## 1   3   3   3   4
## 2   1   1   1   1
## 3   7   6   6   7
## 5   5   6   7   7
## 6   3   3   2   2
## 7   1   1   1   1
print(nrow(data2))
## [1] 3471

#上表與先前的原始資料比較前6筆資料,可以看出原始資料中的第5位受試者已經被剔除,至此確定資料已無NA值

#剩下3472筆資料

本組期中提案中有提到本組資料的一個困境,也就是本組所採用的資料由7點或8典李克特氏量表組成(有的題目是7點,有的題目是8點),量尺由1:Strongly agree 到 7:Very dissatisfied,而8:Do not know or does not apply

在期中提案報告這點時 陳俊宏老師 建議本組可以將8點量尺剔除,因為代表該受試者在本題沒有反應(可視為沒有作答)

因此我們將剔除填寫8點態度的受試者

# 計算含有 8 分的受試者數量
num_subjects <- sum(apply(data2, 1, function(x) any(x == 8)))
print(num_subjects)
## [1] 255

#填寫8分的受試者共有255位

library(dplyr)
## 
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
## 
##     filter, lag
## 下列物件被遮斷自 'package:base':
## 
##     intersect, setdiff, setequal, union
# 使用 dplyr 過濾含有8分的態度的受試者
data3 <- data2 %>%
  filter(if_all(everything(), ~ . != 8))

head(data3)
##   A1a A1b A2a A2b A2c A2d A2e A2f A2g A2h A3a A3b A3c A3d A3e A3f A3g A3h A3i
## 1   2   5   1   1   1   2   1   1   3   1   6   7   6   5   4   3   3   4   1
## 2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## 3   6   6   3   3   3   4   2   4   4   4   7   5   7   4   5   4   2   5   7
## 4   7   7   5   7   5   7   1   5   7   6   6   6   6   4   4   4   3   4   6
## 5   1   3   2   1   2   5   2   3   2   1   3   2   2   2   4   2   3   1   1
## 6   1   1   2   2   1   1   1   1   1   1   2   2   1   2   2   1   1   2   1
##   A3j A3k A3l A3m
## 1   3   3   3   4
## 2   1   1   1   1
## 3   7   6   6   7
## 4   5   6   7   7
## 5   3   3   2   2
## 6   1   1   1   1
print(nrow(data3))
## [1] 3216

#最終的資料共有3216筆資料

將data整理完後的資料data3重新命名為dta,方便後續使用更簡易

dta<-data3

步驟二: EFA探索性因素分析

#上過鄭中平老師的「R在行為科學之應用」,在此利用老師教授的package進行探索性因素分析
#整體設定,含載入套件
source("https://raw.githubusercontent.com/ChungPingCheng/R4BS2/main/R4BS_setup.R")
## 載入需要的套件:pacman
## 載入需要的套件:ggmirt
## 載入需要的套件:brolgar
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## 
## 載入套件:'brolgar'
## 下列物件被遮斷自 'package:insight':
## 
##     n_obs

#平行分析探索因素數

dta |>
 EFA.dimensions::RAWPAR() |> 
 purrr::pluck("eigenvalues") |> 
 as_tibble() |>  
 tidyr::pivot_longer(cols=c(2,4), 
                      names_to = "Type", 
                      values_to = "Eigen") |>
 ggplot2::ggplot() +
  aes(x=Root, y=Eigen, color=Type) +
  geom_line()+
  geom_point(size=rel(2))+
  scale_color_grey(start=.6, end=.1)+
  scale_y_continuous(name='特徵值')+
  scale_x_continuous(name='因素個數', breaks=seq(0,35,5))+
  theme(legend.position='top')


PARALLEL ANALYSIS

Randomization method: generated data

Type of correlations specified for the real data eigenvalues: Pearson

Type of correlations specified for the random data eigenvalues: pearson

Extraction Method: Principal Components

Variables = 23

Cases = 3216

Ndatasets = 100

Percentile = 95

Compare the Real Data eigenvalues below to the corresponding
random data Mean and Percentile eigenvalues
   Root   Real Data    Mean   Percentile
      1      13.470   1.153        1.173
      2       1.581   1.130        1.145
      3       1.044   1.110        1.122
      4       0.888   1.096        1.108
      5       0.794   1.082        1.094
      6       0.535   1.069        1.080
      7       0.496   1.056        1.065
      8       0.458   1.044        1.054
      9       0.425   1.033        1.042
     10       0.386   1.021        1.032
     11       0.363   1.010        1.021
     12       0.317   0.998        1.007
     13       0.296   0.988        0.997
     14       0.283   0.976        0.986
     15       0.268   0.964        0.972
     16       0.240   0.953        0.963
     17       0.232   0.942        0.952
     18       0.215   0.931        0.941
     19       0.207   0.919        0.931
     20       0.161   0.905        0.916
     21       0.131   0.891        0.903
     22       0.126   0.875        0.889
     23       0.084   0.855        0.873

The number of factors according to the parallel analysis = 2

#利用探索性分析的特徵值曲線,可知A資料集有2個因素

#設定因素數為 2
fit <- 
  dta |> lavaan::efa(data=_,nfactors = 2) 
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.162850e-21) 
   is close to zero. This may be a symptom that the model is not identified.
fit

        f1      f2 
A1a  0.636*      .*
A1b       *  0.816*
A2a  0.871*        
A2b  0.560*      .*
A2c  0.540*      .*
A2d  0.458*  0.320*
A2e  0.816*        
A2f  0.370*  0.480*
A2g  0.339*  0.483*
A2h      .*  0.509*
A3a       *  0.952*
A3b       *  0.927*
A3c       *  0.926*
A3d          0.695*
A3e          0.733*
A3f          0.696*
A3g          0.561*
A3h       *  0.812*
A3i      .*  0.651*
A3j      .*  0.827*
A3k      .*  0.803*
A3l      .*  0.729*
A3m          0.893*
library(psych)
# 進行探索性因素分析(EFA)
# 根據特徵圖提取2個因素
efa_result <- fa(data, nfactors = 2, rotate = "varimax")
# 顯示結果
print(efa_result)
Factor Analysis using method =  minres
Call: fa(r = data, nfactors = 2, rotate = "varimax")
Standardized loadings (pattern matrix) based upon correlation matrix
     MR1  MR2   h2   u2 com
A1a 0.38 0.68 0.61 0.39 1.6
A1b 0.74 0.42 0.72 0.28 1.6
A2a 0.25 0.81 0.72 0.28 1.2
A2b 0.29 0.64 0.50 0.50 1.4
A2c 0.31 0.63 0.49 0.51 1.5
A2d 0.39 0.59 0.50 0.50 1.7
A2e 0.24 0.74 0.60 0.40 1.2
A2f 0.54 0.55 0.59 0.41 2.0
A2g 0.53 0.49 0.53 0.47 2.0
A2h 0.52 0.42 0.44 0.56 1.9
A3a 0.80 0.33 0.75 0.25 1.3
A3b 0.77 0.33 0.69 0.31 1.4
A3c 0.78 0.34 0.73 0.27 1.4
A3d 0.59 0.27 0.41 0.59 1.4
A3e 0.63 0.30 0.48 0.52 1.4
A3f 0.67 0.24 0.51 0.49 1.2
A3g 0.54 0.19 0.32 0.68 1.2
A3h 0.75 0.24 0.62 0.38 1.2
A3i 0.64 0.38 0.55 0.45 1.6
A3j 0.78 0.45 0.80 0.20 1.6
A3k 0.76 0.46 0.79 0.21 1.6
A3l 0.70 0.42 0.66 0.34 1.6
A3m 0.81 0.39 0.80 0.20 1.4

                       MR1  MR2
SS loadings           8.58 5.25
Proportion Var        0.37 0.23
Cumulative Var        0.37 0.60
Proportion Explained  0.62 0.38
Cumulative Proportion 0.62 1.00

Mean item complexity =  1.5
Test of the hypothesis that 2 factors are sufficient.

df null model =  253  with the objective function =  20.12 with Chi Square =  77876
df of  the model are 208  and the objective function was  2.69 

The root mean square of the residuals (RMSR) is  0.04 
The df corrected root mean square of the residuals is  0.05 

The harmonic n.obs is  3834 with the empirical chi square  3842  with prob <  0 
The total n.obs was  3881  with Likelihood Chi Square =  10397  with prob <  0 

Tucker Lewis Index of factoring reliability =  0.84
RMSEA index =  0.112  and the 90 % confidence intervals are  0.111 0.114
BIC =  8678
Fit based upon off diagonal values = 0.99
Measures of factor score adequacy             
                                                   MR1  MR2
Correlation of (regression) scores with factors   0.95 0.91
Multiple R square of scores with factors          0.91 0.84
Minimum correlation of possible factor scores     0.81 0.67
# 查看載荷矩陣(factor loadings)
efa_result$loadings

Loadings:
    MR1   MR2  
A1a 0.376 0.684
A1b 0.736 0.419
A2a 0.249 0.811
A2b 0.293 0.641
A2c 0.309 0.627
A2d 0.388 0.594
A2e 0.243 0.737
A2f 0.538 0.550
A2g 0.532 0.492
A2h 0.519 0.418
A3a 0.798 0.331
A3b 0.765 0.329
A3c 0.784 0.338
A3d 0.586 0.266
A3e 0.625 0.303
A3f 0.674 0.237
A3g 0.538 0.187
A3h 0.748 0.241
A3i 0.638 0.381
A3j 0.776 0.447
A3k 0.763 0.457
A3l 0.695 0.421
A3m 0.806 0.390

                 MR1   MR2
SS loadings    8.576 5.249
Proportion Var 0.373 0.228
Cumulative Var 0.373 0.601
# 查看每個變數的共同性(communality)
efa_result$communality
   A1a    A1b    A2a    A2b    A2c    A2d    A2e    A2f    A2g    A2h    A3a 
0.6091 0.7176 0.7207 0.4972 0.4886 0.5033 0.6026 0.5921 0.5252 0.4437 0.7466 
   A3b    A3c    A3d    A3e    A3f    A3g    A3h    A3i    A3j    A3k    A3l 
0.6942 0.7287 0.4141 0.4828 0.5107 0.3240 0.6175 0.5517 0.8017 0.7913 0.6605 
   A3m 
0.8015 
# 查看各因素的解釋變異(variance explained)
efa_result$Vaccounted
MR1 MR2
SS loadings 8.5764 5.2489
Proportion Var 0.3729 0.2282
Cumulative Var 0.3729 0.6011
Proportion Explained 0.6203 0.3797
Cumulative Proportion 0.6203 1.0000
# 可視化因素負載
fa.diagram(efa_result)

步驟三:計算內部一致性

alpha_result <- alpha(dta)
print(alpha_result)

Reliability analysis   
Call: alpha(x = dta)

  raw_alpha std.alpha G6(smc) average_r S/N     ase mean  sd median_r
      0.97      0.97    0.98      0.56  29 0.00081    3 1.4     0.55

    95% confidence boundaries 
         lower alpha upper
Feldt     0.97  0.97  0.97
Duhachek  0.97  0.97  0.97

 Reliability if an item is dropped:
    raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r med.r
A1a      0.97      0.97    0.97      0.56  28  0.00084 0.014  0.55
A1b      0.96      0.96    0.97      0.55  27  0.00087 0.013  0.54
A2a      0.97      0.97    0.97      0.56  28  0.00083 0.014  0.55
A2b      0.97      0.97    0.97      0.57  29  0.00082 0.014  0.56
A2c      0.97      0.97    0.97      0.57  29  0.00083 0.014  0.56
A2d      0.97      0.97    0.98      0.56  28  0.00083 0.014  0.55
A2e      0.97      0.97    0.97      0.57  29  0.00082 0.013  0.56
A2f      0.97      0.97    0.97      0.56  28  0.00085 0.014  0.54
A2g      0.97      0.97    0.97      0.56  28  0.00085 0.014  0.55
A2h      0.97      0.97    0.98      0.56  29  0.00083 0.014  0.56
A3a      0.96      0.96    0.97      0.55  27  0.00087 0.013  0.55
A3b      0.97      0.96    0.97      0.55  27  0.00087 0.013  0.55
A3c      0.96      0.96    0.97      0.55  27  0.00087 0.013  0.55
A3d      0.97      0.97    0.98      0.57  29  0.00083 0.014  0.56
A3e      0.97      0.97    0.97      0.56  28  0.00085 0.014  0.55
A3f      0.97      0.97    0.97      0.56  28  0.00083 0.014  0.55
A3g      0.97      0.97    0.97      0.57  29  0.00081 0.012  0.56
A3h      0.97      0.97    0.97      0.56  28  0.00085 0.014  0.55
A3i      0.97      0.97    0.97      0.56  28  0.00084 0.014  0.55
A3j      0.96      0.96    0.97      0.55  27  0.00088 0.013  0.54
A3k      0.96      0.96    0.97      0.55  27  0.00088 0.013  0.54
A3l      0.97      0.96    0.97      0.55  27  0.00086 0.013  0.55
A3m      0.96      0.96    0.97      0.55  27  0.00088 0.013  0.54

 Item statistics 
       n raw.r std.r r.cor r.drop mean  sd
A1a 3216  0.72  0.73  0.71   0.69  2.5 1.6
A1b 3216  0.85  0.84  0.84   0.83  3.1 1.9
A2a 3216  0.70  0.71  0.70   0.67  2.4 1.6
A2b 3216  0.64  0.66  0.64   0.61  2.2 1.5
A2c 3216  0.64  0.66  0.64   0.61  1.9 1.3
A2d 3216  0.71  0.71  0.70   0.68  2.6 1.8
A2e 3216  0.66  0.67  0.65   0.63  2.6 1.8
A2f 3216  0.79  0.79  0.78   0.76  3.3 1.9
A2g 3216  0.77  0.76  0.75   0.74  3.9 2.1
A2h 3216  0.69  0.69  0.67   0.66  2.4 1.5
A3a 3216  0.85  0.85  0.85   0.83  3.5 2.0
A3b 3216  0.83  0.82  0.82   0.81  3.9 2.0
A3c 3216  0.85  0.85  0.84   0.83  3.6 2.0
A3d 3216  0.68  0.68  0.66   0.65  3.4 1.8
A3e 3216  0.77  0.76  0.75   0.74  3.7 1.9
A3f 3216  0.72  0.72  0.71   0.69  3.0 1.8
A3g 3216  0.61  0.61  0.60   0.58  2.6 1.7
A3h 3216  0.77  0.77  0.76   0.75  3.0 1.8
A3i 3216  0.76  0.76  0.75   0.73  3.1 1.8
A3j 3216  0.89  0.89  0.89   0.88  3.5 1.8
A3k 3216  0.89  0.88  0.89   0.87  3.4 1.8
A3l 3216  0.82  0.82  0.82   0.80  2.9 1.7
A3m 3216  0.88  0.88  0.88   0.87  3.3 1.9

Non missing response frequency for each item
       1    2    3    4    5    6    7 miss
A1a 0.27 0.40 0.16 0.04 0.05 0.05 0.03    0
A1b 0.20 0.31 0.16 0.06 0.10 0.08 0.08    0
A2a 0.33 0.34 0.16 0.03 0.04 0.05 0.04    0
A2b 0.41 0.33 0.11 0.03 0.04 0.04 0.03    0
A2c 0.47 0.33 0.11 0.04 0.03 0.02 0.02    0
A2d 0.30 0.33 0.15 0.04 0.06 0.05 0.06    0
A2e 0.31 0.30 0.16 0.06 0.06 0.05 0.06    0
A2f 0.19 0.25 0.21 0.09 0.08 0.07 0.10    0
A2g 0.13 0.19 0.16 0.12 0.11 0.10 0.18    0
A2h 0.38 0.26 0.13 0.14 0.03 0.03 0.03    0
A3a 0.14 0.27 0.19 0.08 0.10 0.10 0.12    0
A3b 0.09 0.22 0.20 0.09 0.14 0.11 0.15    0
A3c 0.14 0.24 0.18 0.11 0.10 0.10 0.13    0
A3d 0.14 0.25 0.22 0.13 0.10 0.08 0.08    0
A3e 0.12 0.22 0.16 0.16 0.12 0.09 0.13    0
A3f 0.24 0.28 0.18 0.09 0.08 0.06 0.07    0
A3g 0.30 0.29 0.18 0.06 0.06 0.05 0.06    0
A3h 0.24 0.26 0.18 0.12 0.07 0.06 0.07    0
A3i 0.21 0.24 0.19 0.17 0.06 0.06 0.07    0
A3j 0.14 0.22 0.22 0.15 0.11 0.07 0.09    0
A3k 0.15 0.23 0.20 0.15 0.10 0.07 0.09    0
A3l 0.27 0.24 0.17 0.17 0.05 0.05 0.06    0
A3m 0.20 0.22 0.17 0.16 0.08 0.07 0.10    0

#Cronbach’s Alpha=0.9672

步驟四:CFA驗證性因素分析

model <- '
  #latent variable
   
   Approval_of_the_Environment_and_Workplace_Culture=~ A3m + A3a + A3c + A3j + A3b + A3k + A3h + A1b + A3l + A3f + A3i + A3e + A3d + A3g + A2h + A2g
  
  Organizational_Commitment_and_Satisfaction =~ A2a+ A2e + A1a + A2b + A2c + A2d +A2f
  #residual covariances
  A3f ~~ A3g
  A3j ~~ A3k
  A3a ~~ A3b
  A3a ~~ A3c
  A2a ~~ A2e
  A3c ~~ A3b
  A3l ~~ A2h
  A3h ~~ A3f
  A3h ~~ A3g
'

#跑CFA

library(lavaan)
fit <- cfa(model, data = dta)

#檢視結果

summary(fit, fit.measures = TRUE, standardized = TRUE)
lavaan 0.6-19 ended normally after 48 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        56

  Number of observations                          3216

Model Test User Model:
                                                      
  Test statistic                              4543.836
  Degrees of freedom                               220
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                             69219.455
  Degrees of freedom                               253
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.937
  Tucker-Lewis Index (TLI)                       0.928

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)            -114905.246
  Loglikelihood unrestricted model (H1)    -112633.328
                                                      
  Akaike (AIC)                              229922.492
  Bayesian (BIC)                            230262.742
  Sample-size adjusted Bayesian (SABIC)     230084.805

Root Mean Square Error of Approximation:

  RMSEA                                          0.078
  90 Percent confidence interval - lower         0.076
  90 Percent confidence interval - upper         0.080
  P-value H_0: RMSEA <= 0.050                    0.000
  P-value H_0: RMSEA >= 0.080                    0.066

Standardized Root Mean Square Residual:

  SRMR                                           0.042

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                                                       Estimate  Std.Err
  Approval_of_the_Environment_and_Workplace_Culture =~                  
    A3m                                                   1.000         
    A3a                                                   0.957    0.013
    A3c                                                   0.957    0.013
    A3j                                                   0.929    0.011
    A3b                                                   0.917    0.014
    A3k                                                   0.933    0.011
    A3h                                                   0.779    0.014
    A1b                                                   0.916    0.012
    A3l                                                   0.829    0.012
    A3f                                                   0.710    0.015
    A3i                                                   0.774    0.013
    A3e                                                   0.834    0.015
    A3d                                                   0.679    0.015
    A3g                                                   0.546    0.015
    A2h                                                   0.587    0.013
    A2g                                                   0.859    0.016
  Organizational_Commitment_and_Satisfaction =~                         
    A2a                                                   1.000         
    A2e                                                   0.981    0.018
    A1a                                                   0.957    0.021
    A2b                                                   0.872    0.020
    A2c                                                   0.727    0.017
    A2d                                                   1.049    0.024
    A2f                                                   1.178    0.026
  z-value  P(>|z|)   Std.lv  Std.all
                                    
                      1.754    0.913
   73.554    0.000    1.678    0.855
   71.812    0.000    1.679    0.847
   84.534    0.000    1.630    0.903
   67.170    0.000    1.609    0.822
   82.064    0.000    1.637    0.893
   57.349    0.000    1.367    0.759
   73.800    0.000    1.607    0.856
   70.720    0.000    1.454    0.841
   47.624    0.000    1.246    0.680
   58.406    0.000    1.358    0.766
   57.157    0.000    1.463    0.757
   45.819    0.000    1.190    0.663
   35.252    0.000    0.958    0.551
   46.195    0.000    1.030    0.667
   53.091    0.000    1.508    0.726
                                    
                      1.263    0.776
   55.388    0.000    1.238    0.706
   46.274    0.000    1.209    0.776
   43.056    0.000    1.100    0.730
   42.188    0.000    0.918    0.718
   44.587    0.000    1.325    0.752
   46.137    0.000    1.488    0.774

Covariances:
                                                       Estimate  Std.Err
 .A3f ~~                                                                
   .A3g                                                   1.246    0.042
 .A3j ~~                                                                
   .A3k                                                   0.351    0.016
 .A3a ~~                                                                
   .A3b                                                   0.550    0.025
   .A3c                                                   0.517    0.024
 .A2a ~~                                                                
   .A2e                                                   0.565    0.029
 .A3c ~~                                                                
   .A3b                                                   0.538    0.026
 .A3l ~~                                                                
   .A2h                                                   0.404    0.022
 .A3h ~~                                                                
   .A3f                                                   0.636    0.032
   .A3g                                                   0.681    0.034
  Approval_of_the_Environment_and_Workplace_Culture ~~                  
    Orgnztnl_Cm__S                                        1.857    0.061
  z-value  P(>|z|)   Std.lv  Std.all
                                    
   29.730    0.000    1.246    0.640
                                    
   22.173    0.000    0.351    0.549
                                    
   22.024    0.000    0.550    0.485
   21.693    0.000    0.517    0.483
                                    
   19.537    0.000    0.565    0.445
                                    
   21.068    0.000    0.538    0.457
                                    
   18.538    0.000    0.404    0.375
                                    
   20.178    0.000    0.636    0.404
   20.267    0.000    0.681    0.400
                                    
   30.591    0.000    0.838    0.838

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .A3m               0.611    0.019   32.650    0.000    0.611    0.166
   .A3a               1.033    0.029   35.925    0.000    1.033    0.268
   .A3c               1.113    0.031   36.194    0.000    1.113    0.283
   .A3j               0.603    0.018   33.351    0.000    0.603    0.185
   .A3b               1.245    0.034   36.837    0.000    1.245    0.325
   .A3k               0.679    0.020   33.950    0.000    0.679    0.202
   .A3h               1.379    0.036   38.094    0.000    1.379    0.425
   .A1b               0.940    0.026   36.056    0.000    0.940    0.267
   .A3l               0.877    0.024   36.539    0.000    0.877    0.293
   .A3f               1.803    0.046   38.804    0.000    1.803    0.537
   .A3i               1.299    0.034   38.018    0.000    1.299    0.413
   .A3e               1.594    0.042   38.131    0.000    1.594    0.427
   .A3d               1.804    0.046   38.950    0.000    1.804    0.560
   .A3g               2.099    0.053   39.432    0.000    2.099    0.696
   .A2h               1.321    0.034   38.846    0.000    1.321    0.555
   .A2g               2.036    0.053   38.464    0.000    2.036    0.472
   .A2a               1.050    0.031   34.008    0.000    1.050    0.397
   .A2e               1.539    0.043   35.901    0.000    1.539    0.501
   .A1a               0.965    0.028   34.170    0.000    0.965    0.398
   .A2b               1.059    0.030   35.640    0.000    1.059    0.467
   .A2c               0.792    0.022   35.957    0.000    0.792    0.485
   .A2d               1.346    0.038   35.006    0.000    1.346    0.434
   .A2f               1.479    0.043   34.244    0.000    1.479    0.401
    Apprv___E__W_C    3.078    0.091   33.685    0.000    1.000    1.000
    Orgnztnl_Cm__S    1.594    0.063   25.429    0.000    1.000    1.000
print(fit)
lavaan 0.6-19 ended normally after 48 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        56

  Number of observations                          3216

Model Test User Model:
                                                      
  Test statistic                              4543.836
  Degrees of freedom                               220
  P-value (Chi-square)                           0.000

#檢查看是否有修改指數 (如有需要會加入residual covariance)

modificationIndices(fit, maximum.number = 10, sort=TRUE, op="~~")
lhs op rhs mi epc sepc.lv sepc.all sepc.nox
320 A2b ~~ A2c 388.3 0.3652 0.3652 0.3985 0.3985
89 A3m ~~ A3l 275.8 0.2339 0.2339 0.3195 0.3195
225 A3l ~~ A3i 196.7 0.2614 0.2614 0.2449 0.2449
305 A2g ~~ A2f 187.7 0.4591 0.4591 0.2646 0.2646
107 A3a ~~ A1b 145.5 0.1863 0.1863 0.1891 0.1891
219 A1b ~~ A1a 139.9 0.2247 0.2247 0.2360 0.2360
264 A3e ~~ A2g 139.3 0.3940 0.3940 0.2187 0.2187
300 A2g ~~ A2e 132.6 0.3323 0.3323 0.1877 0.1877

#載入圖表套件

library(semPlot)

#將CFA模型給視覺化

semPaths(fit, "std", layout = "circle", title = FALSE)

#步驟7: ANOVA

#載入套件

library(lavaan)

#編制模型

model <- 
'
# 第一個淺在因子(對職場環境與文化的認可)
  LatentVar1 =~ A3m + A3a + A3c + A3j + A3b + A3k + A3h + A1b + A3l + A3f + A3i + A3e + A3d + A3g + A2h + A2g
  
#  第二個淺在因子(組職投入與滿意)
  LatentVar2 =~ A2a+ A2e + A1a + A2b + A2c + A2d +A2f

'

#萃取Factor Score ltnv2 ~ ltnv1

CFAfit <- cfa(model, estimator="MLR", data = dta)

factor_scores <- lavPredict(CFAfit)
#把滿意度分成低與高
dta$group <- ifelse(dta$A1a <= 3, "Low", "High")
dta$aov <- as.factor(dta$group)
combined_df <- cbind(factor_scores, dta)

#跑ANOVA

anova1<- aov(LatentVar1~group, data =combined_df)
anova2<- aov(LatentVar2~group, data =combined_df)

summary(anova1)
              Df Sum Sq Mean Sq F value Pr(>F)
group          1   2593    2593    1213 <2e-16
Residuals   3214   6870       2               
summary(anova2)
              Df Sum Sq Mean Sq F value Pr(>F)
group          1   2342    2342    2643 <2e-16
Residuals   3214   2847       1