---
title: "Exploratory Factor Analysis (EFA) Example in R"
author:
 - name: Ben Kite
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: bakite@ku.edu
advertise: >
    Please  visit [http://crmda.ku.edu/guides](http://crmda.ku.edu/guides)
keywords: Structural Equation Modeling, Exploratory Factor Analysis, R, factanal
abstract: >

    This guide outlines how to specify an exploratory factor analysis
    in R. An example with 6 manifest variables measuring 1 or 2 latent
    factors is presented. The model estimation results can be compared
    to the same model fitted with Mplus.

checked_by: "First Last"
Note to Authors: please_dont_change_the_next 4 lines!
output:
  stationery::crmda_html_document:
    toc: true
    toc_depth: 2
    theme: null
    css: theme/kutils.css
    template: theme/guide-template.html
    mathjax-url: "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
logoleft: theme/logoleft.png
logoright: theme/logo-vert.png
---

```{r setup, include=FALSE}
##This Invisible Chunk is required in all CRMDA documents
outdir <- paste0("tmpout")
if (!file.exists(outdir)) dir.create(outdir, recursive = TRUE)
knitr::opts_chunk$set(echo=TRUE, comment=NA, fig.path=paste0(outdir, "/p-"))
options(width = 70)
```

#### Data Importation and Naming {.bs-callout .bs-callout-grey}
The datafile "job_placement" needs to be read in to the R session.
```{r }
dat <- read.csv("../../data/job_placement.csv", header = FALSE)
```

Because the datafile does not have column (or variable) names, the
variable names need to be specified.
```{r }
colnames(dat) <- c("id", "wjcalc", "wjspl", "wratspl", "wratcalc", "waiscalc", "waisspl", "edlevel", "newschl", "suspend", "expelled", "haveld", "female", "age")
```

In the original data file the missing values are coded as "99999".
These values need to be recoded to NA so that R recognizes them as
missing.
```{r }
dat[dat == 99999] <- NA
```

Then the variables that are to be used in the EFA need to be put into
a separate data frame.  This is the data frame that will be used in
the analysis. The `dat[ , 2:7]` command makes a data frame using all
rows, but only columns 2-7 from the "dat" data frame.
```{r }
dat1 <- dat[ , 2:7]
```

The last part of the data manipulation is to remove the cases with
missing values in the analysis data frame, this is something
equivalent to `LISTWISE = ON` under the `DATA` command in Mplus.
```{r}
dat1 <- na.omit(dat1)
```

#### Exploratory Factor Analysis {.bs-callout .bs-callout-red}
Now the EFA can be run with 1 and 2 factors extracted.
```{r }
output1 <- factanal(dat1, 1, rotation = "varimax")
output1
output2 <- factanal(dat1, 2, rotation = "varimax")
output2
```

#### The EFA Example in Mplus {.bs-callout .bs-callout-blue}
Please click [efa-01.html](../../Mplus/Ex-01-EFA/efa-01.html) if the
reader would like to see the same EFA models fitted with Mplus.

[//]: (All guides must have this as the final stanza)
#### Session Information {.bs-callout .bs-callout-grey}
Below is a summary of the R session used to generate this example.
```{r sessionInfo, echo = FALSE}
sessionInfo()
```

Available under
[Created Commons license 3.0 <img src="http://crmda.dept.ku.edu/images/cc-3.0.png" alt="CC BY"
style="width: 75px;height: 20px;"/>](http://creativecommons.org/licenses/by/3.0/)
