Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export a sorted factor loading table?

Tags:

r

Problem definition: exporting a sorted version of an object of class "loadings"

After running a factor analysis with the fa-function of the psych-package, I get a factor loadings table that looks like the one shown here:

Loadings:
         Factor1 Factor2 Factor3
TH_Q1     0.173   0.548   0.403 
TH_Q2     0.306   0.291   0.825 
TH_Q3     0.334   0.203   0.825 
TH_Q4     0.262   0.536   0.171 
TH_Q5     0.235   0.686         
TH_Q6     0.125   0.836         
TH_Q7     0.200   0.838         
TH_Q8_A1                        
TH_Q8_A2          0.155         
TH_Q9     0.644   0.133   0.171 
TH_Q10    0.608   0.208   0.157 
TH_Q11    0.569   0.161   0.306 
TH_Q12    0.722           0.127 
TH_Q13    0.661   0.311         
TH_Q14    0.562   0.407         
TH_Q15    0.675   0.422         

After running the function print on this table (which is stored in the variable f.loadings), I get a sorted table print(f.loadings, digits=2, cutoff=.3, sort=TRUE):

Loadings:
         Factor1 Factor2 Factor3
TH_Q9     0.64                  
TH_Q10    0.61                  
TH_Q11    0.57            0.31  
TH_Q12    0.72                  
TH_Q13    0.66    0.31          
TH_Q14    0.56    0.41          
TH_Q15    0.68    0.42          
TH_Q1             0.55    0.40  
TH_Q4             0.54          
TH_Q5             0.69          
TH_Q6             0.84          
TH_Q7             0.84          
TH_Q2     0.31            0.82  
TH_Q3     0.33            0.83  
TH_Q8_A1                        
TH_Q8_A2                        

print however returns an "invisible" copy of the object, so I'm not able to export this result in the requested format. I however would like to find a way to export a .csv version of this table.

I wasn't able to find a way to specify parameters of write.csv to do a correct sorting of an object of class "loading". Assigning the result of the print function doesn't solve this either, since it only returns the unsorted version. Thus x <- print(f.loadings, digits=2, cutoff=.3, sort=TRUE) and subsequently calling for the new variable x, still returns the unsorted version of the table.

What function would be suited for sorting a "loadings"-object and returning this object visibly? In other words, how can I export such a sorted table?

Code to generate the table:

f.loadings <- structure(c(0.172693322885797, 0.306277415972136, 0.334012445825371, 
0.261822356615649, 0.234600824098634, 0.124541887813939, 0.200125976802047, 
0.0199775267669519, 0.0771905784767979, 0.643886342785064, 0.608004298828405, 
0.569498016145868, 0.722454442131503, 0.660683752725898, 0.561975379133291, 
0.675119271585253, 0.548184083921831, 0.291215413974386, 0.20334622551054, 
0.535545380240845, 0.685635981787823, 0.836401389336655, 0.837525597359627, 
0.0186113870539496, 0.154659865540958, 0.132908227837058, 0.20832344061795, 
0.160657979843522, 0.0933961709813049, 0.311465272208257, 0.406860675137862, 
0.421946817384512, 0.402664774610544, 0.824934582975472, 0.825220077707656, 
0.170809720550637, -0.0486225264368695, 0.0612401518170266, 0.052596915030506, 
-0.0463868732056794, 0.0208945338424677, 0.171412077700389, 0.156524506151013, 
0.306203004564158, 0.127377474768802, -0.0869197819037828, -0.0962274476959987, 
-0.0465278761105364), .Dim = c(16L, 3L), .Dimnames = list(c("TH_Q1",  "TH_Q2", "TH_Q3", "TH_Q4", "TH_Q5", "TH_Q6", "TH_Q7", "TH_Q8_A1",  "TH_Q8_A2", "TH_Q9", "TH_Q10", "TH_Q11", "TH_Q12", "TH_Q13",  "TH_Q14", "TH_Q15"), c("Factor1", "Factor2", "Factor3")), class = "loadings")
like image 603
smoens Avatar asked Aug 24 '12 17:08

smoens


2 Answers

Here's how you can hack the default printing method for loadings. I have assigned the print statement to newx and exported that. When you assign the printLoadings result to a variable, you have your sorted object at your disposal. The result is now a table of characters. I'll leave it to you as an exercise to convert it to numeric.

> getS3method("print","loadings") #get the hidden method and modify it
printLoadings <- function (x, digits = 3, cutoff = 0.1, sort = FALSE, ...) 
{
   Lambda <- unclass(x)
   p <- nrow(Lambda)
   factors <- ncol(Lambda)
   if (sort) {
      mx <- max.col(abs(Lambda))
      ind <- cbind(1L:p, mx)
      mx[abs(Lambda[ind]) < 0.5] <- factors + 1
      Lambda <- Lambda[order(mx, 1L:p), ]
   }
   cat("\nLoadings:\n")
   fx <- format(round(Lambda, digits))
   names(fx) <- NULL
   nc <- nchar(fx[1L], type = "c")
   fx[abs(Lambda) < cutoff] <- paste(rep(" ", nc), collapse = "")
   newx <- print(fx, quote = FALSE, ...) # I assigned this to a variable
   vx <- colSums(x^2)
   varex <- rbind(`SS loadings` = vx)
   if (is.null(attr(x, "covariance"))) {
      varex <- rbind(varex, `Proportion Var` = vx/p)
      if (factors > 1) 
         varex <- rbind(varex, `Cumulative Var` = cumsum(vx/p))
   }
   cat("\n")
   print(round(varex, digits))
   invisible(newx) #previously returned x
}

mmm <- printLoadings(f.loadings)
> str(mmm)
 chr [1:16, 1:3] " 0.173" " 0.306" " 0.334" " 0.262" " 0.235" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:16] "TH_Q1" "TH_Q2" "TH_Q3" "TH_Q4" ...
  ..$ : chr [1:3] "Factor1" "Factor2" "Factor3"

> as.table(mmm)
         Factor1 Factor2 Factor3
TH_Q1     0.173   0.548   0.403 
TH_Q2     0.306   0.291   0.825 
TH_Q3     0.334   0.203   0.825 
TH_Q4     0.262   0.536   0.171 
like image 64
Roman Luštrik Avatar answered Nov 10 '22 10:11

Roman Luštrik


When a function returns its results with invisible() it simply means that the results aren't printed. However, you can still assign the results to a variable and manipulate it as any other object.

So:

x <- print(f.loadings)
x
Loadings:
         Factor1 Factor2 Factor3
TH_Q1     0.173   0.548   0.403 
TH_Q2     0.306   0.291   0.825 
TH_Q3     0.334   0.203   0.825 
TH_Q4     0.262   0.536   0.171 
TH_Q5     0.235   0.686         
TH_Q6     0.125   0.836         
TH_Q7     0.200   0.838         
TH_Q8_A1                        
TH_Q8_A2          0.155         
TH_Q9     0.644   0.133   0.171 
TH_Q10    0.608   0.208   0.157 
TH_Q11    0.569   0.161   0.306 
TH_Q12    0.722           0.127 
TH_Q13    0.661   0.311         
TH_Q14    0.562   0.407         
TH_Q15    0.675   0.422         

               Factor1 Factor2 Factor3
SS loadings      3.259   3.145   1.747
Proportion Var   0.204   0.197   0.109
Cumulative Var   0.204   0.400   0.509

Similary, str(x) indicates the results is a matrix:

str(x)
 loadings [1:16, 1:3] 0.173 0.306 0.334 0.262 0.235 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:16] "TH_Q1" "TH_Q2" "TH_Q3" "TH_Q4" ...
  ..$ : chr [1:3] "Factor1" "Factor2" "Factor3"

You can now use write.csv(x) to export the results.

like image 30
Andrie Avatar answered Nov 10 '22 11:11

Andrie