############ I. SWLS Download and Data Preparation # Survey URL: # http://rss.acs.unt.edu:8080/users/psychometric/survey/satisfy_life/ survey.data<-read.delim('http://bayes.acs.unt.edu:8083:8083/BayesContent/class/splus/swls.txt', fill=TRUE) # Create data frame survey.df<-as.data.frame(survey.data) # Reverse code SWLS items library(car) survey.df$Q1<-recode(survey.df$Q1,'7=1;6=2;5=3;4=4;3=5;2=6;1=7; ',as.factor.result=FALSE) survey.df$Q2<-recode(survey.df$Q2,'7=1;6=2;5=3;4=4;3=5;2=6;1=7; ',as.factor.result=FALSE) survey.df$Q3<-recode(survey.df$Q3,'7=1;6=2;5=3;4=4;3=5;2=6;1=7; ',as.factor.result=FALSE) survey.df$Q4<-recode(survey.df$Q4,'7=1;6=2;5=3;4=4;3=5;2=6;1=7; ',as.factor.result=FALSE) survey.df$Q5<-recode(survey.df$Q5,'7=1;6=2;5=3;4=4;3=5;2=6;1=7; ',as.factor.result=FALSE) # Create table of frequencies and display survey.table<-table(survey.df$Q4, survey.df$Q5) survey.table # Display as proportions of total N prop.table(survey.table) # Test for independence summary(survey.table) # Plot barchart barplot(survey.table) ################################################### ############# II. Data Exploration of SWLS # SWLS scores swls.scores<-apply(as.matrix(cbind(survey.df$Q1,survey.df$Q2,survey.df$Q3,survey.df$Q4,survey.df$Q5)), 1, sum) summary(swls.scores) survey.df<-cbind(survey.df, swls.scores) par(mfrow=c(1,1)) # Fit regression survey.df.lm<-lm(survey.df$employ_other~survey.df$swls.scores) summary(survey.df.lm) # Plot a scatterplot scatterplot(jitter(survey.df$employ_other)~jitter(survey.df$swls.scores), data=survey.df) ###################################################### ############# III. Summary statistics of SWLS items survey.df<-data.frame(cbind(survey.df, swls.scores)) swls.items<-data.frame(survey.df$Q1,survey.df$Q2, survey.df$Q3,survey.df$Q4, survey.df$Q5) # Summary statistics of SWLS items and the SWLS scores summary(swls.items.scores) # Correlation the SWLS items with the SWLS scores cor(swls.items.scores) # Total test variance var(swls.scores) # Sum of the entries in the variance/covariance matrix cov(swls.items) sum(cov(swls.items)) # Scatterplot matrix of SWLS items scatterplot.matrix(~jitter(Q1)+jitter(Q2)+jitter(Q3)+jitter(Q4)+jitter(Q5)+jitter(swls.scores), reg.line=lm, smooth=TRUE, span=0.5, diagonal = 'density', data=survey.df) ####################################################### ############# IV. Exploratory Factor Analysis swls.items<-data.frame(survey.df$Q1,survey.df$Q2, survey.df$Q3,survey.df$Q4, survey.df$Q5) summary(swls.items) cor(swls.items) cov(swls.items) S<-cov(swls.items) # Exploratory Factor Analysis (EFA) - 1 factor library(stats) factanal1.fit<-factanal(swls.items, factors=1, scores='regression') factanal1.fit swls.items<-cbind(swls.items, factanal1.fit$scores) # Scatterplot matrix of SWLS items with Factor 1 scores scatterplot.matrix(~jitter(survey.df.Q1)+jitter(survey.df.Q2)+jitter(survey.df.Q3)+jitter(survey.df.Q4)+jitter(survey.df.Q5)+jitter(Factor1), reg.line=lm, smooth=TRUE, span=0.5, diagonal = 'density', data=swls.items) ######################################################## ################ V. Confirmatory Factor Analysis of SWLS Items swls.items<-data.frame(survey.df$Q1,survey.df$Q2, survey.df$Q3,survey.df$Q4, survey.df$Q5) # Confirmatory factor analysis (CFA) - 1 factor library(sem) swls.cor<-cor(swls.items) swls.cor[upper.tri(swls.cor)] <- 0 R.swls<-swls.cor model.swls<-matrix(c('F1->Q1','lam11',NA, 'F1->Q2','lam21',NA, 'F1->Q3','lam31',NA, 'F1->Q4','lam41',NA, 'F1->Q5','lam51',NA, 'Q1<->Q1','th1',NA, 'Q2<->Q2','th2',NA, 'Q3<->Q3','th3',NA, 'Q4<->Q4','th4',NA, 'Q5<->Q5','th5',NA, 'F1<->F1',NA,1), ncol=3, byrow=T) obs.vars.swls<-c('Q1','Q2','Q3','Q4','Q5') sem.swls<-sem(model.swls, R.swls, 161, obs.vars.swls) summary(sem.swls) ############################################################################ # Generalizability and Reliability Using SEM Framework swls.items<-data.frame(survey.df$Q1,survey.df$Q2, survey.df$Q3,survey.df$Q4, survey.df$Q5) # Confirmatory factor analysis (CFA) - 1 factor library(sem) swls.cov<-cov(swls.items) swls.cov sum(swls.cov) swls.cov[upper.tri(swls.cov)] <- 0 S.swls<-swls.cov model.swls<-matrix(c('F1->Q1','lam11',NA, 'F1->Q2','lam21',NA, 'F1->Q3','lam31',NA, 'F1->Q4','lam41',NA, 'F1->Q5','lam51',NA, 'Q1<->Q1','th1',NA, 'Q2<->Q2','th2',NA, 'Q3<->Q3','th3',NA, 'Q4<->Q4','th4',NA, 'Q5<->Q5','th5',1, 'F1<->F1',NA,1), ncol=3, byrow=T) obs.vars.swls<-c('Q1','Q2','Q3','Q4','Q5') sem.swls<-sem(model.swls, S.swls, 161, obs.vars.swls, start.tol=1E-10) summary(sem.swls) # Loadings (first 5 elements) and Uniqueness/Error Variance (second 5 elements) # Note that the Uniqueness/Error terms are the 'psi-squared' terms in McDonald's book sem.swls$coeff omega.partA<-sum(sem.swls$coeff[1:5])^2 omega.partA omega.partB<-sum(c(sem.swls$coeff[6],sem.swls$coeff[7],sem.swls$coeff[8],sem.swls$coeff[9],sem.swls$coeff[10])) omega.partB # Calculate total test variance based on loadings and errors total.swls.var<-omega.partA+omega.partB total.swls.var # Calculate McDonald's Omega coefficient omega.swls<-(omega.partA)/(omega.partA+omega.partB) omega.swls # Calculate Cronbach's alpha library(psy) cronbach(swls.items