ecmerkle / blavaan

An R package for Bayesian structural equation modeling
https://ecmerkle.github.io/blavaan
87 stars 23 forks source link

worrisome warnings #93

Open jpritikin opened 1 month ago

jpritikin commented 1 month ago

lavaan 0.6-19 (cran), blavaan 0.5-6.1311 (via github)

Are these warnings harmless?

Warning messages:
1: lavaan->lavParTable():  
   using a single label per parameter in a multiple group setting implies imposing 
   equality constraints across all the groups; If this is not intended, either remove 
   the label(s), or use a vector of labels (one for each group); See the Multiple 
   groups section in the man page of model.syntax. 
2: In TH[[i]][exidx] <- fit$theta[fit$th.idx] :
  number of items to replace is not a multiple of replacement length
3: lavaan->lav_samplestats_step2():  
   correlation between variables sleep and anxiety is (nearly) 1.0 
4: In TH[[i]][exidx] <- fit$theta[fit$th.idx] :
  number of items to replace is not a multiple of replacement length

Code:

library(rstan)
options(mc.cores = parallel::detectCores()/2)
library(blavaan)

future::plan("multicore")

NoYesLevels <- c('No','Not sure','Yes')
EuphoricLevels <- c('Euphoric', 'Mildly euphoric', 'No change', 'Mild discomfort', 'Severe discomfort')
ImpactLevels <- c('Helped','Slightly helped','No impact','Slightly hindered','Hindered')
AbsItemNames <- c('boredom','frustration','anxiety', 'abort','bodyAcute','bodyAfter','sleep')

ImportStudyItems <- function(df) {
  if (any(is.na(match(AbsItemNames, colnames(df))))) stop("Items are mising")

  df[['boredom']] <- ordered(df[['boredom']], levels=NoYesLevels)
  df[['frustration']] <- ordered(df[['frustration']], levels=NoYesLevels)
  df[['anxiety']] <- ordered(df[['anxiety']], levels=NoYesLevels)
  df[['abort']] <- ordered(df[['abort']], levels=NoYesLevels)
  df[['bodyAcute']] <- ordered(df[['bodyAcute']], levels=EuphoricLevels)
  df[['bodyAfter']] <- ordered(df[['bodyAfter']], levels=EuphoricLevels)
  df[['sleep']] <- ordered(df[['sleep']], levels=ImpactLevels)
  df
}

psiData <- ImportStudyItems(read.csv("psi.csv"))
pmData <- ImportStudyItems(read.csv("pm.csv"))
combinedData <- rbind(cbind(psiData, group="psi"),
                      cbind(pmData, group="pm"))
combinedData$group <- factor(combinedData$group, levels=c('psi','pm'))

spec <- '
phenom =~ boredomL*boredom + frustrationL*frustration + anxietyL*anxiety + abortL*abort + bodyAcuteL*bodyAcute + bodyAfterL*bodyAfter + sleepL*sleep

phenom ~ c(0,NA)*1
boredom ~ boredomM*1
frustration ~ frustrationM*1
anxiety ~ anxietyM*1
abort ~ abortM*1
bodyAcute ~ bodyAcuteM*1
bodyAfter ~ bodyAfterM*1
sleep ~ sleepM*1

phenom ~~ 1*phenom
boredom ~~ 1*boredom
frustration ~~ 1*frustration
anxiety ~~ 1*anxiety
abort ~~ 1*abort
bodyAcute ~~ 1*bodyAcute
bodyAfter ~~ 1*bodyAfter
sleep ~~ 1*sleep

boredom | -.431*t1 + .431*t2
frustration | -.431*t1 + .431*t2
anxiety | -.431*t1 + .431*t2
abort | -.431*t1 + .431*t2
bodyAcute | -.842*t1 + -.253*t2 + .253*t3 + .842*t4
bodyAfter | -.842*t1 + -.253*t2 + .253*t3 + .842*t4
sleep | -.842*t1 + -.253*t2 + .253*t3 + .842*t4
'

fit <- blavaan(spec, data=combinedData,
               n.chains = 4,
               ordered = AbsItemNames,
               dp = dpriors(nu="normal(0,1)", alpha="normal(0,1)", lambda="normal(0,1)"),
               group = "group",
               std.lv=TRUE, allow.empty.cell = TRUE, test = "none")
summary(fit)

Data: pm.csv psi.csv

ecmerkle commented 3 weeks ago

I think the first warning is related to lines like this:

boredom ~ boredomM*1

you have two groups, but only one parameter label "boredomM". That implicitly fixes the parameter to be equal across groups. If you didn't want that, you need something like

boredom ~ c("boredom1", "boredom2") * 1

I believe the other warnings are related to lavaan setting up a model with empty cells and etc. I suspect they are harmless but will need to look.

jpritikin commented 3 weeks ago

you have two groups, but only one parameter label "boredomM". That implicitly fixes the parameter to be equal across groups.

Fixing these parameters equal across groups is desired and intentional. I guess that warning is fine.