sem-in-r / seminr

Natural feeling domain-specific language for building structural equation models in R for estimation by covariance-based methods (like LISREL/Lavaan) or partial least squares (like SmartPLS)
58 stars 19 forks source link

fix #333: Cross-Validation fails when using a tibble #349

Open jhorzek opened 4 months ago

jhorzek commented 4 months ago

predict_pls uses a matrix with variable names to subset the data set. This works with data.frames, but not with tibbles, which results in the error reported in issue #333. This fix addresses the issue by first casting the matrix to a vector when subsetting the data.

Example:

library(seminr)
library(tibble)
mobi_mm <- constructs(
  composite("Image",        multi_items("IMAG", 1:5)),
  composite("Expectation",  multi_items("CUEX", 1:3)),
  composite("Value",        multi_items("PERV", 1:2)),
  composite("Satisfaction", multi_items("CUSA", 1:3))
)

mobi_sm <- relationships(
  paths(to = "Satisfaction",
        from = c("Image", "Expectation", "Value"))
)

mobi_pls <- estimate_pls(mobi, mobi_mm, mobi_sm)
cross_validated_predictions <- predict_pls(model = mobi_pls,
                                           technique = predict_DA,
                                           noFolds = 10,
                                           cores = NULL)
# The following fails in the current version of seminr:
mobi_pls <- estimate_pls(tibble::as_tibble(mobi), mobi_mm, mobi_sm)
cross_validated_predictions_tibble <- predict_pls(model = mobi_pls,
                                                  technique = predict_DA,
                                                  noFolds = 10,
                                                  cores = NULL)