From 45f3cce6b8e3d52fb71f43c1f95fb78ec2b77afb Mon Sep 17 00:00:00 2001 From: "Andreas M. Brandmaier" Date: Wed, 17 Apr 2024 09:59:04 +0200 Subject: [PATCH] test for single predictor chi2 vs multigroup --- tests/testthat/test-single-predictor.R | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/testthat/test-single-predictor.R diff --git a/tests/testthat/test-single-predictor.R b/tests/testthat/test-single-predictor.R new file mode 100644 index 0000000..788ae84 --- /dev/null +++ b/tests/testthat/test-single-predictor.R @@ -0,0 +1,41 @@ +library(lavaan) +library(semtree) +set.seed(1238) + +N <- 500 + +# simulate data with Cohen's d = 2 +da <- data.frame(y = c(rnorm(N/2, mean = -1), rnorm(N/2, mean = 1)), + z = rep(c(0,1),each=N/2) ) + +m_lav <- ' +y ~~ y +y ~ 1 +' + +m_lav_constrained <- ' +y ~~ y +y ~ c(a,a)*1 +' + +####Testing semtree with lavaan models #### +fit_lav <- lavaan(model = m_lav, data = Data) + +forest <- semforest(model=fit_lav, data=da, + control = semforest.control( + num.trees = 50, control=semtree_control(method="score"))) + +vim <- varimp(forest) + +plot(vim) + +zimp <- semtree:::aggregateVarimp(vim) + +fit_lav_multigroup <- lavaan(model = m_lav, data = da,group = "z") +fit_lav_multigroup2 <- lavaan(model = m_lav_constrained, data = da,group = "z") +lrt <- anova(fit_lav_multigroup,fit_lav_multigroup2) +chi2 <- lrt$`Chisq diff`[2] + + +cat("Importance: ", zimp,"\n") +cat("MG Chi^2: ",chi2,"\n")