Maraschtrumphy
Maraschtrumphy

Reputation: 31

R Lavaan package ERROR: some latent variable names collide with observed variable names

I try without success to run a confirmatory factor analysis (cfa) with R using the Lavaan package.

When I fit the model, I always receive this message:

Error in lavaan::lavaan(model = cfa1, data = data_study1, model.type = "cfa", : lavaan ERROR: some latent variable names collide with observed variable names: peerinfluence lowrisk

I have tried everything: change variable names, eliminate missing data, make sure my variable class was numeric, but always the same error message.

There is my code:

library(lavaan)
library(MIIVsem)
library(tidyverse)
library(haven)

read_sav("MDUICQ_CFA.sav")

data_study1 <- read_sav("MDUICQ_CFA.sav")

attach(MDUICQ_CFA)

cfa1 <- '
peerinfluence =~ X1_1 + X2_13 +  X1_2
lowrisk =~ X1_9 + X1_10 +  X1_11
'

fit <- cfa(cfa1, data=data_study1)

summary(fit, fit.measures = TRUE)'

I have never used R and I imported my data from SPSS.

Thank you for your help.

Upvotes: 3

Views: 4037

Answers (2)

rempsyc
rempsyc

Reputation: 1018

To add to jordi's answer, I would like to add that in my case, I had calculated the "latent" variables separately beforehand (just simple row means of the items of that scale).

When I tried to run the latent variables in my SEM model, I had the error mentioned by OP, because lavaan detected that the variables already existed in my data frame, and it didn't like that. The solution was to create a copy of the dataset without those precalculated variables (averages). Then it stopped complaining and it worked on the first try.

Upvotes: 0

jordi
jordi

Reputation: 111

I am myself a beginner with lavaan, but I would suggest that one or both of your latent variables (peerinfluence and lowrisk) have the same name as some variable in your dataset.

If you want to use regressions with existing variables, you should use ~ instead of =~, as in:

cfa1 <- '  
    peerinfluence ~ X1_1 + X2_13 +  X1_2  
    lowrisk ~ X1_9 + X1_10 +  X1_11  
    '

Upvotes: 3

Related Questions