Hi everyone! I have an array containing the following fields for over hundred compounds: cpd, activity, fixterm, energy1, energy2, energy3, ... I want to run a multiple linear regression on all entries of an array. Therefore I tried to do this with a for loop. (Maybe there is a direct way of calculating it using apply, but I don't know that either.) Actually i tried the following code: ... > attach(data) > for(i in 1:length(cpd)) { > fitted.model <- lm(activity ~ fixterm + i) > coef(fitted.model) > } ... Unfortunatly this loop doesn't give the intended correlation coefficients of each regression. If I insert a line "print(i)" into the loop the desired values for i are printed correctly. Only the coefficient outputs are missing. Probably the solution is very near, but I just can't see it. Many thanks in advance, Markus --------------------------------- [[alternative HTML version deleted]]
Markus M?hlbacher wrote:> Hi everyone! > > I have an array containing the following fields for over hundred compounds: > cpd, activity, fixterm, energy1, energy2, energy3, ... > > I want to run a multiple linear regression on all entries of an array. Therefore I tried to do this with a for loop. (Maybe there is a direct way of calculating it using apply, but I don't know that either.) > > Actually i tried the following code: > > ... > > attach(data)Now, I guess "data" is a data.frame, not an array as mentioned above. I'd suggest to supply it to the data argument of lm(), don't attach.> > for(i in 1:length(cpd)) { > > fitted.model <- lm(activity ~ fixterm + i)"+ i" does not make any sense here.> > coef(fitted.model)If you want to coefficients to be printed, you have to print() them. Probably you want to store the results from coef() in some object in order to use the results further on... Uwe Ligges> > } > ... > > Unfortunatly this loop doesn't give the intended correlation coefficients of each regression. If I insert a line "print(i)" into the loop the desired values for i are printed correctly. Only the coefficient outputs are missing. > Probably the solution is very near, but I just can't see it. >> Many thanks in advance, > Markus > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
I'm not sure if this is what you want but if you have a matrix as response, you can use the matrix ~ term: example: x <- 1:10 y <- rep(rnorm(10,x,0.5),10) dim(y) <- c(10,10) y <- as.matrix(y) coef(lm(y~x)) Bart Markus "M?hlbacher" wrote:> > Hi everyone! > > I have an array containing the following fields for over hundred > compounds: > cpd, activity, fixterm, energy1, energy2, energy3, ... > > I want to run a multiple linear regression on all entries of an array. > Therefore I tried to do this with a for loop. (Maybe there is a direct way > of calculating it using apply, but I don't know that either.) > > Actually i tried the following code: > > ... > > attach(data) > > for(i in 1:length(cpd)) { > > fitted.model <- lm(activity ~ fixterm + i) > > coef(fitted.model) > > } > ... > > Unfortunatly this loop doesn't give the intended correlation coefficients > of each regression. If I insert a line "print(i)" into the loop the > desired values for i are printed correctly. Only the coefficient outputs > are missing. > Probably the solution is very near, but I just can't see it. > > Many thanks in advance, > Markus > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/Multiple-linear-regression-with-for-loop-tp15703017p15711401.html Sent from the R help mailing list archive at Nabble.com.