[英]How to fit a multitple linear regression model on 1664 explantory variables in R
[英]How to fit a linear regression model with two principal components in 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')
我想那里有一個自動執行此操作的軟件包,但這也應該有用嗎?
答:你不想要pc $旋轉,它是旋轉矩陣,而不是旋轉值矩陣(分數)。
補充一些數據:
x1 = runif(100)
x2 = runif(100)
y = rnorm(2+3*x1+4*x2)
d = cbind(x1,x2)
pc = prcomp(d)
dim(pc$rotation)
## [1] 2 2
哎呀。 “x”組件就是我們想要的。 來自?prcomp:
x:如果'retx'為真,則返回旋轉數據的值(居中(和縮放,如果請求)數據乘以'旋轉'矩陣)。
dim(pc$x)
## [1] 100 2
lm(y~pc$x[,1]+pc$x[,2])
##
## Call:
## lm(formula = y ~ pc$x[, 1] + pc$x[, 2])
## Coefficients:
## (Intercept) pc$x[, 1] pc$x[, 2]
## 0.04942 0.14272 -0.13557
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.