This post is for social science researchers and research psychologists who are doing factor analysis and want to create tables with fit measures in R. If you do not fit that very narrow audience you might not find this post interesting.

**The Problem:**

How to take the fit measures of multiple models and place them into a table (APA style) that can be put directly into a paper.

**The solution:**

First remove scientific notation from outputs (this is a personal preference of mine).

options(scipen=999)

**For Psyc package**, note we have manually calculated the CFI in this piece of code because the psyc package does not have the CFI.

First compute the CFI (if you want that measure):

CFImodel = 1 - ( ( model$STATISTIC - model$dof)/(model$null.chisq-model$null.dof ) )

Extract measures and save them to GoodnessfitMeasures variable.

GoodnessfitMeasures = c(model$STATISTIC, model$PVAL,model$dof, CFImodel, model$TLI, model$RMSEA, model$rms)

Put into columns

require(reshape)

GoodnessfitMeasures = as.data.frame(GoodnessfitMeasures)

GoodnessfitMeasures = melt(GoodnessfitMeasures, id.vars="GoodnessfitMeasures")

Skip the next section if you are not using Lavaan and go to “Continued” below.

**For Lavaan**

GoodnessfitMeasures = fitmeasures(fit, c("chisq", "df", "pvalue", "cfi", "tli", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper", "srmr"))

Create names for colunms

namess = c("Chisq", "DF", "P-Value", "CFI", "TLI", "RMSEA", "RMSEA ci upper", "RMSEA ci lower", "SRMR")

Put GoodnessfitMeasures into data frame (if you already did this for the psych package above no need to do it again).

GoodnessfitMeasures = data.frame(GoodnessfitMeasures)

**Continued**

Use dplyr to bind names and fit measure. This will result in at least two columns. 1. the names of the fit measures Chisq, DF etc. 2. The corresponding fit measures (can have more than one column depending on how many models you have.

all = bind_cols(namess, GoodnessfitMeasures)

There might be long numbers in each Create function to round all numbers of a data frame (function was found here: http://stackoverflow.com/questions/9063889/rounding-a-dataframe-in-r and works very well)

round_df <- function(df, digits) { nums <- vapply(df, is.numeric, FUN.VALUE = logical(1)) df[,nums] <- round(df[,nums], digits = digits) (df) }

Round the numbers on the data frame

GoodnessfitMeasures = round_df(all, digits=3)

Transpose the data frame so that the measures of each model take up a row rather than a column. This makes it easier to compare models if there are many of them. The result will be a matrix

GoodnessfitMeasures = t(GoodnessfitMeasures)

I like to turn the matrix into a data frame

GoodnessfitMeasures = as.data.frame(GoodnessfitMeasures)

Print the table as a latex then use suave to compile as a PDF. I personally then turn the PDF into a word doc using adobe pro. If you don’t have that there are other online options to turn PDFs into word docs.

require(xtable)

xtable(GoodnessfitMeasures)

## Leave a Reply