Reputation: 105
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.
Upvotes: 4
Views: 2874
Reputation: 21532
One way to look at it is that you're solving a system of equations.
y = a*c + b*x , and
a=1-b
(where c is known). So feed those to nleqslv
or BBsolve
or ktsolve
(all packages available at CRAN
Upvotes: 3
Reputation: 4233
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.
Upvotes: 3