Manuel
Manuel

Reputation: 45

R : Calculating p-value using simulations

I wrote this code to run a test statistic on two randomly distributed observations x and y

mean.test <- function(x, y, B=10000,
alternative=c("two.sided","less","greater"))
{
p.value <- 0
alternative <- match.arg(alternative)
s <- replicate(B, (mean(sample(c(x,y), B, replace=TRUE))-mean(sample(c(x,y), B, replace=TRUE))))
t <- mean(x) - mean(y) 
p.value <- 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 <- deparse(substitute(c(x,y)))
names(t) <- "difference in means"
zero <- 0
names(zero) <- "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"))
}

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 :

> set.seed(0)
> mean.test(rnorm(1000,3,2),rnorm(2000,4,3)) 

the output should look like:

    mean test
data: c(rnorm(1000, 3, 2), rnorm(2000, 4, 3))
difference in means = -1.0967, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0

but i got this instead:

      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

can someone explain the bug to me ?

Upvotes: 0

Views: 1687

Answers (1)

csgillespie
csgillespie

Reputation: 60452

As far as I can tell, your code has numerous mistakes and errors in it:

  • quantile(T, 0.01) - here T == TRUE, so you're calculating the quantile of 1.
  • The object s is never used.
  • mean(sample(c(x,y), B, replace=TRUE)) What are you trying to do here? The c() function combines x and y. Sampling makes no sense since you don't know what population they come from
  • When you calculate the test statistic t, it should depend on the variance (and sample size).

Upvotes: 2

Related Questions