如何在R中拟合具有两个主成分的线性回归模型?
假设我有一个数据矩阵 d
pc = prcomp(d)
# pc1 and pc2 are the principal components
pc1 = pc$rotation[,1]
pc2 = pc$rotation[,2]
那么这应该适合线性回归模型,对吗?
r = lm(y ~ pc1+pc2)
但后来我得到了这个错误:
Errormodel.frame.default(formula = y ~ pc1+pc2, drop.unused.levels = TRUE) :
unequal dimensions('pc1')
我猜那里有一个包可以自动执行此操作,但这也应该起作用吗?
Let's say I have a data matrix d
pc = prcomp(d)
# pc1 and pc2 are the principal components
pc1 = pc$rotation[,1]
pc2 = pc$rotation[,2]
Then this should fit the linear regression model right?
r = lm(y ~ pc1+pc2)
But then I get this error :
Errormodel.frame.default(formula = y ~ pc1+pc2, drop.unused.levels = TRUE) :
unequal dimensions('pc1')
I guess there a packages out there who do this automatically, but this should work too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案:你不需要 pc$rotation,它是旋转矩阵,而不是旋转值(分数)的矩阵。
补一些数据:
哎呀。 “x”分量就是我们想要的。来自 ?prcomp:
Answer: you don't want pc$rotation, it's the rotation matrix and not the matrix of rotated values (scores).
Make up some data:
Oops. The "x" component is what we want. From ?prcomp: