Skip to content

Commit

Permalink
test for single predictor chi2 vs multigroup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandmaier committed Apr 17, 2024
1 parent 17d92d1 commit 45f3cce
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/testthat/test-single-predictor.R
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 45f3cce

Please sign in to comment.