使用lavaan::cfa执行验证性因子分析时遭遇「unused arguments」错误的技术求助
unused arguments Error in lavaan's cfa() Function It looks like you’re hitting two key issues here: invalid arguments passed to the cfa() function, and incorrect comment syntax in your model string. Let’s break down how to fix both:
1. Invalid Arguments in cfa()
The error unused arguments (text = theory_syn_eq, reference.indicators = FALSE) occurs because:
- Lavaan’s
cfa()function doesn’t accept atextparameter—you should pass your model using themodelargument (or directly as the first positional input). - There’s no
reference.indicatorsparameter. If your goal is to avoid using reference indicators (i.e., fix latent variable variances instead of locking an indicator’s loading to 1), usestd.lv = TRUEinstead.
2. Incorrect Comment Syntax in Model String
Lavaan uses # to define group models, not for comments. To add descriptive labels for your factors, replace # with // (the proper syntax for comments in lavaan’s model language). Your original string uses # which will cause parsing errors.
Corrected Code
Here’s the fixed version of your code:
# Fix model syntax comments theory_syn_eq <- " ZAV: Z1, Z2, Z3, Z4, Z5, Z6 // Závažnost DOP: D1, D2, D3, D4, D5, D6 // Dopadení PPZ: Pr1, Pr2, Pr3, Pr4, Pr5, Pr6 // Problémproživot ROD: R1, R2, R3, R4, R5, R6 // Rodiče PRA: P1, P2, P3, P4, P5, P6 // Přátele SP: S1, S2, S3, S4, S5, S6 // Školapráce " # Use correct cfa() arguments library(lavaan) theory_syn <- cfa(model = theory_syn_eq, std.lv = TRUE)
Explanation
std.lv = TRUE: This tells lavaan to set each latent variable’s variance to 1, instead of fixing the first indicator’s loading to 1 (the default behavior). This achieves what you were aiming for withreference.indicators = FALSE.//for comments: This ensures lavaan parses your model correctly without misinterpreting factor labels as group specifications.
If you still run into issues, double-check that your dataset includes all variables listed in the model (Z1-Z6, D1-D6, etc.) and that you’ve loaded the lavaan package before running cfa().
内容的提问来源于stack exchange,提问作者David Kružlík

