list.files()
##  [1] "~$SIL OLAH DATA CFA.docx"       "~WRL3908.tmp"                  
##  [3] "data survey 0704.xlsx"          "data_jasp.xlsx"                
##  [5] "gambar1.png"                    "gambar2.png"                   
##  [7] "HASIL OLAH DATA CFA.docx"       "kode-R-CFA.html"               
##  [9] "kode-R-CFA.Rmd"                 "kode lavaan.txt"               
## [11] "kode R CFA.nb.html"             "kode R CFA.Rmd"                
## [13] "korelasi.png"                   "RINGKASAN PERHITUNGAN NEW.xlsx"
## [15] "ringkasan perhitungan.xlsx"     "rsconnect"
dat <- readxl::read_excel("data_jasp.xlsx")

dat <- as.data.frame(dat)

dat

#UJI CFA TAHAP 1

library(lavaan)
## This is lavaan 0.6-12
## lavaan is FREE software! Please report any bugs.
tes_model <-"

PR =~ PR1 + PR2 + PR3 + PR4 + PR5 + PR6

WF =~ WF1 + WF2 + WF3 + WF4 + WF5 + WF6

SG =~ SG1 + SG2 + SG3 + SG4 + SG5 + SG6 + SG7 + SG8


JB =~ JB1 + JB2 + JB3 + JB4 + JB5 + JB6


PK =~ PK1 + PK2 + PK3 + PK4

SD =~ SD1 + SD2 + SD3 + SD4

BO =~ BO1 + BO2 + BO3 + BO4

CO =~ CO1 + CO2 + CO3 + CO4 + CO5 + CO6 + CO7

"

hasil_cfa = lavaan::cfa(tes_model, data = dat, auto.var = TRUE, auto.fix.first = TRUE)
      
ringkasan_hasil <- lavaan::standardizedsolution(hasil_cfa)

ringkasan_hasil
library(semPlot)

semPaths(hasil_cfa, 
 std = "std",
 layout = "circle",
 residuals = F,
 what = "std")
## Warning in qgraph::qgraph(Edgelist, labels = nLab, bidirectional = Bidir, : The
## following arguments are not documented and likely not arguments of qgraph and
## thus ignored: std

library(semPlot)

semPaths(hasil_cfa, 
 std = "std",
 layout = "spring",
 residuals = F,
 what = "std",
 rotation = 3)
## Warning in qgraph::qgraph(Edgelist, labels = nLab, bidirectional = Bidir, : The
## following arguments are not documented and likely not arguments of qgraph and
## thus ignored: std

 wb <- openxlsx::createWorkbook()
      
      
      hs1 <- openxlsx::createStyle(fgFill = "#DCE6F1", halign = "CENTER", textDecoration = "italic",
                                   border = "Bottom")
      
      
      hs2 <- openxlsx::createStyle(fontColour = "#ffffff", fgFill = "#4F80BD",
                                   halign = "center", valign = "center", textDecoration = "bold",
                                   border = "TopBottomLeftRight")
      
      openxlsx::addWorksheet(wb, "ringkasan_hasil", gridLines = TRUE)
      
      openxlsx::writeDataTable(wb, "ringkasan_hasil", ringkasan_hasil, rowNames = FALSE, startRow = 2, startCol = 2, tableStyle = "TableStyleMedium21")
      
      
      
      openxlsx::openXL(wb)

#UJI CFA TAHAP 2

library(lavaan)

tes_model <-"

PR =~  PR5

WF =~ WF1 + WF5 + WF6

SG =~ SG1 + SG2 + SG3 + SG4 + SG5  + SG8


JB =~ JB1 + JB3 + JB4 + JB5


PK =~ PK1 + PK2 + PK3 + PK4

SD =~ SD1 + SD2 + SD3 + SD4

BO =~ BO4

CO =~ CO1 + CO2 + CO3


"

hasil_cfa = lavaan::cfa(tes_model, data = dat, auto.var = TRUE, auto.fix.first = TRUE)
      
ringkasan_hasil <- lavaan::standardizedsolution(hasil_cfa)

ringkasan_hasil
 wb <- openxlsx::createWorkbook()
      
      
      hs1 <- openxlsx::createStyle(fgFill = "#DCE6F1", halign = "CENTER", textDecoration = "italic",
                                   border = "Bottom")
      
      
      hs2 <- openxlsx::createStyle(fontColour = "#ffffff", fgFill = "#4F80BD",
                                   halign = "center", valign = "center", textDecoration = "bold",
                                   border = "TopBottomLeftRight")
      
      openxlsx::addWorksheet(wb, "ringkasan_hasil", gridLines = TRUE)
      
      openxlsx::writeDataTable(wb, "ringkasan_hasil", ringkasan_hasil, rowNames = FALSE, startRow = 2, startCol = 2, tableStyle = "TableStyleMedium21")
      
      
      
      openxlsx::openXL(wb)
library(semPlot)

semPaths(hasil_cfa, 
 std = "std",
 layout = "circle",
 residuals = F,
 what = "std")
## Warning in qgraph::qgraph(Edgelist, labels = nLab, bidirectional = Bidir, : The
## following arguments are not documented and likely not arguments of qgraph and
## thus ignored: std

  goodness_of_fit = lavaan::fitMeasures(hasil_cfa)
    
    # print(goodness_of_fit)
    
    goodness_of_fit = lavaan::fitMeasures(hasil_cfa, c("pvalue","rmsea","nfi","cfi","gfi","agfi","srmr") )
    
goodness_of_fit
## pvalue  rmsea    nfi    cfi    gfi   agfi   srmr 
##  0.000  0.079  0.838  0.876  0.847  0.803  0.069
skor <- lavPredict(hasil_cfa)

skor <- as.data.frame(skor)

library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.2.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.2
korelasi <- round(cor(skor), 3)

korelasi
##       PR    WF    SG    JB    PK    SD    BO    CO
## PR 1.000 0.502 0.594 0.472 0.139 0.164 0.141 0.111
## WF 0.502 1.000 0.687 0.711 0.389 0.455 0.435 0.377
## SG 0.594 0.687 1.000 0.841 0.382 0.304 0.286 0.313
## JB 0.472 0.711 0.841 1.000 0.524 0.583 0.491 0.357
## PK 0.139 0.389 0.382 0.524 1.000 0.722 0.489 0.420
## SD 0.164 0.455 0.304 0.583 0.722 1.000 0.705 0.472
## BO 0.141 0.435 0.286 0.491 0.489 0.705 1.000 0.391
## CO 0.111 0.377 0.313 0.357 0.420 0.472 0.391 1.000
ggcorrplot(korelasi, hc.order = TRUE, type = "lower",
   lab = TRUE)