daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest May 23rd, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sim = function(n1       = 10,
  2.                n2       = 10,
  3.                delta    = 0,
  4.                sd       = 1,
  5.                alpha    = .05,  
  6.                chk_norm = TRUE,
  7.                norm_a   = 0.05,
  8.                rem_out  = TRUE,
  9.                out_a    = 0.1){
  10.  
  11.   ad2 = alpha/2
  12.   # Gen prng data from normal dists
  13.   a = rnorm(n1, 0, sd)
  14.   b = rnorm(n2, delta, sd)
  15.  
  16.  
  17.   # If checkign for outliers filter any out if outside the specified percentile
  18.   if(rem_out){
  19.     a_out = abs(.5 - ecdf(a)(a)) < ad2
  20.     b_out = abs(.5 - ecdf(b)(b)) < ad2
  21.     if(mean(a_out) >= out_a | mean(b_out) >= out_a){
  22.       a = a[!a_out]
  23.       b = b[!b_out]
  24.     }
  25.   }
  26.  
  27.   # Do normality test for both groups
  28.   norm_pA = shapiro.test(a)$p.value
  29.   norm_pB = shapiro.test(b)$p.value
  30.  
  31.   # if checking for normality do a wilcox test instead of t.test
  32.   res = 'if'(chk_norm & (norm_pA < norm_a | norm_pB < norm_a),
  33.              wilcox.test(a, b)$p.value,
  34.              t.test(a, b, var.equal = T)$p.value)
  35.   return(res)
  36. }
  37.  
  38.  
  39. # Run sim for the 4 scenarios
  40. res = list()
  41. res[[1]] = replicate(1e4, sim(chk_norm = T, rem_out  = T))
  42. res[[2]] = replicate(1e4, sim(chk_norm = F, rem_out  = T))
  43. res[[3]] = replicate(1e4, sim(chk_norm = T, rem_out  = F))
  44. res[[4]] = replicate(1e4, sim(chk_norm = F, rem_out  = F))
  45.  
  46. # Inspect % p under 0.05 for the 4 scenarios
  47. sapply(res, function(x) mean(x < 0.05))
  48.  
  49. # Look at null distributions under the 4 scenarios
  50. par(mfrow = c(2, 2))
  51. for(i in 1:length(res)){
  52.   hist(res[[i]], col = "Light Blue",xlab = "p", main = paste("Scenario", i))
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top