#first load some needed libraries
library("psych")
library("lessR")
## Warning: package 'lessR' was built under R version 3.6.3
## 
## lessR 3.9.6  feedback: gerbing@pdx.edu    web: lessRstats.com/new
## -----------------------------------------------------------------
## > d <- Read("")   Read text, Excel, SPSS, SAS or R data file
##   d is default data frame, no need for data= in analysis routines
## 
## > vignette("topic") for help on the following topics
##    "Read": read data and variable labels, write data to a file
##    "BarChart", "Histogram", "Plot": visualizations
##    "Means": analyze means with t-tests and ANOVA
##    "Regression": least-squares, logistic regression
##    "Customize": custom color palettes and more customization
##    "Extract": general, simple data frame subsetting
##    "pivot": 1-d and 2-d simply created pivot tables
## 
## Attaching package: 'lessR'
## The following objects are masked from 'package:psych':
## 
##     reflect, scree
library("lavaan")
## This is lavaan 0.6-7
## lavaan is BETA software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## The following object is masked from 'package:lessR':
## 
##     cfa
## The following object is masked from 'package:psych':
## 
##     cor2cov
library("lavaanPlot")


Ex <- read.csv("C:/Users/Courtney/Desktop/Ex.csv")

ExPar <- parcels(Ex, size = 3, max = TRUE, flip=TRUE,congruence = FALSE)
keysort(ExPar)

ExFac <- 'P =~ P1 + P2 + P3 + P4 + P5 + P6'

lavaanify(model=ExFac)
##    id lhs op rhs user block group free ustart exo label plabel
## 1   1   P =~  P1    1     1     1    1     NA   0         .p1.
## 2   2   P =~  P2    1     1     1    2     NA   0         .p2.
## 3   3   P =~  P3    1     1     1    3     NA   0         .p3.
## 4   4   P =~  P4    1     1     1    4     NA   0         .p4.
## 5   5   P =~  P5    1     1     1    5     NA   0         .p5.
## 6   6   P =~  P6    1     1     1    6     NA   0         .p6.
## 7   7  P1 ~~  P1    0     1     1    0      0   0         .p7.
## 8   8  P2 ~~  P2    0     1     1    0      0   0         .p8.
## 9   9  P3 ~~  P3    0     1     1    0      0   0         .p9.
## 10 10  P4 ~~  P4    0     1     1    0      0   0        .p10.
## 11 11  P5 ~~  P5    0     1     1    0      0   0        .p11.
## 12 12  P6 ~~  P6    0     1     1    0      0   0        .p12.
## 13 13   P ~~   P    0     1     1    0      0   0        .p13.
ex.out <-cfa(ExFac, data=ExPar)
lavaanPlot(model = ex.out)
summary(ex.out, fit.measures=TRUE, standardized = TRUE)
## lavaan 0.6-7 ended normally after 36 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         12
##                                                       
##   Number of observations                            20
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           1.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                                26.549
##   Degrees of freedom                                15
##   P-value                                        0.033
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       2.299
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -33.420
##   Loglikelihood unrestricted model (H1)        -33.420
##                                                       
##   Akaike (AIC)                                  90.839
##   Bayesian (BIC)                               102.788
##   Sample-size adjusted Bayesian (BIC)           65.795
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value RMSEA <= 0.05                          1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   P =~                                                                  
##     P1                1.000                                  NA       NA
##     P2                1.000    0.667    1.500    0.134       NA       NA
##     P3                1.000    0.667    1.500    0.134       NA       NA
##     P4                1.000    0.667    1.500    0.134       NA       NA
##     P5                1.000    0.667    1.500    0.134       NA       NA
##     P6                1.000    0.667    1.500    0.134       NA       NA
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .P1                0.150    0.057    2.626    0.009    0.150    1.176
##    .P2                0.150    0.057    2.626    0.009    0.150    1.176
##    .P3                0.150    0.057    2.626    0.009    0.150    1.176
##    .P4                0.150    0.057    2.626    0.009    0.150    1.176
##    .P5                0.150    0.057    2.626    0.009    0.150    1.176
##    .P6                0.150    0.057    2.626    0.009    0.150    1.176
##     P                -0.022    0.020   -1.142    0.253       NA       NA