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(lavaanPlot) # for plotting later
## Warning: package 'lavaanPlot' was built under R version 4.2.2
#do not forget to set working directory!
# Model (1) Team Sport; t_Performance= (Self_Confidence(Cognitive , Somatic))
# Model (2) Indv Sport; i_Performance= (Self_Confidence(Cognitive , Somatic))

############
# Model(1) , n=142
Mi <- as.matrix(read.csv("matrix i.csv", row.names = 1))
Mi
##                   Self_Confidence_i Performance_i Cognitive_i Somatic_i
## Self_Confidence_i              1.00          0.66       -0.38     -0.46
## Performance_i                  0.66          1.00       -0.55     -0.48
## Cognitive_i                   -0.38         -0.55        1.00        NA
## Somatic_i                     -0.46         -0.48        0.47      1.00
# Model(1) :

###### Indirect effect ,we use labeled syntax
Performance_i_pa<-'
Self_Confidence_i ~ a_co * Cognitive_i + a_so * Somatic_i
Performance_i ~  b * Self_Confidence_i+ Cognitive_i + Somatic_i
ind_co := a_co * b
ind_so := a_so * b'

Performance_i_pa <- sem(model= Performance_i_pa, sample.cov = Mi,sample.nobs = 142)
Performance_i_pa
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         7
## 
##   Number of observations                           142
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
summary(Performance_i_pa, header = FALSE, standardize = TRUE, ci = TRUE)
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                       Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   Self_Confidence_i ~                                                      
##     Cogntv_ (a_co)      -0.210    0.083   -2.547    0.011   -0.372   -0.048
##     Somatc_ (a_so)      -0.361    0.083   -4.375    0.000   -0.523   -0.199
##   Performance_i ~                                                          
##     Slf_Cn_    (b)       0.492    0.065    7.573    0.000    0.365    0.619
##     Cogntv_             -0.313    0.065   -4.788    0.000   -0.441   -0.185
##     Somatc_             -0.107    0.068   -1.565    0.118   -0.240    0.027
##    Std.lv  Std.all
##                   
##    -0.210   -0.210
##    -0.361   -0.361
##                   
##     0.492    0.492
##    -0.313   -0.313
##    -0.107   -0.107
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Self_Confidnc_    0.749    0.089    8.426    0.000    0.575    0.923
##    .Performance_i     0.449    0.053    8.426    0.000    0.344    0.553
##    Std.lv  Std.all
##     0.749    0.754
##     0.449    0.452
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     ind_co           -0.103    0.043   -2.414    0.016   -0.187   -0.019
##     ind_so           -0.178    0.047   -3.788    0.000   -0.270   -0.086
##    Std.lv  Std.all
##    -0.103   -0.103
##    -0.178   -0.178
lavaanPlot(model = Performance_i_pa, coefs = TRUE, covs = TRUE, stars = "regress")
#############################################################
#######
# Model(2) , n= 128
Mt <- as.matrix(read.csv("matrix t.csv", row.names = 1))
Mt
##                   Self_Confidence_t Performance_t Cognitive_t Somatic_t
## Self_Confidence_t              1.00          0.13       -0.53     -0.27
## Performance_t                  0.13          1.00        0.14      0.02
## Cognitive_t                   -0.53          0.14        1.00      0.56
## Somatic_t                     -0.27          0.02        0.56      1.00
# Model(2)
###### Indirect effect ,we use labeled syntax
Performance_t_pa<-'
Self_Confidence_t ~ a_cot * Cognitive_t + a_sot * Somatic_t
Performance_t ~  bt * Self_Confidence_t+ Cognitive_t + Somatic_t
ind_cot := a_cot * bt
ind_sot := a_sot * bt'

Performance_t_pa <- sem(model= Performance_t_pa, sample.cov = Mt,sample.nobs = 128)
Performance_t_pa
## lavaan 0.6.15 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         7
## 
##   Number of observations                           128
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
summary(Performance_t_pa, header = FALSE, standardize = TRUE, ci = TRUE)
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                       Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   Self_Confidence_t ~                                                      
##     Cgntv_t (a_ct)      -0.552    0.090   -6.104    0.000   -0.729   -0.375
##     Somtc_t (a_st)       0.039    0.090    0.432    0.666   -0.138    0.216
##   Performance_t ~                                                          
##     Slf_Cn_   (bt)       0.288    0.100    2.880    0.004    0.092    0.483
##     Cgntv_t              0.346    0.116    2.985    0.003    0.119    0.574
##     Somtc_t             -0.096    0.102   -0.943    0.346   -0.297    0.104
##    Std.lv  Std.all
##                   
##    -0.552   -0.552
##     0.039    0.039
##                   
##     0.288    0.288
##     0.346    0.346
##    -0.096   -0.096
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Self_Confdnc_t    0.712    0.089    8.000    0.000    0.538    0.887
##    .Performance_t     0.909    0.114    8.000    0.000    0.686    1.132
##    Std.lv  Std.all
##     0.712    0.718
##     0.909    0.916
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     ind_cot          -0.159    0.061   -2.605    0.009   -0.278   -0.039
##     ind_sot           0.011    0.026    0.427    0.669   -0.040    0.063
##    Std.lv  Std.all
##    -0.159   -0.159
##     0.011    0.011
lavaanPlot(model = Performance_t_pa, coefs = TRUE, covs = TRUE, stars = "regress")