Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. # Collect data for two groups of sample size nDataPerStep.
  2. # Continue adding to the data for up to maxSteps, but stop if a
  3. # t-test returns a p-value less than sig_lvl.
  4. seq_sampling_model = function(maxSteps = 100,
  5. nDataPerStep = 10,
  6. sig_lvl = 0.05){
  7.  
  8. a = b = NULL
  9. p = matrix(nrow = maxSteps)
  10. for(i in 1:maxSteps){
  11. a = c(a, rnorm(nDataPerStep))
  12. b = c(b, rnorm(nDataPerStep))
  13. p[i] = t.test(a, b)$p.value
  14.  
  15. if(p[i] < sig_lvl){ break }
  16. }
  17. p = p[1:i, ]
  18.  
  19. return(list(a = a, b = b, p = p))
  20. }
  21.  
  22.  
  23. # Run simulation
  24. set.seed(0)
  25. nSim = 1e2
  26. maxSteps = 100
  27. nDataPerStep = 10
  28. sig_lvl = 0.05
  29.  
  30. # Save the *last* p-value
  31. res1 = replicate(nSim, tail(seq_sampling_model(maxSteps, nDataPerStep, sig_lvl)$p, 1))
  32.  
  33. # Save the *lowest* p-value
  34. res2 = replicate(nSim, min(seq_sampling_model(maxSteps, nDataPerStep, sig_lvl)$p))
  35.  
  36. # Plot the null (no difference between a and b) distribution of p-values
  37. par(mfrow = c(2, 1))
  38. hist(res1, main = paste0("Last: ", round(mean(res1 < sig_lvl), 3), "% significant"))
  39. hist(res2, main = paste0("Min: ", round(mean(res2 < sig_lvl), 3), "% significant"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement