devtools::load_all(".")
## ℹ Loading cSEM
library(cSEM)
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)

#  Using "rank=same"
graph_attrs1 = 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",
  
  # Vertically aligning certain nodes (If rankdir=TB, aligning horizontally)
  "{rank=same; OrgPres; Gender}",
  "{rank=same; OrgIden}",
  "{rank=same; AffJoy; AffLove}"
)

# Using "rank=same" and "subgraph" for indicators (All Items in One Rank)
graph_attrs2 = c(
  "rankdir=LR",
  "ranksep=1",
  "splines=polyline",
  
  "{rank=same; OrgPres; Gender}",
  "{rank=same; OrgIden}",
  "{rank=same; AffJoy; AffLove}",
  
  "subgraph cluster_1 {
  graph [style=invis];
  {rank=same; cei1 ; cei2 ; cei3 ; cei4; cei5 ; cei6 ; cei7 ; cei8}
  }",
  
  "subgraph cluster_2 {
  graph [style=invis];
  {rank=same; ma1; ma2; ma3; ma4; ma5; ma6}
  }",
  
  "subgraph cluster_3 {
  graph [style=invis];
  {rank=same; orgcmt1; orgcmt2; orgcmt3; orgcmt7}
  }",
  
  "subgraph cluster_4 {
  graph [style=invis];
  {rank=same; orgcmt5; orgcmt6; orgcmt8}
  }"
)

# Using "rank=same" and "subgraph" for indicators (Items in Two Ranks)
graph_attrs3 = c(
  "rankdir=LR",
  "ranksep=1",
  "splines=polyline",
  
  "{rank=same; OrgPres; Gender}",
  "{rank=same; OrgIden}",
  "{rank=same; AffJoy; AffLove}",
  
  "subgraph cluster_1 {
  graph [style=invis];
  {rank=same; cei1 ; cei2 ; cei3 ; cei4};
  {rank=same; cei5 ; cei6 ; cei7 ; cei8}
  }",
  
  "subgraph cluster_2 {
  graph [style=invis];
  {rank=same; ma1; ma2; ma3};
  {rank=same; ma4; ma5; ma6}
  }",
  
  "subgraph cluster_3 {
  graph [style=invis];
  {rank=same; orgcmt1; orgcmt2};
  {rank=same; orgcmt3; orgcmt7}
  }",
  
  "subgraph cluster_4 {
  graph [style=invis];
  {rank=same; orgcmt5; orgcmt6; orgcmt8}
  }"
)

plot(out)
plot(out,
     .graph_attrs = graph_attrs1)
plot(out,
     .plot_correlations = "exo")
plot(out,
     .plot_correlations = "exo",
     .graph_attrs = graph_attrs2)
plot(out,
     .plot_correlations = "exo",
     .plot_labels = FALSE,
     .graph_attrs = graph_attrs2)
plot(out,
     .plot_correlations = "convcv")
plot(out,
     .plot_correlations = "convcv",
     .graph_attrs = graph_attrs2)
plot(out,
     .plot_correlations = "exoind")
plot(out,
     .plot_correlations = "exoind",
     .graph_attrs = graph_attrs3)
plot(out,
     .plot_correlations = "exoind",
     .graph_attrs = graph_attrs2,
     .plot_labels = FALSE)
plot(out,
     .plot_correlations = "indvcv")
plot(out,
     .plot_correlations = "indvcv",
     .graph_attrs = graph_attrs3)
plot(out,
     .plot_correlations = "indvcv",
     .plot_labels = FALSE,
     .graph_attrs = graph_attrs2)