How to choose a linear regression model with two main components in R?

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 should this fit the linear regression model?

 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 think there are packages that do this automatically, but should this work too?

+4
source share
1 answer

Answer: you do not want pc $ rotation, it is a rotation matrix, not a matrix with rotating values ​​(points).

Make some data:

 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 

Unfortunately. The "x" component is what we want. From? Prcomp:

x: if "retx is true", the value of the rotated data is returned (centered (and scaled if necessary) data multiplied by the "rotation" matrix.

 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 
+13
source

All Articles