source('utils.R') # Section 2; Question 1: # source('chap_12_sect_2_question_1_data.R') # We will do this first example "by hand" and then write a function to do everything for easier generalization: # n = dim(DF)[1] k = length( unique( DF$model ) ) # Use the computational formula: # T_dot_dot = sum( DF$value ) C = T_dot_dot^2 / n # Compute n_j and T_dot_j: # ufactors = unique( DF$model ) n_j = rep( NA, k ) T_dot_j = rep( NA, k ) for( i in 1:k ){ inds = DF$model == ufactors[i] n_j[i] = sum( inds ) T_dot_j[i] = sum( DF[inds,]$value ) } SSTOT = sum( DF$value^2 ) - C SSTR = sum( T_dot_j^2 / n_j ) - C SSE = SSTOT - SSTR F = ( SSTR/(k-1) ) / ( SSE/(n-k) ) alpha = 1 - pf( F, k-1, n-k ) print(sprintf('alpha= %f', alpha)) # Section 2; Question 2: # source('chap_12_sect_2_question_2_data.R') res = wANOVA( DF, 'year', 'value' ) print( res ) # Section 2; Question 3: # source('chap_12_sect_2_question_3_data.R') res = wANOVA( DF, 'sector', 'PERatio' ) print( res ) # Section 2; Question 4: # source('chap_12_sect_2_question_4_data.R') res = wANOVA( DF, 'sector', 'yield' ) print( res ) # Section 2; Question 5: # source('chap_12_sect_2_question_5_data.R') res = wANOVA( DF, 'location', 'age' ) print( res ) # Section 2; Question 6: # source('chap_12_sect_2_question_6_data.R') res = wANOVA( DF, 'group', 'diff' ) print( res ) # Section 2; Question 7: # treatment_df = 4 SSTOT = 377.36 MSE = 10.60 F = 6.40 # From the above (and the formulas in the book) we derive: # k = treatment_df + 1 MSTR = F * MSE SSTR = MSTR * (k-1) SSE = SSTOT - SSTR n = SSE/MSE + k error_df = n-k tota1_df = n-1 # Section 2; Question 8: # source('chap_12_sect_2_question_8_data.R') # Lets check that the standard deviation is approximately the same for each treatment (note that it is not): # tapply( DF$value, DF$treatment, sd ) # Section 2; Question 11: EPage 607 # source('chap_12_sect_2_question_11_data.R') # Compute the F value of the ANOVA test: # ares = aov( halflife ~ sex, data=DF ) sres = summary(ares) F = sres[[1]]$"F value"[1] # Compute the t value of a t-test: # tres = t.test( DF[ DF$sex=='W', ]$halflife, DF[ DF$sex=='M', ]$halflife, var.equal=TRUE ) print(sprintf('Question 11: ANOVA F= %10.6f; t^2= %10.6f', F, tres$statistic^2)) # Section 2; Question 12: EPage 607 # # Case Study 9.2.1 on EPage 460 # source('../Chapter9/case_study_9_2_1_data.R') ares = aov( prop ~ author, data=DF ) sres = summary(ares) F = sres[[1]]$"F value"[1] tres = t.test( DF[ DF$author == 'Twain', ]$prop, DF[ DF$author == 'QCS', ]$prop, var.equal=TRUE ) print(sprintf('Question 12: ANOVA F= %10.6f; t^2= %10.6f', F, tres$statistic^2)) # Section 2; Question 13: EPage 607 # # Question 8.2.2 on EPage 450 # source('../Chapter8/chap_8_sect_2_question_2_data.R') ares = aov( deaths ~ type, data=DF ) sres = summary(ares) F = sres[[1]]$"F value"[1] tres = t.test( DF[ DF$type == 'limited', ]$deaths, DF[ DF$type == 'comprehensive', ]$deaths, var.equal=TRUE ) print(sprintf('Question 13: ANOVA F= %10.6f; t^2= %10.6f', F, tres$statistic^2))