## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## This is lcsm 0.3.2
## 
## Please report any issues or ideas at:
## 
## https://github.com/milanwiedemann/lcsm/issues
## 
## 
## 
## 
## Attaching package: 'psych'
## 
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following objects are masked from 'package:psych':
## 
##     alpha, rescale
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## 
## 
## This is lavaan 0.6-16
## lavaan is FREE software! Please report any bugs.
## 
## 
## Attaching package: 'lavaan'
## 
## 
## The following object is masked from 'package:psych':
## 
##     cor2cov
## 
## 
## Loading required package: grid
## 
## Loading required package: viridisLite
## 
## 
## Attaching package: 'viridis'
## 
## 
## The following object is masked from 'package:scales':
## 
##     viridis_pal
# Read the data
covid <- read.csv ("/Users/tx321/Desktop/Dr. Sun's Lab/Media_COVID_paper/data/used/covid_2023-02-16.csv")

# Make sure the survey1 to survey 13 is date

covid <- covid %>% mutate_at(vars(survey1:survey13), as.Date ,format="%Y-%m-%d")

media_factor <- covid %>% select(participant_id,starts_with("ccvmdef"),survey1:survey13)

# Load the selected columns for FA ---------

media_factor <- covid %>% select(participant_id,starts_with("ccvmdef"),survey1:survey13)

# reverse coded item 1, 11, 12
media_factor <- media_factor %>%   
  dplyr::mutate(dplyr::across(c(ccvmdef0101,ccvmdef0102, ccvmdef0103, ccvmdef0104, ccvmdef0105, ccvmdef0106, ccvmdef0107, ccvmdef0108, ccvmdef0109, ccvmdef0110, ccvmdef0111, ccvmdef0112,ccvmdef0113, ccvmdef1101,ccvmdef1102, ccvmdef1103, ccvmdef1104, ccvmdef1105, ccvmdef1106, ccvmdef1107, ccvmdef1108, ccvmdef1109, ccvmdef1110, ccvmdef1111, ccvmdef1112,ccvmdef1113, ccvmdef1201,ccvmdef1202, ccvmdef1203, ccvmdef1204, ccvmdef1205, ccvmdef1206, ccvmdef1207, ccvmdef1208, ccvmdef1209, ccvmdef1210, ccvmdef1211, ccvmdef1212,ccvmdef1213), ~ 100 -.))

# only get the baseline data

media_factor_base <- media_factor %>% select(ccvmdef0101:ccvmdef1201)

# drop all NAs, N= 138

media_factor_analysis <- media_factor_base[rowSums(is.na(media_factor_base)) != 12, ]

# Principal Component Analysis ------------
  
# Bartlett's Test of Sphericity
# uses all non-missing pairwise values.
bart_spher(media_factor_analysis, use = c("pairwise.complete.obs")) # check if we should do factor analysis, p value should be significant 
##  Bartlett's Test of Sphericity
## 
## Call: bart_spher(x = media_factor_analysis, use = c("pairwise.complete.obs"))
## 
##      X2 = 548.449
##      df = 66
## p-value < 2.22e-16
## Warning: Used n = 136.92.
# KMO test 

KMO(media_factor_analysis) # want it above 0.7
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = media_factor_analysis)
## Overall MSA =  0.72
## MSA for each item = 
## ccvmdef0101 ccvmdef0201 ccvmdef0301 ccvmdef0401 ccvmdef0501 ccvmdef0601 
##        0.70        0.69        0.66        0.80        0.82        0.77 
## ccvmdef0701 ccvmdef0801 ccvmdef0901 ccvmdef1001 ccvmdef1101 ccvmdef1201 
##        0.73        0.74        0.80        0.84        0.50        0.57
media_factor_analysis <- na.omit(media_factor_analysis)

media.pca <- princomp(media_factor_analysis, cor = TRUE, score = T)

summary(media.pca) # print variance accounted for (how many PCs we need: cumulative proportion over 85%, or don't consider the components from the point of the elbow curve)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3     Comp.4     Comp.5
## Standard deviation     1.9289574 1.3506997 1.2408665 1.01593410 0.89902301
## Proportion of Variance 0.3100731 0.1520325 0.1283125 0.08601017 0.06735353
## Cumulative Proportion  0.3100731 0.4621055 0.5904180 0.67642818 0.74378171
##                            Comp.6     Comp.7     Comp.8     Comp.9    Comp.10
## Standard deviation     0.84813706 0.79606136 0.71026825 0.62370723 0.57366564
## Proportion of Variance 0.05994471 0.05280947 0.04204008 0.03241756 0.02742436
## Cumulative Proportion  0.80372641 0.85653589 0.89857597 0.93099353 0.95841788
##                           Comp.11    Comp.12
## Standard deviation     0.51351484 0.48506484
## Proportion of Variance 0.02197479 0.01960732
## Cumulative Proportion  0.98039268 1.00000000
loadings(media.pca) # pc loadings 
## 
## Loadings:
##             Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9
## ccvmdef0101         0.357         0.684  0.205  0.554  0.193              
## ccvmdef0201  0.309 -0.146 -0.443 -0.242  0.132  0.244        -0.304       
## ccvmdef0301  0.334        -0.371 -0.256         0.467 -0.183        -0.206
## ccvmdef0401  0.330        -0.321               -0.263  0.617  0.353  0.164
## ccvmdef0501  0.306  0.242 -0.247  0.205 -0.143 -0.242 -0.595  0.228  0.488
## ccvmdef0601  0.349  0.173         0.317  0.117 -0.496        -0.324 -0.529
## ccvmdef0701  0.362         0.408 -0.188  0.203         0.168  0.247       
## ccvmdef0801  0.357         0.430 -0.185  0.160                0.178       
## ccvmdef0901  0.383         0.336                0.114 -0.151 -0.442       
## ccvmdef1001  0.232 -0.176  0.167  0.185 -0.873         0.212              
## ccvmdef1101        -0.620         0.249  0.236               -0.343  0.461
## ccvmdef1201        -0.579         0.305               -0.297  0.455 -0.424
##             Comp.10 Comp.11 Comp.12
## ccvmdef0101  0.110                 
## ccvmdef0201  0.494   0.371  -0.258 
## ccvmdef0301 -0.289  -0.429   0.333 
## ccvmdef0401 -0.382          -0.155 
## ccvmdef0501          0.116         
## ccvmdef0601  0.172  -0.238         
## ccvmdef0701  0.283   0.215   0.635 
## ccvmdef0801  0.176  -0.463  -0.569 
## ccvmdef0901 -0.581   0.397  -0.104 
## ccvmdef1001  0.168                 
## ccvmdef1101         -0.372   0.165 
## ccvmdef1201          0.220  -0.149 
## 
##                Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9
## SS loadings     1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000
## Proportion Var  0.083  0.083  0.083  0.083  0.083  0.083  0.083  0.083  0.083
## Cumulative Var  0.083  0.167  0.250  0.333  0.417  0.500  0.583  0.667  0.750
##                Comp.10 Comp.11 Comp.12
## SS loadings      1.000   1.000   1.000
## Proportion Var   0.083   0.083   0.083
## Cumulative Var   0.833   0.917   1.000
plot(media.pca,type="lines") # scree plot

biplot(media.pca) 

# Exploratory Factor Analysis ------------

# step 1: decide how many factors we need
media.efa <- principal(media_factor_analysis, nfactors = 12, rotate ="none")

print(media.efa, digits=2, cutoff=.3, sort=TRUE)
## Principal Components Analysis
## Call: principal(r = media_factor_analysis, nfactors = 12, rotate = "none")
## Standardized loadings (pattern matrix) based upon correlation matrix
##             item  PC1   PC2   PC3   PC4   PC5   PC6   PC7   PC8   PC9  PC10
## ccvmdef0901    9 0.74 -0.03 -0.42  0.03  0.05  0.10 -0.12 -0.31  0.01 -0.33
## ccvmdef0701    7 0.70  0.03 -0.51 -0.19  0.18 -0.02  0.13  0.18  0.05  0.16
## ccvmdef0801    8 0.69 -0.12 -0.53 -0.19  0.14  0.08 -0.03  0.13  0.06  0.10
## ccvmdef0601    6 0.67 -0.23  0.10  0.32  0.11 -0.42 -0.01 -0.23 -0.33  0.10
## ccvmdef0301    3 0.64 -0.02  0.46 -0.26 -0.07  0.40 -0.15  0.05 -0.13 -0.17
## ccvmdef0401    4 0.64  0.08  0.40  0.08  0.06 -0.22  0.49  0.25  0.10 -0.22
## ccvmdef0201    2 0.60  0.20  0.55 -0.25  0.12  0.21  0.08 -0.22  0.01  0.28
## ccvmdef0501    5 0.59 -0.33  0.31  0.21 -0.13 -0.21 -0.47  0.16  0.30  0.05
## ccvmdef1101   11 0.06  0.84  0.01  0.25  0.21 -0.03 -0.01 -0.24  0.29  0.00
## ccvmdef1201   12 0.17  0.78 -0.04  0.31  0.08  0.08 -0.24  0.32 -0.26  0.00
## ccvmdef0101    1 0.01 -0.48 -0.02  0.69  0.18  0.47  0.15  0.03  0.04  0.06
## ccvmdef1001   10 0.45  0.24 -0.21  0.19 -0.78  0.08  0.17 -0.06  0.01  0.10
##              PC11  PC12 h2       u2 com
## ccvmdef0901 -0.20 -0.05  1 -1.1e-15 2.8
## ccvmdef0701 -0.11  0.31  1  0.0e+00 3.1
## ccvmdef0801  0.24 -0.28  1 -2.2e-16 3.1
## ccvmdef0601  0.12  0.05  1  4.4e-16 3.8
## ccvmdef0301  0.22  0.16  1  3.3e-16 4.0
## ccvmdef0401 -0.02 -0.08  1 -4.4e-16 3.9
## ccvmdef0201 -0.19 -0.13  1 -2.2e-16 4.2
## ccvmdef0501 -0.06  0.01  1  5.6e-16 4.9
## ccvmdef1101  0.19  0.08  1  4.4e-16 2.0
## ccvmdef1201 -0.11 -0.07  1  8.9e-16 2.5
## ccvmdef0101  0.00  0.02  1  2.2e-16 3.0
## ccvmdef1001  0.02  0.00  1 -6.7e-16 2.3
## 
##                        PC1  PC2  PC3  PC4  PC5  PC6  PC7  PC8  PC9 PC10 PC11
## SS loadings           3.72 1.82 1.54 1.03 0.81 0.72 0.63 0.50 0.39 0.33 0.26
## Proportion Var        0.31 0.15 0.13 0.09 0.07 0.06 0.05 0.04 0.03 0.03 0.02
## Cumulative Var        0.31 0.46 0.59 0.68 0.74 0.80 0.86 0.90 0.93 0.96 0.98
## Proportion Explained  0.31 0.15 0.13 0.09 0.07 0.06 0.05 0.04 0.03 0.03 0.02
## Cumulative Proportion 0.31 0.46 0.59 0.68 0.74 0.80 0.86 0.90 0.93 0.96 0.98
##                       PC12
## SS loadings           0.24
## Proportion Var        0.02
## Cumulative Var        1.00
## Proportion Explained  0.02
## Cumulative Proportion 1.00
## 
## Mean item complexity =  3.3
## Test of the hypothesis that 12 components are sufficient.
## 
## The root mean square of the residuals (RMSR) is  0 
##  with the empirical chi square  0  with prob <  NA 
##  0.3
## Fit based upon off diagonal values = 1
print(media.efa$values)
##  [1] 3.7208768 1.8243896 1.5397496 1.0321221 0.8082424 0.7193365 0.6337137
##  [8] 0.5044810 0.3890107 0.3290923 0.2636975 0.2352879
# 4 components have eigenvalues greater than 1, suggesting that we extract 4 components.

plot(media.efa$values, type = "b",xlab = "Factors", ylab = "Eigen values", main = "SCREE PLOT") 

# Factor analysis using the factanal method

media_factanal <- factanal(media_factor_analysis, factors=4, scores = c("regression"), rotation = "varimax")

print(media_factanal)
## 
## Call:
## factanal(x = media_factor_analysis, factors = 4, scores = c("regression"),     rotation = "varimax")
## 
## Uniquenesses:
## ccvmdef0101 ccvmdef0201 ccvmdef0301 ccvmdef0401 ccvmdef0501 ccvmdef0601 
##       0.907       0.376       0.268       0.592       0.601       0.131 
## ccvmdef0701 ccvmdef0801 ccvmdef0901 ccvmdef1001 ccvmdef1101 ccvmdef1201 
##       0.308       0.214       0.417       0.856       0.108       0.614 
## 
## Loadings:
##             Factor1 Factor2 Factor3 Factor4
## ccvmdef0101                 -0.207   0.213 
## ccvmdef0201  0.108   0.756   0.192         
## ccvmdef0301  0.194   0.831                 
## ccvmdef0401  0.187   0.512   0.128   0.307 
## ccvmdef0501  0.186   0.431  -0.154   0.394 
## ccvmdef0601  0.305   0.284           0.834 
## ccvmdef0701  0.820   0.116                 
## ccvmdef0801  0.876   0.106                 
## ccvmdef0901  0.705   0.191           0.210 
## ccvmdef1001  0.309   0.149   0.145         
## ccvmdef1101                  0.944         
## ccvmdef1201                  0.609         
## 
##                Factor1 Factor2 Factor3 Factor4
## SS loadings      2.253   1.882   1.419   1.054
## Proportion Var   0.188   0.157   0.118   0.088
## Cumulative Var   0.188   0.345   0.463   0.551
## 
## Test of the hypothesis that 4 factors are sufficient.
## The chi square statistic is 25.37 on 24 degrees of freedom.
## The p-value is 0.386
# Factor analysis using fa method
fa.none <- fa(r=media_factor_analysis, 
 nfactors = 4, 
 # covar = FALSE, SMC = TRUE,
 fm="pa", # type of factor analysis we want to use (“pa” is principal axis factoring)
 max.iter = 100, # (50 is the default, but we have changed it to 100
 rotate="varimax")

print(fa.none)
## Factor Analysis using method =  pa
## Call: fa(r = media_factor_analysis, nfactors = 4, rotate = "varimax", 
##     max.iter = 100, fm = "pa")
## Standardized loadings (pattern matrix) based upon correlation matrix
##               PA1   PA3   PA2   PA4   h2   u2 com
## ccvmdef0101 -0.02 -0.09 -0.20  0.29 0.13 0.87 2.0
## ccvmdef0201  0.10  0.82  0.16  0.00 0.71 0.29 1.1
## ccvmdef0301  0.20  0.76 -0.05  0.05 0.62 0.38 1.2
## ccvmdef0401  0.18  0.53  0.15  0.30 0.42 0.58 2.1
## ccvmdef0501  0.18  0.40 -0.12  0.48 0.44 0.56 2.4
## ccvmdef0601  0.31  0.30  0.02  0.70 0.68 0.32 1.8
## ccvmdef0701  0.82  0.14  0.06  0.03 0.69 0.31 1.1
## ccvmdef0801  0.87  0.11 -0.08  0.05 0.78 0.22 1.1
## ccvmdef0901  0.71  0.17  0.08  0.23 0.60 0.40 1.4
## ccvmdef1001  0.32  0.13  0.20  0.11 0.17 0.83 2.4
## ccvmdef1101 -0.01  0.01  0.87 -0.11 0.78 0.22 1.0
## ccvmdef1201  0.10  0.06  0.64 -0.07 0.43 0.57 1.1
## 
##                        PA1  PA3  PA2  PA4
## SS loadings           2.26 1.88 1.34 0.99
## Proportion Var        0.19 0.16 0.11 0.08
## Cumulative Var        0.19 0.34 0.46 0.54
## Proportion Explained  0.35 0.29 0.21 0.15
## Cumulative Proportion 0.35 0.64 0.85 1.00
## 
## Mean item complexity =  1.5
## Test of the hypothesis that 4 factors are sufficient.
## 
## df null model =  66  with the objective function =  4.14 with Chi Square =  538.85
## df of  the model are 24  and the objective function was  0.22 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.04 
## 
## The harmonic n.obs is  136 with the empirical chi square  12.26  with prob <  0.98 
## The total n.obs was  136  with Likelihood Chi Square =  27.74  with prob <  0.27 
## 
## Tucker Lewis Index of factoring reliability =  0.978
## RMSEA index =  0.033  and the 90 % confidence intervals are  0 0.081
## BIC =  -90.16
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                    PA1  PA3  PA2  PA4
## Correlation of (regression) scores with factors   0.93 0.90 0.90 0.80
## Multiple R square of scores with factors          0.86 0.80 0.81 0.65
## Minimum correlation of possible factor scores     0.72 0.61 0.62 0.29
fa.diagram(fa.none)

# Determine Number of Factors to Extract
library(nFactors)
## Loading required package: lattice
## 
## Attaching package: 'nFactors'
## The following object is masked from 'package:lattice':
## 
##     parallel
ev <- eigen(cor(media_factor_analysis)) # get eigenvalues
ap <- parallel(subject=nrow(media_factor_analysis),var=ncol(media_factor_analysis),
  rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# CFA ------
# (The Final one adapted) Reorder item 10 

# drop item 1
media_factor <- covid %>% select(participant_id,starts_with("ccvmdef"),survey1:survey13)

# only get the baseline data
# No need to reverse for CFA after discussing with Xiaoran - Jan 19th, 2024
media_factor_base <- media_factor %>% select(ccvmdef0101:ccvmdef1201)

# drop all NAs, N= 138
media_factor_analysis <- media_factor_base[rowSums(is.na(media_factor_base)) != 12, ]

mf.model.3 <- 'informed = ~ ccvmdef0201 + ccvmdef0301 + ccvmdef0401 + ccvmdef1001
connected = ~ ccvmdef0501 + ccvmdef0601
positive_government = ~ ccvmdef0701 + ccvmdef0801 + ccvmdef0901
negative_measures = ~ ccvmdef1101 + ccvmdef1201'

fit.three <- lavaan::cfa(mf.model.3, data=media_factor_analysis, std.lv=TRUE)

# fitMeasures(fit.three)

summary(fit.three)
## lavaan 0.6.16 ended normally after 39 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        28
## 
##                                                   Used       Total
##   Number of observations                           136         138
## 
## Model Test User Model:
##                                                       
##   Test statistic                                81.921
##   Degrees of freedom                                38
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                          Estimate  Std.Err  z-value  P(>|z|)
##   informed =~                                               
##     ccvmdef0201            18.858    1.985    9.499    0.000
##     ccvmdef0301            17.865    1.968    9.076    0.000
##     ccvmdef0401            14.853    1.964    7.562    0.000
##     ccvmdef1001             6.537    2.300    2.842    0.004
##   connected =~                                              
##     ccvmdef0501            16.120    2.102    7.669    0.000
##     ccvmdef0601            20.938    2.662    7.865    0.000
##   positive_government =~                                    
##     ccvmdef0701            21.183    1.905   11.119    0.000
##     ccvmdef0801            21.301    1.822   11.689    0.000
##     ccvmdef0901            19.117    1.976    9.676    0.000
##   negative_measures =~                                      
##     ccvmdef1101            25.511    5.917    4.312    0.000
##     ccvmdef1201            17.495    4.293    4.075    0.000
## 
## Covariances:
##                          Estimate  Std.Err  z-value  P(>|z|)
##   informed ~~                                               
##     connected               0.656    0.086    7.596    0.000
##     positv_gvrnmnt          0.394    0.091    4.330    0.000
##     negative_mesrs         -0.145    0.109   -1.336    0.182
##   connected ~~                                              
##     positv_gvrnmnt          0.511    0.092    5.544    0.000
##     negative_mesrs          0.140    0.117    1.195    0.232
##   positive_government ~~                                    
##     negative_mesrs         -0.020    0.104   -0.190    0.849
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .ccvmdef0201     228.046   46.113    4.945    0.000
##    .ccvmdef0301     246.329   45.040    5.469    0.000
##    .ccvmdef0401     309.763   45.710    6.777    0.000
##    .ccvmdef1001     559.922   69.168    8.095    0.000
##    .ccvmdef0501     265.671   51.061    5.203    0.000
##    .ccvmdef0601     398.172   82.447    4.829    0.000
##    .ccvmdef0701     196.691   39.077    5.033    0.000
##    .ccvmdef0801     152.422   36.147    4.217    0.000
##    .ccvmdef0901     284.433   43.298    6.569    0.000
##    .ccvmdef1101     227.383  285.155    0.797    0.425
##    .ccvmdef1201     374.324  140.987    2.655    0.008
##     informed          1.000                           
##     connected         1.000                           
##     positv_gvrnmnt    1.000                           
##     negative_mesrs    1.000
fitMeasures(fit.three, c("chisq", "df", "pvalue", "cfi", "rmsea", "srmr", "tli")) 
##  chisq     df pvalue    cfi  rmsea   srmr    tli 
## 81.921 38.000  0.000  0.911  0.092  0.078  0.871
# cfi = 0.911, rmsea = 0.092
# remsea is not good enough

cfa.mi.three <- modificationindices(fit.three) %>%
  as_tibble() %>%
  arrange(-mi)

cfa.mi.three %>%
  head(6) %>%
  pander::pander(caption="Top 6 modification indices for the CFA model")
Top 6 modification indices for the CFA model (continued below)
lhs op rhs mi epc sepc.lv sepc.all
connected =~ ccvmdef0201 9.071 -10.51 -10.51 -0.435
positive_government =~ ccvmdef1001 8.519 7.235 7.235 0.2947
negative_measures =~ ccvmdef0301 7.539 5.265 5.265 0.2214
connected =~ ccvmdef0901 6.142 5.93 5.93 0.2326
connected =~ ccvmdef0401 6.039 7.818 7.818 0.3395
positive_government =~ ccvmdef0501 4.471 -6.404 -6.404 -0.2794
sepc.nox
-0.435
0.2947
0.2214
0.2326
0.3395
-0.2794