Factor Analysis
Exploratory Factor Analysis
n <- 500
dt <- data.table(
x1 = rnorm(n)
, x2 = rnorm(n) + 1
, x3 = rnorm(n) + 2
)
dt <- dt[, x4 := x1 + x2 * 3
][, x5 := x2 + x3 ^ 2
][, x6 := x3 * 4 + x1 * x2]
library(psych)
fa3 <- fa(dt, nfactors=3, rotate="oblimin", fm="minres")
fa_table <- function(obj){
t <- unclass(obj$loadings)
rtn <- data.table(t)
colnames(rtn) <- dimnames(t)[[2]]
rtn$variable <- dimnames(t)[[1]]
vars <- c("variable", dimnames(t)[[2]])
invisible(rtn[, ..vars])
}
fa_table(fa3) %>%
prt(caption="Loadings with Three Factors")
variable | MR1 | MR2 | MR3 |
---|---|---|---|
x1 | 0.1291541 | 0.3209858 | 0.9356915 |
x2 | 0.2819707 | 0.9040814 | -0.3168037 |
x3 | 0.9433285 | -0.3199487 | -0.0642847 |
x4 | 0.3054854 | 0.9508368 | -0.0108175 |
x5 | 0.9427486 | -0.0871145 | -0.1274846 |
x6 | 0.9363983 | -0.2166857 | 0.1629788 |
MR1 | MR2 | MR3 | |
---|---|---|---|
SS loadings | 2.8449949 | 1.9813944 | 1.0229471 |
Proportion Var | 0.4741658 | 0.3302324 | 0.1704912 |
Cumulative Var | 0.4741658 | 0.8043982 | 0.9748894 |
Proportion Explained | 0.4863791 | 0.3387383 | 0.1748826 |
Cumulative Proportion | 0.4863791 | 0.8251174 | 1.0000000 |
R sessionInfo
R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] psych_2.2.5 Wu_0.0.0.9000 flexdashboard_0.5.2 [4] lme4_1.1-29 Matrix_1.4-0 mgcv_1.8-38
[7] nlme_3.1-152 png_0.1-7 scales_1.2.0
[10] nnet_7.3-16 labelled_2.9.1 kableExtra_1.3.4
[13] plotly_4.10.0 gridExtra_2.3 ggplot2_3.3.6
[16] DT_0.23 tableone_0.13.2 magrittr_2.0.3
[19] lubridate_1.8.0 dplyr_1.0.9 plyr_1.8.7
[22] data.table_1.14.2 rmdformats_1.0.4 knitr_1.39
loaded via a namespace (and not attached): [1] httr_1.4.3 sass_0.4.1 tidyr_1.2.0 jsonlite_1.8.0
[5] viridisLite_0.4.0 splines_4.2.0 bslib_0.3.1 assertthat_0.2.1 [9] highr_0.9 yaml_2.3.5 pillar_1.8.0 lattice_0.20-45
[13] glue_1.6.2 digest_0.6.29 rvest_1.0.2 minqa_1.2.4
[17] colorspace_2.0-3 htmltools_0.5.3 survey_4.1-1 pkgconfig_2.0.3
[21] haven_2.5.0 bookdown_0.27 purrr_0.3.4 webshot_0.5.3
[25] svglite_2.1.0 tibble_3.1.8 generics_0.1.3 ellipsis_0.3.2
[29] withr_2.5.0 klippy_0.0.0.9500 lazyeval_0.2.2 mnormt_2.1.0
[33] cli_3.3.0 survival_3.2-13 evaluate_0.15 fansi_1.0.3
[37] MASS_7.3-54 forcats_0.5.1 xml2_1.3.3 tools_4.2.0
[41] hms_1.1.1 mitools_2.4 lifecycle_1.0.1 stringr_1.4.0
[45] munsell_0.5.0 compiler_4.2.0 jquerylib_0.1.4 systemfonts_1.0.4 [49] rlang_1.0.4 grid_4.2.0 nloptr_2.0.3 rstudioapi_0.13
[53] htmlwidgets_1.5.4 rmarkdown_2.14 boot_1.3-28 gtable_0.3.0
[57] DBI_1.1.2 R6_2.5.1 fastmap_1.1.0 utf8_1.2.2
[61] stringi_1.7.8 parallel_4.2.0 Rcpp_1.0.9 vctrs_0.4.1
[65] tidyselect_1.1.2 xfun_0.31