On Jun 7, 2008, at 8:13 AM, jonboym wrote:
I'm trying to do a linear regression between the columns of
matrices. In
example below I want to regress column 1 of matrix xdat with
column1 of ydat
and do a separate regression between the column 2s of each matrix.
But the
output I get seems to give correct slopes but incorrect intercepts and
another set of slopes with value NA. How do I do this correctly?
I'm after
the slope and intercept of each columns regression
xdat <- matrix(1:6,3,2)
xdat
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
ydat <- xdat
ydat[,1] <- xdat[,1]*3 +2
ydat
[,1] [,2]
[1,] 5 4
[2,] 8 5
[3,] 11 6
ydat[,2] <- xdat[,2]*4 - 3
yadt
Error: object "yadt" not found
ydat
[,1] [,2]
[1,] 5 13
[2,] 8 17
[3,] 11 21
lrg <- lm(y~x)
Error in eval(expr, envir, enclos) : object "y" not found
lrg <- lm(ydat~xdat)
lrg
Call:
lm(formula = ydat ~ xdat)
Coefficients:
[,1] [,2]
(Intercept) 2 9
xdat1 3 4
xdat2 NA NA
Try this:
lapply( 1:2, function(i) lm( y~x, data=list(x=xdat[,i], y=ydat[,i]) ) )
Haris Skiadas
Department of Mathematics and Computer Science
Hanover College
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.