-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathThesis.tex
274 lines (196 loc) · 9.21 KB
/
Thesis.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
% ===========================================================================================================
% TEX file generated by R with the 'knitr' package
%
% DO NOT EDIT THE TEX FILE DIRECTLY
% ===========================================================================================================
\documentclass{report}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\input{Preamble}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
% ===========================================================================================================
% Cover
% ===========================================================================================================
\title{Thesis Template using R and knitr}
\author{Alexis Sard\'a-Espinosa}
\date{\today}
\maketitle
% ===========================================================================================================
% Front matter
% ===========================================================================================================
\pagenumbering{Roman}
\chapter*{Dedication}
To me
\chapter*{Abstract}
Abstract goes here
\chapter*{Declaration}
I declare that..
\chapter*{Acknowledgements}
I want to thank...
\tableofcontents
\listoffigures
\listoftables
% ===========================================================================================================
% Main matter
% ===========================================================================================================
\cleardoublepage
\pagenumbering{arabic}
\chapter{Introduction}
\label{ch:introduction}
\input{Introduction/Introduction}
\chapter{Organization}
\label{ch:organization}
\input{Organization/Organization}
\chapter{Examples}
\label{ch:examples}
\section{Basics}
\label{sec:basics}
The \texttt{caret} package is one that can take advantage of parallel execution. Below, I train a random forest on a sample dataset~\cite{lichman2013}.
% This chunk depends on the MD5 of chess.txt; if that file changes, the chunk needs to be updated
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{chess_rf} \hlkwb{<-} \hlkwd{train}\hlstd{(label} \hlopt{~} \hlstd{.,} \hlkwc{data} \hlstd{= chess,}
\hlkwc{method} \hlstd{=} \hlstr{"rf"}\hlstd{,}
\hlkwc{tuneGrid} \hlstd{=} \hlkwd{data.frame}\hlstd{(}\hlkwc{mtry} \hlstd{=} \hlnum{8}\hlopt{:}\hlnum{15}\hlstd{),}
\hlkwc{trControl} \hlstd{=} \hlkwd{trainControl}\hlstd{(}\hlkwc{method} \hlstd{=} \hlstr{"oob"}\hlstd{))}
\hlstd{best} \hlkwb{<-} \hlstd{chess_rf}\hlopt{$}\hlstd{bestTune}\hlopt{$}\hlstd{mtry}
\hlstd{acc} \hlkwb{<-} \hlkwd{round}\hlstd{(chess_rf}\hlopt{$}\hlstd{results}\hlopt{$}\hlstd{Accuracy[chess_rf}\hlopt{$}\hlstd{results}\hlopt{$}\hlstd{mtry} \hlopt{==} \hlstd{best],}
\hlnum{3}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
Using a random forest with the Chess dataset and OOB error estimate, best value of \texttt{mtry} was 15 with an accuracy of 0.994.
Try changing something in the chunk (to invalidate the chunk's cache) and compiling the thesis with/without parallel support to see the difference in run time.
With the \texttt{rand.seed} hook in place, each chunk should have an associated random seed after you compile everything for the first time. The chunk below is not cached, but its seed is restored everytime you compile, so it should always give the same result. If you add chunks with randomness before it and delete the \texttt{seeds.RData} file to update it, it will probably get a different seed assigned.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{rnorm}\hlstd{(}\hlnum{1}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] -0.1180655
\end{verbatim}
\end{kframe}
\end{knitrout}
\section{Figures}
\label{sec:figures}
\graphicspath{Examples/Figs/}
Plots can be created directly in \R{} and the results are added automatically to the \texttt{tex} file. See Fig.~\ref{fig:iris-histograms} for an example of a single plot.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{figure}[htpb]
{\centering \includegraphics[width=\linewidth]{Examples/Figs/iris-histograms-1}
}
\caption[Histograms for the iris dataset]{Histograms for the iris dataset. Look at the list of figures to see difference between short and long captions.}\label{fig:iris-histograms}
\end{figure}
\end{knitrout}
You can create several plots per chunk and have them in the same main figure. See Fig.~\ref{fig:mtcars-subplots} for an example of two plots: Fig.~\ref{fig:mtcars-subplots1} and Fig.~\ref{fig:mtcars-subplots2}.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{figure}[htpb]
{\centering \subfloat[MPG vs HP\label{fig:mtcars-subplots1}]{\includegraphics[width=0.45\linewidth]{Examples/Figs/mtcars-subplots-1} }
\subfloat[MPG vs Weight\label{fig:mtcars-subplots2}]{\includegraphics[width=0.45\linewidth]{Examples/Figs/mtcars-subplots-2} }
}
\caption[Plots using \texttt{mtcars} dataset]{Plots using \texttt{mtcars} dataset.}\label{fig:mtcars-subplots}
\end{figure}
\end{knitrout}
\section{Tables}
\label{sec:tables}
Tables can also be created in \R{} and inserted automatically. Check Table~\ref{tab:co2_head}.
% latex table generated in R 3.3.2 by xtable 1.8-2 package
% Sun Dec 04 12:32:43 2016
\begin{table}[htbp]
\centering
\caption[First rows of the CO2 dataset]{First rows of the CO2 dataset. Look at the list of tables to see the difference between short and long table caption.}
\label{tab:co2_head}
\begin{tabular}{lccccc}
\toprule
& Plant & Type & Treatment & conc & uptake \\
\midrule
1 & Qn1 & Quebec & nonchilled & 95.00 & 16.00 \\
2 & Qn1 & Quebec & nonchilled & 175.00 & 30.40 \\
3 & Qn1 & Quebec & nonchilled & 250.00 & 34.80 \\
4 & Qn1 & Quebec & nonchilled & 350.00 & 37.20 \\
5 & Qn1 & Quebec & nonchilled & 500.00 & 35.30 \\
6 & Qn1 & Quebec & nonchilled & 675.00 & 39.20 \\
\bottomrule
\end{tabular}
\end{table}
Something Useful I found was that you can use the \LaTeX{} package \texttt{makecell} to insert linebreaks in table headers. See the source code for Table~\ref{tab:co2_head_2}.
% latex table generated in R 3.3.2 by xtable 1.8-2 package
% Sun Dec 04 12:32:43 2016
\begin{table}[htbp]
\centering
\caption[Same table as before but with linebreaks]{Same table as before but with linebreaks.}
\label{tab:co2_head_2}
\begin{tabular}{lcccc}
\toprule
\thead{Plant \\ Something} & \thead{Type \\ Yes} & \thead{Treatment \\ No} & \thead{conc \\ whatever} & \thead{uptake \\ ok} \\
\midrule
Qn1 & Quebec & nonchilled & 95.00 & 16.00 \\
Qn1 & Quebec & nonchilled & 175.00 & 30.40 \\
Qn1 & Quebec & nonchilled & 250.00 & 34.80 \\
Qn1 & Quebec & nonchilled & 350.00 & 37.20 \\
Qn1 & Quebec & nonchilled & 500.00 & 35.30 \\
Qn1 & Quebec & nonchilled & 675.00 & 39.20 \\
\bottomrule
\end{tabular}
\end{table}
% ===========================================================================================================
% Bibliography
% ===========================================================================================================
\bibliographystyle{plainnat}
\nocite{*}
\bibliography{References}
\cleardoublepage
% ===========================================================================================================
% Appendices
% ===========================================================================================================
\appendix
\chapter{First Appendix}
In case it's needed.
\end{document}