[R] Linear regression on several groups

Allan Engelhardt allane at cybaea.com
Fri Aug 13 12:35:11 CEST 2010


  Please read the posting guide and include a standalone example.

Maybe you want something like the results from

lm(weight ~ Time, data = ChickWeight, subset = Diet==1)
lm(weight ~ Time, data = ChickWeight, subset = Diet==2)
## ... etc ...

Then you could do

(m <- lm(weight ~ Time*Diet, data = ChickWeight))

To get the Diet==2 coefficients from above you could use something like

sum(coef(m)[c("(Intercept)", "Diet2")])  # Intercept
sum(coef(m)[c("Time", "Time:Diet2")])    # Slope

Hope this helps a little.

Allan

On 12/08/2010 17:11, JesperHybel wrote:
> I have a simple dataset of a numerical dependent Y, a numerical independent X
> and a categorial variable Z with three levels. I want to do linear
> regression Y~X for each level of Z. How can I do this in a single command
> that is without using lm() applied three isolated times?



More information about the R-help mailing list