Reputation: 69
Trying to compare 3 independent populations across the years by the size of their individuals, I have this kind of data set:
year <- c(rep(2000,5),rep(2001,3),rep(2002,7))
region <- c(1,1,2,3,3,1,2,3,rep(1,3),rep(2,3),3)
size <- c(28,24,26,56,47,85,12,24,68,71,42,59,12,25,33)
count <- c(3,8,9,1,2,4,7,12,4,8,3,2,7,15,4)
df <- data.frame(year, region, size, count)
Which gives:
year region size count
2000 1 28 3
2000 1 24 8
2000 2 26 9
2000 3 56 1
2000 3 47 2
2001 1 85 4
2001 2 12 7
2001 3 24 12
2002 1 68 4
2002 1 71 8
2002 1 42 3
2002 2 59 2
2002 2 12 7
2002 2 25 15
2002 3 33 4
I want to make a 2-Way ANOVA:
model.2way <- lm(size ~ year * region, df) # example of code
anova(model.2way)
My issue is that the variable size
is weighted by count
: for each size
, I have count
number of individuals. I got millions of data and can't easily transform my data to have millions of size
values.
Do you know a way to make a 2-Way ANOVA with this kind of weighted data?
Thanks in advance!
Upvotes: 2
Views: 3152
Reputation: 73385
model.2way <- lm(size ~ year * region, df, weights = count)
From ?lm
:
... when the elements of ‘weights’ are positive integers w_i, that each response y_i is the mean of w_i unit-weight observations ...
In other words, a weight of 2 means that case appears twice.
Upvotes: 2