I am flipping each coin 100 times in a bag of 50 coins and then I want to use the Method of Maximum statistics in order to determine the Family Wise Error Rate. However, I keep getting an FWER of 1 which feels wrong.
coins <- rbinom(50, 100, 0.5)
So I start by defining a new function where we input how many times we do randomizations, the coins themselves, and how many times we flip them.
simulate_max <- function(n_number_of_randomizations, input_coins, N_number_of_tosses, alpha = 0.05) {
maxList <- NULL
Then we do a for loop for every time we have specified.
for (iteration in 1:n_number_of_randomizations){
Now we shuffle the list of coins
CoinIteration <- sample(input_coins)
Now we apply the binary test to every coin in the bag
testresults <- map_df(CoinIteration, function(x) tidy(binom.test(x,N_number_of_tosses,p=alpha)) )
Now we want to add the maximum result from every test to the max list.
thisRandMax <- max(testresults$statistic)
maxList <- c(maxList, thisRandMax)
}
Finally, we iterate through every member of the maximum list to subtract the expected value of heads (ie 50 for 50% chance * 100 tosses.
for (iterator2 in 1:length(maxList)){
maxList[iterator2]<-maxList[iterator2]-(0.5*N_number_of_tosses)
}
Return the output from the function
return(data.frame(maxList))
}
Now we apply this simulation for each of the requested iterations.
repsmax = map_df(1:Nreps, ~simulate_max(Nrandomizations,coins,Ntosses))
Now we calculate the fwer by dividing the increased amount by the total number of cells.
fwer = sum(repsmax>0) / (Nreps*Nrandomizations)