Z - Test
# Let's create a function that will calculate the p-value based on the Z distribution.
# Assumptions: Normally distributed population, known sigma OR unknown sigma but sufficeinetly large n
zTest <- function(xbar, pop.mean, s, n, two.sided = FALSE, lower.tail = TRUE ){
z <- (xbar-pop.mean)/(s/sqrt(n))
if(two.sided == FALSE){
if(lower.tail == TRUE){
p <- pnorm(z, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
return(p)
}
else{
p <- pnorm(z, mean = 0, sd = 1, lower.tail = FALSE, log.p = FALSE)
return(p)
}
}
else{
if(z > 0){
p <- (pnorm(z, mean = 0, sd = 1, lower.tail = FALSE, log.p = FALSE)) * 2
return(p)
}
else{
p <- (pnorm(z, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)) * 2
return(p)
}
}
}
t - Test
# Let's create a function that will calculate the p-value based on the t distribution.
tTest <- function(xbar, pop.mean, s, n, two.sided = FALSE, lower.tail = TRUE){
t <- (xbar-pop.mean)/(s/sqrt(n))
if(two.sided == FALSE){
if(lower.tail == TRUE){
pt(t, df=n-1, lower.tail = TRUE)
p <- pt(t, df=n-1, lower.tail = TRUE)
return(p)
}
else{
p <- pt(t, df=n-1, lower.tail = FALSE)
return(p)
}
}
else{
if(t > 0){
p <- (pt(t, df=n-1, lower.tail = FALSE)) * 2
return(p)
}
else{
p <- (pt(t, df=n-1, lower.tail = TRUE)) * 2
return(p)
}
}
}