summary(aov2.a) summary(aov2.b) ##compare the aggregated vs non-aggergated versions: aov2.a <- aov(rt~cond+Error(subj/cond),data=data2) aov2.b <- aov(rt~cond+Error(subj/cond),data=data2b) data2b <- aggregate(data2$rt,list(subj=data2$subj,cond=data2$cond),mean) data2b$rt <- data2b$x ##compare the aggregated vs non-aggergated versions: aov2.a <- aov(rt~cond+Error(subj/cond),data=data2) aov2.b <- aov(rt~cond+Error(subj/cond),data=data2b) summary(aov2.a) summary(aov2.b) model3<- aov(rt~ group + cond + Error(subj/(cond)),data=data3) data3 <- data.frame(subj=as.factor(c(subj, as.numeric(subj)+20)), group = rep(c("Control","Experimental"),each=300), cond = rep(x1,2), rt = rep(rt,2) + rnorm(600)) head(data3) #defining the model is as follows: because group is a between-subject factor, it #is not nested within the Error() notation, but cond is because it is a within-subject #variable. model3<- aov(rt~ group + cond + Error(subj/(cond)),data=data3) summary(model3) data3 <- data.frame(subj=as.factor(c(subj, as.numeric(subj)+20)), group = as.factor(rep(c("Control","Experimental"),each=300)), cond = rep(x1,2), rt = rep(rt,2) + rnorm(600)) head(data3) model3<- aov(rt~ group + cond + Error(subj/(cond)),data=data3) summary(model3) c(3500,6000)/8 sqrt(80*.7*.3) sqrt(80*.7*.3)/80 setwd("~/Dropbox/courses/5220-s2025b/web-5220/psy5220/daily/Day21") knitr::opts_chunk$set(echo = TRUE) library(lavaan) ##needed for holzingerswineford data data <- dplyr::select(HolzingerSwineford1939,x1:x9) cc <- cor(data) pca <- princomp(cc) loadings(pca) f <- factanal(data,factors=3) #f2 <- fa:fa(data,factors=3) f <- factanal(data,factors=3) loadings(f) #f2 <- fa:fa(data,factors=3) library(lavaan) ##needed for holzingerswineford data data <- dplyr::select(HolzingerSwineford1939,x1:x9) cc <- cor(data) pca <- princomp(cc) loadings(pca) plot(pca) library(lavaan) ##needed for holzingerswineford data data <- dplyr::select(HolzingerSwineford1939,x1:x9) cc <- cor(data) pca <- princomp(cc) loadings(pca) plot(pca) library(lavaan) ##needed for holzingerswineford data data <- dplyr::select(HolzingerSwineford1939,x1:x9) cc <- cor(data) pca <- princomp(cc) loadings(pca) plot(pca) ?HolzingerSwineford1939 library(lavaan) ##needed for holzingerswineford data data <- dplyr::select(HolzingerSwineford1939,x1:x9) cc <- cor(data) pca <- princomp(cc) loadings(pca) plot(pca) f <- factanal(data,factors=3) loadings(f) summary(f) #f2 <- fa:fa(data,factors=3) f2 <- fa:fa(data,factors=3) f2 <- psych:fa(data,factors=3) library(psych) f2 <- psych:fa(data,factors=3) f2 <- psych:fa(data,n=3) psych::fa f2 <- psych:fa(data,nfactors=3) data f2 <- psych:fa(cov(data),nfactors=3) f2 cor(data) ?fa f2 <- psych:fa(r=cor(data),nfactors=3) f2 <- psych:fa(r=cor(data),nfactors=2) f <- factanal(data,factors=3) loadings(f) ?GPARotation ??GPArotation loadings(oblimin(f)) library(GPArotation) loadings(oblimin(f)) f <- factanal(data,factors=3) loadings(oblimin(f)) f <- factanal(data,factors=3,rotation="oblimin",scores="regression") loadings(f) library(lavaan) library(semPlot) #this won't compile for me example(cfa) library(lavaan) library(semPlot) example(cfa) HS.model <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit <- cfa(HS.model,data=HolzingerSwineford1939) fit summary(fit,fit.measures=T) HS.model2 <- ' visual =~ x1 + x2 + x3 + x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit2 <- cfa(HS.model2,data=HolzingerSwineford1939) fit2 summary(fit2,fit.measures=T) anova(fit,fit2) anova(fit2,fit) partable(fit) #install.packages("semPlot",dependencies=T) library(psych) ##library(semPlot) ##this won't install for me! ##semPaths(fit, "model","est",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit,cut=.2,digits=2) ##in psych package summary(fit,fit.measures=T,standardize=T) inspect(fit,"rsquare") semPaths(fit) library(semPlot) ##this won't install for me! library(semPlot) semPaths(fit, "model","est",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit,cut=.2,digits=2) ##in psych package summary(fit,fit.measures=T,standardize=T) inspect(fit,"rsquare") semPaths(fit) partable(fit) #install.packages("semPlot",dependencies=T) library(psych) library(semPlot) semPaths(fit, "model","est",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit,cut=.2,digits=2) ##in psych package summary(fit,fit.measures=T,standardize=T) inspect(fit,"rsquare") semPaths(fit) HS.model2 <- ' cognitive =~ x1 + x2 + x3 + x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit2 <- cfa(HS.model2,data=HolzingerSwineford1939) fit2 summary(fit2,fit.measures=T) semPaths(fit2) lavaan.diagram(fit2) anova(fit,fit2) anova(fit2,fit) summary(fit,fit.measures=T) summary(fit2,fit.measures=T) diagram(fit) diagram(fit2) anova(fit,fit2) HS.model3 <- 'f1 =~ x1 + x2 f2 =~ x3 + x4 f3 =~ x5 + x6 f4 =~ x7 + x8 + x9 ' fit3 <- cfa(HS.model3,data=HolzingerSwineford1939) fit3 anova(fit,fit3) #semPlot::semPaths(fit3,title=F,curvePilot=T) lavaan.diagram(fit3,cut=.2,digits=2) semPaths(fit3,"par", weighted = TRUE, nCharNodes = 7, shapeMan = "square", sizeMan = 8, sizeMan2 = 5,layout="tree2") ##Compare to the following model, which has the same structure but it does not have the covariance restrictions HS.modelG <- 'G =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fitG <- cfa(HS.modelG,data=HolzingerSwineford1939) summary(fitG,fit.measures=T) HS.model4 <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 + x9 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4 <- cfa(HS.model4,data=HolzingerSwineford1939) #fit4 <- cfa(HS.model4,data=HolzingerSwineford1939,estimator="WLS") fit4 summary(fit4,fit.measures=T) semPaths(fit4,"par", weighted = FALSE, nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.model4b <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4b <- cfa(HS.model4b,data=HolzingerSwineford1939) summary(fit4b,fit.measures=T) anova(fit4,fit4b) ##Compare to the following model, which has the same structure but it does not have the covariance restrictions HS.modelG <- 'G =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fitG <- cfa(HS.modelG,data=HolzingerSwineford1939) summary(fitG,fit.measures=T) anova(fit,fitG) HS.model4 <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 + x9 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4 <- cfa(HS.model4,data=HolzingerSwineford1939) #fit4 <- cfa(HS.model4,data=HolzingerSwineford1939,estimator="WLS") fit4 summary(fit4,fit.measures=T) semPaths(fit4,"par", weighted = FALSE, nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.model4b <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4b <- cfa(HS.model4b,data=HolzingerSwineford1939) summary(fit4b,fit.measures=T) anova(fit4,fit4b) HS.model4 <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 + x9 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4 <- cfa(HS.model4,data=HolzingerSwineford1939) #fit4 <- cfa(HS.model4,data=HolzingerSwineford1939,estimator="WLS") fit4 summary(fit4,fit.measures=T) semPaths(fit4,"par", weighted = FALSE, nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.model4b <- 'g=~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 f1 =~ x1 + x2 + x3 f2 =~ x4 + x5 + x6 f3 =~ x7 + x8 g ~~ 0*f1 g ~~ 0*f2 g ~~ 0*f3 f1~~ 0*f1 f2~~ 0*f2 f3 ~~ 0*f3 ' fit4b <- cfa(HS.model4b,data=HolzingerSwineford1939) summary(fit4b,fit.measures=T) anova(fit4,fit4b) semPaths(fit4,"model","est",curvePivot = FALSE,edge.label.cex = 0.5) semPaths(fit4,"model","est",layout="circle",curvePivot = FALSE,edge.label.cex = 0.5) semPaths(fit4,"model","est",layout="tree",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit4,cut=.2,digits=2) semPaths(fit4,"par", weighted = FALSE, nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) anova(fit4,fit) anova(fit4,fit) anova(fit4,fit) semPaths(fit4,"model","est",curvePivot = FALSE,edge.label.cex = 0.5) semPaths(fit4,"model","est",layout="circle",curvePivot = FALSE,edge.label.cex = 0.5) semPaths(fit4,"model","est",layout="tree",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit4,cut=.2,digits=2) semPaths(fit4,"par", weighted = FALSE, nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) fit5 <- cfa(HS.model4,data=HolzingerSwineford1939,orthogonal=TRUE) summary(fit5,fit.measures=T) semPaths(fit5,"model","est",curvePivot = FALSE,edge.label.cex = 0.5) residuals(fit5,type="cor") pathAn1 <-'x1 ~ x2 + x3 + x4 x2 ~ x4 x3 ~ x4 x1 + x2 +x4 ~ x5 x5 ~~ x5' paModel <- sem(pathAn1,data=HolzingerSwineford1939) summary(paModel,fit.measures=T) semPaths(paModel,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5) pathAn2 <- 'x1 ~ x2 + x3 + x4 x2 + x3 + x4 ~ x5 x5 ~~ x5' paModel2 <- sem(pathAn2,data=HolzingerSwineford1939) summary(paModel2,fit.measures=T) semPaths(paModel2,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5,edge.label.cex=1) anova(paModel,paModel2) pathAn1 <-'x1 ~ x2 + x3 + x4 x2 ~ x4 x3 ~ x4 x1 + x2 +x4 ~ x5' #x5 ~~ x5' paModel <- sem(pathAn1,data=HolzingerSwineford1939) summary(paModel,fit.measures=T) semPaths(paModel,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5) pathAn2 <- 'x1 ~ x2 + x3 + x4 x2 + x3 + x4 ~ x5 x5 ~~ x5' paModel2 <- sem(pathAn2,data=HolzingerSwineford1939) summary(paModel2,fit.measures=T) semPaths(paModel2,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5,edge.label.cex=1) anova(paModel,paModel2) pathAn1 <-'x1 ~ x2 + x3 + x4 x2 ~ x4 x3 ~ x4 x1 + x2 +x4 ~ x5 x5 ~~ x5' paModel <- sem(pathAn1,data=HolzingerSwineford1939) summary(paModel,fit.measures=T) semPaths(paModel,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5) pathAn2 <- 'x1 ~ x2 + x3 + x4 x2 + x3 + x4 ~ x5 x5 ~~ x5' paModel2 <- sem(pathAn2,data=HolzingerSwineford1939) summary(paModel2,fit.measures=T) semPaths(paModel2,"par", weighted = FALSE, nCharNodes = 7, layout = "spring", sizeMan = 8, sizeMan2 = 5,edge.label.cex=1) anova(paModel,paModel2) HS.sem <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) summary(fit_sem,fit.measures=T, standardized=T) semPaths(fit_sem, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "spring", nCharNodes = 7, edge.label.cex=.9, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) semPaths(fit, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "spring", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.sem2 <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ visual + speed visual ~ speed ' fit_sem2 <- sem(HS.sem2,data=HolzingerSwineford1939) summary(fit_sem2,fit.measures=T, standardized=T) semPaths(fit_sem2, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree2", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.sem2 <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ visual + speed visual ~ speed ' fit_sem2 <- sem(HS.sem2,data=HolzingerSwineford1939) summary(fit_sem2,fit.measures=T, standardized=T) semPaths(fit_sem2, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree2", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 10, sizeMan2 = 5) anova(fit_sem,fit_sem2) HS.sem <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) summary(fit_sem,fit.measures=T, standardized=T) semPaths(fit_sem, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "spring", nCharNodes = 7, edge.label.cex=.9, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) semPaths(fit, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "spring", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.sem2 <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ visual + speed visual ~ speed ' fit_sem2 <- sem(HS.sem2,data=HolzingerSwineford1939) summary(fit_sem2,fit.measures=T, standardized=T) semPaths(fit_sem2, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree2", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 10, sizeMan2 = 5) anova(fit_sem,fit_sem2) fit_sem fit_sem2 summary(fit_sem) summary(fit_sem2) HS.sem <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) summary(fit_sem,fit.measures=T, standardized=T) fit_sem <- 0 HS.sem fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) fit_sem summary(fit_sem) HS.sem <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 x5~~x5' fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) summary(fit_sem,fit.measures=T, standardized=T) HS.sem <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit_sem <- sem(HS.sem,data=HolzingerSwineford1939) summary(fit_sem,fit.measures=T, standardized=T) semPaths(fit, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "spring", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) HS.sem2 <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ visual visual ~ speed ' fit_sem2 <- sem(HS.sem2,data=HolzingerSwineford1939) summary(fit_sem2,fit.measures=T, standardized=T) semPaths(fit_sem2, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree2", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 10, sizeMan2 = 5) anova(fit_sem,fit_sem2) HS.sem3a <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ a*visual + b*speed visual ~ c*speed indirect := a*b total := indirect + c ' fit_sem3a <- sem(HS.sem3a,data=HolzingerSwineford1939) summary(fit_sem3a,fit.measures=T, standardized=T) semPaths(fit_sem3a, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) anova(fit_sem3a,fit_sem2) HS.sem3 <- ' visual =~ x2 + x1 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 textual ~ visual visual ~ speed ' fit_sem3 <- sem(HS.sem3,data=HolzingerSwineford1939) summary(fit_sem3,fit.measures=T, standardized=T) semPaths(fit_sem3, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) anova(fit_sem3,fit_sem2) iphone <- read.csv("data_study1.csv") iphone$phone <- as.numeric(iphone$Smartphone=="iPhone") iphone$Gender <- iphone$Gender=="female" iphone.base <- ' personality =~ Honesty.Humility+ Emotionality+Extraversion+Agreeableness + Conscientiousness+Openness social =~ Avoidance.Similarity+Phone.as.status.object+Social.Economic.Status individ =~ Gender+Age+Time.owned.current.phone phone ~personality+social+individ ' iphone.simple <- 'social =~ Avoidance.Similarity+Phone.as.status.object+Social.Economic.Status individ =~ Gender+Age+Time.owned.current.phone+Honesty.Humility+ Emotionality+Extraversion+Agreeableness + Conscientiousness+Openness phone ~social+individ ' iphone.simple2 <- 'social =~ Avoidance.Similarity+Phone.as.status.object+Social.Economic.Status individ =~ Gender+Age+Time.owned.current.phone+Honesty.Humility+ Agreeableness + Conscientiousness+Openness phone ~social+individ+Emotionality+Extraversion ' fit_phone1 <- sem(iphone.base,data=iphone,optim.force.converged=T) summary(fit_phone1,fit.measures=T, standardized=T) fit_phone2 <- sem(iphone.simple,data=iphone,optim.force.converged=T) summary(fit_phone2,fit.measures=T, standardized=T) fit_phone3 <- sem(iphone.simple2,data=iphone,optim.force.converged=T) summary(fit_phone3,fit.measures=T, standardized=T) semPaths(fit_phone1, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) semPaths(fit_phone2, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) semPaths(fit_phone3, what = "col", whatLabels = "std", style = "mx", rotation =1 , layout = "tree", nCharNodes = 7, shapeMan = "rectangle", sizeMan = 8, sizeMan2 = 5) anova(fit_phone1,fit_phone2) anova(fit_phone1,fit_phone3) BIC(fit_phone1,fit_phone2,fit_phone3) partable(fit) #install.packages("semPlot",dependencies=T) library(psych) library(semPlot) semPaths(fit, "model","est",curvePivot = FALSE,edge.label.cex = 0.5) lavaan.diagram(fit,cut=.2,digits=2) ##in psych package summary(fit,fit.measures=T,standardize=T) inspect(fit,"rsquare") semPaths(fit)