vendredi 10 avril 2020

Randomly draw 2 separate 100 simulations by group from a dataframe

This question is not about sampling data, I know about sample_n but this question is about simulating data from a dataframe to compare their mean from simulation Vs actual (using group_by summarise).

I calculated the actual difference in mean between using below

df %>% 
  group_by(allfour) %>% 
  summarise(hs_completion=mean(hsgrad),
            count=n())

However, I am struggling to draw 100 simulations from each group & then divide each vector by the respective group size to turn these into simulated graduation rates & calculate difference in these rates between two groups. Post this, I need to draw a histogram of these simulated differences & add a red vertical line to this histogram at the value of difference-in-means calculated in the observed data.

I know tidyverse & ggplot, so plotting is not an issue just how do I 100 simulations when the records are limited.

Sample of Dataframe df as below:

    structure(list(hsgrad = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 
1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 
1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L), allfour = structure(c(1L, 
2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L), .Label = c("0", "1"), class = "factor")), row.names = c(NA, 
100L), class = "data.frame")



Aucun commentaire:

Enregistrer un commentaire