Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How can I fit a regression with constraints on the coefficients?

I want to fit a regression model where the sum of b0 and b1 equal 1.

y = b0C + b1x , where C is a constant.

Is there an easy way to do this? I read about the glmc package, but didn't understand how to specify the constraint.

like image 984
laser.p Avatar asked Nov 03 '25 06:11

laser.p


1 Answers

You can use any R function for optimization. Suppose you want to fit your model to the mtcars (mpg ~ hp) dataset. First, we code the equation for the model:

# par1 is c
# par2 is b
lin_reg <- function(par,x){
  par[1]*par[2]+(1-par[2])*x
}

b0 and b1 add up to 1. Hence, you only need to find one of them. Next step is to provide an objective function. We will use the OLS objective:

objective <- function(par, y, x){
  sum((y-lin_reg(par,x))^2)
}

After that, we minimize the objective w.r.t to par:

nlm(f=objective,
    p=c(1,0.5),
    y=mtcars$mpg,
    x=mtcars$hp)

Output

$minimum
[1] 447.6743

# c and b
$estimate
[1] 28.176132  1.068226

$gradient
[1]  8.473213e-08 -1.314359e-05

$code
[1] 1

$iterations
[1] 10

Your model is equivalent to a linear regression with the OLS objective (two parameters with unconstrained values):

 > lm(mpg ~ hp, mtcars)

Call:
lm(formula = mpg ~ hp, data = mtcars)

Coefficients:
(Intercept)           hp  
   30.09886     -0.06823 

Observe that b1 = 1 - b0 = -0.06823, the same result as in lm. You will get a different result if you make b0 and b1 strictly positive.

like image 188
slava-kohut Avatar answered Nov 04 '25 18:11

slava-kohut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!