####### Example 8.1:
### Page 207
### A criminal trial
### A defendant is charged with a crime and must stand trial.
### During the trial, a prosecutor and defense lawyer respectively try to convince the jury that
### the defendant is either guilty or innocent.
### The jury is supposed to be unbiased.
### When deciding the defendant’s fate, the jurors are instructed to assume that the defendant is
### innocent unless proven guilty beyond a shadow of a doubt.
### Let’s rephrase the above example in terms of significance tests.
### The assumption of innocence is replaced with the null hypothesis, H0.
### This stands in contrast to the alternative hypothesis, HA.
### The probability that the test statistic is the observed value (or is more extreme)
### using the assumptions of the null hypothesis.
### This is called the p-value.
### The calculation of the p-value is called a significance test.
### The p-value is based on the sampling distribution of the test statistic under H0
### and the single observed value of it during the trial.
### p-value=P(test statistic is the observed value or is more extreme|H0).
### Table 8.1 Level of significance for range of pvalues
### p-value range significance stars common description
### [0, .001] *** extremely significant
### (.001, .01] ** highly significant
### (.01, .05] * statistically significant
### (.05, .!] · could be significant
### (.1, 1] not significant
### When a decision is made based on the p-value, mistakes can happen.
### If the null hypothesis is falsely rejected, it is a type-I error
### (innocent man is found guilty).
### If the null hypothesis is falsely accepted, it is a type-I error
### (guilty man found not guilty).
### Example 8.2: Which mean?
### Imagine we have a widget-producing machine that
### sometimes goes out of calibration.
### Assume, for simplicity, that the widgets produced are random numbers that usually
### come from a normal distribution with mean a and variance 1.
### When the machine slips out of calibration, the random numbers
### come from normal distribution with mean 1 and variance 1.
### Based on the value of a single one of these random numbers, how can we
### decide whether the machine is in calibration or not?
### Let X be the random number. The hypotheses become
### H0:X is Normal(0,1), HA:X is Normal(1, 1).
### We usually write this as
### H0:μ=0, HA:μ=1,
### Suppose we observe a value 0.7 from the machine.
### Is this evidence that the machine is out of calibration?
### The p-value in this case is the probability that a Normal(0, 1) random variable
### produces a 0.7 or more. This is
1-pnorm(0.7,0,1)
### This p-value is not very small, and there is no evidence that the null hypothesis is false.
### Draw curves on board
### What if we draw a ten sample wigits and the mean is 0.7
1-pnorm(0.7,0,1/sqrt(10))
####### snippet 1 #######
### 8.1 Significance test for a population proportion
### Page 211
### Example 8.3: Poverty-rate increase In the United States, the poverty rate rose from
### 11.3 percent in 2000 to 11.7 percent in 2001 to 12.1 percent in 2002, as reported by the
### United States Census Bureau and that the sample sizes are 50,000 for 2001 and 60,000 for 2002.
### Assume binomial distribution so that SD = sqrt(p0*(1-p0)/n)
### 2001
p0 = .113; n = 50000; SD = sqrt(p0*(1-p0)/n)
pnorm(.117,mean=p0, sd=SD, lower.tail=FALSE)
####### snippet 2 #######
### faster
prop.test(x=5850, n=50000, p=.113, alt="greater")
### 2001
####### snippet 3 #######
prop.test(x=5850, n = 50000, p = .113, alt = "two.sided")
### Page 215
### Significance test for the mean (t-tests)
####### snippet 13 #######
### Example 8.4: SUV gas mileage
### Small sample - use t-distribution
### Big sample - use normal-distribution
mpg = c(11.4,13.1,14.7,14.7,15.0,15.5,15.6,15.9,16.0,16.8)
xbar = mean(mpg)
s = sd(mpg)
n = length(mpg)
c(xbar, s, n)
SE = s/sqrt(n)
(xbar - 17)/SE
pt(-4.285, df = 9, lower.tail = T)
####### snippet 14 #######
t.test(mpg, mu = 17, alt="less")