Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a "loadings" object to a dataframe (R)

I am trying to convert an object of type "loadings" to a dataframe in R. However, my attempts to coerce it via as_tibble() or as.data.frame() have not worked. Here is the code:

iris_pca <- prcomp(iris[1:4], center = TRUE, scale. = TRUE)
iris_pca$rotation[,1:2] %>% 
  varimax() %>% 
  .$loadings

This prints out:

Loadings:
             PC1    PC2   
Sepal.Length  0.596 -0.243
Sepal.Width         -0.961
Petal.Length  0.570  0.114
Petal.Width   0.565       

                PC1  PC2
SS loadings    1.00 1.00
Proportion Var 0.25 0.25
Cumulative Var 0.25 0.50

How can I get this data into a dataframe?

like image 388
Hank Lin Avatar asked Dec 18 '18 03:12

Hank Lin


People also ask

How can I get This data into a Dataframe?

How can I get this data into a dataframe? From the "loadings" object extract the values as numeric. Coerce them into a matrix. Needed dimensions and names you will find within str (l).

How to show a loadings plot for selected components?

Shows a loadings plot for selected components. ## S3 method for class 'pca' plotLoadings ( obj, comp = c (1, 2), type = (if (length (comp == 2)) "p" else "l"), show.legend = TRUE, show.axes = TRUE, ... ) a value or vector with several values - number of components to show the plot for

How are variables assigned to the factor with the largest loading?

Each variable with any loading larger than 0.5 (in modulus) is assigned to the factor with the largest loading, and the variables are printed in the order of the factor they are assigned to, then those unassigned. … further arguments for other methods, ignored for loadings.


1 Answers

From the "loadings" object extract the values as numeric. Coerce them into a matrix. Needed dimensions and names you will find within str(l).

data.frame(matrix(as.numeric(l), attributes(l)$dim, dimnames=attributes(l)$dimnames))
#                      PC1         PC2
# Sepal.Length  0.59593180 -0.24252635
# Sepal.Width  -0.04181096 -0.96087188
# Petal.Length  0.56955777  0.11438157
# Petal.Width   0.56455387  0.06944826

Data

iris_pca <- prcomp(iris[1:4], center=TRUE, scale.=TRUE)
l <- varimax(iris_pca$rotation[, 1:2])$loadings
like image 123
jay.sf Avatar answered Sep 18 '22 12:09

jay.sf