[R] help on regression by subsets in dataset

Gabor Grothendieck ggrothendieck at gmail.com
Mon Aug 8 14:05:08 CEST 2005


On 8/8/05, Krishna <snvk4u at gmail.com> wrote:
> Hi Everyone
> 
> May I request for a small help while performing the regression analysis.
> 
> I would like to know is there any possibility of conducting the
> regression for different data subsets (in the same data file),
> classified on the basis of "grouping variable". The alternative for
> this is running the regression for n number of times which you all
> know is quite cumbersome.


This defines a model which has a separate intercept and slope for
each value of the grouping variable g:

> # sample data
> x <- 1:12
> g <- gl(4,3)
> g
 [1] 1 1 1 2 2 2 3 3 3 4 4 4
Levels: 1 2 3 4
> set.seed(1)
> y <- rnorm(12)

> # now define the model and run the regression
> lm(y ~ g/x - 1)

Call:
lm(formula = y ~ g/x - 1)

Coefficients:
      g1        g2        g3        g4      g1:x      g2:x      g3:x      g4:x  
-0.21697   6.40748   0.24710  -3.29170  -0.10459  -1.20787   0.04418   0.34762




More information about the R-help mailing list