[R] Applying 'lm' in each group
David Winsemius
dwinsemius at comcast.net
Sat Jan 10 20:32:19 CET 2009
Did you read the error message? Generally saying "doesn't work" will
elicit undesirable responses.
Now would be a good time to read the Posting Guide:
http://www.R-project.org/posting-guide.html
Including:
"Say exactly what happened, including any error messages you received. "
> lm(yg~xg)
Error in model.frame.default(formula = yg ~ xg, drop.unused.levels =
TRUE) :
invalid type (list) for variable 'yg'
You need to offer lm an object that it can use. You actually shot
yourself in the foot by turning your data into a list. It's not clear
what you actually want, but perhaps this will move you a bit further
along:
dfg <- data.frame(y=unlist(yg), x=unlist(xg), g=g)
lm(y ~ x | g, data= dfg)
Which would also be the same result as the following (without the
detour to the yg and xg lists.)
lm(y ~ x + g)
It's also possible that what you really wanted was:
lm(y ~ x + as.factor(g)) # which estimates mean differences of grp=2
and grp=3 from grp=1 with a common estimated slope of y on x.
Or even lm(y ~ x * as.factor(g)) which gives you separate slopes and
mean differences from those estimated for group 1 for groups 2 and 3
and is most consistent with the phrase "separate regressions in each
of three groups".
--
David Winsemius
Heritage Laboratories
On Jan 10, 2009, at 12:55 PM, Bhargab Chattopadhyay wrote:
> Hi,
>
>
> I want to do regression in each group. I made the group the
> following way. Now I want to run regression in each of the three
> groups..
> Suppose ,
> x<-
> c
> (0.5578196,6.5411662,13.2728619,2.0217271,6.7216176,3.37220617,2.5773252,7.2600583,15.3731026,3.4140288,8.1335874
> ,
>
> 15
> ..
> 6476637,4.3014084,9.1224379,18.5605355,4.7448394,11.9296663,18.5866354,12.3797674,18.7572812,2.70433816,2.88924220
> ,
> 2.94688208,3.37154364,2.26311786,3.31002593)
>
> y<-
> c
> (18.63654,233.55387,152.61107,103.49646,234.55054,14.2767453,160.78742,150.35391,223.89225,168.55567,190.51031
> ,
> 227.68339,152.42658,208.70115, 223.91982, 221.70702,
> 213.71135,168.0199, 222.69040,228.49353, 164.95750,243.18828,
> 229.94688,313.37154364,202.263786,139.31002593)
>
> n<-length(x)
> m<-3;
> r<-8;
> c<-r*(m-1);
> g1<-rep(1:(m-1),each=r);
> g<-c(g1,rep(m,n-c))
> xg<-split(x,g);
> yg<-split(y,g);
>
> Now if I write lm(yg~xg) then it won't work. Please advice how to
> proceed.
>
> Thanks in advance.
>
> Bhargab
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list