Constrained linear regression coefficients in R [d

2019-02-24 11:14发布

问题:

This question already has an answer here:

  • R : constraining coefficients and error variance over multiple subsample regressions [closed] 1 answer

I'm estimating several ordinary least squares linear regressions in R. I want to constrain the estimated coefficients across the regressions such that they're the same. For example, I have the following:

z1 ~ x + y
z2 ~ x + y

And I would like the estimated coefficient on y in the first regression to be equal to the estimated coefficient on x in the second.

Is there a straight-forward way to do this? Thanks in advance.

More detailed edit

I'm trying to estimate a system of linear demand functions, where the corresponding welfare function is quadratic. The welfare function has the form:

W = 0.5*ax*(Qx^2) + 0.5*ay*(Qy^2) + 0.5*bxy*Qx*Qy + 0.5*byx*Qy*Qx + cx*Qx + cy*Qy

Therefore, it follows that the demand functions are:

dW/dQx = Px = 2*0.5*ax*Qx + 0 + 0.5*bxy*Qy + 0.5*byx*Qy + 0 + cx
dW/dQx = Px = ax*Qx + 0.5*(bxy + byx)*Qy + cx

and

dW/dQy = Py = ay*Qy + 0.5*(byx + bxy)*Qx + cy

I would like to constrain the system so that byx = bxy (the cross-product coefficients in the welfare function). If this condition holds, the two demand functions become:

Px = ax*Qx + bxy*Qy + cy
Py = ay*Qy + bxy*Qy + cy

I have price (Px and Py) and quantity (Qx and Qy) data, but what I'm really interested in is the welfare (W) which I have no data for.

I know how to calculate and code all the matrix formulae for constrained least squares (which would take a fair few lines of code to get the coefficients, standard errors, measures of fit etc that come standard with lm()). But I was hoping there might be an existing R function (i.e. something that can be done to the lm() function) so that I wouldn't have to code all of this.

回答1:

For your specified regression:

Px = ax*Qx + bxy*Qy + cy
Py = ay*Qy + bxy*Qy + cy

We can introduce a grouping factor:

id <- factor(rep.int(c("Px", "Py"), c(length(Px), length(Py))),
             levels = c("Px", "Py"))

We also need to combine data:

z <- c(Px, Py)    ## response
x <- c(Qx, Qy)    ## covariate 1
y <- c(Qy, Qy)    ## covariate 2    

Then we can fit a linear model using lm with a formula:

z ~ x + y + x:id


回答2:

If the x and y values are the same, then you could use this model:

lm( I(z1+z2)~ x +y )  # Need to divide coefficients by 2

If they are separate data then you could rbind the two datasets after renaming z2 to z1.