spindoctor
spindoctor

Reputation: 1895

Printing readable factor analysis output in Rmarkdown

I have to print readable factor analysis output in an Rmarkdown document (can ultimately be pdf, word or html), and I am having real trouble doing this. I had this problem here. I am running psych version 1.8.12.

Reproducible code is below, but I have also put up a github respository here:

Note, when I run results='markup' the pdf compiles, but the table is not readable: it prints the latex code. When I run results='asis', an error is returned ! LaTeX Error: \caption outside float.

I am much less familiar with the packages like kabel and texreg. Would those be an option? `


title: "Factor Analysis Test"
author: "Simon Kiss"
date: '2019-06-07'
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(psych)
```

```{r}
data('Thurstone')
```
```{r}
mod<-fa(Thurstone, 3)
```
```{r results='markup'} 
fa2latex(mod)
````

Upvotes: 0

Views: 1103

Answers (4)

Well, it's a late answer I guess, but "baap" package can solve your problem. You can create a table of factor loadings while expressing unwanted loadings. This also works fine in Rmarkdown formats.

Upvotes: 0

Max Teflon
Max Teflon

Reputation: 1800

The Error is in the fa2latex-output. For some bizarre reason, it changes scriptsize into s\iptsize. The following changes fix the output:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(psych)
```


```{r}
data('Thurstone')
```
```{r}
mod<-fa(Thurstone, 3)
```
```{r, results='asis'} 
a <- fa2latex(mod, silent = T)
cat(gsub('s\\iptsize','scriptsize',a))
```

EDIT

The error just stopped to be reproducible for me, not sure what is going on. Suddenly

```{r, results='asis'} 
a <- fa2latex(mod, silent = F)
```

does work as intended

Upvotes: 1

Dimitrios Zacharatos
Dimitrios Zacharatos

Reputation: 850

in that case you need to add the output of the fa2latex(mod) directly into the document and not inside {r results='markup'} and then produce a pdf not an html or a doc. copy and paste the code bellow into an Rmd document and see what happens.

---
output:
  pdf_document: default
  html_document: default
  word_document: default
---
title: "Factor Analysis Test"
author: "Simon Kiss"
date: '2019-06-07'
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(psych)
```

```{r}
data('Thurstone')
```

```{r}
mod<-fa(Thurstone, 3)
```

% Called in the psych package  fa2latex % Called in the psych package  mod 
\begin{table}[htpb]\caption{fa2latex}
\begin{center}
\begin{scriptsize} 
\begin{tabular} {l r r r r r r }
 \multicolumn{ 6 }{l}{ A factor analysis table from the psych package in R } \cr 
 \hline Variable  &   MR1  &  MR2  &  MR3  &  h2  &  u2  &  com \cr 
  \hline 
Sentences   &  \bf{ 0.90}  &  -0.03  &   0.04  &  0.82  &  0.18  &  1.01 \cr 
 Vocabulary   &  \bf{ 0.89}  &   0.06  &  -0.03  &  0.84  &  0.16  &  1.01 \cr 
 Sent.Completion   &  \bf{ 0.84}  &   0.03  &   0.00  &  0.74  &  0.26  &  1.00 \cr 
 First.Letters   &   0.00  &  \bf{ 0.85}  &   0.00  &  0.73  &  0.27  &  1.00 \cr 
 Four.Letter.Words   &  -0.02  &  \bf{ 0.75}  &   0.10  &  0.63  &  0.37  &  1.04 \cr 
 Suffixes   &   0.18  &  \bf{ 0.63}  &  -0.08  &  0.50  &  0.50  &  1.20 \cr 
 Letter.Series   &   0.03  &  -0.01  &  \bf{ 0.84}  &  0.73  &  0.27  &  1.00 \cr 
 Pedigrees   &  \bf{ 0.38}  &  -0.05  &  \bf{ 0.46}  &  0.51  &  0.49  &  1.96 \cr 
 Letter.Group   &  -0.06  &   0.21  &  \bf{ 0.63}  &  0.52  &  0.48  &  1.25 \cr 
\hline \cr SS loadings & 2.65 &  1.87 &  1.49 &  \cr  
\cr 
            \hline \cr 
MR1   & 1.00 & 0.59 & 0.53 \cr 
 MR2   & 0.59 & 1.00 & 0.52 \cr 
 MR3   & 0.53 & 0.52 & 1.00 \cr 
 \hline 
\end{tabular}
\end{scriptsize}
\end{center}
\label{default}
\end{table} 

Upvotes: 0

Dimitrios Zacharatos
Dimitrios Zacharatos

Reputation: 850

if you want to output a table make sure to convert it to a dataframe first.

title: "Factor Analysis Test"
author: "Simon Kiss"
date: '2019-06-07'
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(psych)
```

```{r}
data('Thurstone')
```

```{r}
mod<-fa(Thurstone, 3)
```
```{r results='markup'} 
knitr::kable(data.frame(psych::fa.sort(mod$loadings)[1:8,]))

Upvotes: 0

Related Questions