# [R] Weighted least squares

Wed May 9 08:22:26 CEST 2007

```On 5/9/07, Adaikalavan Ramasamy <ramasamy at cancer.org.uk> wrote:
> http://en.wikipedia.org/wiki/Weighted_least_squares gives a formulaic
> description of what you have said.

Except it doesn't describe what I think is important in my case - how
do you calculate the degrees of freedom/n for weighted linear
regression?

> I believe the original poster has converted something like this
>
>         y         x
>         0       1.1
>         0       2.2
>         0       2.2
>         0       2.2
>         1       3.3
>         1       3.3
>         2       4.4
>          ...
>
> into something like the following
>
>         y         x     freq
>         0       1.1        1
>         0       2.2        3
>         1       3.3        2
>         2       4.4        1
>          ...

Exactly!  Thanks for providing that example.

>
> Now, the variance of means of each row in table above is ZERO because
> the individual elements that comprise each row are identical. Therefore
> your method of using inverse-variance will not work here.
>
> Then is it valid then to use lm( y ~ x, weights=freq ) ?
>
>
>
>
> S Ellison wrote:
> >
> >> .. what is the usual way to do a linear
> >> regression when you have aggregated data?
> >
> > Least squares generally uses inverse variance weighting. For aggregated data fitted as mean values, you just need the variances for the _means_.
> >
> > So if you have individual means x_i and sd's s_i that arise from aggregated data with n_i observations in group i, the natural weighting is by inverse squared standard error of the mean. The appropriate weight for x_i would then be n_i/(s_i^2). In R, that's n/(s^2), as n and s would be vectors with the same length as x. If all the groups had the same variance, or nearly so, s is a scalar; if they have the same number of observations, n is a scalar.
> >
> > Of course, if they have the same variance and same number of observations, they all have the same weight and you needn't weight them at all: see previous posting!
> >
> > Steve E
> >
> >
> >
> > *******************************************************************
> > This email and any attachments are confidential. Any use, co...{{dropped}}
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help