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

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

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

问题

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

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

  1. factor v1 v2 v3 v4 v5
  2. score newvar1

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

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

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

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

  1. mtcars <- mtcars
  2. library('psych')
  3. 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:

  1. factor v1 v2 v3 v4 v5
  2. 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:

  1. library(psych)
  2. 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:

  1. mtcars &lt;- mtcars
  2. library(&#39;psych&#39;)
  3. 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代码的翻译部分:

  1. library(psych)
  2. set.seed(123)
  3. data(mtcars)
  4. f <- fa(mtcars[,c("mpg", "disp", "hp", "qsec")], nfactors=1, SMC=TRUE, fm="pa", rotate="none", max.iter=1)
  5. #> 最大迭代次数已超出
  6. loadings(f)
  7. #>
  8. #> 因子载荷:
  9. #> PA1
  10. #> mpg -0.860
  11. #> disp 0.872
  12. #> hp 0.928
  13. #> qsec -0.633
  14. #>
  15. #> PA1
  16. #> 因子SS载荷 2.76
  17. #> 方差比例 0.69

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

  1. . factor mpg disp hp qsec, pf factors(1)
  2. (obs=32)
  3. 因子分析/相关性 观测数 = 32
  4. 方法: 主要因子 保留的因子 = 1
  5. 旋转: (未旋转) 参数数 = 4
  6. --------------------------------------------------------------------------
  7. 因子 | 特征值 差异 比例 累积
  8. -------------+------------------------------------------------------------
  9. 因子1 | 2.75999 2.42226 0.9543 0.9543
  10. 因子2 | 0.33773 0.42422 0.1168 1.0711
  11. 因子3 | -0.08649 0.03268 -0.0299 1.0412
  12. 因子4 | -0.11917 . -0.0412 1.0000
  13. --------------------------------------------------------------------------
  14. LR检验: 独立 vs. 饱和: chi2(6) = 94.62 Prob>chi2 = 0.0000
  15. 因子载荷 (模式矩阵) 和唯一方差
  16. ---------------------------------------
  17. 变量 | 因子1 | 唯一性
  18. -------------+----------+--------------
  19. mpg | -0.8595 | 0.2613
  20. disp | 0.8719 | 0.2397
  21. hp | 0.9277 | 0.1393
  22. qsec | -0.6327 | 0.5997
  23. ---------------------------------------

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

英文:

Here is the equivalent in R:

  1. library(psych)
  2. set.seed(123)
  3. data(mtcars)
  4. 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)
  5. #&gt; maximum iteration exceeded
  6. loadings(f)
  7. #&gt;
  8. #&gt; Loadings:
  9. #&gt; PA1
  10. #&gt; mpg -0.860
  11. #&gt; disp 0.872
  12. #&gt; hp 0.928
  13. #&gt; qsec -0.633
  14. #&gt;
  15. #&gt; PA1
  16. #&gt; SS loadings 2.76
  17. #&gt; Proportion Var 0.69

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

Stata:

  1. . factor mpg disp hp qsec, pf factors(1)
  2. (obs=32)
  3. Factor analysis/correlation Number of obs = 32
  4. Method: principal factors Retained factors = 1
  5. Rotation: (unrotated) Number of params = 4
  6. --------------------------------------------------------------------------
  7. Factor | Eigenvalue Difference Proportion Cumulative
  8. -------------+------------------------------------------------------------
  9. Factor1 | 2.75999 2.42226 0.9543 0.9543
  10. Factor2 | 0.33773 0.42422 0.1168 1.0711
  11. Factor3 | -0.08649 0.03268 -0.0299 1.0412
  12. Factor4 | -0.11917 . -0.0412 1.0000
  13. --------------------------------------------------------------------------
  14. LR test: independent vs. saturated: chi2(6) = 94.62 Prob&gt;chi2 = 0.0000
  15. Factor loadings (pattern matrix) and unique variances
  16. ---------------------------------------
  17. Variable | Factor1 | Uniqueness
  18. -------------+----------+--------------
  19. mpg | -0.8595 | 0.2613
  20. disp | 0.8719 | 0.2397
  21. hp | 0.9277 | 0.1393
  22. qsec | -0.6327 | 0.5997
  23. ---------------------------------------

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:

确定