The below code has a minor issue: in the seminr plot, the block titled GPA is cut by the right margin. Please fix it. Provide a complete version of the revised code.

# 0. SET-UP: Check and install required packages
#############################################
required_packages <- c("writexl", "MASS", "lavaan", "pwr", "moments", 
                       "readxl", "seminr", "DiagrammeR", "DiagrammeRsvg", "webshot2")

for(pkg in required_packages){
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

if(!webshot2::is_phantomjs_installed()){
  webshot2::install_phantomjs()
}

#############################################
# 1. SIMULATE THE DATASET (N = 264)
#############################################
set.seed(12345)
n <- 264

# --- SIMULATE COVARIATES --------------------------------------------------
age <- sample(26:48, n, replace = TRUE)
marstat <- sample(c(0,1), n, replace = TRUE, prob = c(0.3, 0.7))

# --- SIMULATE SES ---------------------------------------------------------
ses_item_probs <- c(0.2, 0.6, 0.2)
ses_1 <- sample(1:3, n, replace = TRUE, prob = ses_item_probs)
ses_2 <- sample(1:3, n, replace = TRUE, prob = ses_item_probs)
ses_composite <- (ses_1 + ses_2) / 2 - 2  # Center roughly around zero

# --- SIMULATE LATENT PARENTAL STRESS --------------------------------------
stress_latent <- rnorm(n, mean = 0, sd = 1)

# --- SIMULATE LATENT PARENTAL INVOLVEMENT (PI) ---------------------------
# Now we include the requested effects:
#   • SES positively predicts PI (coefficient = 0.40),
#   • Stress has a small negative (non-significant) effect on PI (coefficient = -0.05)
pi_latent <- 0.40 * ses_composite + (-0.05) * stress_latent + rnorm(n, mean = 0, sd = 1)

# --- SIMULATE GPA (Outcome) -----------------------------------------------
gpa_cont <- 0.30 * ses_composite + 0.35 * pi_latent + (-0.05) * stress_latent + rnorm(n, mean = 0, sd = 1)
gpa <- round(scale(gpa_cont, center = min(gpa_cont), scale = (max(gpa_cont) - min(gpa_cont))) * (3/4) + 2)
gpa <- as.integer(pmax(2, pmin(5, gpa)))

#############################################
# SIMULATE MULTIPLE ITEMS BY ADDING NOISE
#############################################
simulate_items <- function(latent, n_items, min_val, max_val, noise_sd, target_mean = NULL) {
  items <- matrix(NA, n, n_items)
  for(i in 1:n_items){
    raw <- latent + rnorm(n, mean = 0, sd = noise_sd)
    if(!is.null(target_mean)) {
      raw <- raw + (target_mean - mean(raw))
    }
    items[, i] <- as.integer(round(raw))
    items[, i] <- pmax(min_val, pmin(max_val, items[, i]))
  }
  as.data.frame(items)
}

# --- 2a. Parental Involvement Items (15 items: 6 home enrichment, 6 home supervision, 3 home restrictions)
pi_enrich <- simulate_items(pi_latent, 6, 1, 4, noise_sd = 0.7, target_mean = 3)
pi_super  <- simulate_items(pi_latent, 6, 1, 4, noise_sd = 0.7, target_mean = 2.5)
pi_restrict <- simulate_items(pi_latent, 3, 1, 4, noise_sd = 0.5, target_mean = 1.8)
colnames(pi_enrich) <- paste0("parinv_enrich_", 1:6)
colnames(pi_super)  <- paste0("parinv_super_", 1:6)
colnames(pi_restrict) <- paste0("parinv_restrict_", 1:3)

# --- 2b. Parental Stress Items (17 items: 8 LPRRS, 9 PSD)
stress_LPRRS <- simulate_items(stress_latent, 8, 1, 5, noise_sd = 0.8, target_mean = 3.2)
stress_PSD <- simulate_items(stress_latent, 9, 1, 5, noise_sd = 0.8, target_mean = 2.8)
colnames(stress_LPRRS) <- paste0("stress_LPRRS_", 1:8)
colnames(stress_PSD) <- paste0("stress_PSD_", 1:9)

# --- Assemble the full dataset -------------------------------------------
dat <- data.frame(
  pi_enrich, pi_super, pi_restrict,
  stress_LPRRS, stress_PSD,
  ses_1, ses_2,
  GPA = gpa,
  age = age,
  marstat = marstat
)
cat("Dataset dimensions: ", dim(dat)[1], " observations and ", dim(dat)[2], " columns.\n")

writexl::write_xlsx(dat, "inclusion.xlsx")
cat("Data written to 'inclusion.xlsx'\n\n")

#############################################
# 2. POWER CALCULATION (Multiple Regression)
#############################################
library(pwr)
u <- 4
v <- n - u - 1
f2 <- 0.10
power_result <- pwr.f2.test(u = u, v = v, f2 = f2, sig.level = 0.05)
cat("Power Calculation (Multiple Regression):\n")
print(power_result)
cat("\n")

#############################################
# 3. DESCRIPTIVE STATISTICS AND ASSUMPTION CHECKS
#############################################
library(moments)
descriptives <- data.frame(Variable = colnames(dat),
                           Min = sapply(dat, min),
                           Max = sapply(dat, max),
                           Mean = sapply(dat, mean),
                           SD = sapply(dat, sd),
                           Skewness = sapply(dat, skewness),
                           Kurtosis = sapply(dat, kurtosis))
cat("Descriptive Statistics:\n")
print(descriptives)
cat("\n")

#############################################
# 4. CFA for the Parental Involvement Measurement Model
#############################################
cfa_PI <- '
  HomeEnrichment =~ parinv_enrich_1 + parinv_enrich_2 + parinv_enrich_3 + parinv_enrich_4 + parinv_enrich_5 + parinv_enrich_6
  HomeSupervision =~ parinv_super_1 + parinv_super_2 + parinv_super_3 + parinv_super_4 + parinv_super_5 + parinv_super_6
  HomeRestrictions =~ parinv_restrict_1 + parinv_restrict_2 + parinv_restrict_3
'
fit_cfa_PI <- lavaan::cfa(cfa_PI, data = dat)
cat("CFA for Parental Involvement (PI) Model:\n")
summary(fit_cfa_PI, fit.measures = TRUE, standardized = TRUE)
cat("\n")

#############################################
# 5. FULL SEM ANALYSIS (Using lavaan)
#############################################
# Create composite scores for use in the SEM.
dat$SES_comp <- rowMeans(dat[, c("ses_1", "ses_2")])
dat$PI_comp  <- rowMeans(dat[, grepl("parinv", colnames(dat))])
dat$Stress_comp <- rowMeans(dat[, grepl("stress", colnames(dat))])

# Include all requested paths:
# (1) SES --> PI (expected positive, ~0.40)
# (2) Stress --> PI (expected negative, non-significant, ~-0.05)
# (3) SES --> GPA (expected positive, ~0.30)
# (4) PI --> GPA (expected positive, ~0.35)
# (5) Stress --> GPA (expected negative, non-significant, ~-0.05)
# (6) Covariates (age and marstat) have non-significant effects.
sem_model <- '
  PI_comp ~ c1*SES_comp + c2*Stress_comp + c3*age + c4*marstat
  GPA ~ c5*SES_comp + c6*PI_comp + c7*Stress_comp + c8*age + c9*marstat
'
fit_sem <- lavaan::sem(sem_model, data = dat, se = "robust.sem")
cat("SEM Model Summary (lavaan):\n")
summary(fit_sem, standardized = TRUE, rsquare = TRUE)
cat("\n")

# If you still obtain a warning about the latent-variable covariance matrix, try:
# print(lavInspect(fit_sem, "cov.lv"))
# and consider adjusting the simulation error variances.

#############################################
# 6. PLS-SEM ANALYSIS AND PATH MODEL PLOT (Using seminr)
#############################################
library(seminr)
# Define the measurement model.
# Note the revised part: We include Stress as affecting Parental_Involvement.
inclusion_mm <- constructs(
  composite("HomeEnrichment", multi_items("parinv_enrich_", 1:6), weights = mode_A),
  composite("HomeSupervision", multi_items("parinv_super_", 1:6), weights = mode_A),
  composite("HomeRestrictions", multi_items("parinv_restrict_", 1:3), weights = mode_A),
  higher_composite("Parental_Involvement", 
                   dimensions = c("HomeEnrichment", "HomeSupervision", "HomeRestrictions"), 
                   method = two_stage),
  composite("SES", multi_items("", c("ses_1", "ses_2")), weights = mode_A),
  composite("Stress", multi_items("", c(paste0("stress_LPRRS_", 1:8), paste0("stress_PSD_", 1:9))), weights = mode_A),
  composite("GPA", single_item("GPA"))
)

# Define the structural model.
# Notice the added path: Stress --> Parental_Involvement.
inclusion_sm <- relationships(
  paths(from = c("SES", "Stress"), to = "Parental_Involvement"),
  paths(from = c("Parental_Involvement", "SES", "Stress"), to = "GPA")
)

inclusion_pls <- estimate_pls(
  data = dat,
  measurement_model = inclusion_mm,
  structural_model = inclusion_sm,
  inner_weights = path_weighting
)

cat("PLS-SEM Model Estimates (seminr):\n")
print(summary(inclusion_pls))
cat("\n")

boot_inclusion <- bootstrap_model(
  seminr_model = inclusion_pls,
  nboot = 100,
  cores = 2
)
cat("Bootstrapped PLS-SEM results:\n")
print(summary(boot_inclusion))
cat("\n")

# Plot the SEM path model with seminr.
thm <- seminr_theme_create(
  plot.rounding = 2, 
  plot.adj = FALSE,
  sm.node.fill = "cadetblue1",
  mm.node.fill = "lightgray"
)
seminr_theme_set(thm)
sem_plot <- plot(inclusion_pls)
print(sem_plot)

sem_svg  <- export_svg(sem_plot)
svg_filename <- "inclusion_sem_plot.svg"
pdf_filename <- "inclusion_sem_plot.pdf"
writeLines(sem_svg, con = svg_filename)
webshot2::webshot(url = svg_filename, file = pdf_filename, zoom = 3)
cat("SEM path model plot saved as '", svg_filename, "' and '", pdf_filename, "'.\n", sep = "")

#############################################
# 7. END OF SCRIPT
cat("\nScript execution complete.\n")
#############################################