Hello, I have a large dataset of the form subj var1 var2 001 100 200 001 120 226 001 130 238 001 140 245 001 150 300 002 110 205 002 125 209 003 101 233 003 115 254 I would like to perform linear regression of var2 on var1 for each subject separately. It seems like I should be able to use the tapply function as you do for simple operations (like finding a mean of var1 for each subject), but I am not sure of the correct syntax for this. Is there a way to do this? Many thanks, Marcel -- View this message in context: http://r.789695.n4.nabble.com/linear-regression-in-a-ragged-array-tp3393033p3393033.html Sent from the R help mailing list archive at Nabble.com.
On Mar 21, 2011, at 5:25 AM, Marcel Curlin wrote:> Hello, > I have a large dataset of the form > > subj var1 var2 > 001 100 200 > 001 120 226 > 001 130 238 > 001 140 245 > 001 150 300 > 002 110 205 > 002 125 209 > 003 101 233 > 003 115 254 > > I would like to perform linear regression of var2 on var1 for each > subject > separately. It seems like I should be able to use the tapply > function as you > do for simple operations (like finding a mean of var1 for each > subject), but > I am not sure of the correct syntax for this. Is there a way to do > this? >tapply works on vectors, split works on data.frames. lapply(split(dat, dat$subj), function(x) lm(var2 ~ var1, data=x)) -- David Winsemius, MD Heritage Laboratories West Hartford, CT
In general when you want to split up your data and do the same thing on each piece then combine the results back together it is good to look at the plyr package. But for this specific case you should look at the lmList function in the nlme package which may do exactly what you want with the least effort. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Marcel Curlin > Sent: Monday, March 21, 2011 3:26 AM > To: r-help at r-project.org > Subject: [R] linear regression in a ragged array > > Hello, > I have a large dataset of the form > > subj var1 var2 > 001 100 200 > 001 120 226 > 001 130 238 > 001 140 245 > 001 150 300 > 002 110 205 > 002 125 209 > 003 101 233 > 003 115 254 > > I would like to perform linear regression of var2 on var1 for each > subject > separately. It seems like I should be able to use the tapply function > as you > do for simple operations (like finding a mean of var1 for each > subject), but > I am not sure of the correct syntax for this. Is there a way to do > this? > > Many thanks, Marcel > > -- > View this message in context: http://r.789695.n4.nabble.com/linear- > regression-in-a-ragged-array-tp3393033p3393033.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.