robert.ptacnik at niva.no
2007-Nov-28 09:57 UTC
[R] fit linear regression with multiple predictor and constrained intercept
Hi group, I have this type of data x(predictor), y(response), factor (grouping x into many groups, with 6-20 obs/group) I want to fit a linear regression with one common intercept. 'factor' should only modify the slopes, not the intercept. The intercept is expected to be >0. If I use y~ x + factor, I get a different intercept for each factor level, but one slope only if I use y~ x * factor, I get the interaction term I want, but the intercept is not kept constant. Also, if I constrain teh intercept in the regression model (y~a+x*factor), I get estimates both for slope and intercept of each factor level. Robert ---------------------------------------------------------------------------------------------------------------------- NIVAs hovedkontor har flyttet til nye lokaler i CIENS - Forskningssenter for milj? og samfunn; Gaustadall?en 21, 0349 Oslo. Meld deg p? v?rt nyhetsbrev p? www.niva.no
Peter Dalgaard
2007-Nov-28 10:45 UTC
[R] fit linear regression with multiple predictor and constrained intercept
robert.ptacnik at niva.no wrote:> Hi group, > > I have this type of data > x(predictor), y(response), factor (grouping x into many groups, with 6-20 > obs/group) > > I want to fit a linear regression with one common intercept. 'factor' > should only modify the slopes, not the intercept. The intercept is expected > to be >0. > > If I use > y~ x + factor, I get a different intercept for each factor level, but one > slope only > > if I use > y~ x * factor, I get the interaction term I want, but the intercept is not > kept constant. > Also, if I constrain teh intercept in the regression model (y~a+x*factor), > I get estimates both for slope and intercept of each factor level. > > Robert > > > >You seem to be looking for the colon operator. In R, unlike certain other statistical packages, the star implies inclusion of main effects: a*b is a + b + a:b. There is some trickery about when you get factors contrast coded in interaction terms (as far as I remember x:factor and x+x:factor are two different parametrizations of the same model), but you should be able to find that out by a little experimenting. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Prof Brian Ripley
2007-Nov-28 10:48 UTC
[R] fit linear regression with multiple predictor and constrained intercept
I think you are looking for y ~ x + x:factor E.g.> library(car) > lm(repwt ~ repht + repht:sex, data=Davis)Coefficients: (Intercept) repht repht:sexM -59.30865 0.71412 0.05694 where the third term is the difference in slope between males and females.> lm(repwt ~ repht:sex, data=Davis)Coefficients: (Intercept) repht:sexF repht:sexM -59.3086 0.7141 0.7711 for separately reported slopes. If you want to constrain the intercept, fit with and without and take the better fit (or look into package nnls, but that would be overkill here). On Wed, 28 Nov 2007, robert.ptacnik at niva.no wrote:> Hi group, > > I have this type of data > x(predictor), y(response), factor (grouping x into many groups, with 6-20 > obs/group) > > I want to fit a linear regression with one common intercept. 'factor' > should only modify the slopes, not the intercept. The intercept is expected > to be >0. > > If I use > y~ x + factor, I get a different intercept for each factor level, but one > slope only > > if I use > y~ x * factor, I get the interaction term I want, but the intercept is not > kept constant. > Also, if I constrain teh intercept in the regression model (y~a+x*factor), > I get estimates both for slope and intercept of each factor level. > > Robert > > > > > ---------------------------------------------------------------------------------------------------------------------- > NIVAs hovedkontor har flyttet til nye lokaler i CIENS - Forskningssenter > for milj? og samfunn; Gaustadall?en 21, 0349 Oslo. Meld deg p? v?rt > nyhetsbrev p? www.niva.no > ______________________________________________ > 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. >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595