Magean Magean - 3 months ago 10
R Question

R : constraining coefficients and error variance over multiple subsample regressions

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.

I am regressing the variable
q
on the predictors
x1
,
x2
ans
x3
. I now need to perform two experiments :


  1. Constraining the error variance to be the same over all subsamples;

  2. Constraining the coefficients on
    x2
    and
    x3
    as well as the error variance to be the same over the 5 OLS regressions.



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 !

Answer

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

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

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