Jānis Mucenieks
2013-Aug-07 09:36 UTC
[R] CFA and Factor scores from ordered Likert scale items using SEM and Lavaan packages
Hello! I'm trying to do some confirmatory factor analysis with for ordered Likert scale items using sem and lavaan packages. So far I have read a lot and searched a lot but working example has so far eluded me. Can someone please take a look at the data and tell me if what I'm doing is correct (lavaan case) and where is the error (sem case)? Original data (ordered factors): EI.data <- structure(list(Q21a_makeAnythingEntrep_likert = structure(c(3L, 1L, 7L, 1L, 1L, 6L, 1L, 1L, 5L, 1L, 1L, 1L, 3L, 4L, 1L, 7L, 2L, 1L, 7L, 7L, 1L, 3L, 1L, 2L, 5L, 1L, 5L, 2L, 4L, 1L, 1L, 2L), .Label c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor")), Q21b_goalBecomingEntrep_likert = structure(c(3L, 1L, 7L, 1L, 1L, 6L, 1L, 1L, 7L, 1L, 1L, 7L, 3L, 2L, 2L, 7L, 2L, 3L, 7L, 7L, 1L, 4L, 1L, 2L, 4L, 2L, 6L, 3L, 5L, 1L, 1L, 1L), .Label c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor" )), Q21c_effortFirm_likert = structure(c(3L, 3L, 7L, 1L, 1L, 6L, 1L, 1L, 5L, 1L, 1L, 5L, 3L, 3L, 1L, 7L, 2L, 2L, 7L, 7L, 1L, 5L, 1L, 2L, 4L, 2L, 7L, 2L, 5L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor" )), Q21d_createFirm_likert = structure(c(3L, 3L, 7L, 1L, 1L, 6L, 1L, 2L, 7L, 1L, 1L, 7L, 3L, 6L, 3L, 7L, 2L, 2L, 7L, 5L, 1L, 6L, 1L, 2L, 4L, 5L, 7L, 5L, 4L, 1L, 5L, 6L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor" )), Q21e_.thoughtFirm_likert = structure(c(3L, 6L, 7L, 1L, 1L, 6L, 2L, 1L, 7L, 1L, 4L, 7L, 5L, 4L, 2L, 7L, 2L, 2L, 7L, 3L, 1L, 5L, 1L, 2L, 6L, 2L, 7L, 4L, 5L, 2L, 6L, 2L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor" )), Q21f_intentionFirm_likert = structure(c(4L, 4L, 7L, 1L, 1L, 6L, 1L, 2L, 7L, 1L, 1L, 7L, 5L, 6L, 3L, 7L, 2L, 2L, 7L, 2L, 1L, 7L, 1L, 2L, 6L, 4L, 7L, 7L, 4L, 2L, 5L, 5L), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered", "factor" ))), .Names = c("Q21a_makeAnythingEntrep_likert", "Q21b_goalBecomingEntrep_likert", "Q21c_effortFirm_likert", "Q21d_createFirm_likert", "Q21e_.thoughtFirm_likert", "Q21f_intentionFirm_likert"), row.names = c(NA, 32L), class = "data.frame") Original data (continuous numbers): EI.dataN <- structure(list(Q21a_makeAnythingEntrep_likert = c(3L, 1L, 7L, 1L, 1L, 6L, 1L, 1L, 5L, 1L, 1L, 1L, 3L, 4L, 1L, 7L, 2L, 1L, 7L, 7L, 1L, 3L, 1L, 2L, 5L, 1L, 5L, 2L, 4L, 1L, 1L, 2L), Q21b_goalBecomingEntrep_likert = c(3L, 1L, 7L, 1L, 1L, 6L, 1L, 1L, 7L, 1L, 1L, 7L, 3L, 2L, 2L, 7L, 2L, 3L, 7L, 7L, 1L, 4L, 1L, 2L, 4L, 2L, 6L, 3L, 5L, 1L, 1L, 1L), Q21c_effortFirm_likert = c(3L, 3L, 7L, 1L, 1L, 6L, 1L, 1L, 5L, 1L, 1L, 5L, 3L, 3L, 1L, 7L, 2L, 2L, 7L, 7L, 1L, 5L, 1L, 2L, 4L, 2L, 7L, 2L, 5L, 1L, 1L, 1L), Q21d_createFirm_likert = c(3L, 3L, 7L, 1L, 1L, 6L, 1L, 2L, 7L, 1L, 1L, 7L, 3L, 6L, 3L, 7L, 2L, 2L, 7L, 5L, 1L, 6L, 1L, 2L, 4L, 5L, 7L, 5L, 4L, 1L, 5L, 6L), Q21e_.thoughtFirm_likert = c(3L, 6L, 7L, 1L, 1L, 6L, 2L, 1L, 7L, 1L, 4L, 7L, 5L, 4L, 2L, 7L, 2L, 2L, 7L, 3L, 1L, 5L, 1L, 2L, 6L, 2L, 7L, 4L, 5L, 2L, 6L, 2L), Q21f_intentionFirm_likert = c(4L, 4L, 7L, 1L, 1L, 6L, 1L, 2L, 7L, 1L, 1L, 7L, 5L, 6L, 3L, 7L, 2L, 2L, 7L, 2L, 1L, 7L, 1L, 2L, 6L, 4L, 7L, 7L, 4L, 2L, 5L, 5L)), .Names = c("Q21a_makeAnythingEntrep_likert", "Q21b_goalBecomingEntrep_likert", "Q21c_effortFirm_likert", "Q21d_createFirm_likert", "Q21e_.thoughtFirm_likert", "Q21f_intentionFirm_likert"), class = "data.frame", row.names = c(NA, -32L)) Lavaan case: EI.model.lavaan <- ' EI =~ Q21a_makeAnythingEntrep_likert + Q21b_goalBecomingEntrep_likert + Q21c_effortFirm_likert + Q21d_createFirm_likert + Q21e_.thoughtFirm_likert + Q21f_intentionFirm_likert ' EI.fit <- cfa(EI.model.lavaan, data = EI.data) summary(EI.fit, fit.measures = TRUE) predict(EI.fit) Sem continuous items case: EI.dataN.cov <- cov(EI.dataN) model.EI.dataN <- specifyModel() EI -> Q21a_makeAnythingEntrep_likert, NA, 1 EI -> Q21b_goalBecomingEntrep_likert, ei1 EI -> Q21c_effortFirm_likert, ei2 EI -> Q21d_createFirm_likert, ei3 EI -> Q21e_.thoughtFirm_likert, ei4 EI -> Q21f_intentionFirm_likert, ei5 EI <-> EI, var1 Q21a_makeAnythingEntrep_likert <-> Q21a_makeAnythingEntrep_likert, error1 Q21b_goalBecomingEntrep_likert <-> Q21b_goalBecomingEntrep_likert, error2 Q21c_effortFirm_likert <-> Q21c_effortFirm_likert, error3 Q21d_createFirm_likert <-> Q21d_createFirm_likert, error4 Q21e_.thoughtFirm_likert <-> Q21e_.thoughtFirm_likert, error5 Q21f_intentionFirm_likert <-> Q21f_intentionFirm_likert, error6 EI.dataN.cfa <- sem( model.EI.dataN, EI.dataN.cov, nrow(EI.dataN)) summary(EI.dataN.cfa) fscores(EI.dataN.cfa, EI.dataN) Sem ordered items case: hcor <- function(data) hetcor(data, std.err=FALSE)$correlations R.EI.data <- hcor(EI.data) EI.model.sem.o <- specifyModel() EI -> Q21a_makeAnythingEntrep_likert, NA, 1 EI -> Q21b_goalBecomingEntrep_likert, ei1 EI -> Q21c_effortFirm_likert, ei2 EI -> Q21d_createFirm_likert, ei3 EI -> Q21e_.thoughtFirm_likert, ei4 EI -> Q21f_intentionFirm_likert, ei5 EI <-> EI, var1 Q21a_makeAnythingEntrep_likert <-> Q21a_makeAnythingEntrep_likert, error1 Q21b_goalBecomingEntrep_likert <-> Q21b_goalBecomingEntrep_likert, error2 Q21c_effortFirm_likert <-> Q21c_effortFirm_likert, error3 Q21d_createFirm_likert <-> Q21d_createFirm_likert, error4 Q21e_.thoughtFirm_likert <-> Q21e_.thoughtFirm_likert, error5 Q21f_intentionFirm_likert <-> Q21f_intentionFirm_likert, error6 cfaEI.sem.o <- sem(EI.model.sem.o, R.EI.data, nrow(EI.data)) summary(cfaEI.sem.o) fscores(cfaEI.sem.o, EI.data) Error (can't get fscrores): Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric Thank You! Janis [[alternative HTML version deleted]]
John Fox
2013-Aug-08 03:35 UTC
[R] CFA and Factor scores from ordered Likert scale items using SEM and Lavaan packages
Dear J?nis, On Wed, 7 Aug 2013 12:36:11 +0300 J?nis Mucenieks <mucenieks at gmail.com> wrote:> Hello! > I'm trying to do some confirmatory factor analysis with for ordered Likert > scale items using sem and lavaan packages. So far I have read a lot and > searched a lot but working example has so far eluded me. Can someone please > take a look at the data and tell me if what I'm doing is correct (lavaan > case) and where is the error (sem case)? >. . .> > cfaEI.sem.o <- sem(EI.model.sem.o, R.EI.data, nrow(EI.data)) > summary(cfaEI.sem.o) > fscores(cfaEI.sem.o, EI.data) > > Error (can't get fscrores): > Error in colMeans(x, na.rm = TRUE) : 'x' must be numericfscores() can't handle ordinal data. Also, though it's not relevant to this error, you could greatly simplify the model specification by using cfa() instead of specifyModel(). More generally, specifyEquations() is usually more convenient than specifyModel(). Best, John> > Thank You! > Janis > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.------------------------------------------------ John Fox Sen. William McMaster Prof. of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/