# Roger Palay copyright 2016-02-16 # Saline, MI 48176 # hypoth_2test_known <- function( sigma_one, n_one, mean_one, sigma_two, n_two, mean_two, H1_type=0, sig_level=0.05) { # perform a hypothesis test for the difference of # two means when we know the standard deviations of # the two populations and the alternative hypothesis is # != if H1_type==0 # < if H1_type < 0 # > if H1_type > 0 # Do the test at sig_level significance. diff_sd <- sqrt( sigma_one^2/n_one + sigma_two^2/n_two ) if( H1_type==0) { z <- abs( qnorm(sig_level/2))} else { z <- abs( qnorm(sig_level))} to_be_extreme <- z*diff_sd decision <- "Reject" diff <- mean_one - mean_two if( H1_type < 0 ) { crit_low <- - to_be_extreme crit_high = "n.a." if( diff > crit_low) { decision <- "do not reject"} attained <- pnorm( diff, mean=0, sd=diff_sd) alt <- "mu_1 < mu_2" } else if ( H1_type == 0) { crit_low <- - to_be_extreme crit_high <- to_be_extreme if( (crit_low < diff) & (diff < crit_high) ) { decision <- "do not reject"} if( diff < 0 ) { attained <- 2*pnorm(diff, mean=0, sd=diff_sd)} else { attained <- 2*pnorm(diff, mean=0, sd=diff_sd, lower.tail=FALSE) } alt <- "mu_1 != mu_2" } else { crit_low <- "n.a." crit_high <- to_be_extreme if( diff < crit_high) { decision <- "do not reject"} attained <- pnorm(diff, mean=0, sd=diff_sd, lower.tail=FALSE) alt <- "mu_1 > mu_2" } result <- c( alt, sigma_one, n_one, mean_one, sigma_two, n_two, mean_two, diff_sd, diff, sig_level, z, crit_low, crit_high, attained, decision) names(result) <- c("H1:", "sigma_one", "n_one","mean_one", "sigma_two", "n_two","mean_two", "std. err.", "difference", "sig level", "z", "critical low", "critical high", "attained", "decision") return( result) }