0
$\begingroup$

Looking to see if anyone knows how to run a regression analysis with replication in R. I have a data set with multiple values of Y for each value of X. I could take the mean of the Y values, but that would discard information and the statistical analysis would lose power. I have a section of a reference text that describes the math, sure I could try to do it by hand, but where is the fun in that?

I've tried finding examples of code that would take this into account, but have yet to come up with anything. I imagine it is out there, but for whatever reason I cannot come across it. I do know that I am not looking for multivariate regression or regression with multiple regressors.

Example data:

x y
15 5, 2, 5, 6, 1, 3
30 2, 4, 7, 1, 4
40 1, 6, 8
45 5, 4, 9, 10, 2, 9

For my application I have 45 different x variables with 3 associated y data points. I believe that regression with replication should be possible, just a heavy lift if it needs to be calculated by hand instead of in R.

Thank you for your time.

$\endgroup$
4
  • $\begingroup$ Thanks, I had come across that earlier. I think that comment and the textbook I am referencing suggest the same thing, essentially that the size of the groups have to be taken into account when calculating the least-squares regression line. Do you happen to know if that is achievable in R? I do see an argument 'weights' in the help panel. Maybe there would be some promise there? "If response is a matrix a linear model is fitted separately by least-squares to each column of the matrix and the result inherits from "mlm" (“multivariate linear model”)." $\endgroup$
    – Ben L.
    Commented Apr 12 at 1:06
  • $\begingroup$ Also, if all of the group sizes are equal (each x variable has 3 corresponding y variables) would it be necessary to use WLS with weights proportional to the inverse group sizes, since they would all be weighted equally? $\endgroup$
    – Ben L.
    Commented Apr 12 at 1:16
  • 1
    $\begingroup$ Yeah, you probably just need to transform your data so you have 1 y value on each row (repeating the x values appropriately) and then run a regular lm(). $\endgroup$ Commented Apr 12 at 1:54
  • 1
    $\begingroup$ Thanks all. I ended up calculating the weights as a proportion of the group size as discussed, assigned them to the values in the same table in a new column, and ran the lm() with the 'weight' argument using those weights. I believe the problem is resolved. Hopefully this helps the next person looking into a similar question! $\endgroup$
    – Ben L.
    Commented Apr 12 at 3:00

1 Answer 1

1
$\begingroup$

Here is a way to create long format and add a column with weights w proportional to the inverse group sizes.

> dfl <- with(df, Map(data.frame, x=x, y=y)) |> 
+   do.call(what='rbind') |>
+   transform(w=unlist(as.vector(mapply(rep, 1/lengths(df$y), each=lengths(df$y)))))
> dfl
    x  y         w
1  15  5 0.1666667
2  15  2 0.1666667
3  15  5 0.1666667
4  ...

Run WLS

> lm(y ~ x, data=dfl, weights=w)

Call:
lm(formula = y ~ x, data = dfl, weights = w)

Coefficients:
(Intercept)            x  
    1.86984      0.08683  

Data:

> dput(df)
structure(list(x = c(15, 30, 40, 45), y = list(c(5, 2, 5, 6, 
1, 3), c(2, 4, 7, 1, 4), c(1, 6, 8), c(5, 4, 9, 10, 2, 9))), class = "data.frame", row.names = c(NA, 
-4L))
> df
   x                 y
1 15  5, 2, 5, 6, 1, 3
2 30     2, 4, 7, 1, 4
3 40           1, 6, 8
4 45 5, 4, 9, 10, 2, 9
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.