R - HW06 - PvalFunction.R (1570B)
1 #*******************************************************# 2 #*******************************************************# 3 # STAT320 - HW6 Solutions # 4 #*******************************************************# 5 #*******************************************************# 6 #***************************************************# 7 # PVal for Proportions # 8 #***************************************************# 9 10 #***************************************************# 11 #***************************************************# 12 #***************************************************# 13 #***************************************************# 14 #***************************************************# 15 16 #***************************************************# 17 # Fair Coin 18 #***************************************************# 19 #set.seed(257) 20 #***************************************************# 21 22 prop.pval.f = function(x,p0=0.5,NSize=100,Low = TRUE,NSim=100000) 23 { 24 POP = c("T","H") 25 Succ = "T" 26 ONSuccs = numeric(NSim) 27 28 for(i in 1:NSim) 29 { 30 Sample = sample(x=POP, prob =c(p0,1-p0),size = NSize,replace = TRUE) 31 ONSuccs[i] = sum(Sample==Succ) 32 } 33 34 if(Low==TRUE) 35 { 36 p = sum(ONSuccs<=x)/NSim 37 }else 38 { 39 p = sum(ONSuccs>=x)/NSim 40 } 41 return(p) 42 } 43 44 #***************************************************# 45 #***************************************************# 46 47 NObs = 100000 48 N = 564 49 pS = .135 50 51 x = 65 52 Dir = TRUE 53 prop.pval.f(x,p0=pS,NSize = N,Low = Dir,NSim=NObs) 54 55 56 57 58 59 60