Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.A41.4.44.0208071607330.169686-100000@homer07.u.washington.edu>
Date: 2002-08-07T23:10:51Z
From: Thomas Lumley
Subject: your mail
In-Reply-To: <200208072115.g77LFJW18507@aaalab.Stanford.EDU>

On Wed, 7 Aug 2002, Jay Pfaffman wrote:

> I've got a table of 1 row per observation.
>
> 16 questions * n subjects * 4 classrooms (2 teachers * 2 conditions)
>
> I'd like to run some simple regressions that look something like this:
>
>   lm(length ~ elapsed)
>
> I'd like to do the regressions several times, split out by
> questionkey, student, class.  What I've been doing is using a series
> of which's to create new data sets (e.g. class1, class2, class3,
> class4) and running the regressions (or whatever) against each of
> those new data sets.  It seems that there should be better a way to do
> this.
>

Use by()

models<-by(data, list(student), function(thesedata)
	lm(length~elapsed,subset=thesedata))

will return a list of lm() objects.  You can use lapply() to get
information out of these, for example

lapply(models,coef)

returns a list of coefficient vectors or

sapply(models,coef)

binds the vectors into a matrix.  There's an example in help(by)

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._