Skip to contents

This function creates a data frame of factor loadings from a factor analysis. It can be on an object that was created with psych::fa() or it can be used on a data frame. If used on a data frame, it will run it on all columns in the data frame. Also works on grouped data frame if you want to check how the factor loadings may change along the different levels of a group.

Usage

get_loadings(
  model,
  labels = NULL,
  threshold = 0.4,
  print = "short",
  nfactors = 1,
  fm = "pa",
  rotate = "oblimin"
)

Arguments

model

Either a model created using psych::fa() or a data.frame

labels

Either a character vector or data frame. Creates a new column called "labels" for each variable in the factor analysis.

threshold

The threshold with which to not show the factor loadings. Default is 0.4.

print

The printing method. Default is "short" which only prints a dataframe of the factor loadings. The alternative is "long" but that has not been created yet.

nfactors

Number of factors to extract, default is 1.

fm

Factoring method fm="minres" will do a minimum residual as will fm="uls". Both of these use a first derivative. fm="ols" differs very slightly from "minres" in that it minimizes the entire residual matrix using an OLS procedure but uses the empirical first derivative. This will be slower. fm="wls" will do a weighted least squares (WLS) solution, fm="gls" does a generalized weighted least squares (GLS), fm="pa" will do the principal factor solution, fm="ml" will do a maximum likelihood factor analysis. fm="minchi" will minimize the sample size weighted chi square when treating pairwise correlations with different number of subjects per pair. fm ="minrank" will do a minimum rank factor analysis. "old.min" will do minimal residual the way it was done prior to April, 2017 (see discussion below). fm="alpha" will do alpha factor analysis as described in Kaiser and Coffey (1965). Default is "pa".

rotate

"none", "varimax", "quartimax", "bentlerT", "equamax", "varimin", "geominT" and "bifactor" are orthogonal rotations. "Promax", "promax", "oblimin", "simplimax", "bentlerQ, "geominQ" and "biquartimin" and "cluster" are possible oblique transformations of the solution. The default is to do a oblimin transformation, although versions prior to 2009 defaulted to varimax. SPSS seems to do a Kaiser normalization before doing Promax, this is done here by the call to "promax" which does the normalization before calling Promax in GPArotation.

Examples


library(adlgraphs)
library(dplyr)
library(psych)

# first lets get our data set
data <- test_data %>% 
  select(top:run)

# now create the fa object
model <- fa(data, nfactors = 1, fm = "pa", rotate = "oblimin")
# get just the loadings
get_loadings(model)
#> # A tibble: 9 × 4
#>   variables     PA1 communality uniqueness
#>   <chr>       <dbl>       <dbl>      <dbl>
#> 1 top        NA         0.0862       0.914
#> 2 inferior   NA         0.122        0.878
#> 3 dominate   NA         0.0213       0.979
#> 4 deserving  NA         0.00110      0.999
#> 5 special    NA         0.0885       0.912
#> 6 harder     NA         0.0687       0.931
#> 7 controlled  0.685     0.469        0.531
#> 8 small       0.654     0.428        0.572
#> 9 run         0.610     0.373        0.627
# get the loadings with the variable labels based on data object
get_loadings(model, data)
#> # A tibble: 9 × 5
#>   variables  labels                                   PA1 communality uniqueness
#>   <chr>      <chr>                                  <dbl>       <dbl>      <dbl>
#> 1 top        "An ideal society requires some grou… NA         0.0862       0.914
#> 2 inferior   "Some groups of people are simply in… NA         0.122        0.878
#> 3 dominate   "No one group should dominate in soc… NA         0.0213       0.979
#> 4 deserving  "Groups at the bottom are just as de… NA         0.00110      0.999
#> 5 special    "It is unfair for some groups in soc… NA         0.0885       0.912
#> 6 harder     "I have a harder time succeeding tha… NA         0.0687       0.931
#> 7 controlled "Much of our lives are being control…  0.685     0.469        0.531
#> 8 small      "Even though we live in a democracy,…  0.654     0.428        0.572
#> 9 run        "The people who really \"run\" the c…  0.610     0.373        0.627

# we can do all of this in one step with pipes
test_data %>% 
  # select only the variables we want in the factor analysis
  select(top:run) %>% 
  # run the factor analysis
  fa(., nfactors = 1, fm = "pa", rotate = "oblimin") %>% 
  # get the loadings
  get_loadings()
#> # A tibble: 9 × 4
#>   variables     PA1 communality uniqueness
#>   <chr>       <dbl>       <dbl>      <dbl>
#> 1 top        NA         0.0862       0.914
#> 2 inferior   NA         0.122        0.878
#> 3 dominate   NA         0.0213       0.979
#> 4 deserving  NA         0.00110      0.999
#> 5 special    NA         0.0885       0.912
#> 6 harder     NA         0.0687       0.931
#> 7 controlled  0.685     0.469        0.531
#> 8 small       0.654     0.428        0.572
#> 9 run         0.610     0.373        0.627

# Now let's remove the threshold for the loadings and include labels
test_data %>% 
  # select only the variables we want in the factor analysis
  select(top:run) %>% 
  # run the factor analysis
  fa(., nfactors = 1, fm = "pa", rotate = "oblimin") %>% 
  # specify threshold is 0
  get_loadings(threshold = 0, labels = data)
#> # A tibble: 9 × 5
#>   variables  labels                                   PA1 communality uniqueness
#>   <chr>      <chr>                                  <dbl>       <dbl>      <dbl>
#> 1 top        "An ideal society requires some gro…  0.294      0.0862       0.914
#> 2 inferior   "Some groups of people are simply i…  0.350      0.122        0.878
#> 3 dominate   "No one group should dominate in so…  0.146      0.0213       0.979
#> 4 deserving  "Groups at the bottom are just as d… -0.0331     0.00110      0.999
#> 5 special    "It is unfair for some groups in so…  0.297      0.0885       0.912
#> 6 harder     "I have a harder time succeeding th…  0.262      0.0687       0.931
#> 7 controlled "Much of our lives are being contro…  0.685      0.469        0.531
#> 8 small      "Even though we live in a democracy…  0.654      0.428        0.572
#> 9 run        "The people who really \"run\" the …  0.610      0.373        0.627

# alternatively, we could skip the fa step entirely like so
test_data %>% 
  # select only the variables we want in the factor analysis
  select(top:run) %>% 
  # specify number of factors, rotation, and factor method
  get_loadings()
#> # A tibble: 9 × 4
#>   variables     PA1 communality uniqueness
#>   <chr>       <dbl>       <dbl>      <dbl>
#> 1 top        NA         0.0862       0.914
#> 2 inferior   NA         0.122        0.878
#> 3 dominate   NA         0.0213       0.979
#> 4 deserving  NA         0.00110      0.999
#> 5 special    NA         0.0885       0.912
#> 6 harder     NA         0.0687       0.931
#> 7 controlled  0.685     0.469        0.531
#> 8 small       0.654     0.428        0.572
#> 9 run         0.610     0.373        0.627

# we can also specify the number of factors, rotation, and factoring method
test_data %>% 
  # select only the variables we want in the factor analysis
  select(top:run) %>% 
  # specify number of factors, rotation, factor method, and threshold
  get_loadings(nfactors = 2, rotate = "varimax", fm = "minres", threshold = 0.2) 
#> # A tibble: 9 × 5
#>   variables     MR1    MR2 communality uniqueness
#>   <chr>       <dbl>  <dbl>       <dbl>      <dbl>
#> 1 top         0.225  0.587      0.395       0.605
#> 2 inferior    0.292  0.646      0.503       0.497
#> 3 dominate    0.263 -0.415      0.241       0.759
#> 4 deserving  NA     -0.429      0.187       0.813
#> 5 special     0.294 NA          0.0887      0.911
#> 6 harder      0.254 NA          0.0655      0.935
#> 7 controlled  0.642 NA          0.425       0.575
#> 8 small       0.656 NA          0.430       0.570
#> 9 run         0.692 NA          0.503       0.497

# we can also calculate the factor loadings by a grouping variable
test_data %>% 
  # select the grouping variable and the variables to be used in factor analysis
  select(edu_f2, top:run) %>% 
  # group the data
  group_by(edu_f2) %>% 
  # specify number of factors, rotation, factor method, and threshold
  get_loadings(nfactors = 2, rotate = "varimax", fm = "minres", threshold = 0.2) 
#> # A tibble: 18 × 6
#>    groups                       variables     MR1    MR2 communality uniqueness
#>    <chr>                        <chr>       <dbl>  <dbl>       <dbl>      <dbl>
#>  1 No College Degree            top        NA      0.633      0.416       0.584
#>  2 No College Degree            inferior   NA      0.682      0.483       0.517
#>  3 No College Degree            dominate    0.438 -0.332      0.302       0.698
#>  4 No College Degree            deserving  NA     -0.485      0.235       0.765
#>  5 No College Degree            special     0.245 NA          0.0964      0.904
#>  6 No College Degree            harder      0.303 NA          0.106       0.894
#>  7 No College Degree            controlled  0.598  0.208      0.401       0.599
#>  8 No College Degree            small       0.670 NA          0.453       0.547
#>  9 No College Degree            run         0.723 NA          0.528       0.472
#> 10 At Least a Bachelor's Degree top        NA      0.573      0.368       0.632
#> 11 At Least a Bachelor's Degree inferior    0.293  0.588      0.432       0.568
#> 12 At Least a Bachelor's Degree dominate   NA     -0.503      0.288       0.712
#> 13 At Least a Bachelor's Degree deserving   0.263 -0.399      0.229       0.771
#> 14 At Least a Bachelor's Degree special     0.325 NA          0.112       0.888
#> 15 At Least a Bachelor's Degree harder     NA     NA          0.0223      0.978
#> 16 At Least a Bachelor's Degree controlled  0.640  0.215      0.456       0.544
#> 17 At Least a Bachelor's Degree small       0.639 NA          0.429       0.571
#> 18 At Least a Bachelor's Degree run         0.641 NA          0.413       0.587