## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE
)


## -----------------------------------------------------------------------------
library(PLSsemEngine)
set.seed(123)

# Helper function for data simulation
simulate_example_data <- function(n) {
  Service_Quality <- rnorm(n)
  Customer_Satisfaction <- 0.6 * Service_Quality + rnorm(n, sd = 0.6)
  Customer_Loyalty <- 0.55 * Customer_Satisfaction + 0.25 * Service_Quality + rnorm(n, sd = 0.6)
  
  latent_to_item <- function(latent, loading) {
    x <- loading * latent + rnorm(length(latent), sd = sqrt(1 - loading^2))
    x <- scale(x)
    as.numeric(cut(x, breaks = quantile(x, probs = seq(0, 1, length.out = 8)), 
                   labels = 1:7, include.lowest = TRUE))
  }
  
  data.frame(
    SQ1 = latent_to_item(Service_Quality, 0.82), SQ2 = latent_to_item(Service_Quality, 0.78), SQ3 = latent_to_item(Service_Quality, 0.74),
    CS1 = latent_to_item(Customer_Satisfaction, 0.80), CS2 = latent_to_item(Customer_Satisfaction, 0.76), CS3 = latent_to_item(Customer_Satisfaction, 0.72),
    CL1 = latent_to_item(Customer_Loyalty, 0.81), CL2 = latent_to_item(Customer_Loyalty, 0.77), CL3 = latent_to_item(Customer_Loyalty, 0.73)
  )
}

simulated_data <- simulate_example_data(300)

## -----------------------------------------------------------------------------
# Define reflective blocks
measurement_model <- list(
  Service_Quality = c("SQ1", "SQ2", "SQ3"),
  Customer_Satisfaction = c("CS1", "CS2", "CS3"),
  Customer_Loyalty = c("CL1", "CL2", "CL3")
)

# Define structural paths using formulas
structural_model <- list(
  Customer_Satisfaction ~ Service_Quality,
  Customer_Loyalty ~ Customer_Satisfaction + Service_Quality
)

## -----------------------------------------------------------------------------
model <- pls_sem(
  data = simulated_data,
  measurement_model = measurement_model,
  structural_model = structural_model,
  nboot = 100, # Using 100 for speed in this vignette
  k = 5
)

## -----------------------------------------------------------------------------
# Measurement Model
model$measurement_model

# Discriminant Validity
model$discriminant_validity

# Structural Model
model$structural_model

## -----------------------------------------------------------------------------
# Global Model Fit (SRMR, d_ULS, d_G)
model$diagnostics$global_fit

# Export to lavaan syntax
export_lavaan_syntax(measurement_model, structural_model)

