# Question 1: # pt_a = qchisq( 0.95, 14 ) pt_b = qchisq( 0.9, 2 ) pt_c = qchisq( 0.025, 9 ) print( c( pt_a, pt_b, pt_c ) ) # Question 2: # pt_a = 1 - pchisq( 8.672, 17 ) pt_b = pchisq( 10.645, 6 ) pt_c = pchisq( 34.17, 20 ) - pchisq( 9.591, 20 ) pt_d = pchisq( 9.21, 2 ) print( c( pt_a, pt_b, pt_c, pt_d ) ) # Question 3: # pt_a = qchisq( 1-0.99, 9 ) pt_b = qchisq( 0.05, 15 ) pt_c = qchisq( 0.09, 22 ) + pchisq( 9.542, 22 ) pt_d = qchisq( pchisq( 48.232, 31 ) - 0.95, 31 ) print( c( pt_a, pt_b, pt_c, pt_d ) ) # Question 4: # library(stats) qchi2_root = function(x){ pchisq(5.009, x) - 0.0125 } res = uniroot( qchi2_root, c(10, 20) ) pt_a = res$root qchi2_root = function(x){ pchisq(30.144, x) - ( 0.05 - pchisq(27.204, x) ) } res = uniroot( qchi2_root, c(40, 60) ) pt_b = res$root qchi2_root = function(x){ pchisq(19.281, x) - 0.05 } res = uniroot( qchi2_root, c(20, 40) ) pt_c = res$root qchi2_root = function(x){ pchisq(24.769, x) - ( 0.80 - pchisq(10.085, x) ) } res = uniroot( qchi2_root, c(20, 30) ) pt_d = res$root print( c( pt_a, pt_b, pt_c, pt_d ) ) # Question 5: # n = 200 z_p = qnorm(0.95) print( n * ( 1 - 2/(9*n) + z_p * sqrt( 2 / (9*n) ) )^3 ) # Question 6: # n=2 v = pchisq( 2*(n-1), n-1 ) while( v < 0.95 ){ n = n+1 v = pchisq( 2*(n-1), n-1 ) } print( c( n, v ) ) # Question 8: # n = 19 sigma2 = 12 alpha = 0.05 print( ( sigma2 / (n-1) ) * c( qchisq(alpha/2, n-1), qchisq(1-alpha/2, n-1) ) ) # Question 9: # source('chap_7_sect_5_question_9_data.R') n = length(data) s2 = var(data) alpha = 0.05 print( sqrt( ( (n-1) * s2 ) / qchisq( c(1-alpha/2, alpha/2), n-1 ) ) ) lower_limit = sqrt( ( (n-1) * s2 ) / qchisq( 1-alpha, n-1 ) ) upper_limit = sqrt( ( (n-1) * s2 ) / qchisq( alpha, n-1 ) ) print( c( lower_limit, upper_limit ) ) # Question 10: # source('chap_7_sect_5_question_10_data.R') data = DF$Interest_Rate s2 = var(data) n = length(data) alpha = 0.05 ci = sqrt( ( (n-1) * s2 ) / qchisq( c(1-alpha/2, alpha/2), n-1 ) ) print(ci) # Question 12: # source('case_study_7_5_1_data.R') n = length(data) s2 = var(data) alpha = 0.05 z_crit = qnorm(1-alpha/2) ci = sqrt( (n-1) * s2 * c( 1/(n-1 + sqrt(2*(n-1)) * z_crit), 1/(n-1 - sqrt(2*(n-1)) * z_crit) ) ) print( ci ) # Question 13: # library(stats) qchi2_root = function(x){ qchisq(0.95, x-1) / qchisq(0.05, x-1) - 261.9/54.47 } res = uniroot( qchi2_root, c(10, 20) ) print( res$root ) n = 11 # this is closest integer value s2 = ( 54.47 * qchisq(1-alpha/2, n-1) )/(n-1) print(s2) # Question 15: # source('case_study_7_5_1_data.R') n = length(data) s2 = var(data) sigma0 = 30.4 c2 = (n-1)*s2/sigma0^2 print(sprintf('chi^2= %f; p_value= %f', c2, pchisq(c2, n-1))) # Question 16: # source('chap_7_sect_5_question_16_data.R') n = length(data) s2 = var(data) sigma0 = 1 c2 = (n-1)*s2/sigma0^2 print(sprintf('s^2= %f; chi^2= %f; p_value= %f', s2, c2, 1-pchisq(c2, n-1))) # Question 17: # n = 24 ybar = 0.115 s = 0.1017 mu0 = 0.101 sigma0 = 0.1567 t = ( ybar - mu0 )/(s/sqrt(n)) p_value = 1-pt(abs(t), n-1) print(sprintf('t= %f; p_value= %f', t, p_value)) c2 = (n-1)*s^2/sigma0^2 print(sprintf('chi^2= %f; p_value= %f', c2, pchisq(c2, n-1)))