Closed brianmsm closed 1 year ago
After giving the matter some thought, I have seen that the error only occurs with the latest version of lavaan (0.6-14) and when kfa evaluates a 2-factor model specified by its same EFA result:
library(kfa)
library(lavaan)
#> This is lavaan 0.6-14
#> lavaan is FREE software! Please report any bugs.
# simulate data based on a 3-factor model with standardized loadings
sim.mod <- "f1 =~ .7*x1 + .8*x2 + .3*x3 + .7*x4 + .6*x5 + .8*x6 + .4*x7
f2 =~ .8*x8 + .7*x9 + .6*x10 + .5*x11 + .5*x12 + .7*x13 + .6*x14
f3 =~ .6*x15 + .5*x16 + .9*x17 + .4*x18 + .7*x19 + .5*x20
f1 ~~ .2*f2
f2 ~~ .2*f3
f1 ~~ .2*f3
x9 ~~ .2*x10"
set.seed(1161)
sim.data <- simstandard::sim_standardized(sim.mod,
n = 900,
latent = FALSE,
errors = FALSE)[c(2:9,1,10:20)]
# include a custom 2-factor model
custom2f <- paste0("f1 =~ ", paste(colnames(sim.data)[1:10], collapse = " + "),
"\nf2 =~ ",paste(colnames(sim.data)[11:20], collapse = " + "))
mods <- kfa(data = sim.data,
k = NULL, # NULL prompts power analysis to determine number of folds
custom.cfas = custom2f # can be a single object or named list
)
#> [1] "Using 11 cores for parallelization."
#> [1] "Finished EFAs. Starting CFAs"
semPlot::semPaths(mods$cfas[[1]]$`2-factor`)
#> Error in data.frame(label = pars$label, lhs = ifelse(pars$op == "~" | : arguments imply differing number of rows: 43, 1, 63
Created on 2023-02-21 with reprex v2.0.2
When using lavaan 0.6-13 down, there is no error in the line associated with semPlot::semPaths()
:
library(kfa)
library(lavaan)
#> This is lavaan 0.6-13
#> lavaan is FREE software! Please report any bugs.
# simulate data based on a 3-factor model with standardized loadings
sim.mod <- "f1 =~ .7*x1 + .8*x2 + .3*x3 + .7*x4 + .6*x5 + .8*x6 + .4*x7
f2 =~ .8*x8 + .7*x9 + .6*x10 + .5*x11 + .5*x12 + .7*x13 + .6*x14
f3 =~ .6*x15 + .5*x16 + .9*x17 + .4*x18 + .7*x19 + .5*x20
f1 ~~ .2*f2
f2 ~~ .2*f3
f1 ~~ .2*f3
x9 ~~ .2*x10"
set.seed(1161)
sim.data <- simstandard::sim_standardized(sim.mod,
n = 900,
latent = FALSE,
errors = FALSE)[c(2:9,1,10:20)]
# include a custom 2-factor model
custom2f <- paste0("f1 =~ ", paste(colnames(sim.data)[1:10], collapse = " + "),
"\nf2 =~ ",paste(colnames(sim.data)[11:20], collapse = " + "))
mods <- kfa(data = sim.data,
k = NULL, # NULL prompts power analysis to determine number of folds
custom.cfas = custom2f # can be a single object or named list
)
#> [1] "Using 11 cores for parallelization."
#> [1] "Finished EFAs. Starting CFAs"
semPlot::semPaths(mods$cfas[[1]]$`2-factor`)
Created on 2023-02-21 with reprex v2.0.2
Thanks for the thorough investigation. This is not the first time the rendering has been derailed by an issue with the plots, which led me to consider an argument to opt out of the plots, just as you suggest. I'll keep this issue open until I make this addition (relatively soon).
I also like the idea of printing the warnings, but I would want to do so in a more user friendly way (i.e., interpretable for non-technical audiences) than what is provided by warning = TRUE
.
Thanks for the prompt reply! I agree that the warning messages can be hidden by default, although it would be useful also a section in the report associated to convergence and undefined positive matrix problems (which are the most frequent).
I did not realize that I indicated in the first suggestion the warning = TRUE
, my intention was:
knitr::opts_chunk$set(echo = FALSE, error = TRUE)
So that the rendering does not stop, even if some of the chunks end in an error.
Yea at the moment any convergence or model issue problems are captured and counted in the improper solutions column of the model summary statement, but you would have to manually pull the model out of the kfa object to determine the specific problem.
I'm hesitate to set error = TRUE
as the rendering interruption helps identify where there might be bugs that need to be fixed or model issues that we should address more directly.
I've added the plots
argument to kfa_report()
and it should be available on the developmental version.
Unfortunately for now I cannot provide you with a fully reproducible example, however, I will try to explain the problem and how it might be feasible to solve it after having traced where it is originating from.
When evaluating models with the
kfa
function, I got one that had the following warning:This message in this model is only a warning message, since its fit indices and factor loadings are fully estimated.
When I use the
kfa_report()
function, I get the following error and the rendering never finishes executing.When tracing the error, I notice that this originates when the same specific model from which I was getting the above warning, passed as an argument to the
semPlot::semPaths()
function: https://github.com/knickodem/kfa/blob/4ac1bf9eccbac19ff49eed8df09f1667b41cfdf0/inst/rmd/kfa-report.Rmd#L120Since this error is a problem related to semPlot and cannot be solved from this package, I suggest 2 alternatives:
kfa_report(..., plots = FALSE)