0

I am trying to create a function in r that performs hypothesis testing based on the bootstrapping method for a population mean that can return the p value for either one or two sided tests. Below is the code that I wrote. When I call the function ht(Delta$dep_delay, 322, 1000, s_size = length(Delta$dep_delay), "greater", "mean") 0 is always the output no matter what value I put in. I am trying to test if Delta Airline's flights that are outbounding from JFK have an average departure delay greater than 5 minutes.

sample_3054 <- function(data, reps=1000, s_size = length(data), 
stats =  c("mean", "median")){
  data <- na.omit(data)
  data <- as.data.frame(data)
  do(reps) * summarise(sample_n(data,size = s_size, replace = TRUE), 
  sampled = case_when(stats == "mean" ~ mean(data), 
  stats == "median" ~ median(data)))
}

ht <- function(data, specifiedMean, reps = 1000, p = c("equals", "less", "greater"), 
s_size = length(data), stats = c("mean", "median")){
  data <- na.omit(data)
  OBSMean <- mean(data)
  newValue <- data - OBSMean + specifiedMean
  c <- abs(specifiedMean - OBSMean)
  RSample <- sample_30543210(newValue, reps = 1000, s_size = length(newValue), "mean")
  ll <- specifiedMean - c
  ul <- specifiedMean + c
   if(p == "equals"){
    (sum(RSample$sampled <= ll) + sum(RSample$sampled >= ul)) / reps
   }else if(p == "less"){
(sum(RSample$sampled <= ll)) / reps
   }else if(p == "greater"){
(sum(RSample$sampled >= ul)) / reps
   }else{
print("Please choose "equals", "greater", or "less".")
   }
 }
  • How exactly are you calling this function? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 09 '18 at 17:17
  • Have a look at package `infer` and `rsample`. – Jiaxiang Nov 09 '18 at 17:41

0 Answers0