Hi R users, I am new in R. I would like to perform confirmatory factor analysis for a data set of countries. My data are: data <- read.csv("ses.raw", header = TRUE) attach(data) names(data) [1] "idcntry" "momed" "daded" "dadocu" "momocu" "hompos" "finan" The country id is "idcntry", my model is "ses.model", and variables to be included in the analysis are "momed" "daded" "dadocu" "momocu" "hompos" "finan" . How can I run cfa<-sem(ses.model, cov(data[,2:7], nrow(data))) summary(cfa) by country? I am able to perform sem on all data by not by country. I tried by(data[,2:7], idcntry, function(x) sem(ses.model, cov(data[,2:7]), nrow(data))) but the output is the same for all countries. Thank you, Daniel
You may want to take a look at the lavaan package and use the multigroup analysis there (and see if you even need to group by country as well). Otherwise, you could do something like library(sem) library(plyr) cfa_func<-function(a.df){ cfa<-sem(ses.model, cov(a.df[,2:7], nrow(a.df))) print(summary(cfa)) } d_ply(data, "idcntry", cfa_func) -Jarrett On Jul 20, 2010, at 6:22 AM, Daniel Caro wrote:> Hi R users, > > I am new in R. I would like to perform confirmatory factor analysis > for a data set of countries. My data are: > > data <- read.csv("ses.raw", header = TRUE) > attach(data) > names(data) > > [1] "idcntry" "momed" "daded" "dadocu" "momocu" "hompos" "finan" > > > The country id is "idcntry", my model is "ses.model", and variables to > be included in the analysis are "momed" "daded" "dadocu" "momocu" > "hompos" "finan" . How can I run > > cfa<-sem(ses.model, cov(data[,2:7], nrow(data))) > summary(cfa) > > by country? I am able to perform sem on all data by not by country. I tried > > by(data[,2:7], idcntry, function(x) sem(ses.model, cov(data[,2:7]), nrow(data))) > > but the output is the same for all countries. > > Thank you, > Daniel > > ______________________________________________ > 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.
Hi Jarret, Thank you for your answer. I get the following message: Error in cov(a.df[, 2:7], nrow(a.df)) : incompatible dimensions The function seems to run for some countries but then appears to stop when a country has incomplete data (1 var is missing, for example). How to force the function to continue or skip those countries? Thank you, Daniel On Tue, Jul 20, 2010 at 7:24 PM, Jarrett Byrnes <byrnes at msi.ucsb.edu> wrote:> You may want to take a look at the lavaan package and use the multigroup analysis there (and see if you even need to group by country as well). > > Otherwise, you could do something like > > library(sem) > library(plyr) > > cfa_func<-function(a.df){ > > ? ? ? ?cfa<-sem(ses.model, cov(a.df[,2:7], nrow(a.df))) > ? ? ? ?print(summary(cfa)) > } > d_ply(data, "idcntry", cfa_func) > > -Jarrett > > On Jul 20, 2010, at 6:22 AM, Daniel Caro wrote: > >> Hi R users, >> >> I am new in R. I would like to perform confirmatory factor analysis >> for a data set of countries. My data are: >> >> data <- read.csv("ses.raw", header = TRUE) >> attach(data) >> names(data) >> >> [1] "idcntry" "momed" ? "daded" ? "dadocu" ?"momocu" ?"hompos" ?"finan" >> >> >> The country id is "idcntry", my model is "ses.model", and variables to >> be included in the analysis are "momed" ? "daded" ? "dadocu" ?"momocu" >> "hompos" ?"finan" . How can I run >> >> cfa<-sem(ses.model, cov(data[,2:7], nrow(data))) >> summary(cfa) >> >> by country? I am able to perform sem on all data by not by country. I tried >> >> by(data[,2:7], idcntry, function(x) sem(ses.model, cov(data[,2:7]), nrow(data))) >> >> but the output is the same for all countries. >> >> Thank you, >> Daniel >> >> ______________________________________________ >> 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. > >