# load packages
library(cSEM)
## 
## Attaching package: 'cSEM'
## The following object is masked from 'package:stats':
## 
##     predict
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg) 
## Linking to librsvg 2.56.3
# Load external function files
source("cSEM/plot_csem.R")

model_Bergami="
# Measurement models
OrgPres <~ cei1 + cei2 + cei3 + cei4 + cei5 + cei6 + cei7 + cei8 
OrgIden =~ ma1 + ma2 + ma3 + ma4 + ma5 + ma6
AffJoy =~ orgcmt1 + orgcmt2 + orgcmt3 + orgcmt7
AffLove  =~ orgcmt5 + orgcmt6 + orgcmt8
Gender<~ gender

# Structural model 
OrgIden ~ OrgPres 
AffLove ~ OrgPres+OrgIden+Gender
AffJoy  ~ OrgPres+OrgIden+Gender"

csem_results <- csem(.data = BergamiBagozzi2000,.model = model_Bergami,
                   .disattenuate = T,
                   .PLS_weight_scheme_inner = 'factorial',
                   .tolerance = 1e-5,
                   .resample_method = 'bootstrap',.R = 499)

plot_csem(csem_results,
          title = "SEM Diagram")
plot_csem(csem_results,
          title = "SEM Diagram",
          graph_attrs = "rankdir=LR")
plot_csem(csem_results,
          title = "SEM Diagram with No Colors",
          remove_colors = TRUE)
plot_csem(csem_results,
          title = "SEM Structural Model Diagram",
          show_structural_model_only = TRUE,
          graph_attrs = "rankdir=LR")
plot_csem(csem_results,
          title = "SEM Structural Model Diagram",
          show_structural_model_only = TRUE,
          graph_attrs = "rankdir=LR",
          remove_colors = TRUE)
plot_csem(csem_results,
          title = "SEM Diagram",
          edge_lengths = 2,
          graph_attrs = "layout = fdp")
plot_csem(csem_results,
          title = "SEM Diagram",
          graph_attrs = "layout = circo")  #options: dot, circo, fdp, twopi, neato, osage, patchwork
plot_csem(csem_results, 
          title = "SEM Diagram", 
          graph_attrs = "rankdir=LR; splines=polyline") # Or "graph_attrs = c("rankdir=LR", "splines=line")"
# Define ranks for nodes
node_ranks = list(
  rank1 = c("OrgPres", "Gender"),
  rank2 = c("OrgIden"),
  rank3 = c("AffLove", "AffJoy")
)

graph_attrs = c("rankdir=LR", "ranksep=0.5", "nodesep=0.3")

plot_csem(csem_results,
          title = "SEM Structural Model Diagram",
          node_ranks = node_ranks,
          show_structural_model_only = TRUE,
          graph_attrs = "rankdir=LR")
plot_csem(csem_results,
          title = "SEM Diagram",
          graph_attrs = graph_attrs)
plot_csem(csem_results,
          title = "SEM Diagram",
          node_ranks = node_ranks,
          graph_attrs = "rankdir=LR")
plot_csem(csem_results,
          title = "SEM Diagram",
          node_ranks = node_ranks,
          graph_attrs = "rankdir=BT")
# With correlation_lines:
plot_csem(csem_results,
          title = "SEM Diagram with all information",
          run_correlation_lines = TRUE)
# Save the diagram as a PDF file
plot_csem_save("SEM_Custom_Diagram.pdf")
## Plot saved to SEM_Custom_Diagram.pdf
summarize(csem_results)
## ________________________________________________________________________________
## ----------------------------------- Overview -----------------------------------
## 
##  General information:
##  ------------------------
##  Estimation status                  = Ok
##  Number of observations             = 305
##  Weight estimator                   = PLS-PM
##  Inner weighting scheme             = "factorial"
##  Type of indicator correlation      = Pearson
##  Path model estimator               = OLS
##  Second-order approach              = NA
##  Type of path model                 = Linear
##  Disattenuated                      = Yes (PLSc)
## 
##  Resample information:
##  ---------------------
##  Resample method                    = "bootstrap"
##  Number of resamples                = 499
##  Number of admissible results       = 497
##  Approach to handle inadmissibles   = "drop"
##  Sign change option                 = "none"
##  Random seed                        = -320380732
## 
##  Construct details:
##  ------------------
##  Name     Modeled as     Order         Mode      
## 
##  OrgPres  Composite      First order   "modeB"   
##  Gender   Composite      First order   "modeB"   
##  OrgIden  Common factor  First order   "modeA"   
##  AffLove  Common factor  First order   "modeA"   
##  AffJoy   Common factor  First order   "modeA"   
## 
## ----------------------------------- Estimates ----------------------------------
## 
## Estimated path coefficients:
## ============================
##                                                                    CI_percentile   
##   Path                 Estimate  Std. error   t-stat.   p-value         95%        
##   OrgIden ~ OrgPres      0.3913      0.0611    6.4062    0.0000 [ 0.2895; 0.5318 ] 
##   AffLove ~ OrgPres     -0.1196      0.0958   -1.2487    0.2118 [-0.3135; 0.0684 ] 
##   AffLove ~ Gender       0.1179      0.0624    1.8903    0.0587 [-0.0013; 0.2332 ] 
##   AffLove ~ OrgIden     -0.4836      0.0795   -6.0860    0.0000 [-0.6207;-0.3335 ] 
##   AffJoy ~ OrgPres       0.1926      0.0688    2.7996    0.0051 [ 0.0570; 0.3244 ] 
##   AffJoy ~ Gender       -0.1475      0.0475   -3.1036    0.0019 [-0.2321;-0.0543 ] 
##   AffJoy ~ OrgIden       0.6779      0.0597   11.3610    0.0000 [ 0.5537; 0.7981 ] 
## 
## Estimated loadings:
## ===================
##                                                                     CI_percentile   
##   Loading               Estimate  Std. error   t-stat.   p-value         95%        
##   OrgPres =~ cei1         0.7015      0.0870    8.0675    0.0000 [ 0.4944; 0.8231 ] 
##   OrgPres =~ cei2         0.7482      0.0881    8.4915    0.0000 [ 0.5042; 0.8586 ] 
##   OrgPres =~ cei3         0.7957      0.0881    9.0288    0.0000 [ 0.5550; 0.8942 ] 
##   OrgPres =~ cei4         0.8029      0.0909    8.8367    0.0000 [ 0.5562; 0.9114 ] 
##   OrgPres =~ cei5         0.8073      0.0815    9.9040    0.0000 [ 0.5981; 0.9115 ] 
##   OrgPres =~ cei6         0.8958      0.0759   11.8087    0.0000 [ 0.6687; 0.9475 ] 
##   OrgPres =~ cei7         0.7644      0.0855    8.9451    0.0000 [ 0.5255; 0.8622 ] 
##   OrgPres =~ cei8         0.7029      0.1015    6.9275    0.0000 [ 0.4436; 0.8292 ] 
##   Gender =~ gender        1.0000          NA        NA        NA [     NA;     NA ] 
##   OrgIden =~ ma1          0.6858      0.0511   13.4243    0.0000 [ 0.5825; 0.7734 ] 
##   OrgIden =~ ma2          0.6735      0.0567   11.8758    0.0000 [ 0.5511; 0.7708 ] 
##   OrgIden =~ ma3          0.6194      0.0608   10.1903    0.0000 [ 0.4944; 0.7403 ] 
##   OrgIden =~ ma4          0.8233      0.0439   18.7697    0.0000 [ 0.7277; 0.8970 ] 
##   OrgIden =~ ma5          0.7817      0.0520   15.0423    0.0000 [ 0.6739; 0.8777 ] 
##   OrgIden =~ ma6          0.6326      0.0603   10.4871    0.0000 [ 0.5244; 0.7459 ] 
##   AffLove =~ orgcmt5      0.6262      0.0840    7.4561    0.0000 [ 0.4529; 0.7782 ] 
##   AffLove =~ orgcmt6      0.3666      0.1110    3.3010    0.0010 [ 0.1212; 0.5594 ] 
##   AffLove =~ orgcmt8      0.8053      0.0761   10.5815    0.0000 [ 0.6419; 0.9435 ] 
##   AffJoy =~ orgcmt1       0.6287      0.0580   10.8386    0.0000 [ 0.5050; 0.7267 ] 
##   AffJoy =~ orgcmt2       0.6829      0.0505   13.5135    0.0000 [ 0.5811; 0.7836 ] 
##   AffJoy =~ orgcmt3       0.7228      0.0495   14.5912    0.0000 [ 0.6311; 0.8167 ] 
##   AffJoy =~ orgcmt7       0.6540      0.0551   11.8665    0.0000 [ 0.5387; 0.7534 ] 
## 
## Estimated weights:
## ==================
##                                                                     CI_percentile   
##   Weight                Estimate  Std. error   t-stat.   p-value         95%        
##   OrgPres <~ cei1         0.0522      0.1869    0.2795    0.7798 [-0.3571; 0.3901 ] 
##   OrgPres <~ cei2        -0.0383      0.1954   -0.1959    0.8447 [-0.4417; 0.3250 ] 
##   OrgPres <~ cei3         0.3137      0.1920    1.6336    0.1023 [-0.0748; 0.6956 ] 
##   OrgPres <~ cei4         0.1782      0.2348    0.7588    0.4480 [-0.3384; 0.5792 ] 
##   OrgPres <~ cei5         0.2341      0.2519    0.9292    0.3528 [-0.2046; 0.7624 ] 
##   OrgPres <~ cei6         0.3769      0.2554    1.4757    0.1400 [-0.1470; 0.8084 ] 
##   OrgPres <~ cei7         0.0983      0.2177    0.4513    0.6517 [-0.3606; 0.4831 ] 
##   OrgPres <~ cei8        -0.0032      0.1967   -0.0164    0.9869 [-0.3743; 0.4021 ] 
##   Gender <~ gender        1.0000          NA        NA        NA [     NA;     NA ] 
##   OrgIden <~ ma1          0.2126      0.0147   14.4459    0.0000 [ 0.1855; 0.2420 ] 
##   OrgIden <~ ma2          0.2087      0.0162   12.8973    0.0000 [ 0.1754; 0.2401 ] 
##   OrgIden <~ ma3          0.1920      0.0190   10.1191    0.0000 [ 0.1546; 0.2275 ] 
##   OrgIden <~ ma4          0.2552      0.0162   15.7385    0.0000 [ 0.2229; 0.2861 ] 
##   OrgIden <~ ma5          0.2423      0.0156   15.5261    0.0000 [ 0.2112; 0.2721 ] 
##   OrgIden <~ ma6          0.1961      0.0161   12.1479    0.0000 [ 0.1661; 0.2279 ] 
##   AffLove <~ orgcmt5      0.4469      0.0557    8.0282    0.0000 [ 0.3450; 0.5572 ] 
##   AffLove <~ orgcmt6      0.2616      0.0725    3.6098    0.0003 [ 0.0978; 0.3939 ] 
##   AffLove <~ orgcmt8      0.5747      0.0631    9.1081    0.0000 [ 0.4515; 0.7098 ] 
##   AffJoy <~ orgcmt1       0.3045      0.0244   12.4739    0.0000 [ 0.2530; 0.3494 ] 
##   AffJoy <~ orgcmt2       0.3307      0.0224   14.7880    0.0000 [ 0.2917; 0.3759 ] 
##   AffJoy <~ orgcmt3       0.3500      0.0228   15.3315    0.0000 [ 0.3088; 0.3972 ] 
##   AffJoy <~ orgcmt7       0.3167      0.0275   11.5274    0.0000 [ 0.2641; 0.3684 ] 
## 
## Estimated construct correlations:
## =================================
##                                                                    CI_percentile   
##   Correlation          Estimate  Std. error   t-stat.   p-value         95%        
##   OrgPres ~~ Gender      0.0513      0.0587    0.8738    0.3823 [-0.0725; 0.1581 ] 
## 
## Estimated indicator correlations:
## =================================
##                                                               CI_percentile   
##   Correlation     Estimate  Std. error   t-stat.   p-value         95%        
##   cei1 ~~ cei2      0.6437      0.0395   16.3154    0.0000 [ 0.5631; 0.7101 ] 
##   cei1 ~~ cei3      0.4746      0.0482    9.8493    0.0000 [ 0.3773; 0.5540 ] 
##   cei1 ~~ cei4      0.6404      0.0465   13.7758    0.0000 [ 0.5467; 0.7248 ] 
##   cei1 ~~ cei5      0.7131      0.0500   14.2755    0.0000 [ 0.6077; 0.7937 ] 
##   cei1 ~~ cei6      0.5402      0.0547    9.8812    0.0000 [ 0.4254; 0.6422 ] 
##   cei1 ~~ cei7      0.4307      0.0542    7.9461    0.0000 [ 0.3222; 0.5208 ] 
##   cei1 ~~ cei8      0.5740      0.0637    9.0098    0.0000 [ 0.4394; 0.6801 ] 
##   cei2 ~~ cei3      0.7109      0.0324   21.9701    0.0000 [ 0.6407; 0.7658 ] 
##   cei2 ~~ cei4      0.5871      0.0503   11.6831    0.0000 [ 0.4869; 0.6784 ] 
##   cei2 ~~ cei5      0.5371      0.0481   11.1587    0.0000 [ 0.4357; 0.6277 ] 
##   cei2 ~~ cei6      0.6558      0.0441   14.8644    0.0000 [ 0.5566; 0.7341 ] 
##   cei2 ~~ cei7      0.5527      0.0412   13.4019    0.0000 [ 0.4643; 0.6238 ] 
##   cei2 ~~ cei8      0.5841      0.0545   10.7258    0.0000 [ 0.4717; 0.6856 ] 
##   cei3 ~~ cei4      0.5454      0.0523   10.4252    0.0000 [ 0.4285; 0.6449 ] 
##   cei3 ~~ cei5      0.4234      0.0551    7.6781    0.0000 [ 0.3065; 0.5203 ] 
##   cei3 ~~ cei6      0.5984      0.0528   11.3274    0.0000 [ 0.4916; 0.6921 ] 
##   cei3 ~~ cei7      0.6544      0.0376   17.4121    0.0000 [ 0.5772; 0.7255 ] 
##   cei3 ~~ cei8      0.5174      0.0486   10.6396    0.0000 [ 0.4225; 0.6139 ] 
##   cei4 ~~ cei5      0.7418      0.0405   18.3243    0.0000 [ 0.6622; 0.8178 ] 
##   cei4 ~~ cei6      0.5992      0.0496   12.0777    0.0000 [ 0.5002; 0.6858 ] 
##   cei4 ~~ cei7      0.4587      0.0522    8.7884    0.0000 [ 0.3541; 0.5616 ] 
##   cei4 ~~ cei8      0.5866      0.0638    9.1892    0.0000 [ 0.4495; 0.7133 ] 
##   cei5 ~~ cei6      0.6547      0.0434   15.0814    0.0000 [ 0.5571; 0.7281 ] 
##   cei5 ~~ cei7      0.4752      0.0540    8.7932    0.0000 [ 0.3731; 0.5726 ] 
##   cei5 ~~ cei8      0.5837      0.0690    8.4565    0.0000 [ 0.4423; 0.7085 ] 
##   cei6 ~~ cei7      0.7132      0.0427   16.7103    0.0000 [ 0.6260; 0.7939 ] 
##   cei6 ~~ cei8      0.6031      0.0587   10.2739    0.0000 [ 0.4825; 0.7088 ] 
##   cei7 ~~ cei8      0.6902      0.0371   18.5922    0.0000 [ 0.6174; 0.7550 ] 
## 
## ------------------------------------ Effects -----------------------------------
## 
## Estimated total effects:
## ========================
##                                                                    CI_percentile   
##   Total effect         Estimate  Std. error   t-stat.   p-value         95%        
##   OrgIden ~ OrgPres      0.3913      0.0611    6.4062    0.0000 [ 0.2895; 0.5318 ] 
##   AffLove ~ OrgPres     -0.3089      0.0778   -3.9728    0.0001 [-0.4881;-0.1737 ] 
##   AffLove ~ Gender       0.1179      0.0624    1.8903    0.0587 [-0.0013; 0.2332 ] 
##   AffLove ~ OrgIden     -0.4836      0.0795   -6.0860    0.0000 [-0.6207;-0.3335 ] 
##   AffJoy ~ OrgPres       0.4578      0.0571    8.0189    0.0000 [ 0.3547; 0.5820 ] 
##   AffJoy ~ Gender       -0.1475      0.0475   -3.1036    0.0019 [-0.2321;-0.0543 ] 
##   AffJoy ~ OrgIden       0.6779      0.0597   11.3610    0.0000 [ 0.5537; 0.7981 ] 
## 
## Estimated indirect effects:
## ===========================
##                                                                    CI_percentile   
##   Indirect effect      Estimate  Std. error   t-stat.   p-value         95%        
##   AffLove ~ OrgPres     -0.1893      0.0454   -4.1642    0.0000 [-0.2976;-0.1198 ] 
##   AffJoy ~ OrgPres       0.2652      0.0499    5.3206    0.0000 [ 0.1903; 0.3759 ] 
## ________________________________________________________________________________