# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # # Computations used for the problems in this section of the text. # #----- source('utils.R') print( 'Ex 10.35:') Js = c( 5, 6, 7, 6 ) I = length(Js) n = sum( Js ) f = 3.68 p_value = 1 - pf( f, I-1, n-I ) print( 'Ex 10.36:') J = 9 # The exam: # xbars = c( 29.3, 28.0, 30.2, 32.4, 34.2 ) ss = c( 4.99, 5.33, 3.33, 2.94, 2.74 ) x_dot_dot = 30.82 * I * J I = length(xbars) # Do an ANOVA test on this data (following the function ANOVA): # x_i_dot = xbars * J SSTr = sum( x_i_dot^2 ) / J - x_dot_dot^2 / ( I * J ) MSTr = SSTr / (I-1) MSE = sum( ss^2 ) / I f = MSTr / MSE p_value = 1 - pf( f, I-1, I*(J-1) ) # The retention test: # xbars = c( 30.2, 28.8, 26.2, 31.1, 30.2 ) ss = c( 3.82, 5.26, 4.66, 4.91, 3.53 ) x_dot_dot = 29.30 * I * J # Do an ANOVA test on this data (following the function ANOVA): # X_i_dot = xbars * J SSTr = sum( x_i_dot^2 ) / J - x_dot_dot^2 / ( I * J ) MSTr = SSTr / (I-1) MSE = sum( ss^2 ) / I f = MSTr / MSE p_value = 1 - pf( f, I-1, I*(J-1) ) print( 'Ex 10.37:' ) DF = read.csv( "../../Data/CH10/ex10-37.txt", header=TRUE, quote="'" ) res = ANOVA( DF$vibration..microns., DF$Brand ) # Given the MSE compute the group means and the significant spacing w: # xbar_i_dot = res$x_i_dot / res$J xbar_i_dot = sort( xbar_i_dot ) print( xbar_i_dot ) print( diff( xbar_i_dot ) ) q_crit = qtukey( 1-0.05, res$I, res$I*(res$J-1) ) w = q_crit * sqrt( res$MSE / res$J ) print( w ) print( 'Ex 10.38:' ) I = 5 J = 6 xbars = c( 2.58, 2.63, 2.13, 2.41, 2.49 ) x2_dot_dot = 183.4 # Do an ANOVA test on this data: # x_dot_dot = sum( J * xbars ) SST = x2_dot_dot - x_dot_dot^2 / ( I * J ) SSTr = sum( ( J * xbars )^2 ) / J - x_dot_dot^2 / ( I * J ) SSE = SST - SSTr MSTr = SSTr / (I-1) MSE = SSE / ( I*(J-1) ) f = MSTr / MSE p_value = 1 - pf( f, I-1, I*(J-1) ) print( 'Ex 10.39:' ) cs = c( 1, rep( -0.25, 4 ) ) m = sum( cs * xbars ) v = MSE * sum( cs^2 / J ) t_crit = qt( 1 - 0.05/2, I*J - I ) print( "CI" ) print( m + sqrt( v ) * t_crit * c(-1,+1) ) print( 'Ex 10.41:' ) DF = read.csv( "../../Data/CH10/ex10-41.txt", header=TRUE, quote="'" ) ANOVA( DF$X.Alcohol, DF$Labs ) print( 'Ex 10.42:' ) brown = c( 26.8, 27.9, 23.7, 25.0, 26.3, 24.8, 25.7, 24.5 ) green = c( 26.4, 24.2, 28.0, 26.9, 29.1 ) blue = c( 25.7, 27.2, 29.9, 28.5, 29.4, 28.3 ) DF = data.frame( cff=c( brown, green, blue ), color=c( rep( 'Brown', 8 ), rep( 'Green', 5 ), rep( 'Blue', 6 ) ) ) res = ANOVA_USS( DF$cff, DF$color ) u_ci = mean_diff_CI_utils( res$x_i_dot, res$Js, res$MSE ) print( u_ci ) print( 'Ex 10.43:' ) # # Use the data in "res" from the previous exercise: # scheffe_contrasts_ci = function( cs, res, alpha=0.05 ){ # We need a results structure from the output of a call to ANOVA_USS # m = sum( cs * res$xbar_i_dot ) v = sum( cs^2 / res$Js ) * ( res$I - 1 ) * res$MSE f_crit = qf( 1 - alpha, res$I - 1, res$n - res$I ) ci = m + sqrt(v * f_crit) * c(-1,+1) # our CI ci } # The contrast we want to study: # scheffe_contrasts_ci( c( 1, -1, 0 ), res ) scheffe_contrasts_ci( c( 1, 0, -1 ), res ) scheffe_contrasts_ci( c( 0, 1, -1 ), res ) scheffe_contrasts_ci( c( 0.5, 0.5, -1 ), res ) print( 'Ex 10.44:' ) DF = read.csv( "../../Data/CH10/ex10-44.txt", header=TRUE, quote="'" ) res = ANOVA( DF$strength, DF$Mortars ) # Given the MSE compute the group means and the significant spacing w: # xbar_i_dot = res$x_i_dot / res$J xbar_i_dot = sort( xbar_i_dot ) print( xbar_i_dot ) print( diff( xbar_i_dot ) ) q_crit = qtukey( 1-0.05, res$I, res$l*(res$J-1) ) w = q_crit * sqrt( res$MSE / res$J ) print( w ) print( 'Ex 10.46:') stress=c( 55, 53, 54, 26, 37, 32, 78, 91, 85, 92, 100, 96, 49, 51, 50, 80, 85, 83 ) type = c( rep( 1, 3 ), rep( 2, 3 ), rep( 3, 3 ), rep( 4, 3 ), rep( 5, 3 ), rep( 6, 3 ) ) DF = data.frame( stress=stress, type=type ) # Subtract the group mean from every element: # group_means = c( rep( 162, 3 ), rep( 95, 3 ), rep( 254, 3 ), rep( 288, 3 ), rep( 150, 3 ), rep( 248, 3 ) ) / 3 demeanded = stress - group_means qqnorm( demeanded ) qqline( demeanded )