# Roger Palay copyright 2016-02-23 # Saline, MI 48176 # hypoth_2test_unknown <- function( s_one, n_one, x_one, s_two, n_two, x_two, H1_type=0, sig_level=0.05) { # perform a hypothesis test for the difference of # two means when we do not 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. if( n_one < n_two) { df_simple <- n_one - 1} else { df_simple <- n_two - 1} use_sig_level = sig_level; if( H1_type == 0 ) { use_sig_level = sig_level/2} t_simple <- -qt(use_sig_level, df_simple) #compute the degrees of freedom d_one <- s_one^2/n_one d_two <- s_two^2/n_two std_err <- sqrt( d_one+d_two ) df <-(d_one+d_two)^2/ ( d_one^2/(n_one-1)+d_two^2/(n_two-1)) t_full <- -qt(use_sig_level,df) extreme_simple <- t_simple*std_err extreme_full <- t_full * std_err diff <- x_one - x_two diff_standard <- diff / std_err decision_simple <- "Reject" decision_full <- "Reject" if( H1_type < 0 ) { crit_low_simple <- - extreme_simple crit_high_simple <- "n.a." if( diff > crit_low_simple) { decision_simple <- "do not reject"} crit_low_full <- - extreme_full crit_high_full <- "n.a." if( diff > crit_low_full) { decision_full <- "do not reject"} attained_simple <- pt( diff_standard, df_simple) attained_full <- pt( diff_standard, df ) alt <- "mu_1 < mu_2" } else if ( H1_type == 0) { crit_low_simple <- - extreme_simple crit_high_simple <- extreme_simple if( (crit_low_simple < diff) & (diff < crit_high_simple) ) { decision_simple <- "do not reject"} crit_low_full <- - extreme_full crit_high_full <- extreme_full if( (crit_low_full < diff) & (diff < crit_high_full) ) { decision_full <- "do not reject"} if( diff < 0 ) { attained_simple <- 2*pt(diff_standard, df_simple) attained_full <- 2*pt(diff_standard, df) } else { attained_simple <- 2*pt(diff_standard, df_simple, lower.tail=FALSE) attained_full <- 2*pt(diff_standard, df, lower.tail=FALSE) } alt <- "mu_1 != mu_2" } else { crit_low_simple <- "n.a." crit_high_simple <- extreme_simple if( diff < crit_high_simple) { decision_simple <- "do not reject"} crit_low_full <- "n.a." crit_high_full <- extreme_full if( diff < crit_high_full) { decision_full <- "do not reject"} attained_simple <- pt(diff_standard, df_simple, lower.tail=FALSE) attained_full <- pt(diff_standard, df, lower.tail=FALSE) alt <- "mu_1 > mu_2" } result <- c( alt, s_one, n_one, x_one, s_two, n_two, x_two, std_err, diff, sig_level, diff_standard, df, crit_low_full, crit_high_full, attained_full, decision_full, df_simple, crit_low_simple, crit_high_simple, attained_simple, decision_simple ) names(result) <- c("H1:", "s_one", "n_one","mean_one", "s_two", "n_two","mean_two", "std. err.", "difference", "sig level", "t-value", "full df", "full low", "full high", "full Attnd", "full decision", "simple df", "simp low", "simp high", "simp Attnd", "simp decision" ) return( result) }