R中的约束线性回归系数 [英] Constrained linear regression coefficients in R
问题描述
我正在估计R中的几个普通的最小二乘线性回归.我想在回归中约束估计的系数,以使它们相同.例如,我有以下内容:
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
我希望在第一次回归中对y的估计系数等于在第二次回归中对x的估计系数.
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.
更详细的修改
我正在尝试估计一个线性需求函数系统,其中相应的福利函数是二次函数.福利函数的形式为:
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
因此,需求函数为:
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
和
dW/dQy = Py = ay*Qy + 0.5*(byx + bxy)*Qx + cy
我想约束系统,使得byx = bxy(福利函数中的乘积系数).如果此条件成立,则两个需求函数将变为:
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
我有价格(Px
和Py
)和数量(Qx
和Qy
)数据,但是我真正感兴趣的是福利(W
),我没有数据.
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.
我知道如何计算和编码受约束的最小二乘法的所有矩阵公式(要花费很少的几行代码来获得系数,标准误差,拟合度等),这些系数是lm()
的标准配置.但是我希望可能存在一个现有的R函数(即可以对lm()
函数进行的操作),这样我就不必编写所有这些代码了.
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.
推荐答案
对于您指定的回归:
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"))
我们还需要合并数据:
z <- c(Px, Py) ## response
x <- c(Qx, Qy) ## covariate 1
y <- c(Qy, Qy) ## covariate 2
然后我们可以使用带有公式的lm
拟合线性模型:
Then we can fit a linear model using lm
with a formula:
z ~ x + y + x:id
这篇关于R中的约束线性回归系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!