library(lavaan)
## Warning: package 'lavaan' was built under R version 4.1.3
## This is lavaan 0.6-11
## lavaan is FREE software! Please report any bugs.
library(semPlot)
## Warning: package 'semPlot' was built under R version 4.1.3
library(OpenMx)
## Warning: package 'OpenMx' was built under R version 4.1.3
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(knitr)
## Warning: package 'knitr' was built under R version 4.1.3
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(GGally)
## Warning: package 'GGally' was built under R version 4.1.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(readr)
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 4.1.3
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 4.1.3
## Loading required package: survival
## Warning: package 'survival' was built under R version 4.1.3
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
corstarsl <- function(x){ 
  x <- as.matrix(x) 
  R <- rcorr(x)$r 
  p <- rcorr(x)$P 
  mystars <- ifelse(p > 0.05, "ns", ifelse(p > 0.01 & p <= 0.05, "*", "**"))
  R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1] 
  stars 
  Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x)) 
  diag(Rnew) <- paste(diag(R), " ", sep="") 
  rownames(Rnew) <- colnames(x) 
  colnames(Rnew) <- paste(colnames(x), "", sep="") 
  Rnew <- as.matrix(Rnew)
  Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
  Rnew <- as.data.frame(Rnew) 
  Rnew <- cbind(Rnew[1:length(Rnew)-1])
  return(Rnew) 
}


exemplo_cursomeg <- read_table("C:/CursoGenes/exemplo_cursomeg.dat", 
                                col_names = FALSE)
## 
## -- Column specification --------------------------------------------------------
## cols(
##   X1 = col_double(),
##   X2 = col_double(),
##   X3 = col_double(),
##   X4 = col_double(),
##   X5 = col_double(),
##   X6 = col_logical()
## )
exemplo_cursomeg <- exemplo_cursomeg %>% select(1:5)
colnames(exemplo_cursomeg) <-c("A","B","C","D","E")

corstarsl(exemplo_cursomeg) %>% kable("pandoc")
A B C D
A
B 0.90**
C 0.87** 0.97**
D 0.74** 0.93** 0.93**
E -0.08ns -0.29ns -0.31ns -0.36ns
model <-'A ~ B+C+D+E'
fit <- cfa(model, data = exemplo_cursomeg)
summary(fit, fit.measures = TRUE, standardized=T,rsquare=T)
## lavaan 0.6-11 ended normally after 1 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
##                                                       
##   Number of observations                            20
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                                45.375
##   Degrees of freedom                                 4
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)                -57.886
##   Loglikelihood unrestricted model (H1)        -57.886
##                                                       
##   Akaike (AIC)                                 125.771
##   Bayesian (BIC)                               130.750
##   Sample-size adjusted Bayesian (BIC)          115.336
## 
## 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                             NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   A ~                                                                   
##     B                 2.462    0.700    3.515    0.000    2.462    1.101
##     C                 0.809    0.515    1.570    0.117    0.809    0.492
##     D                -8.228    2.512   -3.276    0.001   -8.228   -0.689
##     E                 1.512    0.810    1.867    0.062    1.512    0.145
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .A                19.119    6.046    3.162    0.002   19.119    0.103
## 
## R-Square:
##                    Estimate
##     A                 0.897
semPaths(fit,"std",layout = 'spring', edge.label.cex=.9, curvePivot = TRUE)

ggcorr(exemplo_cursomeg, nbreaks = 6, label = T, low = "red3", high = "green4", 
       label_round = 2, name = "Correlation Scale", label_alpha = T, hjust = 0.75) +
  ggtitle(label = "Correlation Plot") +
  theme(plot.title = element_text(hjust = 0.6))