-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfreq_inference.Rmd
1460 lines (908 loc) · 51.1 KB
/
freq_inference.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\providecommand{\E}{\operatorname{E}}
\providecommand{\V}{\operatorname{Var}}
\providecommand{\Cov}{\operatorname{Cov}}
\providecommand{\se}{\operatorname{se}}
\providecommand{\logit}{\operatorname{logit}}
\providecommand{\iid}{\; \stackrel{\text{iid}}{\sim}\;}
\providecommand{\asim}{\; \stackrel{.}{\sim}\;}
\providecommand{\xs}{x_1, x_2, \ldots, x_n}
\providecommand{\Xs}{X_1, X_2, \ldots, X_n}
\providecommand{\bB}{\boldsymbol{B}}
\providecommand{\bb}{\boldsymbol{\beta}}
\providecommand{\bx}{\boldsymbol{x}}
\providecommand{\bX}{\boldsymbol{X}}
\providecommand{\by}{\boldsymbol{y}}
\providecommand{\bY}{\boldsymbol{Y}}
\providecommand{\bz}{\boldsymbol{z}}
\providecommand{\bZ}{\boldsymbol{Z}}
\providecommand{\be}{\boldsymbol{e}}
\providecommand{\bE}{\boldsymbol{E}}
\providecommand{\bs}{\boldsymbol{s}}
\providecommand{\bS}{\boldsymbol{S}}
\providecommand{\bP}{\boldsymbol{P}}
\providecommand{\bI}{\boldsymbol{I}}
\providecommand{\bD}{\boldsymbol{D}}
\providecommand{\bd}{\boldsymbol{d}}
\providecommand{\bW}{\boldsymbol{W}}
\providecommand{\bw}{\boldsymbol{w}}
\providecommand{\bM}{\boldsymbol{M}}
\providecommand{\bPhi}{\boldsymbol{\Phi}}
\providecommand{\bphi}{\boldsymbol{\phi}}
\providecommand{\bN}{\boldsymbol{N}}
\providecommand{\bR}{\boldsymbol{R}}
\providecommand{\bu}{\boldsymbol{u}}
\providecommand{\bU}{\boldsymbol{U}}
\providecommand{\bv}{\boldsymbol{v}}
\providecommand{\bV}{\boldsymbol{V}}
\providecommand{\bO}{\boldsymbol{0}}
\providecommand{\bOmega}{\boldsymbol{\Omega}}
\providecommand{\bLambda}{\boldsymbol{\Lambda}}
\providecommand{\bSig}{\boldsymbol{\Sigma}}
\providecommand{\bSigma}{\boldsymbol{\Sigma}}
\providecommand{\bt}{\boldsymbol{\theta}}
\providecommand{\bT}{\boldsymbol{\Theta}}
\providecommand{\bpi}{\boldsymbol{\pi}}
\providecommand{\argmax}{\text{argmax}}
\providecommand{\KL}{\text{KL}}
\providecommand{\fdr}{{\rm FDR}}
\providecommand{\pfdr}{{\rm pFDR}}
\providecommand{\mfdr}{{\rm mFDR}}
\providecommand{\bh}{\hat}
\providecommand{\dd}{\lambda}
\providecommand{\q}{\operatorname{q}}
```{r, message=FALSE, echo=FALSE, cache=FALSE}
source("./customization/knitr_options.R")
```
# (PART) Frequentist Inference {-}
# Statistical Inference
## Data Collection as a Probability
- Suppose data are collected in such a way that it is randomly observed according to a probability distribution
- If that probability distribution can be parameterized, then it is possible that the *parameters describe key characteristics of the population of interest*
- **Statistical inference** reverse engineers this process to estimate the unknown values of the parameters and express a measure of uncertainty about these estimates
## Example: Simple Random Sample
Individuals are uniformly and independently randomly sampled from a population.
The measurements taken on these individuals are then modeled as random variables, specifically random realizations from the complete population of values.
Simple random samples form the basis of modern surveys.
## Example: Randomized Controlled Trial
Individuals under study are randomly assigned to one of two or more available treatments.
This induces randomization directly into the study and breaks the relationship between the treatments and other variables that may be influencing the response of interest.
This is the gold standard study design in clinical trials to assess the evidence that a new drug works on a given disease.
## Parameters and Statistics
- A **parameter** is a number that describes a population
- A parameter is often a fixed number
- We usually do not know its value
- A **statistic** is a number calculated from a sample of data
- A statistic is used to estimate a parameter
## Sampling Distribution
The **sampling distribution** of a statistic is the probability disribution of the statistic under repeated realizations of the data from the assumed data generating probability distribution.
*The sampling distribution is how we connect an observed statistic to the population.*
## Central Dogma of Inference
## Example: Fair Coin?
Suppose I claim that a specific coin is fair, i.e., that it lands on heads or tails with equal probability.
I flip it 20 times and it lands on heads 16 times.
1. My data is $x=16$ heads out of $n=20$ flips.
2. My data generation model is $X \sim \mbox{Binomial}(20, p)$.
3. I form the statistic $\hat{p} = 16/20$ as an estimate of $p$.
Let's simulate 10,000 times what my estimate would look like if $p=0.5$ and I repeated the 20 coin flips over and over.
```{r sampling_demo, cache=TRUE}
x <- replicate(n=1e4, expr=rbinom(1, size=20, prob=0.5))
sim_p_hat <- x/20
my_p_hat <- 16/20
```
What can I do with this information?
```{r sampling_plot, cache=TRUE, echo=FALSE}
df <- data.frame(x=sim_p_hat)
ggplot(data=df, aes(x=x)) +
geom_histogram(color="blue", fill="lightgray", binwidth=0.05) +
geom_vline(xintercept=my_p_hat, size=1.5) +
labs(title="Histogram of Sampling Distribution") +
geom_text(aes(my_p_hat, 1500, label="p_hat"), nudge_x=0.05)
```
# Inference Goals and Strategies
## Basic Idea
Data are collected in such a way that there exists a reasonable probability model for this process that involves parameters informative about the population.
Common Goals:
1. Form point estimates the parameters
2. Quantify uncertainty on the estimates
3. Test hypotheses on the parameters
## Normal Example
Suppose a simple random sample of $n$ data points is collected so that the following model of the data is reasonable: $X_1, X_2, \ldots, X_n$ are iid Normal($\mu$, $\sigma^2$).
The goal is to do inference on $\mu$, the population mean.
For simplicity, assume that $\sigma^2$ is known (e.g., $\sigma^2 = 1$).
## Point Estimate of $\mu$
There are a number of ways to form an estimate of $\mu$, but one that has several justifications is the sample mean:
$$\hat{\mu} = \overline{x} = \frac{1}{n}\sum_{i=1}^n x_i,$$
where $x_1, x_2, \ldots, x_n$ are the observed data points.
## Sampling Distribution of $\hat{\mu}$
If we were to repeat this study over and over, how would $\hat{\mu}$ behave?
$$\hat{\mu} = \overline{X} = \frac{1}{n}\sum_{i=1}^n X_i$$
$$\overline{X} \sim \mbox{Normal}(\mu, \sigma^2/n)$$
How do we use this to quantify uncertainty and test hypotheses?
## Pivotal Statistic
One *very useful* strategy is to work backwards from a pivotal statistic, which is a statistic that does not depend on any unknown paramaters.
Example:
$$\frac{\overline{X} - \mu}{\sigma/\sqrt{n}} \sim \mbox{Normal}(0,1)$$
Note that in general for a rv $Y$ it is the case that $(Y - \operatorname{E}[Y])/\sqrt{\operatorname{Var}(Y)}$ has population mean 0 and variance 1.
# Confidence Intervals
## Goal
Once we have a point estimate of a parameter, we would like a measure of its uncertainty.
Given that we are working within a probabilistic framework, the natural language of uncertainty is through probability statements.
We interpret this measure of uncertainty in terms of hypothetical repetitions of the sampling scheme we used to collect the original data set.
## Formulation
Confidence intervals take the form
$$(\hat{\mu} - C_{\ell}, \hat{\mu} + C_{u})$$
where
$${\rm Pr}(\mu - C_{\ell} \leq \hat{\mu} \leq \mu + C_{u})$$
forms the "level" or coverage probability of the interval.
## Interpretation
If we repeat the study many times, then the CI $(\hat{\mu} - C_{\ell}, \hat{\mu} + C_{u})$ will contain the true value $\mu$ with a long run frequency equal to ${\rm Pr}(\mu - C_{\ell} \leq \hat{\mu} \leq \mu + C_{u})$.
A CI calculated on an observed data set is *not* intepreted as: "There is probability ${\rm Pr}(\mu - C_{\ell} \leq \hat{\mu} \leq \mu + C_{u})$ that $\mu$ is in our calculated $(\hat{\mu} - C_{\ell}, \hat{\mu} + C_{u})$." Why not?
## A Normal CI
If $Z \sim$ Normal(0,1), then ${\rm Pr}(-1.96 \leq Z \leq 1.96) = 0.95.$
\begin{eqnarray}
0.95 & = & {\rm Pr} \left(-1.96 \leq \frac{\hat{\mu} - \mu}{\sigma/\sqrt{n}} \leq 1.96 \right) \\
\ & = & {\rm Pr} \left(-1.96 \frac{\sigma}{\sqrt{n}} \leq \hat{\mu} - \mu \leq 1.96\frac{\sigma}{\sqrt{n}} \right) \\
\ & = & {\rm Pr} \left(\mu-1.96\frac{\sigma}{\sqrt{n}} \leq \hat{\mu} \leq \mu+1.96\frac{\sigma}{\sqrt{n}} \right)
\end{eqnarray}
Therefore, $\left(\hat{\mu} - 1.96\frac{\sigma}{\sqrt{n}}, \hat{\mu} + 1.96\frac{\sigma}{\sqrt{n}}\right)$ forms a 95% confidence interval of $\mu$.
## A Simulation
```{r}
mu <- 5
n <- 20
x <- replicate(10000, rnorm(n=n, mean=mu)) # 10000 studies
m <- apply(x, 2, mean) # the estimate for each study
ci <- cbind(m - 1.96/sqrt(n), m + 1.96/sqrt(n))
head(ci)
```
```{r}
cover <- (mu > ci[,1]) & (mu < ci[,2])
mean(cover)
```
## Normal$(0,1)$ Percentiles
Above we constructed a 95% CI. How do we construct (1-$\alpha$)-level CIs?
Let $z_{\alpha}$ be the $\alpha$ percentile of the Normal(0,1) distribution.
If $Z \sim$ Normal(0,1), then
\begin{eqnarray*}
1-\alpha & = & {\rm Pr}(z_{\alpha/2} \leq Z \leq z_{1-\alpha/2}) \\
\ & = & {\rm Pr}(-|z_{\alpha/2}| \leq Z \leq |z_{\alpha/2}|)
\end{eqnarray*}
## Commonly Used Percentiles
```{r}
# alpha/2 upper and lower percentiles for alpha=0.05
qnorm(0.025)
qnorm(0.975)
```
```{r}
# alpha/2 upper and lower percentiles for alpha=0.10
qnorm(0.05)
qnorm(0.95)
```
## $(1-\alpha)$-Level CIs
If $Z \sim$ Normal(0,1), then ${\rm Pr}(-|z_{\alpha/2}| \leq Z \leq |z_{\alpha/2}|) = 1-\alpha.$
Repeating the steps from the 95% CI case, we get the following is a $(1-\alpha)$-Level CI for $\mu$:
$$\left(\hat{\mu} - |z_{\alpha/2}| \frac{\sigma}{\sqrt{n}}, \hat{\mu} + |z_{\alpha/2}| \frac{\sigma}{\sqrt{n}}\right)$$
## One-Sided CIs
The CIs we have considered so far are "two-sided". Sometimes we are also interested in "one-sided" CIs.
If $Z \sim$ Normal(0,1), then $1-\alpha = {\rm Pr}(Z \geq -|z_{\alpha}|)$ and $1-\alpha = {\rm Pr}(Z \leq |z_{\alpha}|).$ We can use this fact along with the earlier derivations to show that the following are valid CIs:
$$(1-\alpha)\mbox{-level upper: } \left(-\infty, \hat{\mu} + |z_{\alpha}| \frac{\sigma}{\sqrt{n}}\right)$$
$$(1-\alpha)\mbox{-level lower: } \left(\hat{\mu} - |z_{\alpha}| \frac{\sigma}{\sqrt{n}}, \infty\right)$$
# Hypothesis Tests
## Example: HT on Fairness of a Coin
Suppose I claim that a specific coin is fair, i.e., that it lands on heads or tails with equal probability.
I flip it 20 times and it lands on heads 16 times.
1. My data is $x=16$ heads out of $n=20$ flips.
2. My data generation model is $X \sim \mbox{Binomial}(20, p)$.
3. I form the statistic $\hat{p} = 16/20$ as an estimate of $p$.
More formally, I want to **test the hypothesis**: $H_0: p=0.5$ vs. $H_1: p \not=0.5$ under the model $X \sim \mbox{Binomial}(20, p)$ based on the **test statistic** $\hat{p} = X/n$.
Let's simulate 10,000 times what my estimate would look like if $p=0.5$ and I repeated the 20 coin flips over and over.
```{r sampling_demo_3, cache=TRUE}
x <- replicate(n=1e4, expr=rbinom(1, size=20, prob=0.5))
sim_p_hat <- x/20
my_p_hat <- 16/20
```
The vector `sim_p_hat` contains 10,000 draws from the **null distribution**, i.e., the distribution of my test statstic $\hat{p} = X/n$ when $H_0: p=0.5$ is true.
```{r sampling_plot_3, cache=TRUE, echo=FALSE, dependson="sampling_demo_3"}
df <- data.frame(sim_p_hat=sim_p_hat)
ggplot(data=df, aes(x=sim_p_hat)) +
geom_histogram(color="blue", fill="lightgray", binwidth=0.05) +
geom_vline(xintercept=my_p_hat, size=1.5) +
labs(title="Histogram of Null Distribution Draws") +
geom_text(aes(my_p_hat, 1500, label="obs. p_hat"), nudge_x=0.075)
```
The deviation of the test statistic from the null hypothesis can be measured by $|\hat{p} - 0.5|$.
Let's compare our observed deviation $|16/20 - 0.5|$ to the 10,000 simulated null data sets. Specifically, let's calculate the frequency by which these 10,000 cases are **as or more extreme** than the observed test statistic.
```{r, cache=TRUE, dependson="sampling_demo_2"}
sum(abs(sim_p_hat-0.5) >= abs(my_p_hat-0.5))/1e4
```
This quantity is called the **p-value** of the hypothesis test.
## A Caveat
This example is a simplification of a more general framework for testing statistical hypotheses.
Given the intuition provided by the example, let's now formalize these ideas.
## Definition
- A **hypothesis test** or **significance test** is a formal procedure for comparing observed data with a hypothesis whose truth we want to assess
- The results of a test are expressed in terms of a probability that measures how well the data and the hypothesis agree
- The **null hypothesis** ($H_0$) is the statement being tested, typically the status quo
- The **alternative hypothesis** ($H_1$) is the complement of the null, and it is often the "interesting" state
## Return to Normal Example
Let's return to our Normal example in order to demonstrate the framework.
Suppose a simple random sample of $n$ data points is collected so that the following model of the data is reasonable: $X_1, X_2, \ldots, X_n$ are iid Normal($\mu$, $\sigma^2$).
The goal is to do test a hypothesis on $\mu$, the population mean.
For simplicity, assume that $\sigma^2$ is known (e.g., $\sigma^2 = 1$).
## HTs on Parameter Values
Hypothesis tests are usually formulated in terms of values of parameters. For example:
$$H_0: \mu = 5$$
$$H_1: \mu \not= 5$$
Note that the choice of 5 here is arbitrary, for illustrative purposes only. In a typical real world problem, the values that define the hypotheses will be clear from the context.
## Two-Sided vs. One-Sided HT
Hypothesis tests can be two-sided or one-sided:
$$H_0: \mu = 5 \mbox{ vs. } H_1: \mu \not= 5 \mbox{ (two-sided)}$$
$$H_0: \mu \leq 5 \mbox{ vs. } H_1: \mu > 5 \mbox{ (one-sided)}$$
$$H_0: \mu \geq 5 \mbox{ vs. } H_1: \mu < 5 \mbox{ (one-sided)}$$
## Test Statistic
A **test statistic** is designed to *quantify the evidence against the null hypothesis in favor of the alternative*. They are usually defined (and justified using math theory) so that the larger the test statistic is, the more evidence there is.
For the Normal example and the two-sided hypothesis $(H_0: \mu = 5 \mbox{ vs. } H_1: \mu \not= 5)$, here is our test statistic:
$$
|z| = \frac{\left|\overline{x} - 5\right|}{\sigma/\sqrt{n}}
$$
What would the test statistic be for the one-sided hypothesis tests?
## Null Distribution (Two-Sided)
The **null distribution** is the sampling distribution of the test statistic when $H_0$ is true.
We saw earlier that $\frac{\overline{X} - \mu}{\sigma/\sqrt{n}} \sim \mbox{Normal}(0,1).$
When $H_0$ is true, then $\mu=5$. So when $H_0$ is true it follows that
$$Z = \frac{\overline{X} - 5}{\sigma/\sqrt{n}} \sim \mbox{Normal}(0,1)$$
and then probabiliy calculations on $|Z|$ are straightforward. Note that $Z$ is pivotal when $H_0$ is true!
## Null Distribution (One-Sided)
When performing a one-sided hypothesis test, such as $H_0: \mu \leq 5 \mbox{ vs. } H_1: \mu > 5$, the null distribution is typically calculated under the "least favorable" value, which is the boundary value.
In this example it would be $\mu=5$ and we would again utilize the null distribution
$$Z = \frac{\overline{X} - 5}{\sigma/\sqrt{n}} \sim \mbox{Normal}(0,1).$$
## P-values
The **p-value** is defined to be the probability that a test statistic from the null distribution is *as or more extreme than the observed statistic*. In our Normal example on the two-sided hypothesis test, the p-value is
$${\rm Pr}(|Z^*| \geq |z|)$$
where $Z^* \sim \mbox{Normal}(0,1)$ and $|z|$ is the value of the test statistic calculated on the data (so it is a fixed number once we observe the data).
## Calling a Test "Significant"
A hypothesis test is called **statistically significant** --- meaning we reject $H_0$ in favor of $H_1$ --- if its p-value is sufficiently small.
Commonly used cut-offs are 0.01 or 0.05, although these are not always appropriate and they are historical artifacts.
Applying a specific p-value cut-off to determine significance determines an error rate, which we define next.
## Types of Errors
There are two types of errors that can be committed when performing a hypothesis test.
1. A **Type I error** or **false positive** is when a hypothesis test is called signficant and the null hypothesis is actually true.
2. A **Type II error** or **false negative** is when a hypothesis test is not called signficant and the alternative hypothesis is actually true.
## Error Rates
- The **Type I error rate** or **false positive rate** is the probability of this type of error given that $H_0$ is true.
- If a hypothesis test is called significant when p-value $\leq \alpha$ then it has a Type I error rate equal to $\alpha$.
- The **Type II error rate** or **false negative rate** is the probability of this type of error given that $H_1$ is true.
- The **power** of a hypothesis test is $1 -$ Type II error rate.
Hypothesis tests are usually derived with a goal to control the Type I error rate while maximizing the power.
# Maximum Likelihood Estimation
## The Normal Example
We formulated both confidence intervals and hypothesis tests under the following idealized scenario:
> Suppose a simple random sample of $n$ data points is collected so that the following model of the data is reasonable: $X_1, X_2, \ldots, X_n$ are iid Normal($\mu$, $\sigma^2$).
> The goal is to do inference on $\mu$, the population mean.
> For simplicity, assume that $\sigma^2$ is known (e.g., $\sigma^2 = 1$).
There is a good reason why we did this.
## MLE $\rightarrow$ Normal Pivotal Statistics
The random variable distributions we introduced last week have maximum likelihood estimators (MLEs) that can be standardized to yield a pivotal statistic with a Normal(0,1) distribution based on MLE theory.
For example, if $X \sim \mbox{Binomial}(n,p)$ then $\hat{p}=X/n$ is the MLE. For large $n$ it approximately holds that
$$
\frac{\hat{p} - p}{\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}} \sim \mbox{Normal}(0,1).
$$
## Likelihood Function
Suppose that we observe $x_1, x_2, \ldots, x_n$ according to the model $X_1, X_2, \ldots, X_n \sim F_{\theta}$.
The joint pdf is $f(\boldsymbol{x} ; \theta)$. We view the pdf as being a function of $\boldsymbol{x}$ for a fixed $\theta$.
The **likelihood function** is obtained by reversing the arguments and viewing this as a function of $\theta$ for a fixed, observed $\boldsymbol{x}$:
$$L(\theta ; \boldsymbol{x}) = f(\boldsymbol{x} ; \theta).$$
## Log-Likelihood Function
The log-likelihood function is
$$ \ell(\theta ; \boldsymbol{x}) = \log L(\theta ; \boldsymbol{x}).$$
When the data are iid, we have
$$ \ell(\theta ; \boldsymbol{x}) = \log \prod_{i=1}^n f(x_i ; \theta) = \sum_{i=1}^n \log f(x_i ; \theta).$$
## Calculating MLEs
The **maximum likelihood estimate** is the value of $\theta$ that maximizes $L(\theta ; \boldsymbol{x})$ for an observe data set $\boldsymbol{x}$.
\begin{align*}
\hat{\theta}_{{\rm MLE}} & = \operatorname{argmax}_{\theta} L(\theta ; \boldsymbol{x}) \\
& = \operatorname{argmax}_{\theta} \ell (\theta ; \boldsymbol{x}) \\
& = \operatorname{argmax}_{\theta} L (\theta ; T(\boldsymbol{x}))
\end{align*}
where the last equality holds for sufficient statistics $T(\boldsymbol{x})$.
The MLE can usually be calculated analytically or numerically.
## Properties
When "certain regularity assumptions" are true, the following properties hold for MLEs.
- Consistent
- Equivariant
- Asymptotically Normal
- Asymptotically Efficient (or Optimal)
- Approximate Bayes Estimator
## Assumptions and Notation
We will assume that $X_1, X_2, \ldots, X_n \stackrel{{\rm iid}}{\sim} F_{\theta}$ and let $\hat{\theta}_n$ be the MLE of $\theta$ based on the $n$ observations.
The only exception is for the Binomial distribution where we will assume that $X \sim \mbox{Binomial}(n, p)$, which is the sum of $n$ iid $\mbox{Bernoulli}(p)$ rv's.
We will assume that the "certain regularity assumptions" are true in the following results.
## Consistency
An estimator is consistent if it converges in probability to the true parameter value. MLEs are consistent so that as $n \rightarrow \infty$,
$$\hat{\theta}_n \stackrel{P}{\rightarrow} \theta,$$
where $\theta$ is the true value.
## Equivariance
\
If $\hat{\theta}_n$ is the MLE of $\theta$, then $g\left(\hat{\theta}_n\right)$ is the MLE of $g(\theta)$.
\
Example: For the Normal$(\mu, \sigma^2)$ the MLE of $\mu$ is $\overline{X}$. Therefore, the MLE of $e^\mu$ is $e^{\overline{X}}$.
## Fisher Information
The **Fisher Information** of $X_1, X_2, \ldots, X_n \stackrel{{\rm iid}}{\sim} F_{\theta}$ is:
\
\begin{align*}
I_n(\theta) & = \operatorname{Var}\left( \frac{d}{d\theta} \log f(\boldsymbol{X}; \theta) \right)
= \sum_{i=1}^n \operatorname{Var}\left( \frac{d}{d\theta} \log f(X_i; \theta) \right) \\
& = - \operatorname{E}\left( \frac{d^2}{d\theta^2} \log f(\boldsymbol{X}; \theta) \right)
= - \sum_{i=1}^n \operatorname{E}\left( \frac{d^2}{d\theta^2} \log f(X_i; \theta) \right)
\end{align*}
## Standard Error
In general, the **standard error** of the standard deviation of sampling distribution of an estimate or statistic.
For MLEs, the standard error is $\sqrt{\V\left(\hat{\theta}_n\right)}$. It has the approximation
$$\operatorname{se}\left(\hat{\theta}_n\right) \approx \frac{1}{\sqrt{I_n(\theta)}}$$
and the standard error estimate is
$$\hat{\operatorname{se}}\left(\hat{\theta}_n\right) = \frac{1}{\sqrt{I_n\left(\hat{\theta}_n\right)}}.$$
## Asymptotic Normal
MLEs converge in distribution to the Normal distribution. Specifically, as $n \rightarrow \infty$,
$$\frac{\hat{\theta}_n - \theta}{\se\left(\hat{\theta}_n\right)} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1)$$
and
$$\frac{\hat{\theta}_n - \theta}{\hat{\se}\left(\hat{\theta}_n\right)} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).$$
## Asymptotic Pivotal Statistic
By the previous result, we now have an approximate (asymptotic) pivotal statistic:
$$Z = \frac{\hat{\theta}_n - \theta}{\hat{\se}\left(\hat{\theta}_n\right)} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).$$
This allows us to construct approximate confidence intervals and hypothesis test as in the idealized $\mbox{Normal}(\mu, \sigma^2)$ (with $\sigma^2$ known) scenario from the previous sections.
## Wald Test
Consider the hypothesis test, $H_0: \theta=\theta_0$ vs $H_1: \theta \not= \theta_0$. We form test statistic
$$z = \frac{\hat{\theta}_n - \theta_0}{\hat{\se}\left(\hat{\theta}_n\right)},$$
which has approximate p-value
$$\mbox{p-value} = {\rm Pr}(|Z^*| \geq |z|),$$
where $Z^*$ is a Normal$(0,1)$ random variable.
## Confidence Intervals
Using this MLE theory, we can form approximate $(1-\alpha)$ level confidence intervals as follows.
Two-sided:
$$\left(\hat{\theta}_n - |z_{\alpha/2}| \hat{\se}\left(\hat{\theta}_n\right), \hat{\theta}_n + |z_{\alpha/2}| \hat{\se}\left(\hat{\theta}_n\right)\right)$$
Upper:
$$\left(-\infty, \hat{\theta}_n + |z_{\alpha}| \hat{\se}\left(\hat{\theta}_n\right)\right)$$
Lower:
$$\left(\hat{\theta}_n - |z_{\alpha}| \hat{\se}\left(\hat{\theta}_n\right), \infty\right)$$
## Optimality
The MLE is such that
$$ \sqrt{n} \left( \hat{\theta}_n - \theta \right) \stackrel{D}{\longrightarrow} \mbox{Normal}(0, \tau^2)$$
for some $\tau^2$. Suppose that $\tilde{\theta}_n$ is any other estimate so that
$$ \sqrt{n} \left( \tilde{\theta}_n - \theta \right) \stackrel{D}{\longrightarrow} \mbox{Normal}(0, \gamma^2).$$
It follows that
$$\frac{\tau^2}{\gamma^2} \leq 1.$$
## Delta Method
Suppose that $g()$ is a differentiable function and $g'(\theta) \not= 0$. Note that for some $t$ in a neighborhood of $\theta$, a first-order Taylor expansion tells us that $g(t) \approx g'(\theta) (t - \theta)$. From this we know that
$$\V\left(g(\hat{\theta}_n) \right) \approx g'(\theta)^2 \V(\hat{\theta}_n)$$
The delta method shows that $\hat{\se}\left(g(\hat{\theta}_n)\right) = |g'(\hat{\theta}_n)| \hat{\se}\left(\hat{\theta}_n\right)$ and
$$\frac{g(\hat{\theta}_n) - g(\theta)}{|g'(\hat{\theta}_n)| \hat{\se}\left(\hat{\theta}_n\right)} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).$$
## Delta Method Example
Suppose $X \sim \mbox{Binomial}(n,p)$ which has MLE, $\hat{p} = X/n$. By the equivariance property, the MLE of the per-trial variance $p(1-p)$ is $\hat{p}(1-\hat{p})$. It can be calculated that $\hat{\se}(\hat{p}) = \sqrt{\hat{p}(1-\hat{p})/n}$.
Let $g(p) = p(1-p)$. Then $g'(p) = 1-2p$. By the delta method,
$$\hat{\se}\left( \hat{p}(1-\hat{p}) \right) = \left| (1-2\hat{p}) \right| \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}.$$
## Multiparameter Fisher Info Matrix
Suppose that $X_1, X_2, \ldots, X_n \stackrel{{\rm iid}}{\sim} F_{\boldsymbol{\theta}}$ where $\boldsymbol{\theta} = (\theta_1, \theta_2, \ldots, \theta_d)^T$ has MLE $\hat{\boldsymbol{\theta}}_n$.
The Fisher Information Matrix $I_n(\boldsymbol{\theta})$ is the $d \times d$ matrix with $(i, j)$ entry
$$ -\sum_{k=1}^n \operatorname{E}\left( \frac{\partial^2}{\partial\theta_i \partial\theta_j} \log f(X_k; \boldsymbol{\theta}) \right).$$
## Multiparameter Asymptotic MVN
Under appropriate regularity conditions, as $n \rightarrow \infty$,
\
$$
\left( \hat{\boldsymbol{\theta}}_n - \boldsymbol{\theta} \right) \stackrel{D}{\longrightarrow} \mbox{MVN}_d \left( \boldsymbol{0}, I_n(\boldsymbol{\theta})^{-1} \right) \mbox{ and }
$$
\
$$
\left( \hat{\boldsymbol{\theta}}_n - \boldsymbol{\theta} \right)^T I_n(\hat{\boldsymbol{\theta}}_n) \left( \hat{\boldsymbol{\theta}}_n - \boldsymbol{\theta} \right) \stackrel{D}{\longrightarrow} \mbox{MVN}_d \left( \boldsymbol{0}, \boldsymbol{I} \right).
$$
# MLE Examples: One Sample
## Exponential Family Distributions
Suppose $X_1, X_2, \ldots, X_n$ are iid from some EFD. Then,
$$
\frac{\partial}{\partial \eta_k} \ell(\boldsymbol{\eta} ; \boldsymbol{x}) = \sum_{i=1}^n T_k(x_i) - n \frac{\partial}{\partial \eta_k} A(\boldsymbol{\eta})
$$
Setting the second equation to 0, it follows that the MLE of $\eta_k$ is the solution to
$$
\frac{1}{n} \sum_{i=1}^n T_k(x_i) = \frac{\partial}{\partial \eta_k} A(\boldsymbol{\eta}).
$$
where note that $\frac{\partial}{\partial \eta_k} A(\boldsymbol{\eta}) = \E[T_k(X)]$.
## Summary of MLE Statistics
In all of these scenarios, $Z$ converges in distribution to Normal$(0,1)$ for large $n$.
| Distribution | MLE | Std Err | $Z$ Statistic |
| ------------ | :---------: | :-------: | :-------------: |
| Binomial$(n,p)$ | $\hat{p} = X/n$ | $\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}$ | $\frac{\hat{p} - p}{\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}}$ |
| Normal$(\mu, \sigma^2)$ | $\hat{\mu} = \overline{X}$ | $\frac{\hat{\sigma}}{\sqrt{n}}$ | $\frac{\hat{\mu} - \mu}{\hat{\sigma}/\sqrt{n}}$ |
| Poisson$(\lambda)$ | $\hat{\lambda} = \overline{X}$ | $\sqrt{\frac{\hat{\lambda}}{n}}$ | $\frac{\hat{\lambda} - \lambda}{\sqrt{\hat{\lambda}/n}}$ |
## Notes
- For the Normal and Poisson distributions, our model is $X_1, X_2, \ldots, X_n$ iid from each respective distribution
- For the Binomial distribution, our model is $X \sim \mbox{Binomial}(n, p)$
- In the Normal model, $\hat{\sigma} = \sqrt{\frac{\sum_{i=1}^n (X_i - \overline{X})^2}{n}}$ is the MLE of $\sigma$
- The above formulas were given in terms of the random variable probability models; on observed data the same formulas are used except we observed data lower case letters, e.g., replace $\overline{X}$ with $\overline{x}$
## Binomial
Approximate $(1-\alpha)$-level two-sided CI:
$$\left(\hat{p} - |z_{\alpha/2}| \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}, \hat{p} + |z_{\alpha/2}| \sqrt{\frac{\hat{p}(1-\hat{p})}{n}} \right)$$
Hypothesis test, $H_0: p=p_0$ vs $H_1: p \not= p_0$:
$$z = \frac{\hat{p} - p_0}{\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}} \mbox{ and } \mbox{p-value} = {\rm Pr}(|Z^*| \geq |z|)$$
where $Z^*$ is a Normal$(0,1)$ random variable.
## Normal
Approximate $(1-\alpha)$-level two-sided CI:
$$\left(\hat{\mu} - |z_{\alpha/2}| \frac{\hat{\sigma}}{\sqrt{n}}, \hat{\mu} + |z_{\alpha/2}| \frac{\hat{\sigma}}{\sqrt{n}} \right)$$
Hypothesis test, $H_0: \mu=\mu_0$ vs $H_1: \mu \not= \mu_0$:
$$z = \frac{\hat{\mu} - \mu_0}{\hat{\sigma}/\sqrt{n}} \mbox{ and } \mbox{p-value} = {\rm Pr}(|Z^*| \geq |z|)$$
where $Z^*$ is a Normal$(0,1)$ random variable.
## Poisson
Approximate $(1-\alpha)$-level two-sided CI:
$$\left(\hat{\lambda} - |z_{\alpha/2}| \sqrt{\frac{\hat{\lambda}}{n}}, \hat{\lambda} + |z_{\alpha/2}| \sqrt{\frac{\hat{\lambda}}{n}} \right)$$
Hypothesis test, $H_0: \lambda=\lambda_0$ vs $H_1: \lambda \not= \lambda_0$:
$$z = \frac{\hat{\lambda} - \lambda_0}{\sqrt{\frac{\hat{\lambda}}{n}}} \mbox{ and } \mbox{p-value} = {\rm Pr}(|Z^*| \geq |z|)$$
where $Z^*$ is a Normal$(0,1)$ random variable.
## One-Sided CIs and HTs
The one-sided versions of these approximate confidence intervals and hypothesis tests work analogously.
The procedures shown for the $\mbox{Normal}(\mu, \sigma^2)$ case with known $\sigma^2$ from last week are utilzied with the appropriate subsitutions as in the above examples.
# MLE Examples: Two-Samples
## Comparing Two Populations
So far we have concentrated on analyzing $n$ observations from a single population.
However, suppose that we want to do inference to compare two populations?
The framework we have described so far is easily extended to accommodate this.
## Two RVs
If $X$ and $Y$ are independent rv's then:
$${\rm E}[X - Y] = {\rm E}[X] - {\rm E}[Y]$$
$${\rm Var}(X-Y) = {\rm Var}(X) + {\rm Var}(Y)$$
## Two Sample Means
Let $X_1, X_2, \ldots, X_{n_1}$ be iid rv's with population mean $\mu_1$ and population variance $\sigma^2_1$.
Let $Y_1, Y_2, \ldots, Y_{n_2}$ be iid rv's with population mean $\mu_2$ and population variance $\sigma^2_2$.
Assume that the two sets of rv's are independent. Then when the CLT applies to each set of rv's, as $\min(n_1, n_2) \rightarrow \infty$, it follows that
$$ \frac{\overline{X} - \overline{Y} - (\mu_1 - \mu_2)}{\sqrt{\frac{\sigma^2_1}{n_1} + \frac{\sigma^2_2}{n_2}}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1)$$
## Two MLEs
Suppose $X_1, X_2, \ldots, X_n \stackrel{{\rm iid}}{\sim} F_{\theta}$ and $Y_1, Y_2, \ldots, Y_m \stackrel{{\rm iid}}{\sim} F_{\gamma}$ with MLEs $\hat{\theta}_n$ and $\hat{\gamma}_m$, respectively. Then as $\min(n, m) \rightarrow \infty$,
$$\frac{\hat{\theta}_n - \hat{\gamma}_m - (\theta - \gamma)}{\sqrt{\hat{\se}(\hat{\theta}_n)^2 + \hat{\se}(\hat{\gamma}_m)^2}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).$$
## Poisson
Let $X_1, X_2, \ldots, X_{n_1}$ be iid $\mbox{Poisson}(\lambda_1)$ and $Y_1, Y_2, \ldots, Y_{n_2}$ be iid $\mbox{Poisson}(\lambda_2)$.
We have $\hat{\lambda}_1 = \overline{X}$ and $\hat{\lambda}_2 = \overline{Y}$. As $\min(n_1, n_2) \rightarrow \infty$,
$$
\frac{\hat{\lambda}_1 - \hat{\lambda}_2 - (\lambda_1 - \lambda_2)}{\sqrt{\frac{\hat{\lambda}_1}{n_1} + \frac{\hat{\lambda}_2}{n_2}}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).
$$
## Normal (Unequal Variances)
Let $X_1, X_2, \ldots, X_{n_1}$ be iid $\mbox{Normal}(\mu_1, \sigma^2_1)$ and $Y_1, Y_2, \ldots, Y_{n_2}$ be iid $\mbox{Normal}(\mu_2, \sigma^2_2)$.
We have $\hat{\mu}_1 = \overline{X}$ and $\hat{\mu}_2 = \overline{Y}$. As $\min(n_1, n_2) \rightarrow \infty$,
$$
\frac{\hat{\mu}_1 - \hat{\mu}_2 - (\mu_1 - \mu_2)}{\sqrt{\frac{\hat{\sigma}^2_1}{n_1} + \frac{\hat{\sigma}^2_2}{n_2}}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).
$$
## Normal (Equal Variances)
Let $X_1, X_2, \ldots, X_{n_1}$ be iid $\mbox{Normal}(\mu_1, \sigma^2)$ and $Y_1, Y_2, \ldots, Y_{n_2}$ be iid $\mbox{Normal}(\mu_2, \sigma^2)$.
We have $\hat{\mu}_1 = \overline{X}$ and $\hat{\mu}_2 = \overline{Y}$. As $\min(n_1, n_2) \rightarrow \infty$,
$$
\frac{\hat{\mu}_1 - \hat{\mu}_2 - (\mu_1 - \mu_2)}{\sqrt{\frac{\hat{\sigma}^2}{n_1} + \frac{\hat{\sigma}^2}{n_2}}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1)
$$
where
$$
\hat{\sigma}^2 = \frac{\sum_{i=1}^{n_1}(X_i - \overline{X})^2 + \sum_{i=1}^{n_2}(Y_i - \overline{Y})^2}{n_1 + n_2}
$$
## Binomial
Let $X \sim \mbox{Binomial}(n_1, p_1)$ and $Y \sim \mbox{Binomial}(n_2, p_2)$.
We have $\hat{p}_1 = X/n_1$ and $\hat{p}_2 = Y/n_2$. As $\min(n_1, n_2) \rightarrow \infty$,
$$
\frac{\hat{p}_1 - \hat{p}_2 - (p_1 - p_2)}{\sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}} \stackrel{D}{\longrightarrow} \mbox{Normal}(0,1).
$$
## Example: Binomial CI
A 95% CI for the difference $p_1 - p_2$ can be obtained by unfolding the above pivotal statistic:
$$\left((\hat{p}_1 - \hat{p}_2) - 1.96 \sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}} \right.,$$
$$\left. (\hat{p}_1 - \hat{p}_2) + 1.96 \sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}} \right)$$
## Example: Binomial HT
Suppose we wish to test $H_0: p_1 = p_2$ vs $H_1: p_1 \not= p_2$.
First form the $z$-statistic:
$$
z = \frac{\hat{p}_1 - \hat{p}_2 }{\sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}}.
$$
Now, calculate the p-value:
$$
{\rm Pr}(|Z^*| \geq |z|)
$$
where $Z^*$ is a Normal(0,1) random variable.
# The *t* Distribution
## Normal, Unknown Variance
Suppose a sample of $n$ data points is modeled by $X_1, X_2, \ldots, X_n \stackrel{{\rm iid}}{\sim} \mbox{Normal}(\mu, \sigma^2)$ where $\sigma^2$ is *unknown*. Recall that $S = \sqrt{\frac{\sum_{i=1}^n (X_i - \overline{X})^2}{n-1}}$ is the sample standard deviation.
The statistic
$$\frac{\overline{X} - \mu}{S/\sqrt{n}}$$
has a $t_{n-1}$ distribution, a *t*-distribution with $n-1$ degrees of freedom.
## Aside: Chi-Square Distribution
Suppose $Z_1, Z_2, \ldots, Z_v \stackrel{{\rm iid}}{\sim} \mbox{Normal}(0, 1)$. Then $Z_1^2 + Z_2^2 + \cdots + Z_v^2$ has a $\chi^2_v$ distribution, where $v$ is the degrees of freedom.
This $\chi^2_v$ rv has a [pdf](https://en.wikipedia.org/wiki/Chi-squared_distribution#Probability_density_function), expected value equal to $v$, and variance equal to $2v$.
Also,
$$\frac{(n-1) S^2}{\sigma^2} \sim \chi^2_{n-1}.$$
## Theoretical Basis of the *t*
Suppose that $Z \sim \mbox{Normal}(0,1)$, $X \sim \chi^2_v$, and $Z$ and $X$ are independent. Then $\frac{Z}{\sqrt{X/v}}$ has a $t_v$ distribution.
Since $\frac{\overline{X} - \mu}{\sigma/\sqrt{n}} \sim \mbox{Normal}(0,1)$ and $\overline{X}$ and $S^2$ are independent (shown later), it follows that the following has a $t_{n-1}$ distribution:
$$\frac{\overline{X} - \mu}{S/\sqrt{n}}.$$
## When Is *t* Utilized?
- The *t* distribution and its corresponding CI's and HT's are utilized when the data are Normal (or approximately Normal) and $n$ is small
- Small typically means that $n < 30$
- In this case the inference based on the *t* distribution will be more accurate
- When $n \geq 30$, there is very little difference between using $t$-statistics and $z$-statistics
## *t* vs Normal
```{r, echo=FALSE, fig.height=6.5, fig.width=9}
x <- seq(-3,3,0.01)
k <- length(x)
y <- c(dt(x, df=4), dt(x, df=30), dnorm(x))
df <- data.frame(x=c(x, x, x), y=y, distribution=c(rep("t, df=4", k), rep("t, df=30", k), rep("Normal", k)))
ggplot(data=df) +
geom_line(aes(x=x, y=y, color=distribution), size=1.2) +
labs(x="x", y="f(x)")
```
## *t* Percentiles
We calculated percentiles of the Normal(0,1) distribution (e.g., $z_\alpha$). We can do the analogous calculation with the *t* distribution.
Let $t_\alpha$ be the $\alpha$ percentile of the *t* distribution. Examples:
```{r}
qt(0.025, df=4) # alpha = 0.025
qt(0.05, df=4)
qt(0.95, df=4)
qt(0.975, df=4)
```
## Confidence Intervals
Here is a $(1-\alpha)$-level CI for $\mu$ using this distribution:
$$
\left(\hat{\mu} - |t_{\alpha/2}| \frac{s}{\sqrt{n}}, \hat{\mu} + |t_{\alpha/2}| \frac{s}{\sqrt{n}} \right),
$$
where as before $\hat{\mu} = \overline{x}$. This produces a wider CI than the $z$ statistic analogue.
## Hypothesis Tests
Suppose we want to test $H_0: \mu = \mu_0$ vs $H_1: \mu \not= \mu_0$ where $\mu_0$ is a known, given number.
The *t*-statistic is
$$
t = \frac{\hat{\mu} - \mu_0}{\frac{s}{\sqrt{n}}}
$$
with p-value
$$
{\rm Pr}(|T^*| \geq |t|)
$$
where $T^* \sim t_{n-1}$.
## Two-Sample *t*-Distribution
Let $X_1, X_2, \ldots, X_{n_1} \stackrel{{\rm iid}}{\sim} \mbox{Normal}(\mu_1, \sigma^2_1)$ and $Y_1, Y_2, \ldots, Y_{n_2} \stackrel{{\rm iid}}{\sim} \mbox{Normal}(\mu_2, \sigma^2_2)$ have unequal variances.
We have $\hat{\mu}_1 = \overline{X}$ and $\hat{\mu}_2 = \overline{Y}$. The unequal variance two-sample t-statistic is
$$
t = \frac{\hat{\mu}_1 - \hat{\mu}_2 - (\mu_1 - \mu_2)}{\sqrt{\frac{S^2_1}{n_1} + \frac{S^2_2}{n_2}}}.
$$
## Two-Sample *t*-Distribution
Let $X_1, X_2, \ldots, X_{n_1} \stackrel{{\rm iid}}{\sim} \mbox{Normal}(\mu_1, \sigma^2)$ and $Y_1, Y_2, \ldots, Y_{n_2} \stackrel{{\rm iid}}{\sim} \mbox{Normal}(\mu_2, \sigma^2)$ have equal variance.
We have $\hat{\mu}_1 = \overline{X}$ and $\hat{\mu}_2 = \overline{Y}$. The equal variance two-sample t-statistic is
$$
t = \frac{\hat{\mu}_1 - \hat{\mu}_2 - (\mu_1 - \mu_2)}{\sqrt{\frac{S^2}{n_1} + \frac{S^2}{n_2}}}.
$$
where
$$
S^2 = \frac{\sum_{i=1}^{n_1}(X_i - \overline{X})^2 + \sum_{i=1}^{n_2}(Y_i - \overline{Y})^2}{n_1 + n_2 - 2}.
$$
## Two-Sample *t*-Distributions
When the two populations have equal variances, the pivotal *t*-statistic follows a $t_{n_1 + n_2 -2}$ distribution.
When there are unequal variances, the pivotal *t*-statistic follows a *t* distribution where the degrees of freedom comes from an approximation using the Welch–Satterthwaite equation (which R calculates).
# Inference in R
## `BSDA` Package
```{r, eval=FALSE}
install.packages("BSDA")
```
```{r, message=FALSE}
library(BSDA)
str(z.test)
```
## Example: Poisson
Apply `z.test()`:
```{r, display=FALSE}
set.seed(210)
```
```{r}
n <- 40
lam <- 14
x <- rpois(n=n, lambda=lam)
lam.hat <- mean(x)
stddev <- sqrt(lam.hat)
z.test(x=x, sigma.x=stddev, mu=lam)
```
## Direct Calculations
Confidence interval:
```{r}
lam.hat <- mean(x)
lam.hat
stderr <- sqrt(lam.hat)/sqrt(n)
lam.hat - abs(qnorm(0.025)) * stderr # lower bound
lam.hat + abs(qnorm(0.025)) * stderr # upper bound
```
Hypothesis test:
```{r}
z <- (lam.hat - lam)/stderr
z # test statistic