title: R : Calculating p-value using simulations
body: <p>I wrote this code to run a test statistic on two randomly distributed observations x and y </p>
  
  <pre><code>mean.test &lt;- function(x, y, B=10000,
  alternative=c("two.sided","less","greater"))
  {
  p.value &lt;- 0
  alternative &lt;- match.arg(alternative)
  s &lt;- replicate(B, (mean(sample(c(x,y), B, replace=TRUE))-mean(sample(c(x,y), B, replace=TRUE))))
  t &lt;- mean(x) - mean(y) 
  p.value &lt;- 2*(1- pnorm(abs(quantile(T,0.01)), mean = 0, sd = 1, lower.tail = 
  TRUE, log.p = FALSE))   #try to calculate p value 
  data.name &lt;- deparse(substitute(c(x,y)))
  names(t) &lt;- "difference in means"
  zero &lt;- 0
  names(zero) &lt;- "difference in means"
  return(structure(list(statistic = t, p.value = p.value,
  method = "mean test", data.name = data.name,
  observed = c(x,y), alternative = alternative,
  null.value = zero),
  class = "htest"))
  }
  </code></pre>
  
  <p>the code uses a Monte-Carlo simulations to generate the distribution function of the test statistic mean(x) - mean(y) and then calculates the p-value, but apparently i miss defined this p-value because for :</p>
  
  <pre><code>&gt; set.seed(0)
  &gt; mean.test(rnorm(1000,3,2),rnorm(2000,4,3)) 
  </code></pre>
  
  <p>the output should look like:</p>
  
  <pre><code>    mean test
  data: c(rnorm(1000, 3, 2), rnorm(2000, 4, 3))
  difference in means = -1.0967, p-value &lt; 2.2e-16
  alternative hypothesis: true difference in means is not equal to 0
  </code></pre>
  
  <p>but i got this instead:</p>
  
  <pre><code>      mean test
  data:  c(rnorm(1000, 3, 2), rnorm(2000, 4, 3))
  difference in means = -1.0967, p-value = 0.8087
  alternative hypothesis: true difference in means is not equal to 0
  </code></pre>
  
  <p>can someone explain the bug to me ?</p>