Magean - 1 year ago 73

R Question

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`

I am regressing the variable

`q`

`x1`

`x2`

`x3`

- Constraining the error variance to be the same over all subsamples;
- Constraining the coefficients on and
`x2`

as well as the error variance to be the same over the 5 OLS regressions.`x3`

So far my approach has been to use the package

`plm`

Please don't hesitate to provide alternative methods. Thanks in advance for your help !

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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
```