Consider the following: > tstdat <- data.frame(x=1:8, y1=rep(1:4, each=2), y2=0.01*rnorm(8)) > > ys <- names(tstdat)[2:3] > > slopes <- rep(NA,2) > names(slopes) <- ys > > for(i in 1:2){ + mdl <- formula(paste(ys[i], "~ x")) + slopes[i] <- coef(lm(mdl, tstdat))[2] + } > slopes y1 y2 0.4761904762 0.0006521472 Is this what you want? This could also be done with sapply. hth. spencer graves Martin Wegmann wrote:> hello, > > I only want to get the slope of a linear regression of ca. 100 variables > against time. > > I can do for each response (100 times) > var1.lm <- lm(response~predictor) > > but I thought that there might be an easier way of doing this. If I am > including more variables it is doing a multiple regression and the output > (slope) differs. > > any idea? > > thanks Martin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Martin - My recollection is that if the left hand side in a model formula is a matrix (in your case an [n x 100] matrix) then either lm() or glm() will return a matrix of coefficients. These are the point estimates for a multivariate regression (meaning, multivariate response). I hunted just a bit: help("lm"), help("glm"), help.search("multivariate"), but I have not found where this behavior is documented in R. I'm sure it is documented somewhere. - tom blackwell - u michigan medical school - ann arbor - On Fri, 27 Jun 2003, Martin Wegmann wrote:> hello, > > I only want to get the slope of a linear regression of ca. 100 variables > against time. > > I can do for each response (100 times) > var1.lm <- lm(response~predictor) > > but I thought that there might be an easier way of doing this. If I am > including more variables it is doing a multiple regression and the output > (slope) differs.
try the following: first cbind all the design vectors, data<-cbind(....) response<-x apply(data, 2, function(x) lm(response~x)$coef[2]) It should give you all the slope coef (not multivariate but with an intercept try lm(response~x-1)$coef if you do not want the intercept). ----- Original Message ----- From: "Martin Wegmann" <baliola at riseup.net> To: "R-list" <r-help at stat.math.ethz.ch> Sent: Friday, June 27, 2003 4:21 PM Subject: [R] regression for several responses> hello, > > I only want to get the slope of a linear regression of ca. 100 variables > against time. > > I can do for each response (100 times) > var1.lm <- lm(response~predictor) > > but I thought that there might be an easier way of doing this. If I am > including more variables it is doing a multiple regression and the output > (slope) differs. > > any idea? > > thanks Martin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
hello, I only want to get the slope of a linear regression of ca. 100 variables against time. I can do for each response (100 times) var1.lm <- lm(response~predictor) but I thought that there might be an easier way of doing this. If I am including more variables it is doing a multiple regression and the output (slope) differs. any idea? thanks Martin