library(lavaan)
## Warning: package 'lavaan' was built under R version 4.2.3
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
library(semTools)
## Warning: package 'semTools' was built under R version 4.2.3
## 
## ###############################################################################
## This is semTools 0.5-6
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
####### 
# Site1
dat1 <- read.csv("data_site_1.csv")
head(dat1) 
##      z1    z2    z3    z4 mean_score treat
## 1  0.56  0.12 -0.92 -2.36    -0.6500     0
## 2 -0.46 -0.51  0.12  0.69    -0.0400     0
## 3  0.20  0.93 -0.29 -0.03     0.2025     0
## 4 -0.86 -2.20 -1.06  0.46    -0.9150     0
## 5  0.90  0.95  0.35  0.27     0.6175     0
## 6 -1.42 -0.64  0.07 -1.40    -0.8475     0
# linear regression syntax
summary(lm(mean_score ~ treat, data = dat1))
## 
## Call:
## lm(formula = mean_score ~ treat, data = dat1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.81507 -0.49132  0.00242  0.49118  2.35492 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.06008    0.07123   0.843   0.4001  
## treat        0.24000    0.10074   2.382   0.0181 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7123 on 198 degrees of freedom
## Multiple R-squared:  0.02787,    Adjusted R-squared:  0.02296 
## F-statistic: 5.676 on 1 and 198 DF,  p-value: 0.01815
# mimic syntax
model1_syntax <- "
SOB =~ z1 + z2 + z3 + z4
SOB ~ treat"
summary(cfa(
  model1_syntax, data = dat1, std.lv = TRUE),
  rsquare = TRUE, standardize = TRUE)
## lavaan 0.6.15 ended normally after 19 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           200
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           1.000
## 
## 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
##   SOB =~                                                                
##     z1                0.896    0.075   11.927    0.000    0.914    0.904
##     z2                0.696    0.072    9.647    0.000    0.710    0.707
##     z3                0.498    0.072    6.940    0.000    0.508    0.508
##     z4                0.298    0.073    4.066    0.000    0.304    0.305
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SOB ~                                                                 
##     treat             0.402    0.156    2.580    0.010    0.394    0.197
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .z1                0.188    0.097    1.942    0.052    0.188    0.184
##    .z2                0.505    0.077    6.573    0.000    0.505    0.501
##    .z3                0.743    0.081    9.176    0.000    0.743    0.742
##    .z4                0.900    0.092    9.806    0.000    0.900    0.907
##    .SOB               1.000                               0.961    0.961
## 
## R-Square:
##                    Estimate
##     z1                0.816
##     z2                0.499
##     z3                0.258
##     z4                0.093
##     SOB               0.039
##########################################################
#####Site2
dat2 <- read.csv("data_site_2.csv")
head(dat2)
##      z1    z2    z3    z4 mean_score treat
## 1 -1.54  1.22 -0.04 -1.02    -0.3450     0
## 2  1.63  1.12  1.72  0.17     1.1600     0
## 3 -0.11 -0.20 -0.70  1.32     0.0775     0
## 4 -0.35 -0.54 -0.27  0.24    -0.2300     0
## 5  0.60  0.64 -0.90 -1.04    -0.1750     0
## 6 -1.51  0.64  0.10 -0.08    -0.2125     0
# linear regression syntax
summary(lm(mean_score ~ treat, data = dat2))
## 
## Call:
## lm(formula = mean_score ~ treat, data = dat2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4624 -0.4027  0.0000  0.4288  1.4424 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.03515    0.05848   0.601   0.5485  
## treat        0.13970    0.08270   1.689   0.0928 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5848 on 198 degrees of freedom
## Multiple R-squared:  0.01421,    Adjusted R-squared:  0.009228 
## F-statistic: 2.853 on 1 and 198 DF,  p-value: 0.09275
# mimic syntax
model2_syntax <- "
SOB =~ z1 + z2 + z3 + z4
SOB ~ treat"
summary(cfa(
  model2_syntax, data = dat1, std.lv = TRUE),
  rsquare = TRUE, standardize = TRUE)
## lavaan 0.6.15 ended normally after 19 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           200
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           1.000
## 
## 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
##   SOB =~                                                                
##     z1                0.896    0.075   11.927    0.000    0.914    0.904
##     z2                0.696    0.072    9.647    0.000    0.710    0.707
##     z3                0.498    0.072    6.940    0.000    0.508    0.508
##     z4                0.298    0.073    4.066    0.000    0.304    0.305
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SOB ~                                                                 
##     treat             0.402    0.156    2.580    0.010    0.394    0.197
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .z1                0.188    0.097    1.942    0.052    0.188    0.184
##    .z2                0.505    0.077    6.573    0.000    0.505    0.501
##    .z3                0.743    0.081    9.176    0.000    0.743    0.742
##    .z4                0.900    0.092    9.806    0.000    0.900    0.907
##    .SOB               1.000                               0.961    0.961
## 
## R-Square:
##                    Estimate
##     z1                0.816
##     z2                0.499
##     z3                0.258
##     z4                0.093
##     SOB               0.039
##########################################################
#####Site3
dat3 <- read.csv("data_site_3.csv")
head(dat3)
##      z1    z2    z3    z4 mean_score treat
## 1  0.73  2.47  1.16  1.67     1.5075     0
## 2  0.25 -0.17 -0.45 -0.29    -0.1650     0
## 3  0.16  0.75  1.12  0.84     0.7175     0
## 4 -0.14 -0.78 -1.27 -0.57    -0.6900     0
## 5  0.26 -0.11 -0.13 -0.26    -0.0600     0
## 6  2.64  1.89  2.09  2.54     2.2900     0
# linear regression syntax
summary(lm(mean_score ~ treat, data = dat3))
## 
## Call:
## lm(formula = mean_score ~ treat, data = dat3)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.40762 -0.63816 -0.05745  0.71050  2.97487 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.08978    0.09260   0.969  0.33348   
## treat        0.36035    0.13096   2.752  0.00648 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.926 on 198 degrees of freedom
## Multiple R-squared:  0.03683,    Adjusted R-squared:  0.03197 
## F-statistic: 7.572 on 1 and 198 DF,  p-value: 0.006479
# mimic syntax
model3_syntax <- "
SOB =~ z1 + z2 + z3 + z4
SOB ~ treat"
summary(cfa(
  model3_syntax, data = dat3, std.lv = TRUE),
  rsquare = TRUE, standardize = TRUE)
## lavaan 0.6.15 ended normally after 26 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           200
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           1.000
## 
## 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
##   SOB =~                                                                
##     z1                0.896    0.055   16.338    0.000    0.914    0.903
##     z2                0.895    0.055   16.337    0.000    0.913    0.903
##     z3                0.896    0.055   16.341    0.000    0.914    0.903
##     z4                0.895    0.055   16.343    0.000    0.913    0.903
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SOB ~                                                                 
##     treat             0.402    0.147    2.736    0.006    0.395    0.197
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .z1                0.188    0.025    7.394    0.000    0.188    0.184
##    .z2                0.188    0.025    7.396    0.000    0.188    0.184
##    .z3                0.188    0.025    7.391    0.000    0.188    0.184
##    .z4                0.188    0.025    7.389    0.000    0.188    0.184
##    .SOB               1.000                               0.961    0.961
## 
## R-Square:
##                    Estimate
##     z1                0.816
##     z2                0.816
##     z3                0.816
##     z4                0.816
##     SOB               0.039