Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fit by group table #145

Merged
merged 10 commits into from
May 21, 2024
Merged

Conversation

LSLindeloo
Copy link
Contributor

@LSLindeloo LSLindeloo commented Feb 17, 2023

jasp-stats/jasp-issues#711 (fixed for SEM)
fixes jasp-stats/jasp-issues#2690
also adds chi^2 diff tests for multiple models and groups

Copy link
Member

@Kucharssim Kucharssim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, just a couple of comments

@@ -203,7 +203,7 @@ checkLavaanModel <- function(model, availableVars) {
modelContainer <- createJaspContainer()
modelContainer$dependOn(c("samplingWeights", "meanStructure", "manifestInterceptFixedToZero", "latentInterceptFixedToZero", "exogenousCovariateFixed", "orthogonal",
"factorScaling", "residualSingleIndicatorOmitted", "residualVariance", "exogenousLatentCorrelation",
"dependentCorrelation", "threshold", "scalingParameter", "efaConstrained", "standardizedVariable", "naAction", "estimator", "test",
"dependentCorrelation", "threshold", "scalingParameter", "efaConstrained", "standardizedVariable", "naAction", "estimator", "modelTest",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change it here? Was this a bug?

@@ -360,11 +360,11 @@ checkLavaanModel <- function(model, availableVars) {
lavopts[["estimator"]] <- options[["estimator"]]
lavopts[["se"]] <- ifelse(options[["errorCalculationMethod"]] == "bootstrap", "standard", options[["errorCalculationMethod"]])
lavopts[["information"]] <- options[["informationMatrix"]]
lavopts[["test"]] <- ifelse(options[["modelTest"]] == "satorraBentler", "Satorra.Bentler",
ifelse(options[["modelTest"]] == "yuanBentler", "Yuan.Bentler",
lavopts[["test"]] <- ifelse(options[["modelTest"]] == "satorraBentler", "satorra.bentler",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here, was this a bug?

R/sem.R Outdated
grouptab$addColumnInfo(name = "AIC", title = gettext("AIC"), type = "number" )
grouptab$addColumnInfo(name = "BIC", title = gettext("BIC"), type = "number" )
grouptab$addColumnInfo(name = "N", title = gettext("n"), type = "integer")
grouptab$addColumnInfo(name = "Chisq", title = gettext("&#967;&sup2;"), type = "number" ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grouptab$addColumnInfo(name = "Chisq", title = gettext("&#967;&sup2;"), type = "number" ,
grouptab$addColumnInfo(name = "Chisq", title = "\u03C7\u00B2", type = "number" ,

R/sem.R Outdated
grouptab$addColumnInfo(name = "PrChisq", title = gettext("p"), type = "pvalue",
overtitle = gettext("Baseline test"))
if (length(options[["models"]]) > 1) {
grouptab$addColumnInfo(name = "dchisq", title = gettext("\u0394\u03C7\u00B2"), type = "number" ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grouptab$addColumnInfo(name = "dchisq", title = gettext("\u0394\u03C7\u00B2"), type = "number" ,
grouptab$addColumnInfo(name = "dchisq", title = "\u0394\u03C7\u00B2", type = "number" ,

if it's only symbols we don't need to translate

R/sem.R Outdated
if (length(options[["models"]]) > 1) {
grouptab$addColumnInfo(name = "dchisq", title = gettext("\u0394\u03C7\u00B2"), type = "number" ,
overtitle = gettext("Difference test"))
grouptab$addColumnInfo(name = "ddf", title = gettext("\u0394df"), type = "integer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we only want to translate the "df"

Suggested change
grouptab$addColumnInfo(name = "ddf", title = gettext("\u0394df"), type = "integer",
grouptab$addColumnInfo(name = "ddf", title = gettextf("%1$sdf", "\u0394"), type = "integer",

@juliuspfadt
Copy link
Contributor

juliuspfadt commented Feb 21, 2024

Screenshot 2024-02-21 at 16 32 27
Screenshot 2024-02-21 at 16 33 11

@juliuspfadt
Copy link
Contributor

although this seems wrong to me, why would the same model with fewer Ns have fewer Df?

@juliuspfadt
Copy link
Contributor

It also seems very annoying that we have to fit the model again for each group, if only lavaan would give us the AIC and BIC

R/sem.R Outdated
Comment on lines 597 to 609
equalityConstraints <- syntax[syntax$op == "==",]
if (nrow(equalityConstraints) > 0) {
for (j in 1:nrow(equalityConstraints)) {
if(equalityConstraints[j, "lhs"] %in% syntax_group[, "plabel"]) {
syntax_group <- rbind(syntax_group, equalityConstraints[j,])
}
}
}
syntax_group[["group"]] <- syntax_group[["block"]] <- rep(1, nrow(syntax_group))
lav_args[["model"]] <- syntax_group
dataset_group <- dataset[dataset[[options[["group"]]]] == groupNames[group],]
lav_args[["data"]] <- dataset_group
fit_group <- try(do.call(lavaan::lavaan, lav_args))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kucharssim, this code does not work when there are equality constraints, which is not surprising. Lorenzo fits the model for the subgroup only, but with invariance testing equality constraints are between the two groups, e.g., the loadings of group1 are equal to the loadings of group2. It seems to me that the equality constraints do not matter for the fit of each group, so we should estimate the model with free parameters unless some the constraints are not between groups but within a group. I wonder how we would find distinguish here though....

@juliuspfadt
Copy link
Contributor

juliuspfadt commented Feb 23, 2024

Okay, this should be ready as well. this is what I eventually settled on:
Screenshot 2024-02-23 at 14 37 53

@juliuspfadt
Copy link
Contributor

now like this:
Screenshot 2024-04-30 at 19 36 39

@juliuspfadt juliuspfadt force-pushed the chisqPerGroup branch 2 times, most recently from ce79b5d to 5332b4e Compare April 30, 2024 17:39
Copy link
Member

@Kucharssim Kucharssim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality looks really good, I didn't find anything suspicious in the code...

The tests fail spectacularly though, can you look into that? It seems to me these failures could be introduced by the PR, so it would be good to polish that before merging

@juliuspfadt
Copy link
Contributor

I suppose I should've checked, weird that I didn't notice the test failing. It's caused by imposing meanstructure but then not fixing any intercepts to 0. I wonder if in qml there is an option for something like a triple of checkboxes, they can be all checked but not unchecked simultaneously. Suppose .quitAnalysis will have to do for now.

@Kucharssim Kucharssim merged commit e065778 into jasp-stats:master May 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: SEM module mislabels models
3 participants