EFA, CFA, CB-SEM, and PLS-SEM syntax generation
Source:vignettes/efa-cfa-sem-pls-syntax.Rmd
efa-cfa-sem-pls-syntax.RmdThe model is the third part of the research design. surveyframe stores the measurement and structural specification and writes the syntax for the modelling stage, and you fit the models with lavaan or seminr. This vignette builds the 3 models from the published Thailand study (Sharafuddin, Madhavan, and Wangtueai 2024). The syntax steps need no raw data, so every chunk below runs as written.
There are 5 constructs. Digital marketing effectiveness (DME) is built from relevance and engagement (DMRE), accessibility and usefulness (DMAU), ease of use (DMEU), and perceived value (DMPV). Destination service quality (DSQ) is built from accommodation (DSQA) and local transport (DSQT). The remaining 3 are destination sustainability quality (DSUQ), tourist satisfaction (TS), and behavioural intention (BI).
dmre <- paste0("dmre_", 1:4); dmau <- paste0("dmau_", 1:3)
dmeu <- paste0("dmeu_", 1:3); dmpv <- paste0("dmpv_", 1:4)
dsqa <- paste0("dsqa_", 1:3); dsqt <- paste0("dsqt_", 1:5)
dsuq <- paste0("dsuq_", 1:5); ts <- paste0("ts_", 1:3); bi <- paste0("bi_", 1:3)EFA planning syntax
sframe_syntax_efa() writes a short R block that
estimates an exploratory factor solution with the optional
psych package. This screens the digital marketing items
before the confirmatory model.
cat(sframe_syntax_efa(items = c(dmre, dmau, dmeu, dmpv), nfactors = 4,
extraction = "minres", rotation = "oblimin"))
#> # EFA syntax generated by surveyframe
#> rlang::check_installed("psych", reason = "to estimate an EFA solution")
#> efa_items <- data[, c("dmre_1", "dmre_2", "dmre_3", "dmre_4", "dmau_1", "dmau_2", "dmau_3", "dmeu_1", "dmeu_2", "dmeu_3", "dmpv_1", "dmpv_2", "dmpv_3", "dmpv_4")]
#> psych::fa(efa_items, nfactors = 4, fm = "minres", rotate = "oblimin")CFA syntax: the lower-order measurement model
The confirmatory measurement model has the 9 lower-order constructs
as reflective factors. sframe_syntax_cfa() writes lavaan
syntax from a model object, with no need for lavaan at this stage.
construct_def <- list(
DMRE = dmre, DMAU = dmau, DMEU = dmeu, DMPV = dmpv,
DSQA = dsqa, DSQT = dsqt, DSUQ = dsuq, TS = ts, BI = bi
)
cfa_model <- sframe_model_spec(
id = "lower_order_cfa",
label = "Lower-order measurement model",
type = "cfa",
constructs = Map(function(id, items) sframe_construct(id, id, items),
names(construct_def), construct_def)
)
cat(sframe_syntax_cfa(model = cfa_model))
#> # lavaan CFA syntax generated by surveyframe
#> # Model: Lower-order measurement model
#> # Recommended fitting option: std.lv = TRUE
#> # Fit with lavaan only when lavaan is installed.
#>
#> # DMRE (reflective)
#> DMRE =~ dmre_1 + dmre_2 + dmre_3 + dmre_4
#>
#> # DMAU (reflective)
#> DMAU =~ dmau_1 + dmau_2 + dmau_3
#>
#> # DMEU (reflective)
#> DMEU =~ dmeu_1 + dmeu_2 + dmeu_3
#>
#> # DMPV (reflective)
#> DMPV =~ dmpv_1 + dmpv_2 + dmpv_3 + dmpv_4
#>
#> # DSQA (reflective)
#> DSQA =~ dsqa_1 + dsqa_2 + dsqa_3
#>
#> # DSQT (reflective)
#> DSQT =~ dsqt_1 + dsqt_2 + dsqt_3 + dsqt_4 + dsqt_5
#>
#> # DSUQ (reflective)
#> DSUQ =~ dsuq_1 + dsuq_2 + dsuq_3 + dsuq_4 + dsuq_5
#>
#> # TS (reflective)
#> TS =~ ts_1 + ts_2 + ts_3
#>
#> # BI (reflective)
#> BI =~ bi_1 + bi_2 + bi_3CB-SEM syntax: the structural model
The covariance-based reading treats the 5 constructs as first-order
reflective factors over their full item sets, with the 9 direct paths
and the 4 indirect effects from the hypotheses.
sframe_syntax_sem() writes the measurement, structural, and
indirect-effect lines.
sem_model <- sframe_model_spec(
id = "tourism_structural",
label = "Digital marketing structural model",
type = "cb_sem",
constructs = list(
sframe_construct("DME", "Digital marketing effectiveness", c(dmre, dmau, dmeu, dmpv)),
sframe_construct("DSQ", "Destination service quality", c(dsqa, dsqt)),
sframe_construct("DSUQ", "Destination sustainability quality", dsuq),
sframe_construct("TS", "Tourist satisfaction", ts),
sframe_construct("BI", "Behavioural intention", bi)
),
paths = list(
sframe_path("DME", "DSQ", label = "h1"), sframe_path("DME", "DSUQ", label = "h2"),
sframe_path("DME", "TS", label = "h3"), sframe_path("DME", "BI", label = "h4"),
sframe_path("DSQ", "TS", label = "h5"), sframe_path("DSQ", "BI", label = "h6"),
sframe_path("DSUQ", "TS", label = "h7"), sframe_path("DSUQ", "BI", label = "h8"),
sframe_path("TS", "BI", label = "h9")
),
indirect = list(
sframe_indirect_effect("DME", "DSQ", "TS", label = "h10"),
sframe_indirect_effect("DME", "DSUQ", "TS", label = "h11"),
sframe_indirect_effect("DME", c("DSQ", "TS"), "BI", label = "h12"),
sframe_indirect_effect("DME", c("DSUQ", "TS"), "BI", label = "h13")
),
options = list(estimator = "MLR", missing = "fiml", standardised = TRUE)
)
sframe_validate_model(sem_model)
#> <sframe validation>
#> Model: tourism_structural (cb_sem)
#> Status: valid
#> Checks: 10 run, 0 with problems
cat(sframe_syntax_sem(sem_model))
#> # lavaan CB-SEM syntax generated by surveyframe
#> # Model: Digital marketing structural model
#> # Recommended summary option: standardized = TRUE
#> # Estimator: MLR
#> # Missing data method: fiml
#>
#> DME =~ dmre_1 + dmre_2 + dmre_3 + dmre_4 + dmau_1 + dmau_2 + dmau_3 + dmeu_1 + dmeu_2 + dmeu_3 + dmpv_1 + dmpv_2 + dmpv_3 + dmpv_4
#> DSQ =~ dsqa_1 + dsqa_2 + dsqa_3 + dsqt_1 + dsqt_2 + dsqt_3 + dsqt_4 + dsqt_5
#> DSUQ =~ dsuq_1 + dsuq_2 + dsuq_3 + dsuq_4 + dsuq_5
#> TS =~ ts_1 + ts_2 + ts_3
#> BI =~ bi_1 + bi_2 + bi_3
#>
#> # Structural paths
#> BI ~ h4*DME + h6*DSQ + h8*DSUQ + h9*TS
#> DSQ ~ h1*DME
#> DSUQ ~ h2*DME
#> TS ~ h3*DME + h5*DSQ + h7*DSUQ
#>
#> # Indirect and total effects
#> h10 := h1*h5
#> h11 := h2*h7
#> h12 := h1*h5*h9
#> h13 := h2*h7*h9
#> total_DME_TS := h3 + h10 + h11
#> total_DME_BI := h4 + h12 + h13PLS-SEM syntax: the two-stage higher-order model
The published study used PLS-SEM with a 2-stage treatment of the
higher-order constructs. The second stage models digital marketing
effectiveness and service quality as composites of their lower-order
construct scores, then estimates the structural paths.
sframe_syntax_pls() writes the seminr code, including the
bootstrap, reliability, AVE, and HTMT calls.
pls_model <- sframe_model_spec(
id = "tourism_pls",
label = "Two-stage higher-order PLS model",
type = "pls_sem",
constructs = list(
sframe_construct("DME", "Digital marketing effectiveness",
c("DMRE", "DMAU", "DMEU", "DMPV"), mode = "composite"),
sframe_construct("DSQ", "Destination service quality",
c("DSQA", "DSQT"), mode = "composite"),
sframe_construct("DSUQ", "Destination sustainability quality", dsuq, mode = "composite"),
sframe_construct("TS", "Tourist satisfaction", ts, mode = "composite"),
sframe_construct("BI", "Behavioural intention", bi, mode = "composite")
),
paths = list(
sframe_path("DME", "DSQ"), sframe_path("DME", "DSUQ"),
sframe_path("DME", "TS"), sframe_path("DME", "BI"),
sframe_path("DSQ", "TS"), sframe_path("DSQ", "BI"),
sframe_path("DSUQ", "TS"), sframe_path("DSUQ", "BI"),
sframe_path("TS", "BI")
),
options = list(bootstrap = 1000)
)
cat(sframe_syntax_pls(pls_model))
#> # seminr PLS-SEM syntax generated by surveyframe
#> rlang::check_installed("seminr", reason = "to fit PLS-SEM models")
#> library(seminr)
#>
#> measurement_model <- constructs(
#> composite("DME", c("DMRE", "DMAU", "DMEU", "DMPV")),
#> composite("DSQ", c("DSQA", "DSQT")),
#> composite("DSUQ", multi_items("dsuq_", 1:5)),
#> composite("TS", multi_items("ts_", 1:3)),
#> composite("BI", multi_items("bi_", 1:3))
#> )
#>
#> structural_model <- relationships(
#> paths(from = "DME", to = c("DSQ", "DSUQ", "TS", "BI")),
#> paths(from = "DSQ", to = c("TS", "BI")),
#> paths(from = "DSUQ", to = c("TS", "BI")),
#> paths(from = "TS", to = c("BI"))
#> )
#>
#> pls_model <- estimate_pls(
#> data = data,
#> measurement_model = measurement_model,
#> structural_model = structural_model
#> )
#>
#> boot_model <- bootstrap_model(
#> seminr_model = pls_model,
#> nboot = 1000,
#> cores = 1,
#> seed = 123
#> )
#>
#> model_summary <- summary(pls_model)
#>
#> # Reflective constructs: loadings, reliability, AVE, and HTMT
#> model_summary$loadings
#> model_summary$reliability
#> model_summary$validity$htmt
#>
#> # Structural model: collinearity, R squared, f squared, and PLSpredict
#> model_summary$vif_antecedents
#> model_summary$paths
#> model_summary$fSquare
#> predict_summary <- summary(predict_pls(pls_model, technique = predict_DA, noFolds = 10, reps = 10))
#>
#> # Bootstrapped structural paths
#> boot_summary <- summary(boot_model)
#> boot_summary$bootstrapped_pathsConstruct modes
A construct’s mode is reflective,
composite, formative, or
single_item. lavaan syntax generation is intended for
reflective measurement models, which is why the CFA and CB-SEM models
use the default reflective mode. PLS-SEM syntax uses composite
constructs, as above.
Model JSON and a reporting template
A model serialises to JSON for storage in a .sframe
file, and sframe_model_outline() writes a short reporting
outline.
cat(sframe_model_outline(sem_model, include_json = FALSE))
#> # Model: Digital marketing structural model
#>
#> Type: cb_sem
#> Engine: lavaan
#>
#> ## Constructs
#> - DME (reflective): dmre_1, dmre_2, dmre_3, dmre_4, dmau_1, dmau_2, dmau_3, dmeu_1, dmeu_2, dmeu_3, dmpv_1, dmpv_2, dmpv_3, dmpv_4
#> - DSQ (reflective): dsqa_1, dsqa_2, dsqa_3, dsqt_1, dsqt_2, dsqt_3, dsqt_4, dsqt_5
#> - DSUQ (reflective): dsuq_1, dsuq_2, dsuq_3, dsuq_4, dsuq_5
#> - TS (reflective): ts_1, ts_2, ts_3
#> - BI (reflective): bi_1, bi_2, bi_3
#>
#> ## Structural Paths
#> - DME -> DSQ
#> - DME -> DSUQ
#> - DME -> TS
#> - DME -> BI
#> - DSQ -> TS
#> - DSQ -> BI
#> - DSUQ -> TS
#> - DSUQ -> BI
#> - TS -> BI
#>
#> ## Reporting Notes
#> Report:
#> - estimator and missing-data handling
#> - chi-square, degrees of freedom and p value
#> - CFI, TLI, RMSEA with its confidence interval, and SRMR
#> - standardised loadings
#> - reliability (alpha, omega or composite reliability) and AVE
#> - discriminant validity
#> - any modifications made to the model and the reason for each
#> - structural paths with confidence intervals
#> - R squared for each endogenous construct
#> - indirect and total effects with bootstrap confidence intervalsWhere the syntax goes next
The generated lavaan syntax is copied into lavaan::cfa()
or lavaan::sem() after choosing the estimator, ordered-item
handling, and missing-data treatment. The generated seminr syntax is
copied into a script where seminr is installed and the bootstrap
settings and construct modes are set. The published study fitted the
2-stage PLS model in seminr with 1000 bootstrap resamples.