# Roger Palay copyright 2016-01-31 # Saline, MI 48176 # hypoth_test_known <- function( H0_mu, sigma, H1_type=0, sig_level=0.05, samp_size, samp_mean) { # perform a hypothesis test for the mean=H0_mu # when we know sigma and the alternative hypothesis is # != if H1_type==0 # < if H1_type < 0 # > if H1_type > 0 # Do the test at sig_level significance, for a # sample of size samp_size that yields a sample # mean = samp_mean. samp_sd <- sigma/sqrt(samp_size) if( H1_type==0) { z <- abs( qnorm(sig_level/2))} else { z <- abs( qnorm(sig_level))} to_be_extreme <- z*samp_sd decision <- "Reject" if( H1_type < 0 ) { crit_low <- H0_mu - to_be_extreme crit_high = "n.a." if( samp_mean > crit_low) { decision <- "do not reject"} attained <- pnorm( samp_mean, H0_mu, samp_sd) alt <- paste("mu < ", H0_mu) } else if ( H1_type == 0) { crit_low <- H0_mu - to_be_extreme crit_high <- H0_mu + to_be_extreme if( (crit_low < samp_mean) & (samp_mean < crit_high) ) { decision <- "do not reject"} if( samp_mean < H0_mu ) { attained <- 2*pnorm(samp_mean, H0_mu, samp_sd)} else { attained <- 2*pnorm(samp_mean, H0_mu, samp_sd, lower.tail=FALSE) } alt <- paste( "mu != ", H0_mu) } else { crit_low <- "n.a." crit_high <- H0_mu + to_be_extreme if( samp_mean < crit_high) { decision <- "do not reject"} attained <- pnorm(samp_mean, H0_mu, samp_sd, lower.tail=FALSE) alt <- paste("mu > ",H0_mu) } z <- (samp_mean - H0_mu)/samp_sd result <- c(H0_mu, alt, sigma, samp_size, sig_level, samp_mean, samp_sd, to_be_extreme, z, crit_low, crit_high, attained, decision) names(result) <- c("H0_mu", "H1:", "sigma", "n", "sig level", "samp mean", "sd samp mean", "how far", "test stat", "critical low", "critical high", "attained", "decision") return( result) }