forked from vasishth/ESSLLI2015Vasishth_Week1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture1.Rnw
More file actions
1086 lines (722 loc) · 27.4 KB
/
lecture1.Rnw
File metadata and controls
1086 lines (722 loc) · 27.4 KB
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
\documentclass[slidestop,compress,mathserif,red]{beamer}
%\documentclass[handout]{beamer}
%\usepackage{beamerthemesplit}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{gb4e}
\usepackage{qtree}
\usepackage{hyperref}
\usepackage{ulem}
\usepackage{amsmath,amssymb,amsfonts}
\setbeamerfont{page number in head/foot}{size=\large}
\setbeamertemplate{footline}[frame number]
%\setbeamertemplate{footline}%
%{%
%\hfill\insertpagenumber\ of \ref{TotPages}\hspace{.5cm}\vspace{.5cm}
%\hfill\insertpagenumber\ of 28\hspace{.5cm}\vspace{.5cm}
%}%
\mode<presentation>
{
%\usetheme{Singapore}
%\usetheme{Berlin}
%\setbeamercovered{transparent}
}
%\mode<handout>
%{
%\usepackage{pgfpages}
%\pgfpagesuselayout{4 on 1}[a4paper,landscape,border shrink=5mm]
%}
\usetheme{Antibes}
%\usecolortheme{beetle}
%\usecolortheme{seagull}
\usecolortheme{lily}
\title[Lecture 1]{Statistical methods for linguistic research: Foundational Ideas}
\author{Shravan Vasishth}
\institute{Universit\"at Potsdam\\
vasishth@uni-potsdam.de\\
http://www.ling.uni-potsdam.de/$\sim$vasishth
}
\date{\today}
\begin{document}
\maketitle
<<setup,include=FALSE,cache=FALSE>>=
library(knitr)
library(coda)
# set global chunk options, put figures into folder
options(replace.assign=TRUE,show.signif.stars=FALSE)
opts_chunk$set(fig.path='figures/figure-', fig.align='center', fig.show='hold')
options(replace.assign=TRUE,width=75)
opts_chunk$set(dev='postscript')
#library(rstan)
#set.seed(9991)
# save workspace image, if you want
#the.date <- format(Sys.time(), "%b%d%Y")
#save.image(file=paste0("homework01-",the.date,".RData")
@
\begin{frame}\frametitle{What this course is about}
\begin{enumerate}
\item In this course, I aim to provide a first encounter with the foundational ideas in frequentist statistical theory.
\item We will cover what I consider the absolute minimum you need to know before you do any data analysis.
\item We will mainly use simulation to understand the key concepts, and this requires some knowledge of the language R.
\item So download and install R from:
http://cran.r-project.org/
\item Why R? Why not Excel, or SPSS? R is free, and is more powerful than any of these alternatives.
\end{enumerate}
\end{frame}
\begin{frame}\frametitle{Why I prepared this course}
\begin{enumerate}
\item
A poor understanding of statistical theory has been causing a lot of confusion in psychology, linguistics, and other areas.
\item
My hope is that, with a little study, we can stop making the same mistakes over and over again.
\end{enumerate}
\end{frame}
\begin{frame}\frametitle{Why I prepared this course}
\begin{enumerate}
\item The meaning of the p-value is widely misunderstood, and the p-value is grossly abused, leading to invalid claims.
\item Even experienced scientists can't give a correct definition of some basic concepts in statistics.
\item There is a very strong tendency to use statistical models as a black box for delivering p-values to make decisions about ``significance''.
\item Beginning users of statistics look for ``guidelines'' from experts; but guidelines usually distort the details.
\item There is no substitute for judgement tempered with understanding; no one-size-fits-all solution.
\end{enumerate}
\end{frame}
\begin{frame}\frametitle{Why I prepared this course}
At a minimum, I hope that you will realize that the following binary decision strategy is not the purpose of statistical data analysis:
\begin{enumerate}
\item
Get $p<0.05$ $\rightarrow$ decide that your favored hypothesis is true
\item
Get $p>0.05$ $\rightarrow$ decide that the null hypothesis is true
\end{enumerate}
\end{frame}
\begin{frame}\frametitle{Prerequisites for this course}
\begin{enumerate}
\item
For this course, I only assume that you are willing to put in some work on your own.
\item
This means reading the lecture notes, and doing the homework.
\item
A certain amount of fearlessness is also assumed.
\item
Only pre-calculus math and high school algebra is assumed, but if you know some calculus (perhaps a passive knowledge?), you will get more out of this course.
\end{enumerate}
\end{frame}
\begin{frame}\frametitle{Course lecture notes (optional during ESSLLI)}
I have two sets of notes. Choose the one you like more.
\begin{enumerate}
\item A less technical, more intuitive presentation:
https://github.com/vasishth/Statistics-lecture-notes-Potsdam/tree/master/IntroductoryStatistics
\item A more technical presentation assuming basic calculus and linear algebra:
https://github.com/vasishth/LM
\item During ESSLLI, it is enough to just follow my slides. Read the notes later, after you go home!
\end{enumerate}
I suggest reading the first set of notes and then the second set.
\end{frame}
\begin{frame}\frametitle{Self-paced exercises}
You can do short auto-graded exercises each day on the datacamp course page.
Please sign up as a ``team'' member so that we can get some statistics on course participation and completion (we won't track individual identities). Sign up here:
http://bit.ly/1MHfVgw
\end{frame}
\begin{frame}\frametitle{What this course is about}
We will cover the following topics:
\begin{enumerate}
\item Basic R constructs needed for this course. (But please do one of the datacamp intro courses or one of the online R introductions.)
\item Basic probability theory, random variables, including jointly distributed RVs, univariate probability distributions, Maximum Likelihood Estimation.
\item The sampling distribution of the mean, null hypothesis, t-tests, confidence intervals.
\item Type I error, Type II error, power, Type M and Type S errors.
\item An introduction to linear modeling and generalized linear models.
\item An introduction to linear mixed models.
\end{enumerate}
\end{frame}
\section{Basic R constructs}
\begin{frame}[fragile]\frametitle{for-loops}
One construct we will use often is calculating some (varying) quantity repeatedly, and then storing the result of that calculation in a vector.
An example:
<<forloops,cache=TRUE>>=
## number of iterations:
nsim<-10
## vector for storing results:
results<-rep(NA,10)
for(i in 1:nsim){
results[i]<-1+2*i
}
results
@
\end{frame}
\section{Random variables, pdfs, cdfs}
\begin{frame}\frametitle{The definition of a random variable}
A random variable $X$ is a function $X : S \rightarrow \mathbb{R}$ that associates to each outcome
$\omega \in S$ exactly one number $X(\omega) = x$.
$S_X$ is all the $x$'s (all the possible values of X, the support of X). I.e., $x \in S_X$.
\textbf{Discrete example}: number of coin tosses till H
\begin{itemize}
\item $X: \omega \rightarrow x$
\item $\omega$: H, TH, TTH,\dots (infinite)
\item $x=0,1,2,\dots; x \in S_X$
\end{itemize}
We will write $X(\omega)=x$:
$H \rightarrow 1$\newline
$TH \rightarrow 2$\newline
$\vdots$
%\textbf{Continuous example}: fixation durations in reading
%\begin{itemize}
% \item $X: \omega \rightarrow x$
% \item $\omega$: 145.21, 352.43, 270, \dots
% \item $x=145.21, 352.43, 270,\dots; x \in S_X$
%\end{itemize}
\end{frame}
\begin{frame}\frametitle{Probability mass/distribution function}
Every discrete random variable X has associated with it a \textbf{probability mass function (PMF)}. Continuous RVs have \textbf{probability distribution functions} (PDFs). We will call both PDFs (for simplicity).
\begin{equation}
p_X : S_X \rightarrow [0, 1]
\end{equation}
defined by
\begin{equation}
p_X(x) = P(X(\omega) = x), x \in S_X
\end{equation}
This pmf tells us the probability of having getting a heads on 1, 2, \dots tosses.
\end{frame}
\begin{frame}\frametitle{The cumulative distribution function}
The \textbf{cumulative distribution function} in the discrete case is
\begin{equation}
F(a)=\sum_{\hbox{all } x \leq a} p(x)
\end{equation}
%In the continuous case, the cdf is:
%\begin{equation}
%F(a)=\int_{-\infty}^{a} p(x)\, dx
%\end{equation}
The cdf tells us the \textit{cumulative} probability of getting a heads in 1 or less tosses; 2 or less tosses,\dots.
It will soon become clear why we need this.
\end{frame}
\subsection{The binomial random variable}
\begin{frame}\frametitle{Discrete example: The binomial random variable}
Suppose that we toss a coin $n=10$ times. There are two possible outcomes, success and failure, each with probability $\theta$ and $(1-\theta)$ respectively.
Then,
the probability of $x$ successes out of $n$ is defined by the pmf:
\begin{equation}
p_X(x)=P(X=x) = {n \choose x} \theta^x (1-\theta)^{n-x}
\end{equation}
[assuming a binomial distribution]
\end{frame}
\begin{frame}\frametitle{Discrete example: The binomial random variable}
Example: $n=10$ coin tosses. Let the probability of success be $\theta=0.5$.
We start by asking the question:
What's the probability of x or fewer successes, where x is some number between 0 and 10?
Let's compute this.
We use the built-in CDF function \texttt{pbinom}.
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
<<calculatecdfbinomial,include=TRUE,echo=TRUE,cache=TRUE>>=
## sample size
n<-10
## prob of success
p<-0.5
probs<-rep(NA,11)
for(x in 0:10){
## Cumulative Distribution Function:
probs[x+1]<-round(pbinom(x,size=n,prob=p),digits=2)
}
@
<<echo=FALSE>>=
xtabl.probs<-data.frame(x=0:10,prob=probs)
@
We have just computed the cdf of this random variable.
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
% latex table generated in R 3.1.2 by xtable 1.7-4 package
% Wed Jul 15 07:17:07 2015
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
& $P(X\leq x)$ & cumulative probability \\
\hline
1 & 0 & 0.00 \\
2 & 1 & 0.01 \\
3 & 2 & 0.05 \\
4 & 3 & 0.17 \\
5 & 4 & 0.38 \\
6 & 5 & 0.62 \\
7 & 6 & 0.83 \\
8 & 7 & 0.95 \\
9 & 8 & 0.99 \\
10 & 9 & 1.00 \\
11 & 10 & 1.00 \\
\hline
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
<<cdfbinomial,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'>>=
## Plot the CDF:
plot(1:11,probs,xaxt="n",xlab="x",
ylab=expression(P(X<=x)),main="CDF")
axis(1,at=1:11,labels=0:10)
@
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
Another question we can ask involves the pmf:
What is the probability of getting exactly x successes? For example, if x=1, we want P(X=1).
We can get the answer from (a) the cdf, or (b) the pmf:
<<>>=
## using cdf:
pbinom(1,size=10,prob=0.5)-pbinom(0,size=10,prob=0.5)
## using pmf:
choose(10,1) * 0.5 * (1-0.5)^9
@
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
The built-in function in R for the pmf is \texttt{dbinom}:
<<binomcomputePequalszero>>=
## P(X=1)
choose(10,1) * 0.5 * (1-0.5)^9
## using the built-in function:
dbinom(1,size=10,prob=0.5)
@
\end{frame}
\begin{frame}[fragile]\frametitle{Discrete example: The binomial random variable}
<<pdfbinomial,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'>>=
## Plot the pmf:
plot(1:11,dbinom(0:10,size=10,prob=0.5),main="PMF",
xaxt="n",ylab="P(X=x)",xlab="x")
axis(1,at=1:11,labels=0:10)
@
\end{frame}
\begin{frame}\frametitle{Summary: Random variables}
To summarize, a discrete random variable X will be defined by
\begin{enumerate}
\item the function $X: S\rightarrow \mathbb{R}$, where S is the set of outcomes (i.e., outcomes are $\omega \in S$).
\item $X(\omega) = x$, and $S_X$ is the \textbf{support} of X (i.e., $x\in S_X$).
\item A PMF is defined for X:
\begin{equation*}
p_X : S_X \rightarrow [0, 1]
\end{equation*}
\begin{equation}
p_X(x)= {n \choose x} \theta^x (1-\theta)^{n-x}
\end{equation}
\item A CDF is defined for X:
\begin{equation*}
F(a)=\sum_{\hbox{all } x \leq a} p(x)
\end{equation*}
\end{enumerate}
\end{frame}
\subsection{The normal random variable}
\begin{frame}\frametitle{Continuous example: The normal random variable}
The pdf of the normal distribution is:
\begin{equation}
f_{X}(x)=\frac{1}{\sqrt{2\pi \sigma^2}}
e^{-\frac{1}{2}\frac{(x-\mu)^{2}}{\sigma^2}},\quad -\infty < x < \infty
\end{equation}
We write $X\sim \mathtt{norm}(\mathtt{mean}=\mu,\,\mathtt{sd}=\sigma)$.
The associated $\texttt{R}$ function for the pdf is \texttt{dnorm(x, mean = 0, sd = 1)}, and the one for cdf is \texttt{pnorm}.
Note the default values for $\mu$ and $\sigma$ are 0 and 1 respectively. Note also that R defines the PDF in terms of $\mu$ and $\sigma$, not $\mu$ and $\sigma^2$ ($\sigma^2$ is the norm in statistics textbooks).
\end{frame}
\begin{frame}[fragile]\frametitle{Continuous example: The normal RV}
<<pdfnormal,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'>>=
plot(function(x) dnorm(x), -3, 3,
main = "Normal density",ylim=c(0,.4),
ylab="density",xlab="X")
@
\end{frame}
\begin{frame}[fragile]\frametitle{Probability: The area under the curve}
\framesubtitle{Also see shiny app}
<<shadenormal,echo=FALSE>>=
## function for plotting CDF:
plot.prob<-function(x,
x.min,
x.max,
prob,
mean,
sd,
gray.level,main){
plot(x,dnorm(x,mean,sd),
type = "l",xlab="",
ylab="",main=main)
abline(h = 0)
## shade X<x
x1 = seq(x.min, qnorm(prob), abs(prob)/5)
y1 = dnorm(x1, mean, sd)
polygon(c(x1, rev(x1)),
c(rep(0, length(x1)), rev(y1)),
col = gray.level)
}
shadenormal<-
function (prob=0.5,
gray1=gray(0.3),
x.min=-6,
x.max=abs(x.min),
x = seq(x.min, x.max, 0.01),
mean=0,
sd=1,main="P(X<0)")
{
plot.prob(x=x,x.min=x.min,x.max=x.max,
prob=prob,
mean=mean,sd=sd,
gray.level=gray1,main=main)
}
@
<<plotshadenormal,include=TRUE,echo=FALSE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'>>=
shadenormal(prob=0.975,main="P(X<1.96)")
@
\end{frame}
\begin{frame}[fragile]\frametitle{Continuous example: The normal RV}
Computing probabilities using the CDF:
<<computingprobsnormal>>=
## The area under curve between +infty and -infty:
pnorm(Inf)-pnorm(-Inf)
## The area under curve between 2 and -2:
pnorm(2)-pnorm(-2)
## The area under curve between 1 and -1:
pnorm(1)-pnorm(-1)
@
\end{frame}
\begin{frame}[fragile]\frametitle{Finding the quantile given the probability}
We can also go in the other direction: given a probability $p$, we can find the quantile $x$ of a $Normal(\mu,\sigma)$ such that $P(X<x)=p$.
For example:
The quantile $x$ given $X\sim N(\mu=500,\sigma=100)$ such that $P(X<x)=0.975$ is
<<>>=
qnorm(0.975,mean=500,sd=100)
@
This will turn out to be very useful in statistical inference.
\end{frame}
\begin{frame}\frametitle{Standard or unit normal random variable}
If $X$ is normally distributed with parameters $\mu$ and $\sigma$, then $Z=(X-\mu)/\sigma$ is normally distributed with parameters $\mu=0,\sigma = 1$.
We conventionally write $\Phi (x)$ for the CDF of N(0,1):
\begin{equation}
\Phi (x)=\frac{1}{\sqrt{2\pi}} \int_{-\infty}^{x} e^{\frac{-y^2}{2}} \, dy
\quad \textrm{where } y=(x-\mu)/\sigma
\end{equation}
\end{frame}
\begin{frame}[fragile]\frametitle{Standard or unit normal random variable}
For example: $\Phi(2)$:
<<pnormtwo>>=
pnorm(2)
@
For negative $x$ we write:
\begin{equation}
\Phi (-x)= 1- \Phi (x),\quad -\infty < x < \infty
\end{equation}
See the shiny app for a visualization.
\end{frame}
\begin{frame}[fragile]\frametitle{Standard or unit normal random variable}
In R:
<<negativexpnorm>>=
1-pnorm(2)
## alternatively:
pnorm(2,lower.tail=F)
@
\end{frame}
\begin{frame}[fragile]\frametitle{Standard or unit normal random variable}
If $Z$ is a standard normal random variable (SNRV) then
\begin{equation}
p\{ Z\leq -x\} = P\{Z>x\}, \quad -\infty < x < \infty
\end{equation}
Since $Z=((X-\mu)/\sigma)$ is an SNRV whenever $X$ is normally distributed with parameters $\mu$ and $\sigma$, then the CDF of $X$ can be expressed as:
\begin{equation}
F_X(a) = P\{ X\leq a \} = P\left( \frac{X - \mu}{\sigma} \leq \frac{a - \mu}{\sigma}\right) = \Phi\left( \frac{a - \mu}{\sigma} \right)
\end{equation}
The standardized version of a normal
random variable X is used to compute specific probabilities relating to X.
We will soon see the relevance of the SNRV in hypothesis testing.
\end{frame}
\subsection{Summary: dnorm, pnorm, qnorm}
\begin{frame}[fragile]\frametitle{dnorm, pnorm, qnorm}
\begin{enumerate}
\item
For the normal distribution we have built in functions:
\begin{enumerate}
\item dnorm: the pdf
\item pnorm: the cdf
\item qnorm: the inverse of the cdf
\end{enumerate}
\item Other distributions also have analogous functions:
\begin{enumerate}
\item Binomial: dbinom, pbinom, qbinom
\item t-distribution: dt, pt, qt
\end{enumerate}
\end{enumerate}
We will be using the t-distribution's dt, pt, and qt functions a lot in statistical inference.
\end{frame}
\section{Maximum Likelihood Estimation}
\begin{frame}[fragile]\frametitle{Maximum Likelihood Estimation}
We now turn to an important topic: maximum likelihood estimation.
\end{frame}
\subsection{The binomial distribution}
\begin{frame}[fragile]\frametitle{MLE: The binomial distribution}
Suppose we toss a fair coin 10 times, and count the number of heads each time; we repeat this experiment 5 times in all. The observed sample values are $x_1, x_2,\dots, x_5$.
<<>>=
(x<-rbinom(5,size=10,prob=0.5))
@
The joint probability of getting all these values (assuming independence) depends on the parameter we set for the probability $\theta$:
$P(X_1=x_1,X_2=x_2,\dots,X_n=x_n)$
$=f(X_1=x_1,X_2=x_2,\dots,X_n=x_n;\theta)$
\end{frame}
\begin{frame}[fragile]\frametitle{MLE: The binomial distribution}
$P(X_1=x_1,X_2=x_2,\dots,X_n=x_n)$
$= f(X_1=x_1,X_2=x_2,\dots,X_n=x_n;\theta)$
So, the above probability is a function of $\theta$.
When this quantity is expressed as a function of $\theta$, we call it the \textbf{likelihood function}.
\end{frame}
\begin{frame}[fragile]\frametitle{MLE: The binomial distribution}
The value of $\theta$ for which this function has the maximum value is the \textbf{maximum likelihood estimate}.
<<likfun0,echo=TRUE,fig.width=6>>=
## probability parameter fixed at 0.5
theta<-0.5
prod(dbinom(x,size=10,prob=theta))
## probability parameter fixed at 0.1
theta<-0.1
prod(dbinom(x,size=10,prob=theta))
@
\end{frame}
\begin{frame}[fragile]\frametitle{MLE: The binomial distribution}
Let's compute the product for a range of probabilities:
<<likfun,echo=TRUE>>=
theta<-seq(0,1,by=0.01)
store<-rep(NA,length(theta))
for(i in 1:length(theta)){
store[i]<-prod(dbinom(x,size=10,prob=theta[i]))
}
@
\end{frame}
\begin{frame}[fragile]\frametitle{MLE: The binomial distribution}
<<likfunplot,echo=FALSE,fig.height=4>>=
plot(1:length(store),store,xaxt="n",xlab="theta",
ylab="f(x1,...,xn|theta")
axis(1,at=1:length(theta),labels=theta)
@
\end{frame}
\begin{frame}\frametitle{MLE: The binomial distribution}
\framesubtitle{Detailed derivations: see lecture notes}
We can obtain this estimate of $\theta$ that maximizes likelihood by computing:
\begin{equation}
\hat \theta = \frac{x}{n}
\end{equation}
where $n$ is sample size, and $x$ is the number of successes.
\medskip
For the analytical derivation, see the Linear Modeling lecture notes:
https://github.com/vasishth/LM
\end{frame}
\subsection{The normal distribution}
\begin{frame}\frametitle{MLE: The normal distribution}
\framesubtitle{Detailed derivations: see lecture notes}
For the normal distribution, where $X \sim N(\mu,\sigma)$, we can get MLEs of $\mu$ and $\sigma$ by computing:
\begin{equation}
\hat \mu = \frac{1}{n}\sum x_i = \bar{x}
\end{equation}
and
\begin{equation}
\hat \sigma ^2 = \frac{1}{n}\sum (x_i-\bar{x})^2
\end{equation}
you will sometimes see the ``unbiased'' estimate (and this is what R computes) but for large sample sizes the difference is not important:
\begin{equation}
\hat \sigma ^2 = \frac{1}{n-1}\sum (x_i-\bar{x})^2
\end{equation}
\end{frame}
\begin{frame}\frametitle{The significance of the MLE}
The significance of these MLEs is that, having assumed a particular underlying pdf, we can estimate the (unknown) parameters (the mean and variance) of the distribution that generated our particular data.
This leads us to the distributional properties of the mean \textbf{under repeated sampling}.
\end{frame}
\section{The sampling distribution of the mean}
\subsection{Sampling from the normal distribution}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
When we have a \textbf{single sample}, we know how to compute MLEs of the sample mean and standard deviation, $\hat \mu$ and $\hat \sigma$.
Suppose now that you had many repeated samples; from each sample, you can compute the mean each time. We can simulate this situation:
<<>>=
x<-rnorm(100,mean=500,sd=50)
mean(x)
x<-rnorm(100,mean=500,sd=50)
mean(x)
@
\end{frame}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
Let's repeatedly simulate sampling 1000 times:
<<>>=
nsim<-1000
n<-100
mu<-500
sigma<-100
samp_distrn_means<-rep(NA,nsim)
samp_distrn_sd<-rep(NA,nsim)
for(i in 1:nsim){
x<-rnorm(n,mean=mu,sd=sigma)
samp_distrn_means[i]<-mean(x)
samp_distrn_sd[i]<-sd(x)
}
@
\end{frame}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
Plot the distribution of the means under repeated sampling:
<<samplingdistrnmeans,echo=FALSE,fig.height=4>>=
hist(samp_distrn_means,main="Samp. distrn. means",
freq=F,xlab=expression(hat(mu)),ylab="density")
@
\end{frame}
\subsection{Sampling from the exponential distribution}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
Interestingly, it is not necessary that the distribution that we are sampling from be the normal distribution.
<<>>=
for(i in 1:nsim){
x<-rexp(n)
samp_distrn_means[i]<-mean(x)
samp_distrn_sd[i]<-sd(x)
}
@
\end{frame}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
<<samplingdistrnmeansexp,echo=FALSE,fig.height=4>>=
hist(samp_distrn_means,main="Samp. distrn. means",
freq=F,xlab=expression(hat(mu)),ylab="density")
@
\end{frame}
\subsection{The central limit theorem}
\begin{frame}[fragile]\frametitle{The central limit theorem}
\begin{enumerate}
\item
For large enough sample sizes, the sampling distribution of the means will be approximately normal, regardless of the underlying distribution (as long as this distribution has a mean and variance defined for it).
\item
This will be the basis for statistical inference.
\end{enumerate}
\end{frame}
\subsection{Standard error}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
We can compute the standard deviation of the sampling distribution of means:
<<>>=
## estimate from simulation:
sd(samp_distrn_means)
@
\end{frame}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
A further interesting fact is that we can compute this standard deviation of the sampling distribution \textbf{from a single sample} of size $n$:
$\frac{\hat\sigma}{\sqrt{n}}$
<<>>=
x<-rnorm(100,mean=500,sd=100)
hat_sigma<-sd(x)
hat_sigma/sqrt(n)
@
See linear modeling notes on github for an analytical proof.
\end{frame}
\begin{frame}[fragile]\frametitle{The sampling distribution of the mean}
\begin{enumerate}
\item
So, from a sample of size $n$, and sd $\sigma$ or an MLE $\hat\sigma$, we can compute
the standard deviation of the sampling distribution of the means.
\item We will call this standard deviation the estimated \textbf{standard error}.
$SE = \frac{\hat\sigma}{\sqrt{n}}$
I say \textit{estimated} because we are estimating SE using an an estimate of $\sigma$.
\end{enumerate}
\end{frame}
\subsection{Confidence intervals}
\begin{frame}[fragile]\frametitle{Confidence intervals}
The standard error allows us to define a so-called \textbf{95\% confidence interval}:
\begin{equation}
\hat\mu \pm 2 SE
\end{equation}
So, for the mean, we define a 95\% confidence interval as follows:
\begin{equation}
\hat\mu \pm 2 \frac{\hat\sigma}{\sqrt{n}}
\end{equation}
\end{frame}
\begin{frame}[fragile]\frametitle{Confidence intervals}
In our example:
<<confint1>>=
## lower bound:
mu-(2*hat_sigma/sqrt(n))
## upper bound:
mu+(2*hat_sigma/sqrt(n))
@
\end{frame}
\begin{frame}[fragile]\frametitle{The meaning of the 95\% CI}
If you take repeated samples and compute the CI each time, 95\% of those CIs will contain the true population mean.
<<confint2>>=
lower<-rep(NA,nsim)
upper<-rep(NA,nsim)
for(i in 1:nsim){
x<-rnorm(n,mean=mu,sd=sigma)
lower[i]<-mean(x) - 2 * sd(x)/sqrt(n)
upper[i]<-mean(x) + 2 * sd(x)/sqrt(n)
}
@
\end{frame}
\begin{frame}[fragile]\frametitle{The meaning of the 95\% CI}
<<confint3>>=
## check how many CIs contain mu:
CIs<-ifelse(lower<mu & upper>mu,1,0)
table(CIs)
## approx. 95% of the CIs contain true mean:
table(CIs)[2]/sum(table(CIs))
@
\end{frame}
\begin{frame}[fragile]\frametitle{The meaning of the 95\% CI}
<<repeatedCIsplot,echo=FALSE,fig.height=4>>=
se <- function(x)
{
y <- x[!is.na(x)] # remove the missing values, if any
sqrt(var(as.vector(y))/length(y))
}
ci <- function (scores){
m <- mean(scores,na.rm=TRUE)
stderr <- se(scores)
len <- length(scores)
upper <- m + qt(.975, df=len-1) * stderr
lower <- m + qt(.025, df=len-1) * stderr
return(data.frame(lower=lower,upper=upper))