rr-lavaan

lavaan - number of items to replace is not a multiple of replacement length


I am trying to simulate some data and then fit the model to the same data using lavaan. This is how my code looks like:

model <- '
A =~ A1 + A2 + A3
A ~~ A
A1 | -1*t1
A2 | 0*t2
A3 | 1*t3
B =~ B1 + B2 + B3
B ~~ B
B1 ~~ A2
B2 ~~ A3
'

data <- simulateData(model, sample.nobs = 100)

fit <- cfa(model = model, 
            data = data)

However, it's not working. The model doesn't run and when you call summary on the fit object, all you get is starting values. When I run warnings(), this is what I get:

Warning messages:
1: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
2: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
3: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
4: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
5: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
6: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
7: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
8: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
9: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
10: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
11: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
12: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length
13: In lavaan::lavaan(model = model, data = data, model.type = "cfa",  ... :
  lavaan WARNING:
    Model estimation FAILED! Returning starting values.
14: In th[th.idx > 0L] <- TAU[, 1L] :
  number of items to replace is not a multiple of replacement length

So I am wondering what am I doing wrong here? Any help would be appreciated, thanks!


Solution

  • for each categorical indicator, its first threshold will always be labeled t1. So if these are all binary, then your syntax should work if you update the threshold labels. Also, if you don't want your thresholds to be fixed to their parameter values, then treat them as starting values in your syntax (in which case they can still be freely estimated, but the starting values will be used as parameters when generating data).

    model <- '
    A =~ A1 + A2 + A3
    A ~~ A
    A1 | -1?t1
    A2 | 0?t1
    A3 | 1?t1
    B =~ B1 + B2 + B3
    B ~~ B
    B1 ~~ A2
    B2 ~~ A3
    '