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

Hello,

I want run linear regression by adding constraint in it. I am sharing the code, please correct the codes. I am sharing the sample data too.

Thank you in advance

code

# Column (1): Run regression directly
m1 <- lm(int_inc ~ repo_rate + yield_10y, data = df)
summary(m1)

# Column (2): Omit constant term
m2 <- lm(int_inc ~ repo_rate + yield_10y - 1, data = df)
summary(m2)

# Column (3): Impose the restriction that beta(repo_rate) + beta(yield_10y) = 1
df20$constraint <- 1 - df$repo_rate - df$yield_10y
m3 <- lm(int_inc ~ repo_rate + yield_10y + I(1 - repo_rate - yield_10y)-1, data = df)
summary(m3)

# Column (4): Omit constant term and impose the restriction that beta(repo_rate) + beta(yield_10y) = 1
m4 <- lm(int_inc ~ repo_rate + yield_10y + I(1-repo_rate - yield_10y), data = df)
summary(m4)

sample data

         date repo_rate yield_10y    int_inc
1  30-06-2009      4.75     5.748 0.05761865
2  30-06-2010      5.25     7.567 0.05752961
3  30-06-2011      6.75     8.171 0.06657415
4  30-06-2012      6.75     8.170 0.07259009
5  30-06-2013      7.25     7.864 0.06837341
6  30-06-2014      8.00     8.665 0.06866595
7  30-06-2015      7.25     7.704 0.06656640
8  30-06-2016      6.50     7.775 0.05967732
9  30-06-2017      6.25     6.451 0.05678696
10 30-06-2018      6.25     7.413 0.05780110
11 30-06-2019      5.75     7.577 0.06309163
12 30-06-2020      4.00     6.596 0.06627005
13 30-06-2021      4.00     5.939 0.06128204
14 30-06-2022      4.90     6.620 0.06209456
15 30-09-2009      4.75     6.207 0.06075164
16 30-09-2010      6.00     8.058 0.05911744
17 30-09-2011      6.75     8.110 0.06923054
18 30-09-2012      6.75     8.568 0.07197139
19 30-09-2013      7.50     7.734 0.06900444
20 30-09-2014      8.00     8.851 0.06916249
21 30-09-2015      6.75     7.757 0.06595805
22 30-09-2016      6.50     7.456 0.06229348
23 30-09-2017      6.00     6.918 0.05714001
24 30-09-2018      6.50     7.740 0.05750529
25 30-09-2019      5.40     7.473 0.06298246
26 30-09-2020      4.00     6.060 0.06347615
27 30-09-2021      4.00     6.035 0.06260779
28 30-09-2022      5.90     7.167 0.06447264
29 31-03-2009      5.00     7.462 0.06180726
30 31-03-2010      5.00     8.147 0.05627560
31 31-03-2011      6.75     8.820 0.06215618
32 31-03-2012      6.75     8.135 0.07051454
33 31-03-2013      6.75     8.631 0.06740869
34 31-03-2014      8.00     8.356 0.06714129
35 31-03-2015      7.50     7.585 0.06523115
36 31-03-2016      6.75     6.848 0.06055056
37 31-03-2017      6.25     6.799 0.05833759
38 31-03-2018      6.00     7.894 0.05349361
39 31-03-2019      6.25     6.678 0.06029703
40 31-03-2020      4.40     5.844 0.06573758
41 31-03-2021      4.00     6.364 0.05801631
42 31-03-2022      4.00     7.511 0.06203087
43 31-03-2023      6.50     7.382 0.06891122
44 31-12-2008      6.50     9.038 0.07508244
45 31-12-2009      4.75     7.004 0.05879457
46 31-12-2010      6.25     7.681 0.06046377
47 31-12-2011      6.75     8.314 0.07005760
48 31-12-2012      6.75     8.066 0.06989172
49 31-12-2013      7.75     8.175 0.06861628
50 31-12-2014      8.00     8.660 0.06747680
51 31-12-2015      6.75     7.803 0.06397770
52 31-12-2016      6.25     7.249 0.05888112
53 31-12-2017      6.00     6.436 0.05593594
54 31-12-2018      6.50     7.810 0.06171040
55 31-12-2019      5.15     6.464 0.06223399
56 31-12-2020      4.00     5.805 0.06122402
57 31-12-2021      4.00     6.165 0.06285493
58 31-12-2022      6.25     7.414 0.06596470

Try

m3 <- lm(I(int_inc-yield_10y) ~ I( repo_rate - yield_10y)-1, data = df)

More generally, you need to use some form of constrained optimization to perform a regression with constraints on the coefficients. For linear constraints, this would be a linear program if performing least absolute value regression, quadratic programming if performing least squares regression. For quadratic, more general nonlinear, or discrete constraints (e.g., beta_1 = 0 if beta_2 >= 5) you need something fancier than linear/quadratic programming.

If you are doing least squares with linear constraints, you can always impose the constraints by rewriting the equation. Alternatively, there is a closed form formula (that I can't remember off the top of my head.)

Including linear inequality constraints?

No, equality constraints only. That's the example @zeeshan0112 was asking about.

1 Like