Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Example dataset
- Data <- data.frame(
- ID = sample(c("A", "B", "C", "D"), 50, replace = TRUE),
- Act = sample(c("eat", "sleep", "play"), 50, replace = TRUE),
- Period = sample(c("pre", "post"), 50, replace = TRUE)
- )
- # Separate my data by period
- DataPre <- as.data.frame(Data[ which(Data $Period == "pre"), ])
- DataPost <- as.data.frame(Data[ which(Data $Period == "post"), ])
- # Get the minimum # observations for each ID across both periods
- Num <- Data %>%
- group_by(ID, Period) %>%
- summarise(number=n()) %>%
- group_by(ID) %>%
- summarise(min=min(number))
- # Function to get the mean proportion per ID
- meanAct <- function(x){
- x %>%
- group_by(ID, Act) %>%
- summarise (n = n()) %>%
- mutate(freq = n / sum(n))
- }
- # See "8888" Here I want to subsample the Num$Min for each ID
- DataResults <- function(x, rep){
- reps <- replicate(rep, meanAct(x[sample(1:nrow(x), 8888, replace=FALSE),]))
- meanfreq <- apply(simplify2array(reps[3, 1:2]), 1, mean)
- sd <- apply(simplify2array(reps[3, 1:2]), 1, sd)
- lower <- meanfreq - 1.96*(sd/sqrt(8888))
- upper <- meanfreq + 1.96*(sd/sqrt(8888))
- meanAct <- as.vector(reps[[1]])
- output <- data.frame(meanAct, meanfreq, sd, lower, upper)
- print(output)
- }
- # Print results
- DataResults(DataPre, 1000)
- DataResults(DataPost, 1000)
- # Somehow I get the mean for the population by averaging across all IDs
- DataMeanGroup <- DataMean %>%
- group_by(Period) %>%
- summarise (mean = mean(prop))
Add Comment
Please, Sign In to add comment