I have not really used lm before and I was hoping for some help on a simple problem. Here is a toy version of the problem I want to solve. y x grp -.9 1 a -.8 2 a -.7 3 a -.7 1.5 b -.5 2.5 b -.3 3.5 b -.19 2.7 c -.11 3.7 c -.41 4.7 c I want to fit a model that has one y-intercept and three slopes, one for each group. Could someone please show me how to do this? Thanks very much for any help. Bill Simpson -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Bill Simpson <wsi at gcal.ac.uk> writes:> I have not really used lm before and I was hoping for some help on a > simple problem.> I want to fit a model that has one y-intercept and three slopes, one for > each group.Actually, this is not so simple because it goes somewhat against the grain of the way the S/R model formulas are designed (they generally assume that identical intercepts only make sense with identical slopes). However,> lm(y~x:(grp-1)+1)Call: lm(formula = y ~ x:(grp - 1) + 1) Coefficients: (Intercept) x.grpa x.grpb x.grpc -0.80977 0.01847 0.13124 0.14259 The form grp-1 gives you the "no-intercept" coding of the grp factor and its interaction (":") with x specifies separate slopes for each group. Once you've used "-1" the intercept disappears from the model, so you need to add it back in with "+1". -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Date: Mon, 09 Oct 2000 12:43:20 +0200 (CEST) > From: Martyn Plummer <plummer at iarc.fr> > > On 09-Oct-00 Peter Dalgaard BSA wrote: > > Bill Simpson <wsi at gcal.ac.uk> writes: > > > >> I have not really used lm before and I was hoping for some help on a > >> simple problem. > > > >> I want to fit a model that has one y-intercept and three slopes, one for > >> each group. > > > > Actually, this is not so simple because it goes somewhat against the > > grain of the way the S/R model formulas are designed (they generally > > assume that identical intercepts only make sense with identical > > slopes). > > > > However, > > > >> lm(y~x:(grp-1)+1) > > > > Call: > > lm(formula = y ~ x:(grp - 1) + 1) > > > > Coefficients: > > (Intercept) x.grpa x.grpb x.grpc > > -0.80977 0.01847 0.13124 0.14259 > > > > The form grp-1 gives you the "no-intercept" coding of the grp factor > > and its interaction (":") with x specifies separate slopes for each > > group. Once you've used "-1" the intercept disappears from the model, > > so you need to add it back in with "+1". > > Is that really necessary?It shouldn't be.> R> lm(y ~ grp:x, data=foo) > > Call: > lm(formula = y ~ grp:x, data = foo) > > Coefficients: > (Intercept) grpa.x grpb.x grpc.x > -0.80977 0.01847 0.13124 0.14259In 1.1.1 each of these will only work in some codings, in particular not for ordered factors. But it should work for all (does in S) and is fixed in the R-devel snapshot. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks very much Peter, Martyn, and Brian for the help.> > lm(y~x:(grp-1)+1) > > Call: > lm(formula = y ~ x:(grp - 1) + 1) > > Coefficients: > (Intercept) x.grpa x.grpb x.grpc > -0.80977 0.01847 0.13124 0.14259It seems to me that there is something seriously wrong here since for the data I posted (I just re-checked it), intercept=-1, slope a=.1, slope b=.2, and slope c = .3 (These fit the data exactly) This leaves aside the comments of Martyn and Brian. Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Thanks very much Peter, Martyn, and Brian for the help. > > > > lm(y~x:(grp-1)+1) > > > > Call: > > lm(formula = y ~ x:(grp - 1) + 1) > > > > Coefficients: > > (Intercept) x.grpa x.grpb x.grpc > > -0.80977 0.01847 0.13124 0.14259 > > It seems to me that there is something seriously wrong here > since for the data I posted (I just re-checked it), > intercept=-1, slope a=.1, slope b=.2, and slope c = .3 (These > fit the data exactly)It was a typo in your data. Well, two actually. You posted y x grp -.9 1 a -.8 2 a -.7 3 a -.7 1.5 b -.5 2.5 b -.3 3.5 b -.19 2.7 c -.11 3.7 c -.41 4.7 c What you meant was: y x grp -.9 1 a -.8 2 a -.7 3 a -.7 1.5 b -.5 2.5 b -.3 3.5 b -.19 2.7 c .11 3.7 c .41 4.7 c Bill Venables. -- Bill Venables, Statistician, CMIS Environmetrics Project CSIRO Marine Labs, PO Box 120, Cleveland, Qld, AUSTRALIA. 4163 Tel: +61 7 3826 7251 Email: Bill.Venables at cmis.csiro.au Fax: +61 7 3826 7304 http://www.cmis.csiro.au/bill.venables/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._