# Roger Palay copyright 2016-01-31 # Saline, MI 48176 # hypoth_test_unknown <- function( H0_mu, H1_type=0, sig_level=0.05, samp_size, samp_mean, samp_sd) { # perform a hypothesis test for the mean=H0_mu # when we do not 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 and a sample standard deviation = samp_sd s_x <- samp_sd/sqrt(samp_size) if( H1_type == 0) { t <- abs( qt(sig_level/2,samp_size-1))} else { t <- abs( qt(sig_level,samp_size-1))} to_be_extreme <- t*s_x 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 <- pt( (samp_mean-H0_mu)/s_x, samp_size-1) 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*pt((samp_mean-H0_mu)/s_x,samp_size-1)} else { attained <- 2*pt((samp_mean-H0_mu)/s_x, samp_size-1, 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 <- pt((samp_mean-H0_mu)/s_x, samp_size-1, lower.tail=FALSE) alt <- paste("mu > ",H0_mu) } ts <- (samp_mean - H0_mu)/(samp_sd/sqrt(samp_size)) result <- c(H0_mu, alt, s_x, samp_size, sig_level, t, samp_mean, samp_sd, ts, to_be_extreme, crit_low, crit_high, attained, decision) names(result) <- c("H0_mu", "H1:", "std. error", "n", "sig level", "t" ,"samp mean", "samp stdev", "test stat", "how far", "critical low", "critical high", "attained", "decision") return( result) }