# How to Run Regression with Replication in R

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.

• 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”)."
– Ben L.
Commented Apr 12 at 1:06
• 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?
– Ben L.
Commented Apr 12 at 1:16
• 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(). Commented Apr 12 at 1:54
• 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!
– Ben L.
Commented Apr 12 at 3:00

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