R:多个子样本回归上的约束系数和误差方差 [英] R : constraining coefficients and error variance over multiple subsample regressions
问题描述
我正在与R合作处理145个观测值的样本.我创建了五个子样本,每个子样本有29个观察值,而响应变量q
已排序.结果,subset1包含数据帧中输出最低的29行,subset2包含以下29行,依此类推.
I'm working with R on a sample of 145 observations. I have created five subsamples each with 29 observations, while the response variable q
has been sorted. As a result, subset1 contains the 29 lines of the data frame with the lowest output, subset2 contains the following 29 lines, etc.
我正在对预测变量x1
,x2
和x3
的变量q
进行回归.我现在需要执行两个实验:
I am regressing the variable q
on the predictors x1
, x2
ans x3
. I now need to perform two experiments :
- 将所有子样本的误差方差约束为相同;
- 在5个OLS回归中,将
x2
和x3
的系数以及误差方差限制为相同.
- Constraining the error variance to be the same over all subsamples;
- Constraining the coefficients on
x2
andx3
as well as the error variance to be the same over the 5 OLS regressions.
到目前为止,我的方法是使用软件包plm
,该软件包可以执行面板回归.但是,我不知道具体限制误差方差或特定系数.此外,我认为必须有一种方法可以使用R中包含的更基本的工具来做到这一点.
So far my approach has been to use the package plm
which allows to perform panel regressions. However, I don't know to specifically constrain the error variance, or specific coefficients. Besides, I think there must be a way to do this with the more basic tools incorporated in R.
请不要犹豫,提供其他方法.预先感谢您的帮助!
Please don't hesitate to provide alternative methods. Thanks in advance for your help !
推荐答案
看起来这就是您所需要的:
Looks like this is all you need:
set.seed(0)
dat <- data.frame(q = sort(rnorm(145)), x1 = rnorm(145), x2 = rnorm(145),
x3 = rnorm(145), group = gl(5, 29))
fit <- lm(q ~ x1 * group + x2 + x3, data = dat)
#Coefficients:
#(Intercept) x1 group2 group3 group4 group5
# -1.211435 0.049316 0.610405 1.128571 1.631891 2.502886
# x2 x3 x1:group2 x1:group3 x1:group4 x1:group5
# -0.027927 -0.015151 -0.004244 -0.074085 -0.044885 -0.074637
在这里,我介绍了分组因子变量group
.所有五组的模型估计均同时进行.使用公式:
Here, I have introduced a grouping factor variable group
. Model estimation for all five groups are done at the same time. With formula:
q ~ x1 * group + x2 + x3
对于所有组,我们的系数x2
和x3
是相同的.尽管交互作用x1*group
提示我们对于不同组的x1
具有不同的截距和斜率.
we have coefficients of x2
and x3
being the same for all groups. While the interaction x1*group
suggests that we have different intercept and slope for x1
for different groups.
如果您不想为每个组使用不同的截距,则可以使用公式:
If you don't want different intercept for each group, you can use formula:
q ~ x1 + x1 : group + x2 + x3
这篇关于R:多个子样本回归上的约束系数和误差方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!