Magean Magean - 8 months ago 36
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

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
on the predictors
. I now need to perform two experiments :

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

  2. Constraining the coefficients on
    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
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:

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)

#(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