p-values

Author

Prof. Eric A. Suess

p-values

n1 <- 30; n2 <- 30
mu1 <- 20; mu2 <- 20
sigma1 <- 5; sigma2 <- 5

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2), type = "l")
lines(dnorm(x_range, mu2, sigma2), col = "red")

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2/sqrt(n1)), type = "l")
lines(dnorm(x_range, mu2, sigma2/sqrt(n2)), col = "red")

B <- 1

x <- rnorm(n1, mean = mu1, sd = sigma1)
y <- rnorm(n2, mean = mu2, sd = sigma2)

hist(x)

hist(y)

t.test(x, y, var.equal = FALSE)

    Welch Two Sample t-test

data:  x and y
t = 2.0308, df = 57.751, p-value = 0.04688
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.04186283 5.83338647
sample estimates:
mean of x mean of y 
 21.88547  18.94785 
mu1 <- 20; mu2 <- 20

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2), type = "l")
lines(dnorm(x_range, mu2, sigma2), col = "red")

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2/sqrt(n1)), type = "l")
lines(dnorm(x_range, mu2, sigma2/sqrt(n2)), col = "red")

B <- 1000

p_values <- replicate(B, t.test( rnorm(n1, mean = mu1, sd = sigma1), rnorm(n2, mean = mu2, sd = sigma2), var.equal = FALSE)$p.value)  

hist(p_values)

plot(p_values, type = "l")
abline(a = 0.05, b = 0, col = "purple")

mu1 <- 20; mu2 <- 22

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2), type = "l")
lines(dnorm(x_range, mu2, sigma2), col = "red")

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2/sqrt(n1)), type = "l")
lines(dnorm(x_range, mu2, sigma2/sqrt(n2)), col = "red")

B <- 1000

p_values <- replicate(B, t.test( rnorm(n1, mean = mu1, sd = sigma1), rnorm(n2, mean = mu2, sd = sigma2), var.equal = FALSE)$p.value)  

hist(p_values)

plot(p_values, type = "l")
abline(a = 0.05, b = 0, col = "purple")

mu1 <- 20; mu2 <- 24

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2), type = "l")
lines(dnorm(x_range, mu2, sigma2), col = "red")

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2/sqrt(n1)), type = "l")
lines(dnorm(x_range, mu2, sigma2/sqrt(n2)), col = "red")

B <- 1000

p_values <- replicate(B, t.test( rnorm(n1, mean = mu1, sd = sigma1), rnorm(n2, mean = mu2, sd = sigma2), var.equal = FALSE)$p.value)  

hist(p_values)

plot(p_values, type = "l")
abline(a = 0.05, b = 0, col = "purple")

mu1 <- 20; mu2 <- 26

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2), type = "l")
lines(dnorm(x_range, mu2, sigma2), col = "red")

x_range <- seq(mu1 - 3*sigma1, mu1 + 3*sigma1, length.out = 100)
plot(dnorm(x_range, mu1, sigma2/sqrt(n1)), type = "l")
lines(dnorm(x_range, mu2, sigma2/sqrt(n2)), col = "red")

B <- 1000

p_values <- replicate(B, t.test( rnorm(n1, mean = mu1, sd = sigma1), rnorm(n2, mean = mu2, sd = sigma2), var.equal = FALSE)$p.value)  

hist(p_values)

plot(p_values, type = "l")
abline(a = 0.05, b = 0, col = "purple")