I have a grouped data structure of different households answering a weekly poll and I observe them over 52 weeks (in the example below four weeks). Now I want to use the Gini coefficient to quantify the degree of (in-)equality of poll answers across all households at a given week (where 0 = all households have answered the same number of polls; 1 = one household answered all polls).
Example data:
da_poll <- data.frame(household = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4), week = c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4), participation = c(1,1,1,1,0,0,0,1,0,1,0,1,1,1,1,0))
da_poll
household week participation
1 1 1 1
2 1 2 1
3 1 3 1
4 1 4 1
5 2 1 0
6 2 2 0
7 2 3 0
8 2 4 1
9 3 1 0
10 3 2 1
11 3 3 0
12 3 4 1
13 4 1 1
14 4 2 1
15 4 3 1
16 4 4 0
I now started computing the Gini coefficient for every week:
library(DescTools)
da_poll = group_by(da_poll, household) %>%
mutate(n_polls = cumsum(participation == 1)) %>%
group_by(week) %>%
mutate(gini_polls = Gini(n_polls))
da_poll
# A tibble: 16 x 5
# Groups: week [4]
household week participation n_polls gini_polls
<dbl> <dbl> <dbl> <int> <dbl>
1 1 1 1 1 0
2 1 2 1 2 0.143
3 1 3 1 3 0.259
4 1 4 1 4 0.167
5 2 1 1 1 0
6 2 2 0 1 0.143
7 2 3 0 1 0.259
8 2 4 1 2 0.167
9 3 1 1 1 0
10 3 2 1 2 0.143
11 3 3 0 2 0.259
12 3 4 1 3 0.167
13 4 1 1 1 0
14 4 2 1 2 0.143
15 4 3 1 3 0.259
16 4 4 0 3 0.167
Now I want to add a second variable indicating the change in the Gini coefficient (Gini after household h fills out poll at week w – Gini before household w fills out poll at w) through a household participating in the poll in a week. How can I solve this issue?