Lecture Notes Week 1
Lecture Notes Week 1
Probability basics
Sets X = {1, 2, 6, 12, 15} 2∈X 3∉X
Random experiments Multiple outcomes (>1), which outcome you get is unknown
Probability basics
Conditional probability P(being green)
Probability basics
Engineering Female students
Conditional probability
students
Female students
Independence?
Engineering Female students Engineering
students students
228371 Lecture set 1: Foundations of Inference
Lecture 1: Revision, Sampling, CLT, Inference
ത
𝑃 𝐴 = 𝑃 𝐴 ∩ 𝐵 + 𝑃(𝐴 ∩ 𝐵)
Maths
𝑃 𝐴 = 𝑃 𝐴 𝐵𝑛 𝑃(𝐵𝑛 )
𝑛
Probability basics
Bayes theorem 𝑃 𝐴|𝐵 → 𝑃(𝐵|𝐴) 𝑃 𝑓𝑒𝑚𝑎𝑙𝑒|𝑒𝑛𝑔𝑖𝑛𝑒𝑒𝑟 → 𝑃 𝑒𝑛𝑔𝑖𝑛𝑒𝑒𝑟 𝑓𝑒𝑚𝑎𝑙𝑒
Probability basics
Random Variables
Sampling
Random Sampling
Each unit in the population is equally likely to end up being picked.
Key to understanding how one random sample will differ from the next, and for extrapolating from sample to population
228371 Lecture set 1: Foundations of Inference
Lecture 1: Revision, Sampling, CLT, Inference
Probability basics
Distributions statistical models of our data
which one to use depends on type of data (discrete / continuous), and the shape of the data
Haphazard sampling -> choose which rats to get the treatment by “reaching in and grabbing a rat without looking”
Might “feel” random, but maybe the less energetic rats are easier to catch? Or those with shorter legs?
Random sampling -> label your rats with numbers, pick numbers at random, those rats get the treatment.
Moral: formal assignment of random numbers to the experimental units is the way to go.
228371 Lecture set 1: Foundations of Inference
Lecture 1: Revision, Sampling, CLT, Inference
CLT is useful because it gives you an idea of how to get from sample estimates, to population estimates
Foundation of most inferences on this course
Statistical Inference
Learning about a population or process from samples
1. Get data
2. Fit distribution
3. Make probabilistic statements
Inference – to come to a conclusion using evidence and reasoning, under conditions of uncertainty
Statistical inference – to come to a conclusion about a population, based on sample(s) and distributions, using probabilities
Confidence Intervals – the probability that the true value of a population will fall between two values for a certain proportion of times.
Want to make statements like: “With 95 % confidence, the population proportion lies between x and y”
If we did the experiment many, many times, build our confidence intervals, 95 % of those intervals would contain the true
population.
The more confident you want to be, the wider the interval will be. ± 2 standard deviations
The large number of samples you have, the smaller the interval will be (CLT).
Confidence Intervals – the probability that the true value of a population will fall between two values for a certain proportion of times.
Confidence Intervals – the probability that the true value of a population will fall between two values for a certain proportion of times.
90% x
Confidence Sample
interval estimate
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
Confidence Intervals – the probability that the true value of a population will fall between two values for a certain proportion of times.
𝑝(1
Ƹ − 𝑝)Ƹ
𝑝Ƹ ± 𝑧 ∗
𝑛
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
Zyban plus nicotine patch: 245 participants, 95 not smoking at end of 9 weeks.
95 ො
𝑝(1− ො
𝑝) 0.388(1−0.388)
Success: 𝑝Ƹ = 245 = 0.388 Standard error: = = 0.031
𝑛 245
95 % confidence level → 𝑧 ∗ = 1.96
in R → prop.test(95,245)
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
Zyban plus nicotine patch: 245 participants, 95 not smoking at end of 9 weeks.
in R →
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
1. The width of confidence intervals depends on sample size (through the standard error)
2. Intervals are based on the normal distribution (CLT), for this assumption to be reasonable – need a large
enough sample size: n𝑝,Ƹ 𝑛 1 − 𝑝Ƹ ≥ 5
0 1 0 1
𝑝Ƹ 𝑝Ƹ ~ 0.5
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
1. The width of confidence intervals depends on sample size (through the standard error)
2. Intervals are based on the normal distribution (CLT), for this assumption to be reasonable – need a large
enough sample size: n𝑝,Ƹ 𝑛 1 − 𝑝Ƹ ≥ 5
DISASTER!
0 1 1
𝑝Ƹ ~ 0.05
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
∗
𝑝Ƹ1 1 − 𝑝Ƹ1 𝑝Ƹ 2 1 − 𝑝Ƹ 2
𝑝Ƹ1 − 𝑝Ƹ 2 ± 𝑧 +
𝑛1 𝑛2
where z* is the value of the standard normal variable with area between –z* and z* equal to the desired
conference level.
Condition 1: Sample proportions are available based on independent, randomly selected samples from the two
populations
𝑝Ƹ1 1 − 𝑝Ƹ1 𝑝Ƹ 2 1 − 𝑝Ƹ 2
𝑝Ƹ1 − 𝑝Ƹ 2 ± 𝑧∗ +
𝑛1 𝑛2
in R → prop.test(c(85,52), c(244,244))
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
𝑝Ƹ1 1 − 𝑝Ƹ1 𝑝Ƹ 2 1 − 𝑝Ƹ 2
𝑝Ƹ1 − 𝑝Ƹ 2 ± 𝑧∗ +
𝑛1 𝑛2
We know how to calculate mean and standard deviation (hopefully!). But both of these are SAMPLE properties.
ҧ Sample standard deviation (s) → population mean (μ), population standard error (σ)
Sample Mean (𝑥),
t-distribution is wider and flatter than a normal distribution, but approaches normal distribution as n → ∞
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
Student’s t-distribution
𝑥ҧ − μ 𝑥ҧ − μ 𝑛(𝑥ҧ − μ)
𝑡= = =
𝑠. 𝑒. (𝑥)ҧ 𝑠/ 𝑛 𝑠
𝑥ҧ : sample mean
s: sample standard deviation
n: sample size
μ: population mean
σ: population standard error
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
∗
𝑠 ∗
𝑥ҧ ± 𝑡 s. e. 𝑥ҧ → 𝑥ҧ ± 𝑡
𝑛
where t* is the value in a t-distribution with degrees of freedom: df = n-1, such that the area between –t* and t*
equals the desired conference level.
Condition: Population of measurements is bell-shaped and a random sample of any size is measure, OR
Condition: Population of measurements is not bell-shaped but a large random sample is needed n ≥ 30
𝑠
𝑥ҧ ± 𝑡∗
Case Study: Bats and insects 𝑛
How far can bats detect insects from?
𝑠 18.1
𝑥ҧ ± 𝑡 ∗ = 532 ± 1.98 = 532 ± 3.58
𝑛 100
Interval is for the mean difference, based on differences for each pair.
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
12 adult males
Measured time for blood to clot BEFORE taking 2 asprin
Measured time for blood to clot AFTER taking 2 asprin (3 hours later)
1 1
𝑥1ҧ − 𝑥ҧ2 ± 𝑡 ∗ 𝑠𝑝 +
𝑛1 𝑛2
Where 𝑡 ∗ is found using a t-distribution with 𝑑𝑓 = 𝑛1 + 𝑛2 − 2, and 𝑠𝑝 is the pooled standard deviation.
𝑛1 − 1 𝑠12 + 𝑛2 − 1 𝑠22
𝑠𝑝 =
𝑛1 + 𝑛2 − 2
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
𝑠12 𝑠22
𝑥1ҧ − 𝑥ҧ2 ± 𝑡 ∗ +
𝑛1 𝑛2
Where 𝑡 ∗ is the value in a t-distribution with area between - 𝑡 ∗ and + 𝑡 ∗ equal to the desired confidence
level. But the df now depends on the observed variances – tricky, and approximate (Welch’s
approximation):
𝑠12 𝑠22
+
𝑛1 𝑛2
𝑑𝑓 ≈
2 2 𝑠22 2
1 𝑠1 1
+
𝑛1 − 1 𝑛1 𝑛2 − 1 𝑛2
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
At the end of 8 weeks, the students are given a Degree of Reading Power (DRP) test.
Class 1 (activity): 24 61 59 46 43 44 52 43 58 67 62 57 71 49 54 43 53 57 49 56 33
Class 2 (no activity): 42 33 46 37 43 41 10 42 55 19 17 55 26 54 60 28 62 20 53 48 37 85 42
What is the 95% Confidence Interval for the difference in the means?
Class 1 (activity): 24 61 59 46 43 44 52 43 58 67 62 57 71 49 54 43 53 57 49 56 33
Class 2 (no activity): 42 33 46 37 43 41 10 42 55 19 17 55 26 54 60 28 62 20 53 48 37 85 42
1 1 1 1
𝑥ҧ1 − 𝑥ҧ2 ± 𝑡 ∗ 𝑠𝑝 + 𝑛 → 51.48 − 41.92 ± 2.018 ∗ 14.55 + = (1.10, 18.82)
𝑛1 2 21 23
We are 95% confidence that the difference in mean scores is between 1.10 and 18.82. +ve, but could be small or large.
228371 Lecture set 1: Foundations of Inference
Lecture 2: Confidence Intervals
Rule of thumb:
Hypothesis
“A hypothesis is an idea which is suggested as a possible explanation for a particular situation or
condition, but which has not been proven to be correct”
For us: Need the hypothesis to be translated into a statement about a parameter
Hypothesis test
To establish whether the hypothesis can be rejected based on evidence (data) presented
Errors
Can be wrong in two different ways - Type 1 and Type 2 errors.
PLAN:
(1) Hypothesise,
(2) Test to reject hypothesis,
(3) Think about how wrong we might be
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing PLAN:
(1) Hypothesise,
(2) Test to reject hypothesis,
(3) Think about how wrong we might be
Case Study: Member of a Jury
A person is accused of a crime. Under NZ law, an accused is presumed innocent until proven guilty.
(1) Two scenarios: H0: The defendant is NOT guilty, or Ha: The defendant is guilty
(2) You look at the evidence and decide whether to reject H0.
(3) Worry.
H0 rejected, Ha accepted:
Has an innocent person been falsely convicted?
Type 1 error false positive
H0 not rejected:
Has the criminal been erroneously freed?
Type 2 error false negative
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
Null hypothesis: H0 is the hypothesis that is set up to see if we can reject it or not.
Innocent until proven guilty → H0: The defendant is NOT guilty
Which way round you set up your null and alternative hypotheses is based on the intent of the test:
If it is of interest to show a hypothesis is TRUE, make it the alternative hypothesis
If it is of interest to show a hypothesis is FALSE, make it the null hypothesis
Decision table
Decision H0 false H0 true
Accept H0 Type II error Correct decision
Reject H0 Correct decision Type 1 error
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
(1) H0: You do NOT have the disease, or Ha: You do have the disease
Which scenario is worse? Depends on the disease, and the consequence of wrong diagnosis.
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
Type 1 error (false positive) probability: α Type 2 error (false negative) probability: β
We want α and β as small as possible, HOWEVER, α + β ≈ constant. If you decrease α, you increase β.
Usually focus on type 1 errors and select α that is tolerable for the specific situation. Also call this α,
the significance level of a test – the probability of making a type 1 error.
So, we decide how significant we want the results from our test to be, run the test, and get something
called a “p-value”. We compare the p-value to our significance level, and if p is LESS than α, we say the
results are statistically significant at the α level.
P-value is…
- the tail probability -z z
- the probability of getting a sample as, or more, unlikely given the null hypothesis, than the sample
you actually got
- just the total amount of ‘answer’ space that represents samples as, or more, unlikely than the
result of the sample you observed.
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
- For example, p value =0.04 means only 4% of all possible samples are more extreme. This sample is
unlikely to occur if H0 is true, and so provides evidence against H0, and H0 can be rejected if, say,
our significance level is 0.05.
- A p-value of 0.3, means 30% of all possible samples are more extreme. This is not enough evidence
to reject H0 unless our significance level is really high (in which case our chance of type 1 errors –
false positives, will be really high)
One-tailed Two-tailed
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
Recap
Null & Alternative Hypotheses H0: claim about populations initially assumed to be true
Ha: claim that is contradictory to the null hypothesis
Type I and Type II errors Type I: False positive, reject null but its true
Type 2: False negative, do not reject null but its false
Test statistic calculated from the sample used to provide evidence about the plausibility of H0
P-value Tail probability for which the statistic under H0 is more extreme than the observed
value of the statistic.
P-value is low, the null must go.
𝑝ො1 −𝑝ො2
Test statistic: 𝑧= 1 1
ො
𝑝(1− ො
𝑝) +
𝑛1 𝑛2
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
-z z
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
ALTERNATIVELY In R →
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
ALTERNATIVELY In R →
2
𝑂𝑖 − 𝐸𝑖
Χ2 =
𝐸𝑖
𝑖
Hypothesis Testing
HOW TO:
(1) State your null hypothesis and alternative: H0: ### Ha: ###
(2) Find the appropriate test statistic: z, t, … (note: see last pages in Engineering Stats book)
(3) Calculate test statistic under H0 i.e., when H0 parameter value is true
Note: a p-value is computed assuming H0 is true, and μ0 is the value for that computation
228371 Lecture set 1: Foundations of Inference
Lecture 3: Hypothesis Testing
Are data approximately normal or have a large enough sample (n ≥ 30)? → CLT
H0 : μ = μ0 versus Ha : μ ≠ μ0 (two-sided)
μ0 = 1.5
μ0 = 25
H0 : μx – μy = 0 versus
Ha : μx – μy ≠ 0 ***
H0 : μ1 = μ2 versus Ha : μ1 < μ2
Are data approximately normal or have a large enough sample (n ≥ 30)? → CLT
H0 : μ1 = μ2 versus Ha : μ1 < μ2
Two types of rubber are being considered for use in shoe soles.
8 people wear shoes with one type of rubber (A) on one foot, and the other type on the other foot (B).
After 2 months the amount of wear is measured on each shoe.
Note: Can also just run the test with raw data and tell it that the data are paired: