# Installation check and installation of required packages for CFA and EGA if (!require("psych")) install.packages("psych") if (!require("lavaan")) install.packages("lavaan") if (!require("qgraph")) install.packages("qgraph") if (!require("semPlot")) install.packages("semPlot") # Load libraries library(psych) library(lavaan) library(qgraph) library(semPlot) # Define the model for Confirmatory Factor Analysis (CFA) modelo_afc <- ' F1 =~ Q2 + Q3 + Q6 + Q7 # Define first factor with items Q2, Q3, Q6, Q7 F2 =~ Q4 + Q5 + Q8 # Define second factor with items Q4, Q5, Q8 ' # Fit the CFA model using the loaded dataset ajuste_afc <- cfa(modelo_afc, data=Dataset_ajustado) # Summary of the CFA results, including fit measures summary(ajuste_afc, fit.measures=TRUE) # Provides a detailed summary including fit indices # Perform Exploratory Graph Analysis (EGA) with selected data # Select and adjust based on the columns present in your dataset cor_matrix <- cor(Dataset_ajustado[,c("Q2", "Q3", "Q6", "Q7", "Q4", "Q5", "Q8")], use="complete.obs") ega <- qgraph(cor_matrix, graph="cor", layout="spring") # Generate and visualize the Path Diagram of the CFA model using semPlot path_diagram <- semPaths(ajuste_afc, whatLabels="std", layout="tree", rotation=2)