PCA 分数对原始变量的最小二乘拟合
我有 100 个变量,我想使用变量 var15-v25 进行因子分析。为此,我首先将变量提取到另一个对象中(例如 f
),&然后进行主成分分析。
现在我想将 PCA 分数与原始数据集合并,以使用 PCA 分数作为预测变量来运行回归。
有人可以建议我合并这两个数据集的方法吗?我使用的代码如下:
spss_data_factor <- sqldf("SELECT Respondent_Serial,Q4_01_Q4,Q4_02_Q4,Q4_03_Q4,Q4_04_Q4,Q4_05_Q4,Q4_06_Q4,Q4_07_Q4,Q4_08_Q4,Q4_09_Q4,Q4_10_Q4 FROM spss_data_rel")
f <- princomp(spss_data_factor1, cor = TRUE)
summary(f, loadings=TRUE)
f$scores[, 1:5]
I have 100 vars, and I want to do factor analysis using variables var15-v25. To do that first I extracted the variables into another object (say f
), & then run the principal component analysis.
Now I want to merge PCA scores with the original dataset to run regression using PCA scores as predictors.
Can anybody please suggest me the method to merge these 2 datasets. The code I used are the following:
spss_data_factor <- sqldf("SELECT Respondent_Serial,Q4_01_Q4,Q4_02_Q4,Q4_03_Q4,Q4_04_Q4,Q4_05_Q4,Q4_06_Q4,Q4_07_Q4,Q4_08_Q4,Q4_09_Q4,Q4_10_Q4 FROM spss_data_rel")
f <- princomp(spss_data_factor1, cor = TRUE)
summary(f, loadings=TRUE)
f$scores[, 1:5]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请避免使用 R 基础包中的名称 -
factor
是一种保留。它会工作得很好,但它可能会在开发的某个时刻让您感到困惑...并且您的factor
不是一个文件,它是princomp
类的 R 对象。不管怎样,您想定义一个以因子得分作为预测变量的回归模型吗?小菜一碟...并且不需要合并:
您可能还希望将原始数据集转换为矩阵,以便在响应矩阵的每一列上执行
ncol(mtcars)
回归。lm
函数支持response ~ terms
公式,其中response
可以是一个矩阵。请参阅?lm
:所以,你可以做这样的事情:
我希望这对你有帮助......
无论如何,如果你想进行因子分析,请参阅此链接。您应该安装 William Revelle 的
psych
软件包。Please avoid using names from R base packages -
factor
is kind of reserved. It will work just fine, but it may confuse you at some point of development... And yourfactor
is not a file, it's a R object ofprincomp
class.Anyway, you want to define a regression model with factor scores as predictors? Piece of cake... and no merging is required:
You may also want to convert original dataset to matrix, in order to carry out
ncol(mtcars)
regressions, on each column of response matrix.lm
function supportsresponse ~ terms
formula, whereresponse
can be a matrix. See?lm
:So, you can do something like this:
I hope that this was helpful...
Anyway, if you want to perform a factor analysis, please see this link. You should install William Revelle's
psych
package.谢谢你aL3xa!我找到了解决方案的答案。我把它放在这里,因为有人可能会发现它有帮助。
问候,
一个
Thank you aL3xa! I found the answer of the solution. I'm putting it here as somebody might find it helpful.
Regards,
A