-
Notifications
You must be signed in to change notification settings - Fork 6
/
chapter3Bayes_ex5and6.tex
999 lines (816 loc) · 41.4 KB
/
chapter3Bayes_ex5and6.tex
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
\documentclass[]{tufte-handout}
% ams
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\makeatletter
\@ifpackageloaded{fontspec}{}{\usepackage{fontspec}}
\makeatother
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\makeatletter
\@ifpackageloaded{soul}{
\renewcommand\allcapsspacing[1]{{\addfontfeature{LetterSpace=15}#1}}
\renewcommand\smallcapsspacing[1]{{\addfontfeature{LetterSpace=10}#1}}
}{}
\makeatother
\fi
% graphix
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}
% booktabs
\usepackage{booktabs}
% url
\usepackage{url}
% hyperref
\usepackage{hyperref}
% units.
\usepackage{units}
\setcounter{secnumdepth}{-1}
% citations
\usepackage{natbib}
\bibliographystyle{plainnat}
% pandoc syntax highlighting
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
% longtable
\usepackage{longtable,booktabs}
% multiplecol
\usepackage{multicol}
% strikeout
\usepackage[normalem]{ulem}
% morefloats
\usepackage{morefloats}
% tightlist macro required by pandoc >= 1.14
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
% title / author / date
\title{Tufte Handout}
\author{JJ Allaire and Yihui Xie}
\date{2016-09-05}
\begin{document}
\maketitle
\section{Introduction}\label{introduction}
The Tufte handout style is a style that Edward Tufte uses in his books
and handouts. Tufte's style is known for its extensive use of sidenotes,
tight integration of graphics with text, and well-set typography. This
style has been implemented in LaTeX and HTML/CSS\footnote{See Github
repositories
\href{https://github.com/tufte-latex/tufte-latex}{tufte-latex} and
\href{https://github.com/edwardtufte/tufte-css}{tufte-css}},
respectively. We have ported both implementations into the
\href{https://github.com/rstudio/tufte}{\textbf{tufte} package}. If you
want LaTeX/PDF output, you may use the \texttt{tufte\_handout} format
for handouts, and \texttt{tufte\_book} for books. For HTML output, use
\texttt{tufte\_html}. These formats can be either specified in the YAML
metadata at the beginning of an R Markdown document (see an example
below), or passed to the \texttt{rmarkdown::render()} function. See
\citet{R-rmarkdown} more information about \textbf{rmarkdown}.
\begin{Shaded}
\begin{Highlighting}[]
\OtherTok{---}
\FunctionTok{title:} \StringTok{"An Example Using the Tufte Style"}
\FunctionTok{author:} \StringTok{"John Smith"}
\FunctionTok{output:}
\FunctionTok{tufte:}\NormalTok{:tufte_handout: default}
\FunctionTok{tufte:}\NormalTok{:tufte_html: default}
\OtherTok{---}
\end{Highlighting}
\end{Shaded}
There are two goals of this package:
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\itemsep1pt\parskip0pt\parsep0pt
\item
To produce both PDF and HTML output with similar styles from the same
R Markdown document;
\item
To provide simple syntax to write elements of the Tufte style such as
side notes and margin figures, e.g.~when you want a margin figure, all
you need to do is the chunk option \texttt{fig.margin = TRUE}, and we
will take care of the deails for you, so you never need to think about
\texttt{\textbackslash{}begin\{marginfigure\} \textbackslash{}end\{marginfigure\}}
or
\texttt{\textless{}span class="marginfigure"\textgreater{} \textless{}/span\textgreater{}};
the LaTeX and HTML code under the hood may be complicated, but you
never need to learn or write such code.
\end{enumerate}
If you have any feature requests or find bugs in \textbf{tufte}, please
do not hesitate to file them to
\url{https://github.com/rstudio/tufte/issues}. For general questions,
you may ask them on StackOverflow:
\url{http://stackoverflow.com/tags/rmarkdown}.
\section{Headings}\label{headings}
This style provides first and second-level headings (that is,
\texttt{\#} and \texttt{\#\#}), demonstrated in the next section. You
may get unexpected output if you try to use \texttt{\#\#\#} and smaller
headings.
\newthought{In his later books}\footnote{\href{http://www.edwardtufte.com/tufte/books_be}{Beautiful
Evidence}}, Tufte starts each section with a bit of vertical space, a
non-indented paragraph, and sets the first few words of the sentence in
small caps. To accomplish this using this style, call the
\texttt{newthought()} function in \textbf{tufte} in an \emph{inline R
expression} \texttt{`r `} as demonstrated at the beginning of this
paragraph.\footnote{Note you should not assume \textbf{tufte} has been
attached to your R session. You should either \texttt{library(tufte)}
in your R Markdown document before you call \texttt{newthought()}, or
use \texttt{tufte::newthought()}.}
\section{Figures}\label{figures}
\subsection{Margin Figures}\label{margin-figures}
Images and graphics play an integral role in Tufte's work. To place
figures in the margin you can use the \textbf{knitr} chunk option
\texttt{fig.margin = TRUE}. For example:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(ggplot2)}
\NormalTok{mtcars2 <-}\StringTok{ }\NormalTok{mtcars}
\NormalTok{mtcars2$am <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(}
\NormalTok{mtcars$am, }\DataTypeTok{labels =} \KeywordTok{c}\NormalTok{(}\StringTok{'automatic'}\NormalTok{, }\StringTok{'manual'}\NormalTok{)}
\NormalTok{)}
\KeywordTok{ggplot}\NormalTok{(mtcars2, }\KeywordTok{aes}\NormalTok{(hp, mpg, }\DataTypeTok{color =} \NormalTok{am)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{() +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{() +}
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{'bottom'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{marginfigure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-margin-1} \caption[MPG vs horsepower, colored by transmission]{MPG vs horsepower, colored by transmission.}\label{fig:fig-margin}
\end{marginfigure}
Note the use of the \texttt{fig.cap} chunk option to provide a figure
caption. You can adjust the proportions of figures using the
\texttt{fig.width} and \texttt{fig.height} chunk options. These are
specified in inches, and will be automatically scaled down to fit within
the handout margin.
\subsection{Arbitrary Margin Content}\label{arbitrary-margin-content}
In fact, you can include anything in the margin using the \textbf{knitr}
engine named \texttt{marginfigure}. Unlike R code chunks
\texttt{```\{r\}}, you write a chunk starting with
\texttt{```\{marginfigure\}} instead, then put the content in the chunk.
See an example on the right about the first fundamental theorem of
calculus.
\begin{marginfigure}
We know from \emph{the first fundamental theorem of calculus} that for
\(x\) in \([a, b]\):
\[\frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x).\]
\end{marginfigure}
For the sake of portability between LaTeX and HTML, you should keep the
margin content as simple as possible (syntax-wise) in the
\texttt{marginefigure} blocks. You may use simple Markdown syntax like
\texttt{**bold**} and \texttt{\_italic\_} text, but please refrain from
using footnotes, citations, or block-level elements (e.g.~blockquotes
and lists) there.
\subsection{Full Width Figures}\label{full-width-figures}
You can arrange for figures to span across the entire page by using the
chunk option \texttt{fig.fullwidth = TRUE}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{ggplot}\NormalTok{(diamonds, }\KeywordTok{aes}\NormalTok{(carat, price)) +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{() +}
\StringTok{ }\KeywordTok{facet_grid}\NormalTok{(~}\StringTok{ }\NormalTok{cut)}
\end{Highlighting}
\end{Shaded}
\begin{figure*}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-fullwidth-1} \caption[A full width figure]{A full width figure.}\label{fig:fig-fullwidth}
\end{figure*}
Other chunk options related to figures can still be used, such as
\texttt{fig.width}, \texttt{fig.cap}, \texttt{out.width}, and so on. For
full width figures, usually \texttt{fig.width} is large and
\texttt{fig.height} is small. In the above example, the plot size is
\(10 \times 2\).
\subsection{Main Column Figures}\label{main-column-figures}
Besides margin and full width figures, you can of course also include
figures constrained to the main column. This is the default type of
figures in the LaTeX/HTML output.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{ggplot}\NormalTok{(diamonds, }\KeywordTok{aes}\NormalTok{(cut, price)) +}\StringTok{ }\KeywordTok{geom_boxplot}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{figure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-main-1} \caption[A figure in the main column]{A figure in the main column.}\label{fig:fig-main}
\end{figure}
\section{Sidenotes}\label{sidenotes}
One of the most prominent and distinctive features of this style is the
extensive use of sidenotes. There is a wide margin to provide ample room
for sidenotes and small figures. Any use of a footnote will
automatically be converted to a sidenote. \footnote{This is a sidenote
that was entered using a footnote.}
If you'd like to place ancillary information in the margin without the
sidenote mark (the superscript number), you can use the
\texttt{margin\_note()} function from \textbf{tufte} in an inline R
expression.
\marginnote{This is a margin note. Notice that there is no number preceding the note.}
This function does not process the text with Pandoc, so Markdown syntax
will not work here. If you need to write anything in Markdown syntax,
please use the \texttt{marginfigure} block described previously.
\section{References}\label{references}
References can be displayed as margin notes for HTML output. For
example, we can cite R here \citep{R-base}. To enable this feature, you
must set \texttt{link-citations: yes} in the YAML metadata, and the
version of \texttt{pandoc-citeproc} should be at least 0.7.2. You can
always install your own version of Pandoc from
\url{http://pandoc.org/installing.html} if the version is not
sufficient. To check the version of \texttt{pandoc-citeproc} in your
system, you may run this in R:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{system2}\NormalTok{(}\StringTok{'pandoc-citeproc'}\NormalTok{, }\StringTok{'--version'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
If your version of \texttt{pandoc-citeproc} is too low, or you did not
set \texttt{link-citations: yes} in YAML, references in the HTML output
will be placed at the end of the output document.
\section{Tables}\label{tables}
You can use the \texttt{kable()} function from the \textbf{knitr}
package to format tables that integrate well with the rest of the Tufte
handout style. The table captions are placed in the margin like figures
in the HTML output.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr::}\KeywordTok{kable}\NormalTok{(}
\NormalTok{mtcars[}\DecValTok{1}\NormalTok{:}\DecValTok{6}\NormalTok{, }\DecValTok{1}\NormalTok{:}\DecValTok{6}\NormalTok{], }\DataTypeTok{caption =} \StringTok{'A subset of mtcars.'}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[c]{@{}lrrrrrr@{}}
\caption{A subset of mtcars.}\tabularnewline
\toprule
& mpg & cyl & disp & hp & drat & wt\tabularnewline
\midrule
\endfirsthead
\toprule
& mpg & cyl & disp & hp & drat & wt\tabularnewline
\midrule
\endhead
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\tabularnewline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\tabularnewline
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\tabularnewline
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\tabularnewline
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\tabularnewline
Valiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460\tabularnewline
\bottomrule
\end{longtable}
\section{Block Quotes}\label{block-quotes}
We know from the Markdown syntax that paragraphs that start with
\texttt{\textgreater{}} are converted to block quotes. If you want to
add a right-aligned footer for the quote, you may use the function
\texttt{quote\_footer()} from \textbf{tufte} in an inline R expression.
Here is an example:
\begin{quote}
``If it weren't for my lawyer, I'd still be in prison. It went a lot
faster with two people digging.''
\hfill --- Joe Martin
\end{quote}
Without using \texttt{quote\_footer()}, it looks like this (the second
line is just a normal paragraph):
\begin{quote}
``Great people talk about ideas, average people talk about things, and
small people talk about wine.''
--- Fran Lebowitz
\end{quote}
\section{Responsiveness}\label{responsiveness}
The HTML page is responsive in the sense that when the page width is
smaller than 760px, sidenotes and margin notes will be hidden by
default. For sidenotes, you can click their numbers (the superscripts)
to toggle their visibility. For margin notes, you may click the circled
plus signs to toggle visibility.
\section{More Examples}\label{more-examples}
The rest of this document consists of a few test cases to make sure
everything still works well in slightly more complicated scenarios.
First we generate two plots in one figure environment with the chunk
option \texttt{fig.show = \textquotesingle{}hold\textquotesingle{}}:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(mtcars2, }\KeywordTok{aes}\NormalTok{(hp, mpg, }\DataTypeTok{color =} \NormalTok{am)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{()}
\NormalTok{p}
\NormalTok{p +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{figure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-two-together-1} \includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-two-together-2} \caption[Two plots in one figure environment]{Two plots in one figure environment.}\label{fig:fig-two-together}
\end{figure}
Then two plots in separate figure environments (the code is identical to
the previous code chunk, but the chunk option is the default
\texttt{fig.show = \textquotesingle{}asis\textquotesingle{}} now):
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(mtcars2, }\KeywordTok{aes}\NormalTok{(hp, mpg, }\DataTypeTok{color =} \NormalTok{am)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{()}
\NormalTok{p}
\end{Highlighting}
\end{Shaded}
\begin{figure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-two-separate-1} \caption[Two plots in separate figure environments (the first plot)]{Two plots in separate figure environments (the first plot).}\label{fig:fig-two-separate1}
\end{figure}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{figure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-two-separate-2} \caption[Two plots in separate figure environments (the second plot)]{Two plots in separate figure environments (the second plot).}\label{fig:fig-two-separate2}
\end{figure}
You may have noticed that the two figures have different captions, and
that is because we used a character vector of length 2 for the chunk
option \texttt{fig.cap} (something like
\texttt{fig.cap = c(\textquotesingle{}first plot\textquotesingle{}, \textquotesingle{}second plot\textquotesingle{})}).
Next we show multiple plots in margin figures. Similarly, two plots in
the same figure environment in the margin:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p}
\NormalTok{p +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{(}\DataTypeTok{method =} \StringTok{'lm'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{marginfigure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-margin-together-1} \includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-margin-together-2} \caption[Two plots in one figure environment in the margin]{Two plots in one figure environment in the margin.}\label{fig:fig-margin-together}
\end{marginfigure}
Then two plots from the same code chunk placed in different figure
environments:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr::}\KeywordTok{kable}\NormalTok{(}\KeywordTok{head}\NormalTok{(iris, }\DecValTok{15}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[c]{@{}rrrrl@{}}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width &
Species\tabularnewline
\midrule
\endhead
5.1 & 3.5 & 1.4 & 0.2 & setosa\tabularnewline
4.9 & 3.0 & 1.4 & 0.2 & setosa\tabularnewline
4.7 & 3.2 & 1.3 & 0.2 & setosa\tabularnewline
4.6 & 3.1 & 1.5 & 0.2 & setosa\tabularnewline
5.0 & 3.6 & 1.4 & 0.2 & setosa\tabularnewline
5.4 & 3.9 & 1.7 & 0.4 & setosa\tabularnewline
4.6 & 3.4 & 1.4 & 0.3 & setosa\tabularnewline
5.0 & 3.4 & 1.5 & 0.2 & setosa\tabularnewline
4.4 & 2.9 & 1.4 & 0.2 & setosa\tabularnewline
4.9 & 3.1 & 1.5 & 0.1 & setosa\tabularnewline
5.4 & 3.7 & 1.5 & 0.2 & setosa\tabularnewline
4.8 & 3.4 & 1.6 & 0.2 & setosa\tabularnewline
4.8 & 3.0 & 1.4 & 0.1 & setosa\tabularnewline
4.3 & 3.0 & 1.1 & 0.1 & setosa\tabularnewline
5.8 & 4.0 & 1.2 & 0.2 & setosa\tabularnewline
\bottomrule
\end{longtable}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p}
\end{Highlighting}
\end{Shaded}
\begin{marginfigure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-margin-separate-1} \caption[Two plots in separate figure environments in the margin (the first plot)]{Two plots in separate figure environments in the margin (the first plot).}\label{fig:fig-margin-separate1}
\end{marginfigure}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr::}\KeywordTok{kable}\NormalTok{(}\KeywordTok{head}\NormalTok{(iris, }\DecValTok{12}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[c]{@{}rrrrl@{}}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width &
Species\tabularnewline
\midrule
\endhead
5.1 & 3.5 & 1.4 & 0.2 & setosa\tabularnewline
4.9 & 3.0 & 1.4 & 0.2 & setosa\tabularnewline
4.7 & 3.2 & 1.3 & 0.2 & setosa\tabularnewline
4.6 & 3.1 & 1.5 & 0.2 & setosa\tabularnewline
5.0 & 3.6 & 1.4 & 0.2 & setosa\tabularnewline
5.4 & 3.9 & 1.7 & 0.4 & setosa\tabularnewline
4.6 & 3.4 & 1.4 & 0.3 & setosa\tabularnewline
5.0 & 3.4 & 1.5 & 0.2 & setosa\tabularnewline
4.4 & 2.9 & 1.4 & 0.2 & setosa\tabularnewline
4.9 & 3.1 & 1.5 & 0.1 & setosa\tabularnewline
5.4 & 3.7 & 1.5 & 0.2 & setosa\tabularnewline
4.8 & 3.4 & 1.6 & 0.2 & setosa\tabularnewline
\bottomrule
\end{longtable}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{(}\DataTypeTok{method =} \StringTok{'lm'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{marginfigure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-margin-separate-2} \caption[Two plots in separate figure environments in the margin (the second plot)]{Two plots in separate figure environments in the margin (the second plot).}\label{fig:fig-margin-separate2}
\end{marginfigure}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr::}\KeywordTok{kable}\NormalTok{(}\KeywordTok{head}\NormalTok{(iris, }\DecValTok{5}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{longtable}[c]{@{}rrrrl@{}}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width &
Species\tabularnewline
\midrule
\endhead
5.1 & 3.5 & 1.4 & 0.2 & setosa\tabularnewline
4.9 & 3.0 & 1.4 & 0.2 & setosa\tabularnewline
4.7 & 3.2 & 1.3 & 0.2 & setosa\tabularnewline
4.6 & 3.1 & 1.5 & 0.2 & setosa\tabularnewline
5.0 & 3.6 & 1.4 & 0.2 & setosa\tabularnewline
\bottomrule
\end{longtable}
We blended some tables in the above code chunk only as
\emph{placeholders} to make sure there is enough vertical space among
the margin figures, otherwise they will be stacked tightly together. For
a practical document, you should not insert too many margin figures
consecutively and make the margin crowded.
You do not have to assign captions to figures. We show three figures
with no captions below in the margin, in the main column, and in full
width, respectively.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# a boxplot of weight vs transmission; this figure}
\CommentTok{# will be placed in the margin}
\KeywordTok{ggplot}\NormalTok{(mtcars2, }\KeywordTok{aes}\NormalTok{(am, wt)) +}\StringTok{ }\KeywordTok{geom_boxplot}\NormalTok{() +}
\StringTok{ }\KeywordTok{coord_flip}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{marginfigure}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-nocap-margin-1} \end{marginfigure}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# a figure in the main column}
\NormalTok{p <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(mtcars, }\KeywordTok{aes}\NormalTok{(wt, hp)) +}\StringTok{ }\KeywordTok{geom_point}\NormalTok{()}
\NormalTok{p}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-nocap-main-1}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# a fullwidth figure}
\NormalTok{p +}\StringTok{ }\KeywordTok{geom_smooth}\NormalTok{(}\DataTypeTok{method =} \StringTok{'lm'}\NormalTok{) +}\StringTok{ }\KeywordTok{facet_grid}\NormalTok{(~}\StringTok{ }\NormalTok{gear)}
\end{Highlighting}
\end{Shaded}
\begin{figure*}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/fig-nocap-fullwidth-1} \end{figure*}
\section{Some Notes on Tufte CSS}\label{some-notes-on-tufte-css}
There are a few other things in Tufte CSS that we have not mentioned so
far. If you prefer \textsf{sans-serif fonts}, use the function
\texttt{sans\_serif()} in \textbf{tufte}. For epigraphs, you may use a
pair of underscores to make the paragraph italic in a block quote, e.g.
\begin{quote}
\emph{I can win an argument on any topic, against any opponent. People
know this, and steer clear of me at parties. Often, as a sign of their
great respect, they don't even invite me.}
\hfill --- Dave Barry
\end{quote}
We hope you will enjoy the simplicity of R Markdown and this R package,
and we sincerely thank the authors of the Tufte-CSS and Tufte-LaTeX
projects for developing the beautiful CSS and LaTeX classes. Our
\textbf{tufte} package would not have been possible without their heavy
lifting.
To see the R Markdown source of this example document, you may follow
\href{https://github.com/rstudio/tufte/raw/master/inst/rmarkdown/templates/tufte_html/skeleton/skeleton.Rmd}{this
link to Github}, use the wizard in RStudio IDE
(\texttt{File -\textgreater{} New File -\textgreater{} R Markdown -\textgreater{} From Template}),
or open the Rmd file in the package:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{file.edit}\NormalTok{(}
\NormalTok{tufte:::}\KeywordTok{template_resources}\NormalTok{(}
\StringTok{'tufte_html'}\NormalTok{, }\StringTok{'..'}\NormalTok{, }\StringTok{'skeleton'}\NormalTok{, }\StringTok{'skeleton.Rmd'}
\NormalTok{)}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{4}
\itemsep1pt\parskip0pt\parsep0pt
\item
Test of a proportion
\end{enumerate}
\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
In the standard Rhine test of extra-sensory perception (ESP), a set of
cards is used where each card has a circle, a square, wavy lines, a
cross, or a star. A card is selected at random from the deck, and a
person tries to guess the symbol on the card. This experiment is
repeated 20 times, and the number of correct guesses y is recorded.
Let p denote the probability that the person makes a correct guess,
where p = .2 if the person does not have ESP and is just guessing at
the card symbol. To see if the person truly has some ESP, we would
like to test the hypothesis H : p = .2.
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(LearnBayes)}
\CommentTok{#A. If the person identifies y = 8 cards correctly, compute the p-value.}
\CommentTok{#HO: p = 0.2}
\CommentTok{#HA: p != 0.2}
\CommentTok{#We are dealing with a binomial distribution: discrete number of successes}
\CommentTok{#y = 8 successes}
\NormalTok{\{}\DecValTok{1} \NormalTok{-}\StringTok{ }\KeywordTok{pbinom}\NormalTok{(}\DecValTok{8}\NormalTok{, }\DecValTok{20}\NormalTok{, }\FloatTok{0.2}\NormalTok{)\}*}\DecValTok{2} \CommentTok{#pvalue = 0.01996357}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] 0.01996357
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(}\DecValTok{1}\NormalTok{:}\DecValTok{20}\NormalTok{, }\DecValTok{1}\NormalTok{-}\KeywordTok{pbinom}\NormalTok{(}\DecValTok{1}\NormalTok{:}\DecValTok{20}\NormalTok{, }\DecValTok{20}\NormalTok{, }\FloatTok{0.2}\NormalTok{), }\DataTypeTok{xlab =} \StringTok{"Number of correct guesses"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"p-value (probability of observed value occuring by chance)"}\NormalTok{, }\DataTypeTok{main=} \StringTok{"P-values for ESP hypothesis testing }\CharTok{\textbackslash{}n}\StringTok{ by the number of correct guesses"}\NormalTok{)}
\KeywordTok{abline}\NormalTok{(}\DataTypeTok{v =} \DecValTok{8}\NormalTok{,}\DataTypeTok{lty=}\DecValTok{2}\NormalTok{, }\DataTypeTok{col=} \StringTok{'red'}\NormalTok{)}
\KeywordTok{segments}\NormalTok{(}\DataTypeTok{x0=}\KeywordTok{c}\NormalTok{(}\FloatTok{5.0}\NormalTok{,}\FloatTok{5.5}\NormalTok{,}\DecValTok{6}\NormalTok{), }\DataTypeTok{y0=}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{), }\DataTypeTok{y1=}\KeywordTok{c}\NormalTok{(}\FloatTok{0.12}\NormalTok{,}\FloatTok{0.60}\NormalTok{,}\FloatTok{0.20}\NormalTok{)) }
\KeywordTok{text}\NormalTok{(}\DataTypeTok{x =} \DecValTok{13}\NormalTok{, }\FloatTok{0.2}\NormalTok{, }\DataTypeTok{labels =} \StringTok{"p-value = 0.01996 for the scenario }\CharTok{\textbackslash{}n}\StringTok{ with 8 correct guesses"}\NormalTok{)}
\NormalTok{u <-}\StringTok{ }\KeywordTok{par}\NormalTok{(}\StringTok{"usr"}\NormalTok{)}
\NormalTok{v <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}
\KeywordTok{grconvertX}\NormalTok{(u[}\DecValTok{1}\NormalTok{:}\DecValTok{2}\NormalTok{], }\StringTok{"user"}\NormalTok{, }\StringTok{"ndc"}\NormalTok{),}
\KeywordTok{grconvertY}\NormalTok{(u[}\DecValTok{3}\NormalTok{:}\DecValTok{4}\NormalTok{], }\StringTok{"user"}\NormalTok{, }\StringTok{"ndc"}\NormalTok{)}
\NormalTok{)}
\NormalTok{v <-}\StringTok{ }\KeywordTok{c}\NormalTok{( (v[}\DecValTok{1}\NormalTok{]+v[}\DecValTok{2}\NormalTok{])/}\DecValTok{2}\NormalTok{, v[}\DecValTok{2}\NormalTok{], (v[}\DecValTok{3}\NormalTok{]+v[}\DecValTok{4}\NormalTok{])/}\DecValTok{2}\NormalTok{, v[}\DecValTok{4}\NormalTok{] )}
\KeywordTok{par}\NormalTok{( }\DataTypeTok{fig=}\NormalTok{v, }\DataTypeTok{new=}\OtherTok{TRUE}\NormalTok{, }\DataTypeTok{mar=}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{) )}
\KeywordTok{plot}\NormalTok{(}\DecValTok{0}\NormalTok{:}\DecValTok{20}\NormalTok{, }\KeywordTok{dbinom}\NormalTok{(}\DecValTok{0}\NormalTok{:}\DecValTok{20}\NormalTok{, }\DecValTok{20}\NormalTok{, }\FloatTok{0.2}\NormalTok{), }\DataTypeTok{xlab=} \StringTok{"Number of Correct Guesses"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"Probability Density"}\NormalTok{)}
\KeywordTok{box}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-5-1}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#B) Suppose you believe a priori that the probability that p = .2 is .5 and if p != .2 you assign a beta(1, 4) prior on the proportion. Using the function pbetat, compute the posterior probability of the hypothesis H. Compare your answer with the p-value computed in part (a).}
\KeywordTok{pbetat}\NormalTok{(.}\DecValTok{2}\NormalTok{, .}\DecValTok{5}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{4}\NormalTok{), }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{12}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## $bf
## [1] 0.5175417
##
## $post
## [1] 0.3410395
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#$bf [1] 0.5175417 (Bayes Factor, in support of the null)}
\CommentTok{#$post [1] 0.3410395 (posterior probability of null)}
\CommentTok{#p=0.34 is much higher than that derived from the binomial. Basically, under the Bayesian framework, we are more skeptial about rejecting the null hypothesis that there is no ESP than under the frequentist framework.}
\NormalTok{### pbetat function ###}
\CommentTok{#function (p0, prob, ab, data) }
\CommentTok{#\{}
\CommentTok{# a = ab[1]}
\CommentTok{# b = ab[2]}
\CommentTok{# s = data[1]}
\CommentTok{# f = data[2]}
\CommentTok{# lbf = s * log(p0) + f * log(1 - p0) + lbeta(a, b) - lbeta(a + }
\CommentTok{# s, b + f)}
\CommentTok{# bf = exp(lbf)}
\CommentTok{# post = prob * bf/(prob * bf + 1 - prob)}
\CommentTok{# return(list(bf = bf, post = post))}
\CommentTok{#\}}
\end{Highlighting}
\end{Shaded}
\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\setcounter{enumi}{2}
\itemsep1pt\parskip0pt\parsep0pt
\item
The posterior probability computed in part (b) depended on your belief
about plausible values of the proportion p when p != .2. For each of
the following priors, compute the posterior probability of H: (1) p ∼
beta(.5, 2), (2) p ∼ beta(2, 8), (3) p ∼ beta(8, 32).
\end{enumerate}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{pbetat}\NormalTok{(.}\DecValTok{2}\NormalTok{, .}\DecValTok{5}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\FloatTok{0.5}\NormalTok{,}\DecValTok{2}\NormalTok{), }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{12}\NormalTok{)) }\CommentTok{#[1] 0.3900752}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## $bf
## [1] 0.6395464
##
## $post
## [1] 0.3900752
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{pbetat}\NormalTok{(.}\DecValTok{2}\NormalTok{, .}\DecValTok{5}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{8}\NormalTok{), }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{12}\NormalTok{)) }\CommentTok{#0.328591}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## $bf
## [1] 0.4894051
##
## $post
## [1] 0.328591
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{pbetat}\NormalTok{(.}\DecValTok{2}\NormalTok{, .}\DecValTok{5}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{32}\NormalTok{), }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{12}\NormalTok{)) }\CommentTok{#0.3855337}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## $bf
## [1] 0.6274287
##
## $post
## [1] 0.3855337
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#show the 4 different beta distributions}
\CommentTok{#par(mfrow=c(2,2))}
\NormalTok{x <-}\StringTok{ }\KeywordTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DataTypeTok{by =} \FloatTok{0.01}\NormalTok{)}
\KeywordTok{plot}\NormalTok{(x, }\KeywordTok{dbeta}\NormalTok{(x, }\DataTypeTok{shape1 =} \DecValTok{1}\NormalTok{, }\DataTypeTok{shape2 =} \DecValTok{4}\NormalTok{), }\DataTypeTok{type=}\StringTok{"l"}\NormalTok{, }\DataTypeTok{lty=}\DecValTok{1}\NormalTok{, }\DataTypeTok{xlab=}\StringTok{""}\NormalTok{, }\DataTypeTok{ylab=}\StringTok{"Density"}\NormalTok{, }\DataTypeTok{main=}\StringTok{"Beta(1,4) Distribution"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-7-1}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(x, }\KeywordTok{dbeta}\NormalTok{(x, }\DataTypeTok{shape1 =} \FloatTok{0.5}\NormalTok{, }\DataTypeTok{shape2 =} \DecValTok{2}\NormalTok{), }\DataTypeTok{type=}\StringTok{"l"}\NormalTok{, }\DataTypeTok{lty=}\DecValTok{1}\NormalTok{, }\DataTypeTok{xlab=}\StringTok{""}\NormalTok{, }\DataTypeTok{ylab=}\StringTok{"Density"}\NormalTok{, }\DataTypeTok{main=}\StringTok{"Beta(0.5,2) Distribution"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-7-2}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(x, }\KeywordTok{dbeta}\NormalTok{(x, }\DataTypeTok{shape1 =} \DecValTok{2}\NormalTok{, }\DataTypeTok{shape2 =} \DecValTok{8}\NormalTok{), }\DataTypeTok{type=}\StringTok{"l"}\NormalTok{, }\DataTypeTok{lty=}\DecValTok{1}\NormalTok{, }\DataTypeTok{xlab=}\StringTok{""}\NormalTok{, }\DataTypeTok{ylab=}\StringTok{"Density"}\NormalTok{, }\DataTypeTok{main=}\StringTok{"Beta(2,8) Distribution"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-7-3}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(x, }\KeywordTok{dbeta}\NormalTok{(x, }\DataTypeTok{shape1 =} \DecValTok{8}\NormalTok{, }\DataTypeTok{shape2 =} \DecValTok{32}\NormalTok{), }\DataTypeTok{type=}\StringTok{"l"}\NormalTok{, }\DataTypeTok{lty=}\DecValTok{1}\NormalTok{, }\DataTypeTok{xlab=}\StringTok{""}\NormalTok{, }\DataTypeTok{ylab=}\StringTok{"Density"}\NormalTok{, }\DataTypeTok{main=}\StringTok{"Beta(8,32) Distribution"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-7-4}
\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\setcounter{enumi}{3}
\itemsep1pt\parskip0pt\parsep0pt
\item
On the basis of your Bayesian computations, do you think that y =8 is
convincing evidence that the person really has some ESP? Explain.
\end{enumerate}
\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
No. Given a sensible prior with the probability density centered
around the null hypothesis that the proportion of correct guesses
equals 0.2, I wouldn't expect this person to have ESP.
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#robustness to different values of alpha}
\NormalTok{postGrid <-}\StringTok{ }\KeywordTok{matrix}\NormalTok{(}\OtherTok{NA}\NormalTok{, }\DataTypeTok{nrow =} \DecValTok{100}\NormalTok{, }\DataTypeTok{ncol=}\DecValTok{100}\NormalTok{)}
\KeywordTok{colnames}\NormalTok{(postGrid) <-}\StringTok{ }\DecValTok{1}\NormalTok{:}\DecValTok{100}
\KeywordTok{rownames}\NormalTok{(postGrid) <-}\StringTok{ }\DecValTok{1}\NormalTok{:}\DecValTok{100}
\NormalTok{for(i in }\KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DataTypeTok{by=}\DecValTok{1}\NormalTok{))\{}
\NormalTok{for (j in }\KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{100}\NormalTok{, }\DataTypeTok{by=}\DecValTok{1}\NormalTok{))\{}
\NormalTok{post <-}\StringTok{ }\KeywordTok{unlist}\NormalTok{(}\KeywordTok{pbetat}\NormalTok{(.}\DecValTok{2}\NormalTok{, .}\DecValTok{5}\NormalTok{, }\KeywordTok{c}\NormalTok{(i,j), }\KeywordTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{12}\NormalTok{))[}\DecValTok{2}\NormalTok{])}
\NormalTok{postGrid[i, j] <-}\StringTok{ }\NormalTok{post}
\NormalTok{\}}
\NormalTok{\}}
\KeywordTok{library}\NormalTok{(gplots)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
##
## Attaching package: 'gplots'
\end{verbatim}
\begin{verbatim}
## The following object is masked from 'package:stats':
##
## lowess
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{graphics.off}\NormalTok{()}
\KeywordTok{heatmap.2}\NormalTok{(postGrid, }\DataTypeTok{dendrogram=}\StringTok{'none'}\NormalTok{, }\DataTypeTok{Rowv=}\OtherTok{FALSE}\NormalTok{, }\DataTypeTok{Colv=}\OtherTok{FALSE}\NormalTok{,}\DataTypeTok{trace=}\StringTok{'none'}\NormalTok{, , }\DataTypeTok{xlab =} \StringTok{"beta"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"alpha"}\NormalTok{)}
\KeywordTok{min}\NormalTok{(postGrid)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] 0.1154475
\end{verbatim}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{5}
\itemsep1pt\parskip0pt\parsep0pt
\item
Suppose you drive on a particular interstate roadway and typically
drive at a constant speed of 70 mph. One day, you pass one car and get
passed by 17 cars. Suppose that the speeds are normally distributed
with unknown mean μ and standard deviation σ = 10. If you pass s cars
and f cars pass you, the likelihood of μ is given by L(μ) ∝
Φ(70,μ,σ)\^{}s * (1 − Φ(70,μ,σ))\^{}f, where Φ(y, μ, σ) is the cdf of
the normal distribution with mean μ and standard deviation σ. Assign
the unknown mean μ a flat prior density.
\end{enumerate}
\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\itemsep1pt\parskip0pt\parsep0pt
\item
If s = 1 and f = 17, plot the posterior density of μ.
\end{enumerate}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{s=}\DecValTok{1}
\NormalTok{f=}\DecValTok{17}
\NormalTok{sigma=}\DecValTok{10}
\NormalTok{x =}\StringTok{ }\KeywordTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{,}\DataTypeTok{length=}\DecValTok{200}\NormalTok{)}
\NormalTok{prior =}\StringTok{ }\KeywordTok{dbeta}\NormalTok{(x,}\DecValTok{1} \NormalTok{,}\DecValTok{1}\NormalTok{) }
\CommentTok{#plot(x, prior, type="l") #plot the prior}
\NormalTok{mu=}\DecValTok{1}\NormalTok{:}\DecValTok{200}
\NormalTok{likelihood =}\StringTok{ }\KeywordTok{pnorm}\NormalTok{(}\DecValTok{70}\NormalTok{, mu, sigma)^s *}\StringTok{ }\NormalTok{(}\DecValTok{1} \NormalTok{-}\StringTok{ }\KeywordTok{pnorm}\NormalTok{(}\DecValTok{70}\NormalTok{, mu, sigma))^f}
\NormalTok{like <-}\StringTok{ }\NormalTok{function(mu, sigma, s,f)\{}
\NormalTok{likelihood =}\StringTok{ }\KeywordTok{pnorm}\NormalTok{(}\DecValTok{70}\NormalTok{, mu, sigma)^s *}\StringTok{ }\NormalTok{(}\DecValTok{1} \NormalTok{-}\StringTok{ }\KeywordTok{pnorm}\NormalTok{(}\DecValTok{70}\NormalTok{, mu, sigma))^f}
\NormalTok{\}}
\KeywordTok{plot}\NormalTok{(mu, likelihood/}\KeywordTok{sum}\NormalTok{(likelihood)) }\CommentTok{#plot the likelihood function}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-9-1}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{posterior =}\StringTok{ }\NormalTok{prior*likelihood/}\KeywordTok{sum}\NormalTok{(prior*likelihood)}
\KeywordTok{plot}\NormalTok{(mu, posterior, }\DataTypeTok{type =} \StringTok{'l'}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"posterior"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Posterior Density of μ"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Warning in title(...): conversion failure on
## 'Posterior Density of μ' in 'mbcsToSbcs': dot
## substituted for <ce>
\end{verbatim}
\begin{verbatim}
## Warning in title(...): conversion failure on
## 'Posterior Density of μ' in 'mbcsToSbcs': dot
## substituted for <bc>
\end{verbatim}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-9-2}
\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\setcounter{enumi}{1}
\itemsep1pt\parskip0pt\parsep0pt
\item
Using the density found in part (a), find the posterior mean of μ.
\end{enumerate}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{#how to find the posterior mean? Get a weighted average of all your points.}
\NormalTok{postMean <-}\StringTok{ }\KeywordTok{sum}\NormalTok{(mu *}\StringTok{ }\NormalTok{posterior) }\CommentTok{#87.11109}
\end{Highlighting}
\end{Shaded}
\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\setcounter{enumi}{2}
\itemsep1pt\parskip0pt\parsep0pt
\item
Find the probability that the average speed of the cars on this
interstate roadway exceeds 80 mph.
\end{enumerate}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{sum}\NormalTok{(posterior[}\DecValTok{81}\NormalTok{:}\DecValTok{200}\NormalTok{]) }\CommentTok{#0.9468573}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] 0.9146066
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cord.x <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{80}\NormalTok{,}\KeywordTok{seq}\NormalTok{(}\DecValTok{80}\NormalTok{,}\DecValTok{139}\NormalTok{,}\FloatTok{0.01}\NormalTok{),}\DecValTok{139}\NormalTok{) }
\NormalTok{cord.y <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\KeywordTok{like}\NormalTok{(}\KeywordTok{seq}\NormalTok{(}\DecValTok{80}\NormalTok{,}\DecValTok{139}\NormalTok{,}\FloatTok{0.01}\NormalTok{), sigma, s, f),}\DecValTok{0}\NormalTok{) }
\KeywordTok{curve}\NormalTok{(}\KeywordTok{like}\NormalTok{(x, sigma, s, f), }\DataTypeTok{xlim=}\KeywordTok{c}\NormalTok{(}\DecValTok{40}\NormalTok{,}\DecValTok{139}\NormalTok{), }\DataTypeTok{ylab =} \StringTok{"posterior"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Posterior Density of μ"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Warning in title(...): conversion failure on
## 'Posterior Density of μ' in 'mbcsToSbcs': dot
## substituted for <ce>
\end{verbatim}
\begin{verbatim}
## Warning in title(...): conversion failure on
## 'Posterior Density of μ' in 'mbcsToSbcs': dot
## substituted for <bc>
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{polygon}\NormalTok{(cord.x,cord.y,}\DataTypeTok{col=}\StringTok{'skyblue'}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{chapter3Bayes_ex5and6_files/figure-latex/unnamed-chunk-11-1}
\bibliography{skeleton.bib}
\end{document}