[R] Simultaneous OLS fitting with constraints on coeffcients

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 17 00:54:45 CEST 2012


On Mon, Apr 16, 2012 at 6:02 PM, Allan Sikk <a.sikk at ucl.ac.uk> wrote:
> Hi,
>
> Is it possible in R to fit two equations simultaneously:
>
> y1 = a1 - b1*t   and
> y2 = a2 - b2*t
>
> so that the following constraint applies: a1/b1 = a2/b2 ?
>

Try this:

set.seed(123)

# simulated data
a2 <- 2; b1 <- 3; b2 <- 4; a1 <- b1 * a2/b2
y <- rnorm( 100, c(a1 - b1 * seq(50), a1 - b2 * seq(50)) )

# fit it
g <- gl(2, 50)
sq <- c(seq(50), seq(50))
lm(y ~ g / sq + 0)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list