R Force regression coefficients to add up to 1

0 votes

I'm trying to run a simple OLS regression with a restriction that the sum of the coefficients of two variables add up to 1.

I want:

Y = α + β1 * x1 + β2 * x2 + β3 * x3,
where β1 + β2 = 1

I have found how to make a relation between coefficients like:

β1 = 2* β2

But I haven't found how to make restrictions like:

β1 = 1 - β2

How would I do it in this simple example?

data <- data.frame(
  A = c(1,2,3,4),
  B = c(3,2,2,3),
  C = c(3,3,2,3),
  D = c(5,3,3,4)
)

lm(formula = 'D ~ A + B + C', data = data)
Mar 10, 2022 in Machine Learning by Dev
• 6,000 points
1,404 views

1 answer to this question.

0 votes

b1 + b2 = 1
Let us fit this to the model

result <- lm(Y ~  offset(x1) + I(x2 - x1) + x3, data = data_frame)

This means
Y = a +x1+b2*(x2-x1)+b3*x3

after substituting b1 = 1- b2
new_x = x2-x1 and the coefficient for x1 is 1
b1+b2+b3 = 1

result <-  lm(Y ~ offset(x1) + I(x2 - x1) + I(x3 - x1), data = data_frame)

Y = a +x1 + b2*(x2-x1)+b3*(x3-x1)

after substituting b1 =1-b2-b3

b1+b2+b3+..... = 1

I believe the pattern is obvious... all you have to do is subtract one variable, x1, from the other variables (x2, x3, etc.) and set the coefficient of that variable, x1, to 1.

Example:  b1 + b2 = 1

# Data
data_frame<- iris[, 1:4]
colnames(data_frame) <- c("Y", paste0("x", 1:3, collaapse=""))
# b1 + b2 = 1
result <- lm(Y ~  offset(x1) + I(x2 - x1) + x3, data = data_frame)
coeff_2 <- coef(result)
b_1 <- 1 - coeff_2[2]
b_2 <- coeff_2[2]
answered Mar 23, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer

How to add random and/or fixed effects into cloglog regression in R

The standard glm function can be used ...READ MORE

answered Apr 13, 2022 in Machine Learning by anonymous
446 views
0 votes
1 answer
0 votes
1 answer

How to get a regression summary in scikit-learn like R does?

In sklearn, there is no R type ...READ MORE

answered Mar 15, 2022 in Machine Learning by Dev
• 6,000 points
3,079 views
0 votes
1 answer

How to export regression equations for grouped data?

First, you'll need a linear model with ...READ MORE

answered Mar 14, 2022 in Machine Learning by Dev
• 6,000 points
310 views
0 votes
1 answer
0 votes
1 answer

How to add regression line equation and R2 on graph?

Below is one solution: # GET EQUATION AND ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,240 points
6,483 views
0 votes
1 answer

Steps to evaluate Linear Regression in R

 These are sequential steps which need to ...READ MORE

answered Jul 25, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
955 views
0 votes
1 answer

How to get early stopping for lasso regression

I believe you're referring to regularization. In ...READ MORE

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
533 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP