Hi everyone. I have a DF with the first column being my independant variable and all other columns the dependent variables. Something like: x y1 y2 y3 ... ... ... ... ... ... ... ... What I'm trying to do is to perform a linear model for each of my "y". It is pretty simple with loops, but I'm trying to vectorize it using *apply*. For instance, I tried something like: apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])}) But apparently it does not work. Any help would be greatly appreciated. Phil -- View this message in context: http://r.789695.n4.nabble.com/linear-regression-by-column-tp4432564p4432564.html Sent from the R help mailing list archive at Nabble.com.
On Feb 29, 2012, at 1:53 PM, Filoche wrote:> Hi everyone. > > I have a DF with the first column being my independant variable and > all > other columns the dependent variables. > > Something like: > > x y1 y2 y3 > ... ... ... ... > ... ... ... ... > > What I'm trying to do is to perform a linear model for each of my > "y". It is > pretty simple with loops, but I'm trying to vectorize it using > *apply*. > > For instance, I tried something like: > > apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])})apply( DF[2:5], 2, function(x){lm(DF[,1] ~ x)}) You need to use the variable name that you created in the function call and loop over columns, not rows.> > But apparently it does not work.For about four or five reasons.> .David Winsemius, MD West Hartford, CT
Seemingly Similar Threads
- Merge or combine data frames with missing columns
- corrupt data frame: columns will be truncated or padded with NAs in: format.data.frame(x, digits = digits)
- corrupt data frame: columns will be truncated or padded with NAs in: format.data.frame(x, digits = digits)
- Arranging column data to create plots
- Selecting a subplot of pairs