[英]psych - Getting factor loadings as data.frame for LaTeX export
I am using the psych
package's fa
command for factor analysis, and so have an object of class fa
.我正在使用psych
包的fa
命令进行因子分析,因此有一个 object 的 class fa
。 I can query the loadings with fac$loadings
, but I want to only extract the table containing the loadings, so I can use xtable
(or similar) to convert it into LaTeX format.我可以使用fac$loadings
查询负载,但我只想提取包含负载的表,因此我可以使用xtable
(或类似工具)将其转换为 LaTeX 格式。
Example code:示例代码:
library(psych)
library(xtable)
data(bfi)
fac <- fa(r=cor(bfi, use="complete.obs"), nfactors=5, fm="ml", rotate="none")
fac$loadings
ld <- someMagicalFunction(fac$loadings)
xtable(ld)
Can anyone tell me what I can use for someMagicalFunction
?谁能告诉我我可以为someMagicalFunction
使用什么?
When you look at fac$loading
, you see that it is a S3 object. 当您查看fac$loading
,您会看到它是一个S3对象。 Removing the class attribute gives you a matrix
which can then be passed to xtable
: 删除class属性会为您提供一个matrix
,然后可以将其传递给xtable
:
str(fac$loadings)
class(fac$loadings)
xtable(unclass(fac$loadings))
That works fine. 这很好。
An alternative is to use the fa2latex function in psych: 另一种方法是在心理中使用fa2latex函数:
Using your example: 使用你的例子:
library(psych)
fac <- fa(bfi,5)
fa2latex(fac)
will give you an APA ready LaTeX table. 将为您提供一个APA准备好的LaTeX表。
Bill 法案
The result of xtable is in HTML language. xtable 的结果是 HTML 语言。 If you want to save it as a file, you can use:如果要将其保存为文件,可以使用:
print.xtable(x, type="HTML", file="table.html")
Another alternative is to call fac$Vaccounted
that will pull the proportional variance, cumulative variance, etc that can then be put into a df or kable object:另一种方法是调用fac$Vaccounted
来提取比例方差、累积方差等,然后将其放入 df 或 kable object 中:
fac$Vaccounted %>% kable()
Do the same with fac$weights
to access the loadings:对fac$weights
执行相同的操作以访问负载:
fac$weights
That should cover all the output you needed.这应该涵盖您需要的所有 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.