在Stata中,”factor”是否与R中的”princomp”执行相同的功能?

huangapple go评论86阅读模式
英文:

Does factor in Stata do the same thing as princomp in R?

问题

我需要在R中重新创建一个使用Stata的factor命令创建的变量。

我正在寻找R中类似于Stata的factor命令的相应命令,例如:

factor v1 v2 v3 v4 v5 
score newvar1

根据这份Stata文档,这是一个主成分因子分析。

我尝试使用R中的因子分析 fa() 命令来执行相同的操作。然而,我在语法上遇到了困难。我认为相似的R代码应该如下所示:

library(psych)    
df$newvar1 <- fa(df, values = c(v1, v2, v3, v4, v5))

然而,当我使用mtcars虚拟数据进行测试时,我没有得到一个因子变量作为结果:

mtcars <- mtcars
library('psych')
mtcars$newvar1 <- fa(mtcars, values=c(mpg, cyl, hp))

错误信息:

Error in $<-.data.frame(*tmp*, fa, value = list(residual =
c(0.124759140589603, : replacement has 49 rows, data has 32 In
addition: Warning message: In fa.stats(r = r, f = f, phi = phi, n.obs
= n.obs, np.obs = np.obs, : The estimated weights for the factor scores are probably incorrect. Try a different factor score
estimation method.

我是不是漏掉了某些参数,还是我误解了语法?

英文:

Edit: for spelling and based on initial response, because I am still struggling with the syntax

I need to recreate a variable in R that was created in Stata using the factor command.

I am looking for the analogous command in R to Stata's factor command, written for example as:

factor v1 v2 v3 v4 v5 
score newvar1

which, according to this Stata documentation, is a principal-factor analysis.

I am trying to use the factor analysis fa() command in R do the same thing. However, I am struggling with the syntax. I think the analogous R code to the Stata code above should look something like this:

library(psych)    
df$newvar1 &lt;- fa(df, values = c(v1, v2, v3, v4, v5))

However, when I trial it out using the mtcars dummy data, I do not get a factor variable as a result:

mtcars &lt;- mtcars
library(&#39;psych&#39;)
mtcars$newvar1 &lt;- fa(mtcars, values=c(mpg, cyl, hp))

> Error in $&lt;-.data.frame(*tmp*, fa, value = list(residual =
> c(0.124759140589603, : replacement has 49 rows, data has 32 In
> addition: Warning message: In fa.stats(r = r, f = f, phi = phi, n.obs
> = n.obs, np.obs = np.obs, : The estimated weights for the factor scores are probably incorrect. Try a different factor score
> estimation method.

Am I missing some argument, or am I misinterpreting the syntax?

答案1

得分: 1

以下是R代码的翻译部分:

library(psych)
set.seed(123)
data(mtcars)
f <- fa(mtcars[,c("mpg", "disp", "hp", "qsec")], nfactors=1, SMC=TRUE, fm="pa", rotate="none", max.iter=1)
#> 最大迭代次数已超出
loadings(f)
#> 
#> 因子载荷:
#>      PA1   
#> mpg  -0.860
#> disp  0.872
#> hp    0.928
#> qsec -0.633
#> 
#>                 PA1
#> 因子SS载荷    2.76
#> 方差比例 0.69

以下是Stata代码的翻译部分:

. factor mpg disp hp qsec, pf factors(1)
(obs=32)

因子分析/相关性                      观测数 = 32
    方法: 主要因子                        保留的因子 = 1
    旋转: (未旋转)                        参数数 = 4

    --------------------------------------------------------------------------
         因子  |   特征值   差异        比例   累积
    -------------+------------------------------------------------------------
        因子1  |      2.75999      2.42226            0.9543       0.9543
        因子2  |      0.33773      0.42422            0.1168       1.0711
        因子3  |     -0.08649      0.03268           -0.0299       1.0412
        因子4  |     -0.11917            .           -0.0412       1.0000
    --------------------------------------------------------------------------
    LR检验: 独立 vs. 饱和:  chi2(6)  =   94.62 Prob>chi2 = 0.0000

因子载荷 (模式矩阵) 和唯一方差

    ---------------------------------------
        变量 |  因子1 |   唯一性 
    -------------+----------+--------------
             mpg |  -0.8595 |      0.2613  
            disp |   0.8719 |      0.2397  
              hp |   0.9277 |      0.1393  
            qsec |  -0.6327 |      0.5997  
    ---------------------------------------

请注意,fa()函数的fm="pa"选项默认执行迭代主轴因子分析(等同于Stata的ipf选项)。要使fa()函数执行一次性主轴因子分析解决方案,需要设置max.iter=1

英文:

Here is the equivalent in R:

library(psych)
set.seed(123)
data(mtcars)
f &lt;- fa(mtcars[,c(&quot;mpg&quot;, &quot;disp&quot;, &quot;hp&quot;, &quot;qsec&quot;)], nfactors=1, SMC=TRUE, fm=&quot;pa&quot;, rotate=&quot;none&quot;, max.iter=1)
#&gt; maximum iteration exceeded
loadings(f)
#&gt; 
#&gt; Loadings:
#&gt;      PA1   
#&gt; mpg  -0.860
#&gt; disp  0.872
#&gt; hp    0.928
#&gt; qsec -0.633
#&gt; 
#&gt;                 PA1
#&gt; SS loadings    2.76
#&gt; Proportion Var 0.69

<sup>Created on 2023-01-10 by the reprex package (v2.0.1)</sup>

Stata:

. factor mpg disp hp qsec, pf factors(1)
(obs=32)

Factor analysis/correlation                      Number of obs    =         32
    Method: principal factors                    Retained factors =          1
    Rotation: (unrotated)                        Number of params =          4

    --------------------------------------------------------------------------
         Factor  |   Eigenvalue   Difference        Proportion   Cumulative
    -------------+------------------------------------------------------------
        Factor1  |      2.75999      2.42226            0.9543       0.9543
        Factor2  |      0.33773      0.42422            0.1168       1.0711
        Factor3  |     -0.08649      0.03268           -0.0299       1.0412
        Factor4  |     -0.11917            .           -0.0412       1.0000
    --------------------------------------------------------------------------
    LR test: independent vs. saturated:  chi2(6)  =   94.62 Prob&gt;chi2 = 0.0000

Factor loadings (pattern matrix) and unique variances

    ---------------------------------------
        Variable |  Factor1 |   Uniqueness 
    -------------+----------+--------------
             mpg |  -0.8595 |      0.2613  
            disp |   0.8719 |      0.2397  
              hp |   0.9277 |      0.1393  
            qsec |  -0.6327 |      0.5997  
    ---------------------------------------

Note, the fa() function with fm=&quot;pa&quot; does iterated principal axis factoring by default (equivalent to Stata's, ipf option). To have fa() do the one-shot principal axis factoring solution, set max.iter=1.

huangapple
  • 本文由 发表于 2023年1月9日 03:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定