psych: principal - loadings components
This was partly answered, but since it is my package, I will give a somewhat more complete answer.
The summary table of the PCA or FA factor loadings tables is calculated in the print function. It it is returned (invisibly by print). However, it is available as the Vaccounted object.
i.e. summary table of the PCA or FA output
set.seed(0)
x <- replicate(8, rnorm(10))
pca.x <- principal(x, nf=4, rotate="varimax")
p <- print(pca.x)
round(p$Vaccounted,2) #shows the summary of the loadings table
PC1 PC3 PC4 PC2
SS loadings 2.32 1.93 1.37 1.34
Proportion Var 0.29 0.24 0.17 0.17
Cumulative Var 0.29 0.53 0.70 0.87
Proportion Explained 0.33 0.28 0.20 0.19
Cumulative Proportion 0.33 0.61 0.81 1.00
This works for the fa function as well.
psych - Getting factor loadings as data.frame for LaTeX export
When you look at fac$loading
, you see that it is a S3 object. Removing the class attribute gives you a matrix
which can then be passed to xtable
:
str(fac$loadings)
class(fac$loadings)
xtable(unclass(fac$loadings))
psych - Getting cumulative Variance from fa loadings
After running str(fac)
it seems that you can get the data you're interested in with
df <- fac$Vaccounted
> df
ML1 ML2 ML3 ML4 ML5
SS loadings 4.4291169 2.42278894 1.5623383 1.3308373 0.96561620
Proportion Var 0.1581827 0.08652818 0.0557978 0.0475299 0.03448629
Cumulative Var 0.1581827 0.24471092 0.3005087 0.3480386 0.38252492
Proportion Explained 0.4135227 0.22620272 0.1458671 0.1242531 0.09015437
Cumulative Proportion 0.4135227 0.63972545 0.7855925 0.9098456 1.00000000
Getting data frame from loadings of factor analysis (fa function in psych)
x <- c(1, 2, 1, 2)
y <- c(1, 2, 3, 4)
z <- c(4, 3, 2, 1)
df <- data.frame(x, y, z)
fit <- psych::fa(df, nfactors = 2)
x <- fit$loadings
via stats:::print.loadings
:
Lambda <- unclass(x)
p <- nrow(Lambda)
factors <- ncol(Lambda)
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))
}
tibble::rownames_to_column(as.data.frame(varex), "x")
## x MR1 MR2
## 1 SS loadings 2.1954555 3.000000e-30
## 2 Proportion Var 0.7318185 1.000000e-30
## 3 Cumulative Var 0.7318185 7.318185e-01
And, for the first table:
cutoff <- 0.1 # (the default for the `print.loadings()` function)
Lambda <- unclass(x)
p <- nrow(Lambda)
fx <- setNames(Lambda, NULL)
fx[abs(Lambda) < cutoff] <- NA_real_
fx <- as.data.frame(fx)
rownames(fx) <- NULL
fx
## MR1 MR2
## 1 0.4476761 NA
## 2 0.9987596 NA
## 3 -0.9987596 NA
Export Cronbach Alpha Results - R
The output can be saved as a txt file this way. You can also subset the object created with the alpha function using the $ operator to get only the information you are interested in.
setwd("~/Desktop")
out <- psych::alpha(d)
capture.output(out,file = "alpha.txt")
Export describe.by (package psych) as csv file in R
Function describeBy()
(this should be used instead of descrive.by()
that is deprecated) produces list of data frame and therefore can't be written to file with write.table()
.
library(psych)
estatistica <- describeBy(pag,list(pag$Jogo))
estatistica
: A
var n mean sd median trimmed mad min max range skew kurtosis se
Jogo* 1 3 1.00 0.00 1 1.00 0.00 1 1 0 NaN NaN 0.00
Pais* 2 3 2.00 1.00 2 2.00 1.48 1 3 2 0.00 -2.33 0.58
Numero 3 3 2.67 2.89 1 2.67 0.00 1 6 5 0.38 -2.33 1.67
--------------------------------------------------------------------
: B
var n mean sd median trimmed mad min max range skew kurtosis se
Jogo* 1 3 2.00 0.00 2 2.00 0.00 2 2 0 NaN NaN 0.00
Pais* 2 3 2.00 1.00 2 2.00 1.48 1 3 2 0.00 -2.33 0.58
Numero 3 3 3.33 3.21 2 3.33 1.48 1 7 6 0.34 -2.33 1.86
To solve this problem one way would be to put all list elements into one data frame with do.call()
and rbind()
and then write to file. This will make data frame where group names will be added before original variable names.
estatistica2<-do.call("rbind",estatistica)
estatistica2
var n mean sd median trimmed mad min max range skew kurtosis se
A.Jogo* 1 3 1.00 0.00 1.0 1.00 0.00 1 1 0 NaN NaN 0.00
A.Pais* 2 3 2.00 1.00 2.0 2.00 1.48 1 3 2 0.00 -2.33 0.58
A.Numero 3 3 2.67 2.89 1.0 2.67 0.00 1 6 5 0.38 -2.33 1.67
B.Jogo* 1 3 2.00 0.00 2.0 2.00 0.00 2 2 0 NaN NaN 0.00
B.Pais* 2 3 2.00 1.00 2.0 2.00 1.48 1 3 2 0.00 -2.33 0.58
B.Numero 3 3 3.33 3.21 2.0 3.33 1.48 1 7 6 0.34 -2.33 1.86
C.Jogo* 1 2 3.00 0.00 3.0 3.00 0.00 3 3 0 NaN NaN 0.00
C.Pais* 2 2 2.50 0.71 2.5 2.50 0.74 2 3 1 0.00 -2.75 0.50
C.Numero 3 2 1.50 0.71 1.5 1.50 0.74 1 2 1 0.00 -2.75 0.50
D.Jogo* 1 2 4.00 0.00 4.0 4.00 0.00 4 4 0 NaN NaN 0.00
D.Pais* 2 2 2.50 0.71 2.5 2.50 0.74 2 3 1 0.00 -2.75 0.50
D.Numero 3 2 3.50 0.71 3.5 3.50 0.74 3 4 1 0.00 -2.75 0.50
Selecting factor loadings above threshold in R
After consulting various online sources I have made the following changes in my function. So now it incrementally writes the output from the loop.
write.factors <- function(loadings, cutoff_p = 0.295, cutoff_n = -0.295, file_name = "factors.csv"){
f <- data.frame(unclass(loadings))
sink(file_name)
for(c in 1:ncol(f)){
variables <- rownames(f)
ff <- data.frame(variables, f[,c])
colnames(ff)[2] <- colnames(f)[c]
nd <- subset(ff, ff[,2] > cutoff_p | ff[,2] < cutoff_n)
nd <- droplevels(nd)
write.csv(nd)
cat('____________________________')
cat('\n')
}
sink()
}
write.factors(fa$loadings)
Related Topics
Programming with Ggplot2 and Dplyr
Enriching a Ggplot2 Plot with Multiple Geom_Segment in a Loop
How Create a Sequence of Strings with Different Numbers in R
How to Load CSV File into Sparkr on Rstudio
How to Insert Appendix After References in Rmd Using Rstudio
R - Lattice Xyplot - How to Add Error Bars to Groups and Summary Lines
How to Scale the Size of Line and Point Separately in Ggplot2
How to Know a Function or an Operation in R Is Vectorized
Equal Frequency Discretization in R
Control Number Formatting in Shiny's Implementation of Datatable
Use of .By and .Eachi in the Data.Table Package
Using Both Color and Size Attributes in Hexagon Binning (Ggplot2)
How to Use an R Script from Github
Let Each Plot in Facet_Grid Have Its Own Y-Axis Value
Calculating Standard Deviation Across Rows
How to Create a Pie Chart with Percentage Labels Using Ggplot2