Reporting and visualization
Source:vignettes/reporting-and-visualization.Rmd
reporting-and-visualization.RmdGoal
This guide focuses on output workflows:
- Prepare comparison tables for markdown reports.
- Export fit tables to Word.
- Visualize standardized factor loadings.
Setup
library(psymetrics)
library(lavaan)
model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'
fit_mlr <- cfa(
model,
data = HolzingerSwineford1939,
estimator = "MLR"
)
fit_ulsm <- cfa(
model,
data = HolzingerSwineford1939,
estimator = "ULSM"
)
fit_table <- compare_model_fit(MLR = fit_mlr, ULSM = fit_ulsm)Format results for markdown, HTML, and auto output
To request markdown output explicitly:
format_results(fit_table, output = "markdown")|MODEL | NOBS | ESTIMATOR | NPAR | Chi2(24) | p (Chi2) | CFI | TLI | RMSEA | RMSEA CI | SRMR |
|:-----|:----:|:---------:|:----:|:--------:|:--------:|:-----:|:-----:|:-----:|:--------------:|:-----:|
|MLR | 301 | MLR | 21 | 87.13 | < .001 | 0.925 | 0.888 | 0.093 | [0.073, 0.115] | 0.065 |
|ULSM | 301 | ULSM | 21 | 90.60 | < .001 | 0.931 | 0.897 | 0.096 | [0.073, 0.120] | 0.059 |In an HTML report/article, running that call renders a formatted table:
| MODEL | NOBS | ESTIMATOR | NPAR | Chi2(24) | p (Chi2) | CFI | TLI | RMSEA | RMSEA CI | SRMR |
|---|---|---|---|---|---|---|---|---|---|---|
| MLR | 301 | MLR | 21 | 87.13 | < .001 | 0.925 | 0.888 | 0.093 | [0.073, 0.115] | 0.065 |
| ULSM | 301 | ULSM | 21 | 90.60 | < .001 | 0.931 | 0.897 | 0.096 | [0.073, 0.120] | 0.059 |
By default, format_results(fit_table) uses
output = "auto" and chooses markdown or HTML according to
the rendering context:
In HTML-capable contexts, you can explicitly request HTML:
format_results(fit_table, output = "html")| MODEL | NOBS | ESTIMATOR | NPAR | Chi2(24) | p (Chi2) | CFI | TLI | RMSEA | RMSEA CI | SRMR |
|---|---|---|---|---|---|---|---|---|---|---|
| MLR | 301 | MLR | 21 | 87.13 | < .001 | 0.925 | 0.888 | 0.093 | [0.073, 0.115] | 0.065 |
| ULSM | 301 | ULSM | 21 | 90.60 | < .001 | 0.931 | 0.897 | 0.096 | [0.073, 0.120] | 0.059 |
Export to Word
save_table(
fit_table,
path = "model_fit.docx",
orientation = "landscape"
)Practical notes
-
format_results()is useful for reproducible reports. -
save_table()is ideal for manuscript-ready.docxtables. - Use plotting as a communication layer after checking fit and parameters.
Next steps
- Return to Get started with fit indices.
- Explore the Reference for argument-level control.
