devtools::load_all(".")
## ℹ Loading cSEM
library(cSEM)

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Sample.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

Aff<~AffJoy+AffLove

# Structural model 
OrgIden ~ OrgPres 
Aff ~ OrgPres+OrgIden+Gender
"


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

graph_attrs = c(
  # Coloring specific nodes
  "OrgPres [style=filled, fillcolor=orange]",
  "Gender [style=filled, fillcolor=orange]",
  "OrgIden [style=filled, fillcolor=lightblue]",
  "AffLove [style=filled, fillcolor=lightblue]",
  "AffJoy [style=filled, fillcolor=lightblue]",
  
  # Adjusting global graph attributes, e.g., rank separation
  "rankdir=LR",
  "ranksep=1.5",
  "nodesep=0.5",
  "splines=line",
  
  # Setting background color
  #"bgcolor=lightgray",
  
  # Vertically aligning certain nodes (If rankdir=TB, align them horizontally instead.)
  "{rank=same; OrgPres; Gender}",
  "{rank=same; OrgIden}",
  "{rank=same; AffJoy; AffLove}"
)

plot(out)
plot(out,
     .plot_labels = FALSE)
plot(out,
     .plot_structural_model_only = TRUE)
plot(out,
     .plot_structural_model_only = TRUE,
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "none")
plot(out,
     .plot_correlations = "none",
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "exo")
plot(out,
     .plot_correlations = "exo",
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "convcv")
plot(out,
     .plot_correlations = "convcv",
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "convcv",
     .graph_attrs = graph_attrs)
plot(out,
     .plot_correlations = "convcv",
     .graph_attrs = graph_attrs,
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "exoind")
plot(out,
     .plot_correlations = "exoind",
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "indvcv")
plot(out,
     .plot_correlations = "indvcv",
     .plot_labels = FALSE)