[R] Constraint Linear regression

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 20 16:04:46 CET 2012


On Tue, Mar 20, 2012 at 12:54 AM, priya fernandes
<priyyafernandes at gmail.com> wrote:
> Hi there,
>
> I am trying to use linear regression to solve the following equation -
>
> y <- c(0.2525, 0.3448, 0.2358, 0.3696, 0.2708, 0.1667, 0.2941, 0.2333,
> 0.1500, 0.3077, 0.3462, 0.1667, 0.2500, 0.3214, 0.1364)
> x2 <- c(0.368, 0.537, 0.379, 0.472, 0.401, 0.361, 0.644, 0.444, 0.440,
> 0.676, 0.679, 0.622, 0.450, 0.379, 0.620)
> x1 <- 1-x2
>
> # equation
> lmFit <- lm(y ~ x1 + x2)
>
> lmFit
> Call:
> lm(formula = y ~ x1 + x2)
>
> Coefficients:
> (Intercept)           x1           x2
>    0.30521     -0.09726           NA
>
> I would like to *constraint the coefficients of x1 and x2 to be between 0,1*.
> Is there a way of adding constraints to lm?
>

Assuming we set the intercept to zero the unconstrained solution does
satisfy those constraints:

lm(y ~ x1 + x2 + 0)

An approach which explicitly set the constraints (also removing the
intercept) would be nls:

nls(y ~ a * x1 + b * x2,
    lower = c(a = 0, b = 0), upper = c(a = 1, b = 1),
    start = c(a = 0.5, b = 0.5),
    alg = "port")

-- 
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