From e34df118ec91563d94668c73665551fa290d5f7b Mon Sep 17 00:00:00 2001 From: Petersen Date: Wed, 2 Oct 2024 09:18:13 -0500 Subject: [PATCH] 20241002 - remove code --- 15-Factor-Analysis-PCA.Rmd | 633 ------------------------------------- 1 file changed, 633 deletions(-) diff --git a/15-Factor-Analysis-PCA.Rmd b/15-Factor-Analysis-PCA.Rmd index b1a487ea..9d6a7245 100644 --- a/15-Factor-Analysis-PCA.Rmd +++ b/15-Factor-Analysis-PCA.Rmd @@ -2142,636 +2142,3 @@ semPaths( layout = "tree2", edge.label.cex = 1.3) ``` - -#### Class Examples {#classExamples-cfa} - -```{r} -standardDeviations <- rep(1, 6) -sampleSize <- 300 -``` - -##### Model 1 {#classExamplesModel1-cfa} - -###### Create the covariance matrix {#classExamplesModel1covarianceMatrix-cfa} - -```{r} -correlationMatrixModel1 <- matrix(.6, nrow = 6, ncol = 6) -diag(correlationMatrixModel1) <- 1 -rownames(correlationMatrixModel1) <- colnames(correlationMatrixModel1) <- - paste("V", 1:6, sep = "") - -covarianceMatrixModel1 <- psych::cor2cov( - correlationMatrixModel1, - sigma = standardDeviations) -covarianceMatrixModel1 -``` - -###### Specify the model {#classExamplesModel1syntax-cfa} - -```{r} -cfaModel1 <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 + V4 + V5 + V6 -' -``` - -###### Model syntax in table form: {#classExamplesModel1tabular-cfa} - -```{r} -lavaanify(cfaModel1) -``` - -###### Fit the model {#classExamplesModel1fit-cfa} - -```{r} -cfaModel1Fit <- cfa( - cfaModel1, - sample.cov = covarianceMatrixModel1, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) -``` - -###### Display summary output {#classExamplesModel1output-cfa} - -```{r} -summary( - cfaModel1Fit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) -``` - -###### Estimates of model fit {#classExamplesModel1fitCriteria-cfa} - -```{r} -fitMeasures( - cfaModel1Fit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) -``` - -###### Residuals {#classExamplesModel1residuals-cfa} - -```{r} -residuals(cfaModel1Fit, type = "cor") -``` - -###### Modification indices {#classExamplesModel1modIndices-cfa} - -```{r} -modificationindices(cfaModel1Fit, sort. = TRUE) -``` - -###### Internal Consistency Reliability {#classExamplesModel1reliability-cfa} - -[Internal consistency reliability](#internalConsistency-reliability) of items composing the latent factors, as quantified by [omega ($\omega$)](#coefficientOmega) and [average variance extracted](#averageVarianceExtracted) (AVE), was estimated using the `semTools` package [@R-semTools].\index{factor analysis!confirmatory}\index{reliability!internal consistency}\index{reliability!internal consistency!omega}\index{reliability!internal consistency!average variance extracted} - -```{r} -compRelSEM(cfaModel1Fit) -AVE(cfaModel1Fit) -``` - -###### Path diagram {#classExamplesModel1pathDiagram-cfa} - -A path diagram of the model generated using the `semPlot` package [@R-semPlot] is in Figure \@ref(fig:cfaPathDiagramClass1).\index{factor analysis!confirmatory} - -```{r cfaPathDiagramClass1, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 1 Diagram."} -semPaths( - cfaModel1Fit, - what = "std", - layout = "tree2", - edge.label.cex = 1.5) -``` - -##### Model 2 {#classExamplesModel2-cfa} - -###### Create the covariance matrix {#classExamplesModel2covarianceMatrix-cfa} - -```{r} -correlationMatrixModel2 <- matrix(0, nrow = 6, ncol = 6) -diag(correlationMatrixModel2) <- 1 -rownames(correlationMatrixModel2) <- colnames(correlationMatrixModel2) <- - paste("V", 1:6, sep = "") - -covarianceMatrixModel2 <- psych::cor2cov( - correlationMatrixModel2, - sigma = standardDeviations) -covarianceMatrixModel2 -``` - -###### Specify the model {#classExamplesModel2syntax-cfa} - -```{r} -cfaModel2 <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 + V4 + V5 + V6 -' -``` - -###### Model syntax in table form: {#classExamplesModel2tabular-cfa} - -```{r} -lavaanify(cfaModel2) -``` - -###### Fit the model {#classExamplesModel2fit-cfa} - -```{r} -cfaModel2Fit <- cfa( - cfaModel2, - sample.cov = covarianceMatrixModel2, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) -``` - -###### Display summary output {#classExamplesModel2output-cfa} - -```{r} -summary( - cfaModel2Fit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) -``` - -###### Estimates of model fit {#classExamplesModel2fitCriteria-cfa} - -```{r} -fitMeasures( - cfaModel2Fit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) -``` - -###### Residuals {#classExamplesModel2residuals-cfa} - -```{r} -residuals(cfaModel2Fit, type = "cor") -``` - -###### Modification indices {#classExamplesModel2modIndices-cfa} - -```{r} -modificationindices(cfaModel1Fit, sort. = TRUE) -``` - -###### Internal Consistency Reliability {#classExamplesModel2reliability-cfa} - -[Internal consistency reliability](#internalConsistency-reliability) of items composing the latent factors, as quantified by [omega ($\omega$)](#coefficientOmega) and [average variance extracted](#averageVarianceExtracted) (AVE), was estimated using the `semTools` package [@R-semTools].\index{factor analysis!confirmatory}\index{reliability!internal consistency}\index{reliability!internal consistency!omega}\index{reliability!internal consistency!average variance extracted} - -```{r} -compRelSEM(cfaModel2Fit) -AVE(cfaModel2Fit) -``` - -###### Path diagram {#classExamplesModel2pathDiagram-cfa} - -A path diagram of the model generated using the `semPlot` package [@R-semPlot] is in Figure \@ref(fig:cfaPathDiagramClass2).\index{factor analysis!confirmatory} - -```{r cfaPathDiagramClass2, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 2 Diagram."} -semPaths( - cfaModel2Fit, - what = "std", - layout = "tree2", - edge.label.cex = 1.5) -``` - -##### Model 3 {#classExamplesModel3-cfa} - -###### Create the covariance matrix {#classExamplesModel3covarianceMatrix-cfa} - -```{r} -correlationMatrixModel3 <- matrix(c( - 1,.6,.6,0,0,0, - .6,1,.6,0,0,0, - 0,0,1,0,0,0, - 0,0,0,1,.6,.6, - 0,0,0,.6,1,.6, - 0,0,0,.6,.6,1), - nrow = 6, - ncol = 6) -rownames(correlationMatrixModel3) <- colnames(correlationMatrixModel3) <- - paste("V", 1:6, sep = "") - -covarianceMatrixModel3 <- psych::cor2cov( - correlationMatrixModel3, - sigma = standardDeviations) -covarianceMatrixModel3 -``` - -###### Specify the model {#classExamplesModel3syntax-cfa} - -```{r} -cfaModel3A <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 + V4 + V5 + V6 -' - -cfaModel3B <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 - f2 =~ V4 + V5 + V6 -' -``` - -###### Model syntax in table form: {#classExamplesModel3tabular-cfa} - -```{r} -lavaanify(cfaModel3A) -lavaanify(cfaModel3B) -``` - -###### Fit the model {#classExamplesModel3fit-cfa} - -```{r} -cfaModel3AFit <- cfa( - cfaModel3A, - sample.cov = covarianceMatrixModel3, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) - -cfaModel3BFit <- cfa( - cfaModel3B, - sample.cov = covarianceMatrixModel3, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) -``` - -###### Display summary output {#classExamplesModel3output-cfa} - -```{r} -summary( - cfaModel3AFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) - -summary( - cfaModel3BFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) -``` - -###### Estimates of model fit {#classExamplesModel3fitCriteria-cfa} - -```{r} -fitMeasures( - cfaModel3AFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) - -fitMeasures( - cfaModel3BFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) -``` - -###### Compare model fit {#classExamplesModel3nestedModelComparison-cfa} - -Below is a test of whether the two-factor model fits better than the one-factor model.\index{factor analysis!confirmatory} -A significant chi-square difference test indicates that the two-factor model fits significantly better than the one-factor model.\index{factor analysis!confirmatory}\index{chi-square!difference test} - -```{r} -anova(cfaModel3BFit, cfaModel3AFit) -``` - -###### Residuals {#classExamplesModel3residuals-cfa} - -```{r} -residuals(cfaModel3AFit, type = "cor") -residuals(cfaModel3BFit, type = "cor") -``` - -###### Modification indices {#classExamplesModel3modIndices-cfa} - -```{r} -modificationindices(cfaModel3AFit, sort. = TRUE) -modificationindices(cfaModel3BFit, sort. = TRUE) -``` - -###### Internal Consistency Reliability {#classExamplesModel3reliability-cfa} - -[Internal consistency reliability](#internalConsistency-reliability) of items composing the latent factors, as quantified by [omega ($\omega$)](#coefficientOmega) and [average variance extracted](#averageVarianceExtracted) (AVE), was estimated using the `semTools` package [@R-semTools].\index{factor analysis!confirmatory}\index{reliability!internal consistency}\index{reliability!internal consistency!omega}\index{reliability!internal consistency!average variance extracted} - -```{r} -compRelSEM(cfaModel3AFit) -compRelSEM(cfaModel3BFit) - -AVE(cfaModel3AFit) -AVE(cfaModel3BFit) -``` - -###### Path diagram {#classExamplesModel3pathDiagram-cfa} - -Path diagrams of the models generated using the `semPlot` package [@R-semPlot] are in Figures \@ref(fig:cfaPathDiagramClass3A) and \@ref(fig:cfaPathDiagramClass3B).\index{factor analysis!confirmatory} - -```{r cfaPathDiagramClass3A, out.width = "100%", fig.align = "center", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 3a Diagram."} -semPaths(cfaModel3AFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.5) -``` - -```{r cfaPathDiagramClass3B, out.width = "100%", fig.cap = "Confirmatory Factor Analysis Model 3b Diagram."} -semPaths(cfaModel3BFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.5) -``` - -##### Model 4 {#model4Examples} - -##### Create the covariance matrix {#classExamplesModel4-cfa} - -```{r} -correlationMatrixModel4 <- matrix(c( - 1,.6,.6,.3,.3,.3, - .6,1,.6,.3,.3,.3, - .3,.3,1,.3,.3,.3, - .3,.3,.3,1,.6,.6, - .3,.3,.3,.6,1,.6, - .3,.3,.3,.6,.6,1), - nrow = 6, - ncol = 6) -rownames(correlationMatrixModel4) <- colnames(correlationMatrixModel4) <- - paste("V", 1:6, sep = "") - -covarianceMatrixModel4 <- psych::cor2cov( - correlationMatrixModel4, - sigma = standardDeviations) -covarianceMatrixModel4 -``` - -###### Specify the model {#classExamplesModel4syntax-cfa} - -```{r} -cfaModel4A <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 - f2 =~ V4 + V5 + V6 -' - -cfaModel4B <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 - f2 =~ V4 + V5 + V6 - - #Regression path - f2 ~ f1 -' - -cfaModel4C <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 - f2 =~ V4 + V5 + V6 - - #Higher-order factor - A1 = ~ fixloading*f1 + fixloading*f2 -' - -cfaModel4D <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 + V4 + V5 + V6 - - #Correlated residuals/errors - V1 ~~ fixcor*V2 - V2 ~~ fixcor*V3 - V1 ~~ fixcor*V3 - V4 ~~ fixcor*V5 - V5 ~~ fixcor*V6 - V4 ~~ fixcor*V6 -' - -cfaModel4E <- ' - #Factor loadings - f1 =~ V1 + V2 + V3 + V4 + V5 + V6 #construct latent factor - f2 =~ fixloading*V4 + fixloading*V5 + fixloading*V6 #method latent factor - - #Make construct and method latent factors orthogonal (uncorrelated) - f1 ~~ 0*f2 -' -``` - -###### Model syntax in table form: {#classExamplesModel4tabular-cfa} - -```{r} -lavaanify(cfaModel4A) -lavaanify(cfaModel4B) -lavaanify(cfaModel4C) -lavaanify(cfaModel4D) -lavaanify(cfaModel4E) -``` - -###### Fit the model {#classExamplesModel4fit-cfa} - -```{r} -cfaModel4AFit <- cfa( - cfaModel4A, - sample.cov = covarianceMatrixModel4, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) - -cfaModel4BFit <- cfa( - cfaModel4B, - sample.cov = covarianceMatrixModel4, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) - -cfaModel4CFit <- cfa( - cfaModel4C, - sample.cov = covarianceMatrixModel4, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) - -cfaModel4DFit <- cfa( - cfaModel4D, - sample.cov = covarianceMatrixModel4, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) - -cfaModel4EFit <- cfa( - cfaModel4E, - sample.cov = covarianceMatrixModel4, - sample.nobs = sampleSize, - estimator = "ML", - std.lv = TRUE) -``` - -###### Display summary output {#classExamplesModel4output-cfa} - -```{r} -summary( - cfaModel4AFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) - -summary( - cfaModel4BFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) - -summary( - cfaModel4CFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) - -summary( - cfaModel4DFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) - -summary( - cfaModel4EFit, - fit.measures = TRUE, - standardized = TRUE, - rsquare = TRUE) -``` - -###### Estimates of model fit {#classExamplesModel4fitCriteria-cfa} - -```{r} -fitMeasures( - cfaModel4AFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) - -fitMeasures( - cfaModel4BFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) - -fitMeasures( - cfaModel4CFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) - -fitMeasures( - cfaModel4DFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) - -fitMeasures( - cfaModel4EFit, - fit.measures = c( - "chisq", "df", "pvalue", - "rmsea", "cfi", "tli", "srmr")) -``` - -###### Residuals {#classExamplesModel4residuals-cfa} - -```{r} -residuals(cfaModel4AFit, type = "cor") -residuals(cfaModel4BFit, type = "cor") -residuals(cfaModel4CFit, type = "cor") -residuals(cfaModel4DFit, type = "cor") -residuals(cfaModel4EFit, type = "cor") -``` - -###### Modification indices {#classExamplesModel4modIndices-cfa} - -```{r} -modificationindices(cfaModel4AFit, sort. = TRUE) -modificationindices(cfaModel4BFit, sort. = TRUE) -modificationindices(cfaModel4CFit, sort. = TRUE) -modificationindices(cfaModel4DFit, sort. = TRUE) -modificationindices(cfaModel4EFit, sort. = TRUE) -``` - -###### Internal Consistency Reliability {#classExamplesModel4reliability-cfa} - -[Internal consistency reliability](#internalConsistency-reliability) of items composing the latent factors, as quantified by [omega ($\omega$)](#coefficientOmega) and [average variance extracted](#averageVarianceExtracted) (AVE), was estimated using the `semTools` package [@R-semTools].\index{factor analysis!confirmatory}\index{reliability!internal consistency}\index{reliability!internal consistency!omega}\index{reliability!internal consistency!average variance extracted} - -```{r} -compRelSEM(cfaModel4AFit) -compRelSEM(cfaModel4BFit) -compRelSEM(cfaModel4CFit) -compRelSEM(cfaModel4CFit, higher = "A1") -compRelSEM(cfaModel4DFit) -compRelSEM(cfaModel4EFit) - -AVE(cfaModel4AFit) -AVE(cfaModel4BFit) -AVE(cfaModel4CFit) -AVE(cfaModel4DFit) -AVE(cfaModel4EFit) -``` - -###### Path diagram {#classExamplesModel4pathDiagram-cfa} - -Path diagrams of the models generated using the `semPlot` package [@R-semPlot] are in Figures \@ref(fig:cfaModel4AFit), \@ref(fig:cfaModel4BFit), \@ref(fig:cfaModel4CFit), \@ref(fig:cfaModel4DFit), and \@ref(fig:cfaModel4EFit).\index{factor analysis!confirmatory} - -```{r cfaModel4AFit, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 4a Diagram."} -semPaths( - cfaModel4AFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.3) -``` - -```{r cfaModel4BFit, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 4b Diagram."} -semPaths( - cfaModel4BFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.3) -``` - -```{r cfaModel4CFit, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 4c Diagram."} -semPaths( - cfaModel4CFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.3) -``` - -```{r cfaModel4DFit, fig.height = 6, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 4d Diagram."} -semPaths( - cfaModel4DFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.3) -``` - -```{r cfaModel4EFit, fig.height = 6, out.width = "100%", fig.align = "center", fig.cap = "Confirmatory Factor Analysis Model 4e Diagram."} -semPaths( - cfaModel4EFit, - what = "std", - layout = "tree2", - edge.label.cex = 1.3) -``` - -###### Equivalently fitting models {#classExamplesModel4equivalent-cfa} - -Markov equivalent directed acyclic graphs (DAGs) were depicted using the `dagitty` package [@R-dagitty].\index{factor analysis!confirmatory} -Path diagrams of equivalent models are below.\index{factor analysis!confirmatory} - -```{r} -dagModel <- lavaanToGraph(cfaModel4AFit) - -par(mfrow = c(4, 4)) -``` - -```{r cfaModel4equivalent, out.width = "100%", fig.align = "center", fig.cap = "Equivalently Fitting Models to Confirmatory Factor Analysis Model 4."} -lapply(equivalentDAGs(dagModel, n = 16), plot) -```