Skip to content
Levi Brackman (Rabbi, PhD)

Scholar, Podcaster. Author, Seeker, Social Scientist, Entrepreneur

Levi Brackman (Rabbi, PhD)

Scholar, Podcaster. Author, Seeker, Social Scientist, Entrepreneur

Goodness of Fit Measures Table APA for Factor Analysis

Levi Brackman, April 21, 2016April 22, 2016

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).

[code lang=”r”]options(scipen=999)[/code]

 

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):

[code lang=”r”]CFImodel =  1  –  ( ( model$STATISTIC – model$dof)/(model$null.chisq-model$null.dof ) )[/code]

Extract measures and save them to GoodnessfitMeasures variable.

[code lang=”r”]GoodnessfitMeasures = c(model$STATISTIC, model$PVAL,model$dof, CFImodel, model$TLI, model$RMSEA, model$rms)[/code]

Put into columns

[code lang=”r”]require(reshape)[/code]

[code lang=”r”]GoodnessfitMeasures = as.data.frame(GoodnessfitMeasures)[/code]

[code lang=”r”]GoodnessfitMeasures = melt(GoodnessfitMeasures, id.vars="GoodnessfitMeasures")
[/code]

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

For Lavaan

[code lang=”r”]GoodnessfitMeasures = fitmeasures(fit, c("chisq", "df", "pvalue", "cfi", "tli", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper", "srmr"))[/code]

Create names for colunms

[code lang=”r”]namess = c("Chisq", "DF", "P-Value", "CFI", "TLI", "RMSEA", "RMSEA ci upper", "RMSEA ci lower", "SRMR")[/code]

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

[code lang=”r”]GoodnessfitMeasures = data.frame(GoodnessfitMeasures)[/code]

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.

[code lang=”r”]all = bind_cols(namess, GoodnessfitMeasures)[/code]

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)

[code lang=”r”]round_df <- function(df, digits) {
nums <- vapply(df, is.numeric, FUN.VALUE = logical(1))

df[,nums] <- round(df[,nums], digits = digits)

(df)
}
[/code]

Round the numbers on the data frame

[code 1=”r” language=”lang”]GoodnessfitMeasures = round_df(all, digits=3)[/code]

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

[code lang=”r”]GoodnessfitMeasures = t(GoodnessfitMeasures)[/code]

I like to turn the matrix into a data frame

[code lang=”r”]GoodnessfitMeasures = as.data.frame(GoodnessfitMeasures)[/code]

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.

[code lang=”r”] require(xtable)[/code]

[code lang=”r”] xtable(GoodnessfitMeasures)[/code]

Code Science Statistics

Post navigation

Previous post
Next post

Related Posts

We (and our investments) Regress to Mediocrity

March 16, 2016

(Here is an interactive app I created to help you understand the ideas expressed in this post. Take a look either before or after reading the post. We all have days where we are much more productive than usual. At such times we feel good about ourselves and think that…

Read More

Why I love Las Vegas: The Law of Large Numbers

March 25, 2016March 25, 2016

(View the app I created that illustrates the Law of Large Numbers either before or after reading this article here) Las Vegas is caters to people’s vices of all kinds. Sheindy and I love visiting Sin City although we do not gamble, drink or partake in any of the other…

Read More

Enterprise-Level Data Science: Lessons From the Frontlines

February 28, 2019September 2, 2019

(First published by datascience.com) Data science, machine learning (ML) and artificial intelligence (AI) are relatively new endeavors for enterprise-level business. Many companies are batch training as well as batch scoring ML models. Predictions are stored in a database to be retrieved either by applications or humans. However, real-time training on…

Read More

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Me and this Site

  • Navigating My Writing
  • Short Bio
  • Join Newsletter

Three Latest Posts

  • Before Bezos and Amazon: The Biblical Covenantal Relationship Had an Inbuilt ‘Day One’ Mindset September 20, 2024
  • Rabbinic Reinterpretation of the Torah: Ethical Adjustments in the Oral Law – Ki Teitzei September 16, 2024
  • Navigating Nepotism and Divine Choice in Torah Portion Tsav March 29, 2024

Writing in Chronological Order

Writing by Category

Search the Site

©2025 Levi Brackman (Rabbi, PhD) | WordPress Theme by SuperbThemes