## Hypothesis Test, Population Proportion

On your USB drive, create a new directory, copy model.R to that directory, rename the file in the new directory, double click on the file to open Rstudio. Then copy all of the text below the line and paste it into your Rstudio editor pane.
```
# Line 1: a small demonstration of hypothesis testing,
#   in this case for a population proportion
#
#  First, we will get a population
#  In this case we will get a large population
source("../gnrnd5.R")
gnrnd5(34981412407,967845)
#let us look at the head and tail values
tail(L1,40)
min(L1)
max(L1)

# ##########################
# ##  Problem: test the null hypothesis
# ##      that the population proportion of the
# ##      value  5 is  0.20
# ##      against the alternative hypothesis that
# ##      the population proportion of the value
# ##      5 is greater than 0.20.
# ##      Run the test at the 0.05 level of significance.
# ##########################

# take a simple random sample of size 90
#
#  Be careful:  Every time we do this we get
#               a different random sample
#
L2 <- as.integer( runif(90, 1, 4126) )
# L2  holds the index values of our simple random sample
L2
L3 <- L1[ L2 ]   # L3 holds the simple random sample
L3
# we need to find the proportion of 5's in the
# sample
table( L3 )
num_5s <- table(L3)
num_5s
n <- length( L3 )
phat <- num_5s / n
phat
#
# using the null hypothesis that p=0.20 find
# we note that the n*p > 10 and n*(1-p)>10
# so we can use the normal approximation ...
#
# find the standard deviation of sample proportions
sdsp <- sqrt( 0.20*(1-0.20)/n)
sdsp
#
#  find the z value that has 5% of the area to its right
z <- qnorm(0.05, lower.tail=FALSE)
z
#  The critical value is
#     p + z*sdsp
0.20 + z * sdsp
#  reject the null hypothesis if the sample proportion
#  is greater than that value

#
#  alternatively we could use the attained significance
#  approach.  How strange would it be to get the sample
#  proportion that we found if the true proportion is
#  0.20
pnorm( phat, mean=0.20, sd= sdsp,
lower.tail=FALSE)
# reject if that is less than 0.05

#
#   Of course, we could use the function
#   hypoth_test_prop() to do both of these approaches in
#  one easy step.
#
source("../hypo_prop.R")
hypoth_test_prop( 0.20, num_5s, 90, 1, 0.05)

#
#
#################################
# go back and execute lines 24-77 many more times.
#    Each time you get a different random sample.
#    Keep track of the number of times that you reject or
#    do not reject the null hypothesis.  By the way, the
#    true proportion of 5'2 is 0.2613333
#################################

#
#  now we will do the same thing for a different population
#
gnrnd5(34981412407,869895)
#let us look at the head and tail values
tail(L1,40)
min(L1)
max(L1)

# ##########################
# ##  Problem: test the null hypothesis
# ##      that the population proportion of the
# ##      value  5 is  0.20
# ##      against the alternative hypothesis that
# ##      the population proportion of the value
# ##      5 is greater than 0.20.
# ##      Run the test at the 0.05 level of significance.
# ##########################

# take a simple random sample of size 90
#
#  Be careful:  Every time we do this we get
#               a different random sample
#
L2 <- as.integer( runif(90, 1, 4126) )
# L2  holds the index values of our simple random sample
L2
L3 <- L1[ L2 ]   # L3 holds the simple random sample
L3
# we need to find the proportion of 5's in the
# sample
table( L3 )
num_5s <- table(L3)
num_5s
n <- length( L3 )
phat <- num_5s / n
phat
#
# using the null hypothesis that p=0.20 find
# we note that the n*p > 10 and n*(1-p)>10
# so we can use the normal approximation ...
#
# find the standard deviation of sample proportions
sdsp <- sqrt( 0.20*(1-0.20)/n)
sdsp
#
#  find the z value that has 5% of the area to its right
z <- qnorm(0.05, lower.tail=FALSE)
z
#  The critical value is
#     p + z*sdsp
0.20 + z * sdsp
#  reject the null hypothesis if the sample proportion
#  is greater than that value

#
#  alternatively we could use the attained significance
#  approach.  How strange would it be to get the sample
#  proportion that we found if the true proportion is
#  0.20
pnorm( phat, mean=0.20, sd= sdsp,
lower.tail=FALSE)
# reject if that is less than 0.05

#
#   Of course, we could use the function
#   hypoth_test_prop() to do both of these approaches in
#  one easy step.
#

hypoth_test_prop( 0.20, num_5s, 90, 1, 0.05)

#
#
#################################
# go back and execute lines 114-162 many more times.
#    Each time you get a different random sample.
#    Keep track of the number of times that you reject or
#    do not reject the null hypothesis.  By the way, the
#    true proportion of 5'2 is 0.1968485
#################################

#
```