% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Descriptive Epidemiology using epiR},
  pdfauthor={Mark Stevenson},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{cslreferences}%
  {\setlength{\parindent}{0pt}%
  \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces}%
  {\par}

\title{Descriptive Epidemiology using epiR}
\author{Mark Stevenson}
\date{2020-06-06}

\begin{document}
\maketitle

\setmainfont{Calibri Light}

Epidemiology is the study of the frequency, distribution and
determinants of health-related states in populations and the application
of such knowledge to control health problems (Centers for Disease
Control and Prevention \protect\hyperlink{ref-cdc:2006}{2006}).

This vignette provides instruction on the way R and \texttt{epiR} can be
used for descriptive epidemiological analyses, that is, to describe how
the frequency of disease varies by individual, place and time.

\hypertarget{indivdual}{%
\subsection{Indivdual}\label{indivdual}}

Descriptions of disease frequency involves reporting either the
\textbf{prevalence} or \textbf{incidence} of disease.

Some definitions. Strictly speaking, `prevalence' equals the number of
cases of a given disease or attribute that exists in a population at a
specified point in time. Prevalence risk is the proportion of a
population that has a specific disease or attribute at a specified point
in time. Many authors use the term `prevalence' when they really mean
prevalence risk, and these notes will follow this convention.

Two types of prevalence are reported in the literature: (1)
\textbf{point prevalence} equals the proportion of a population in a
diseased state at a single point in time, (2) \textbf{period prevalence}
equals the proportion of a population with a given disease or condition
over a specific period of time (i.e.~the number of existing cases at the
start of a follow-up period plus the number of incident cases that occur
during the follow-up period).

Incidence provides a measure of how frequently susceptible individuals
become disease cases as they are observed over time. An incident case
occurs when an individual changes from being susceptible to being
diseased. The count of incident cases is the number of such events that
occur in a population during a defined follow-up period. There are two
ways to express incidence:

\textbf{Incidence risk} (also known as cumulative incidence) is the
proportion of initially susceptible individuals in a population who
become new cases during a defined follow-up period.

\textbf{Incidence rate} (also known as incidence density) is the number
of new cases of disease that occur per unit of individual time at risk
during a defined follow-up period.

In addition to reporting the point estimate of disease frequency, it is
important to provide an indication of the uncertainty around that point
estimate. The \texttt{epi.conf} function in the \texttt{epiR} package
allows you to calculate confidence intervals for prevalence, incidence
risk and incidence rates.

Let's say we're interested in the prevalence of disease X in a
population comprised of 1000 individuals. Two hundred are tested and
four returned a positive result. Assuming 100\% test sensitivity and
specificity, what is the estimated prevalence of disease X in this
population?

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(epiR)}

\NormalTok{ncas \textless{}{-}}\StringTok{ }\DecValTok{4}\NormalTok{; npop \textless{}{-}}\StringTok{ }\DecValTok{200}
\NormalTok{tmp \textless{}{-}}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(ncas, npop))}
\KeywordTok{epi.conf}\NormalTok{(tmp, }\DataTypeTok{ctype =} \StringTok{"prevalence"}\NormalTok{, }\DataTypeTok{method =} \StringTok{"exact"}\NormalTok{, }\DataTypeTok{N =} \DecValTok{1000}\NormalTok{, }\DataTypeTok{design =} \DecValTok{1}\NormalTok{, }
   \DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{) }\OperatorTok{*}\StringTok{ }\DecValTok{100}
\CommentTok{\#\textgreater{}      est     lower    upper}
\CommentTok{\#\textgreater{} ncas   2 0.5475566 5.041361}
\end{Highlighting}
\end{Shaded}

The estimated prevalence of disease X in this population is 2.0 (95\%
confidence interval {[}CI{]} 0.54 -- 5.0) cases per 100 individuals at
risk.

Another example. A study was conducted by Feychting, Osterlund, and
Ahlbom (\protect\hyperlink{ref-feychting_et_al:1998}{1998}) to report
the frequency of cancer among the blind. A total of 136 diagnoses of
cancer were made from 22,050 person-years at risk. What was the
incidence rate of cancer in this population?

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ncas \textless{}{-}}\StringTok{ }\DecValTok{136}\NormalTok{; ntar \textless{}{-}}\StringTok{ }\DecValTok{22050}
\NormalTok{tmp \textless{}{-}}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(ncas, ntar))}
\KeywordTok{epi.conf}\NormalTok{(tmp, }\DataTypeTok{ctype =} \StringTok{"inc.rate"}\NormalTok{, }\DataTypeTok{method =} \StringTok{"exact"}\NormalTok{, }\DataTypeTok{N =} \DecValTok{1000}\NormalTok{, }\DataTypeTok{design =} \DecValTok{1}\NormalTok{, }
   \DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{) }\OperatorTok{*}\StringTok{ }\DecValTok{1000}
\CommentTok{\#\textgreater{}         est    lower    upper}
\CommentTok{\#\textgreater{} ncas 6.1678 5.174806 7.295817}
\end{Highlighting}
\end{Shaded}

The incidence rate of cancer in this population was 6.2 (95\% CI 5.2 to
7.3) cases per 1000 person-years at risk.

Now lets say we want to compare the frequency of disease across several
populations. An effective way to do this is to used a ranked error bar
plot. With a ranked error bar plot the points represent the point
estimate of the measure of disease frequency and the error bars indicate
the 95\% confidence interval around each estimate. The disease frequency
estimates are then sorted from lowest to highest.

Generate some data. First we'll generate a distribution of disease
prevalence estimates. Let's say it has a mode of 0.60 and we're 80\%
certain that the prevalence is greater than 0.35. Use the
\texttt{epi.betabuster} function to generate shape1 and shape2
parameters that can be used for a beta distribution to satisfy these
constraints:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{tmp \textless{}{-}}\StringTok{ }\KeywordTok{epi.betabuster}\NormalTok{(}\DataTypeTok{mode =} \FloatTok{0.60}\NormalTok{, }\DataTypeTok{conf =} \FloatTok{0.80}\NormalTok{, }\DataTypeTok{greaterthan =} \OtherTok{TRUE}\NormalTok{, }\DataTypeTok{x =} \FloatTok{0.35}\NormalTok{, }
   \DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{, }\DataTypeTok{max.shape1 =} \DecValTok{100}\NormalTok{, }\DataTypeTok{step =} \FloatTok{0.001}\NormalTok{)}
\NormalTok{tmp}\OperatorTok{$}\NormalTok{shape1; tmp}\OperatorTok{$}\NormalTok{shape2}
\CommentTok{\#\textgreater{} [1] 2.357}
\CommentTok{\#\textgreater{} [1] 1.904667}
\end{Highlighting}
\end{Shaded}

Take 100 draws from a beta distribution using the shape1 and shape2
values calculated above and plot them as a frequency histogram:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{library}\NormalTok{(ggplot2)}

\NormalTok{dprob \textless{}{-}}\StringTok{ }\KeywordTok{rbeta}\NormalTok{(}\DataTypeTok{n =} \DecValTok{100}\NormalTok{, }\DataTypeTok{shape1 =}\NormalTok{ tmp}\OperatorTok{$}\NormalTok{shape1, }\DataTypeTok{shape2 =}\NormalTok{ tmp}\OperatorTok{$}\NormalTok{shape2)}
\NormalTok{dat \textless{}{-}}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{dprob =}\NormalTok{ dprob)}

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ dprob)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \FloatTok{0.01}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Prevalence"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{10}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of draws"}\NormalTok{)}
\CommentTok{\#\textgreater{} Warning: Removed 2 rows containing missing values (geom\_bar).}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/dfreq01-fig-1.pdf}
\caption{\label{fig:dfreq01}Frequency histogram of disease prevalence
estimates for our simulated population.}
\end{figure}

Generate a vector of population sizes using the uniform distribution.
Calculate the number of diseased individuals in each population using
\texttt{dprob} (calculated above). Finally, calculate the prevalence of
disease in each population and its 95\% confidence interval using
\texttt{epi.conf}. The function \texttt{epi.conf} provides several
options for confidence interval calculation method for prevalence. Here
we'll use the exact method:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dat}\OperatorTok{$}\NormalTok{npop \textless{}{-}}\StringTok{ }\KeywordTok{round}\NormalTok{(}\KeywordTok{runif}\NormalTok{(}\DataTypeTok{n =} \DecValTok{100}\NormalTok{, }\DataTypeTok{min =} \DecValTok{20}\NormalTok{, }\DataTypeTok{max =} \DecValTok{1500}\NormalTok{), }\DataTypeTok{digits =} \DecValTok{0}\NormalTok{)}
\NormalTok{dat}\OperatorTok{$}\NormalTok{ncas \textless{}{-}}\StringTok{ }\KeywordTok{round}\NormalTok{(dat}\OperatorTok{$}\NormalTok{dprob }\OperatorTok{*}\StringTok{ }\NormalTok{dat}\OperatorTok{$}\NormalTok{npop, }\DataTypeTok{digits =} \DecValTok{0}\NormalTok{)}

\NormalTok{tmp \textless{}{-}}\StringTok{ }\KeywordTok{as.matrix}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(dat}\OperatorTok{$}\NormalTok{ncas, dat}\OperatorTok{$}\NormalTok{npop))}
\NormalTok{tmp \textless{}{-}}\StringTok{ }\KeywordTok{epi.conf}\NormalTok{(tmp, }\DataTypeTok{ctype =} \StringTok{"prevalence"}\NormalTok{, }\DataTypeTok{method =} \StringTok{"exact"}\NormalTok{, }\DataTypeTok{N =} \DecValTok{1000}\NormalTok{, }\DataTypeTok{design =} \DecValTok{1}\NormalTok{, }
   \DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{) }\OperatorTok{*}\StringTok{ }\DecValTok{100}
\NormalTok{dat \textless{}{-}}\StringTok{ }\KeywordTok{cbind}\NormalTok{(dat, tmp)}
\KeywordTok{head}\NormalTok{(dat)}
\CommentTok{\#\textgreater{}       dprob npop ncas      est    lower    upper}
\CommentTok{\#\textgreater{} 1 0.8009935  858  687 80.06993 77.23735 82.69419}
\CommentTok{\#\textgreater{} 2 0.4592104  252  116 46.03175 39.75835 52.39958}
\CommentTok{\#\textgreater{} 3 0.8920874 1363 1216 89.21497 87.44620 90.81251}
\CommentTok{\#\textgreater{} 4 0.7488760   24   18 75.00000 53.28872 90.22696}
\CommentTok{\#\textgreater{} 5 0.3515879  324  114 35.18519 29.98760 40.65647}
\CommentTok{\#\textgreater{} 6 0.7735446  428  331 77.33645 73.07148 81.21880}
\end{Highlighting}
\end{Shaded}

Sort the data in order of variable \texttt{est} and assign a 1 to
\texttt{n} identifier as variable \texttt{rank}:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dat \textless{}{-}}\StringTok{ }\NormalTok{dat[}\KeywordTok{sort.list}\NormalTok{(dat}\OperatorTok{$}\NormalTok{est),]}
\NormalTok{dat}\OperatorTok{$}\NormalTok{rank \textless{}{-}}\StringTok{ }\DecValTok{1}\OperatorTok{:}\KeywordTok{nrow}\NormalTok{(dat)}
\end{Highlighting}
\end{Shaded}

Now create the ranked error bar plot:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ rank, }\DataTypeTok{y =}\NormalTok{ est)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_errorbar}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{ymin =}\NormalTok{ lower, }\DataTypeTok{ymax =}\NormalTok{ upper), }\DataTypeTok{width =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_point}\NormalTok{() }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{100}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Rank"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{100}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Prevalence (cases per 100 individuals}
\StringTok{     at risk)"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/dfreq02-fig-1.pdf}
\caption{\label{fig:dfreq02}Ranked error bar plot showing the prevalence
of disease (and its 95\% confidence interval) for 100 population units.}
\end{figure}

\hypertarget{time}{%
\subsection{Time}\label{time}}

Epidemic curve data are often presented in one of two formats:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  One row for each individual identified as a case with an event date
  assigned to each.
\item
  One row for every event date with an integer representing the number
  of cases identified on that date.
\end{enumerate}

We first generate some data, with one row for every individual
identified as a case:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{n.males \textless{}{-}}\StringTok{ }\DecValTok{100}\NormalTok{; n.females \textless{}{-}}\StringTok{ }\DecValTok{50}
\NormalTok{odate \textless{}{-}}\StringTok{ }\KeywordTok{seq}\NormalTok{(}\DataTypeTok{from =} \KeywordTok{as.Date}\NormalTok{(}\StringTok{"2004{-}07{-}26"}\NormalTok{), }\DataTypeTok{to =} \KeywordTok{as.Date}\NormalTok{(}\StringTok{"2004{-}08{-}13"}\NormalTok{), }\DataTypeTok{by =} \DecValTok{1}\NormalTok{)}
\NormalTok{prob \textless{}{-}}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{9}\OperatorTok{:}\DecValTok{1}\NormalTok{); prob \textless{}{-}}\StringTok{ }\NormalTok{prob}\OperatorTok{/}\KeywordTok{sum}\NormalTok{(prob)}
\NormalTok{modate \textless{}{-}}\StringTok{ }\KeywordTok{sample}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ odate, }\DataTypeTok{size =}\NormalTok{ n.males, }\DataTypeTok{replace =} \OtherTok{TRUE}\NormalTok{, }\DataTypeTok{p =}\NormalTok{ prob)}
\NormalTok{fodate \textless{}{-}}\StringTok{ }\KeywordTok{sample}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ odate, }\DataTypeTok{size =}\NormalTok{ n.females, }\DataTypeTok{replace =} \OtherTok{TRUE}\NormalTok{)}
\NormalTok{dat \textless{}{-}}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{sex =} \KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\StringTok{"Male"}\NormalTok{, n.males), }\KeywordTok{rep}\NormalTok{(}\StringTok{"Female"}\NormalTok{, n.females)), }
   \DataTypeTok{odate =} \KeywordTok{c}\NormalTok{(modate, fodate))}
\end{Highlighting}
\end{Shaded}

Plot the epidemic curve using the \texttt{ggplot2} and \texttt{scales}
packages:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{library}\NormalTok{(ggplot2); }\KeywordTok{library}\NormalTok{(scales)}

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.Date}\NormalTok{(odate))) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{1}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"1 week"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%d \%b"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{30}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve01-fig-1.pdf}
\caption{\label{fig:epicurve01}Frequency histogram showing counts of
incident cases of disease as a function of time, 26 July to 13 August
2004.}
\end{figure}

Produce a separate epidemic curve for males and females using the
\texttt{facet\_grid} option in \texttt{ggplot2}:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.Date}\NormalTok{(odate))) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{1}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"1 week"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%d \%b"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{30}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{facet\_grid}\NormalTok{( }\OperatorTok{\textasciitilde{}}\StringTok{ }\NormalTok{sex)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve02-fig-1.pdf}
\caption{\label{fig:epicurve02}Frequency histogram showing counts of
incident cases of disease as a function of time, 26 July to 13 August
2004, conditioned by sex.}
\end{figure}

Let's say some event occurred on 31 July 2003. Mark this date on your
epidemic curve using \texttt{geom\_vline}:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.Date}\NormalTok{(odate))) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{1}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"1 week"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%d \%b"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{30}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{facet\_grid}\NormalTok{( }\OperatorTok{\textasciitilde{}}\StringTok{ }\NormalTok{sex) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom\_vline}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{xintercept =} \KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{as.Date}\NormalTok{(}\StringTok{"31/07/2004"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%Y"}\NormalTok{))), }
   \DataTypeTok{linetype =} \StringTok{"dashed"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve03-fig-1.pdf}
\caption{\label{fig:epicurve03}Frequency histogram showing counts of
incident cases of disease as a function of time, 26 July to 13 August
2004, conditioned by sex. An important event that occurred on 31 July
2004 is indicated by the vertical dashed line.}
\end{figure}

Plot the total number of disease events by day, coloured according to
sex:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.Date}\NormalTok{(odate), }\DataTypeTok{group =}\NormalTok{ sex, }\DataTypeTok{fill =}\NormalTok{ sex)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{1}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"1 week"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%d \%b"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{30}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom\_vline}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{xintercept =} \KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{as.Date}\NormalTok{(}\StringTok{"31/07/2004"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%Y"}\NormalTok{))), }
   \DataTypeTok{linetype =} \StringTok{"dashed"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{scale\_fill\_manual}\NormalTok{(}\DataTypeTok{values =} \KeywordTok{c}\NormalTok{(}\StringTok{"\#d46a6a"}\NormalTok{, }\StringTok{"\#738ca6"}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Sex"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \KeywordTok{c}\NormalTok{(}\FloatTok{0.90}\NormalTok{, }\FloatTok{0.80}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve04-fig-1.pdf}
\caption{\label{fig:epicurve04}Frequency histogram showing counts of
incident cases of disease as a function of time, 26 July to 13 August
2004, grouped by sex.}
\end{figure}

It can be difficult to appreciate differences in male and female disease
counts as a function of date with the above plot format so we dodge the
data instead.

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \KeywordTok{as.Date}\NormalTok{(odate), }\DataTypeTok{group =}\NormalTok{ sex, }\DataTypeTok{fill =}\NormalTok{ sex)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{1}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{, }\DataTypeTok{position =} \StringTok{"dodge"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"1 week"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%d \%b"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{30}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom\_vline}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{xintercept =} \KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{as.Date}\NormalTok{(}\StringTok{"31/07/2004"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%Y"}\NormalTok{))), }
   \DataTypeTok{linetype =} \StringTok{"dashed"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{scale\_fill\_manual}\NormalTok{(}\DataTypeTok{values =} \KeywordTok{c}\NormalTok{(}\StringTok{"\#d46a6a"}\NormalTok{, }\StringTok{"\#738ca6"}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Sex"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \KeywordTok{c}\NormalTok{(}\FloatTok{0.90}\NormalTok{, }\FloatTok{0.80}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve05-fig-1.pdf}
\caption{\label{fig:epicurve05}Frequency histogram showing counts of
incident cases of disease as a function of time, 26 July to 13 August
2004, grouped by sex.}
\end{figure}

We now provide code to deal with the situation where the data are
presented with one row for every case event date and an integer
representing the number of cases identified on each date.

Simulate some data in this format. In the code below the variable
\texttt{ncas} represents the number of cases identified on a given date.
The variable \texttt{dcontrol} is a factor with two levels: \texttt{neg}
and \texttt{pos}. Level \texttt{neg} flags dates when no disease control
measures were in place; level \texttt{pos} flags dates when disease
controls measures were in place.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{odate \textless{}{-}}\StringTok{ }\KeywordTok{seq}\NormalTok{(}\DataTypeTok{from =} \KeywordTok{as.Date}\NormalTok{(}\StringTok{"1/1/00"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%y"}\NormalTok{), }
   \DataTypeTok{to =} \KeywordTok{as.Date}\NormalTok{(}\StringTok{"1/1/05"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%y"}\NormalTok{), }\DataTypeTok{by =} \StringTok{"1 month"}\NormalTok{)}
\NormalTok{ncas \textless{}{-}}\StringTok{ }\KeywordTok{round}\NormalTok{(}\KeywordTok{runif}\NormalTok{(}\DataTypeTok{n =} \KeywordTok{length}\NormalTok{(odate), }\DataTypeTok{min =} \DecValTok{0}\NormalTok{, }\DataTypeTok{max =} \DecValTok{100}\NormalTok{), }\DataTypeTok{digits =} \DecValTok{0}\NormalTok{)}
\NormalTok{dat \textless{}{-}}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(odate, ncas)}
\NormalTok{dat}\OperatorTok{$}\NormalTok{dcontrol \textless{}{-}}\StringTok{ "neg"}
\NormalTok{dat}\OperatorTok{$}\NormalTok{dcontrol[dat}\OperatorTok{$}\NormalTok{odate }\OperatorTok{\textgreater{}=}\StringTok{ }\KeywordTok{as.Date}\NormalTok{(}\StringTok{"1/1/03"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%y"}\NormalTok{) }\OperatorTok{\&}\StringTok{ }
\StringTok{   }\NormalTok{dat}\OperatorTok{$}\NormalTok{odate }\OperatorTok{\textless{}=}\StringTok{ }\KeywordTok{as.Date}\NormalTok{(}\StringTok{"1/6/03"}\NormalTok{, }\DataTypeTok{format =} \StringTok{"\%d/\%m/\%y"}\NormalTok{)] \textless{}{-}}\StringTok{ "pos"}
\KeywordTok{head}\NormalTok{(dat)}
\CommentTok{\#\textgreater{}        odate ncas dcontrol}
\CommentTok{\#\textgreater{} 1 2000{-}01{-}01   92      neg}
\CommentTok{\#\textgreater{} 2 2000{-}02{-}01   81      neg}
\CommentTok{\#\textgreater{} 3 2000{-}03{-}01   63      neg}
\CommentTok{\#\textgreater{} 4 2000{-}04{-}01   70      neg}
\CommentTok{\#\textgreater{} 5 2000{-}05{-}01    8      neg}
\CommentTok{\#\textgreater{} 6 2000{-}06{-}01   17      neg}
\end{Highlighting}
\end{Shaded}

Generate an epidemic curve. Note \texttt{weight\ =\ ncas} in the
aesthetics argument for \texttt{ggplot2}:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(dat, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ odate, }\DataTypeTok{weight =}\NormalTok{ ncas, }\DataTypeTok{fill =} \KeywordTok{factor}\NormalTok{(dcontrol))) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_histogram}\NormalTok{(}\DataTypeTok{binwidth =} \DecValTok{60}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"gray"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.1}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_date}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{date\_breaks}\NormalTok{(}\StringTok{"6 months"}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{date\_format}\NormalTok{(}\StringTok{"\%b \%Y"}\NormalTok{), }
     \DataTypeTok{name =} \StringTok{"Date"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{200}\NormalTok{), }\DataTypeTok{name =} \StringTok{"Number of cases"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_fill\_manual}\NormalTok{(}\DataTypeTok{values =} \KeywordTok{c}\NormalTok{(}\StringTok{"\#2f4f4f"}\NormalTok{, }\StringTok{"red"}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{guides}\NormalTok{(}\DataTypeTok{fill =} \OtherTok{FALSE}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element\_text}\NormalTok{(}\DataTypeTok{angle =} \DecValTok{90}\NormalTok{, }\DataTypeTok{hjust =} \DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/epicurve06-fig-1.pdf}
\caption{\label{fig:epicurve06}Frequency histogram showing counts of
incident cases of disease as a function of time, 1 January 2000 to 1
January 2005. Colours indicate the presence or absence of disease
control measures.}
\end{figure}

\hypertarget{place}{%
\subsection{Place}\label{place}}

Two types of maps are often used when describing patterns of disease by
place:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Choropleth maps. Choropleth mapping involves producing a summary
  statistic of the outcome of interest (e.g.~count of disease events,
  prevalence, incidence) for each component area within a study region.
  A map is created by `filling' (i.e.~colouring) each component area
  with colour, providing an indication of the magnitude of the variable
  of interest and how it varies geographically.
\item
  Point maps.
\end{enumerate}

\textbf{Choropleth maps}

For illustration we make a choropleth map of sudden infant death
syndrome (SIDS) babies in North Carolina counties for 1974 using the
\texttt{nc.sids} data provided with the \texttt{spData} package.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(spData); }\KeywordTok{library}\NormalTok{(rgeos); }\KeywordTok{library}\NormalTok{(rgdal); }\KeywordTok{library}\NormalTok{(plyr); }\KeywordTok{library}\NormalTok{(RColorBrewer)}

\NormalTok{ncsids.shp \textless{}{-}}\StringTok{ }\KeywordTok{readOGR}\NormalTok{(}\KeywordTok{system.file}\NormalTok{(}\StringTok{"shapes/sids.shp"}\NormalTok{, }\DataTypeTok{package =} \StringTok{"spData"}\NormalTok{)[}\DecValTok{1}\NormalTok{])}
\CommentTok{\#\textgreater{} OGR data source with driver: ESRI Shapefile }
\CommentTok{\#\textgreater{} Source: "C:\textbackslash{}Program Files\textbackslash{}R\textbackslash{}R{-}3.6.3\textbackslash{}library\textbackslash{}spData\textbackslash{}shapes\textbackslash{}sids.shp", layer: "sids"}
\CommentTok{\#\textgreater{} with 100 features}
\CommentTok{\#\textgreater{} It has 22 fields}
\NormalTok{ncsids.shp}\OperatorTok{@}\NormalTok{data \textless{}{-}}\StringTok{ }\NormalTok{ncsids.shp}\OperatorTok{@}\NormalTok{data[,}\KeywordTok{c}\NormalTok{(}\StringTok{"BIR74"}\NormalTok{,}\StringTok{"SID74"}\NormalTok{)]}
\KeywordTok{head}\NormalTok{(ncsids.shp}\OperatorTok{@}\NormalTok{data)}
\CommentTok{\#\textgreater{}   BIR74 SID74}
\CommentTok{\#\textgreater{} 0  1091     1}
\CommentTok{\#\textgreater{} 1   487     0}
\CommentTok{\#\textgreater{} 2  3188     5}
\CommentTok{\#\textgreater{} 3   508     1}
\CommentTok{\#\textgreater{} 4  1421     9}
\CommentTok{\#\textgreater{} 5  1452     7}
\end{Highlighting}
\end{Shaded}

The \texttt{ncsids.shp} spatialPolygonsDataframe lists for each county
in the North Carolina USA the number SIDS deaths for 1974.

Prepare the spatialPolygonsDataframe by creating a 1 to \emph{n}
identifier called \texttt{id}. We then \texttt{fortify} the
spatialPolygonsDataframe to allow it to be used with \texttt{ggplot2}.
Finally, join the attribute data from spatialPolygonsDataframe
\texttt{ncsids.shp} to the fortified \texttt{ncsids.df}, using variable
\texttt{id} as the key:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ncsids.shp}\OperatorTok{$}\NormalTok{id \textless{}{-}}\StringTok{ }\DecValTok{1}\OperatorTok{:}\KeywordTok{nrow}\NormalTok{(ncsids.shp}\OperatorTok{@}\NormalTok{data)}
\NormalTok{ncsids.df \textless{}{-}}\StringTok{ }\KeywordTok{fortify}\NormalTok{(ncsids.shp, }\DataTypeTok{region =} \StringTok{"id"}\NormalTok{)}
\NormalTok{ncsids.df \textless{}{-}}\StringTok{ }\KeywordTok{join}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ ncsids.df, }\DataTypeTok{y =}\NormalTok{ ncsids.shp}\OperatorTok{@}\NormalTok{data, }\DataTypeTok{by =} \StringTok{"id"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Choropleth map of the counties of the North Carolina showing SIDS counts
for 1974:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ ncsids.df) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{theme\_bw}\NormalTok{() }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_polygon}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ long, }\DataTypeTok{y =}\NormalTok{ lat, }\DataTypeTok{group =}\NormalTok{ group, }\DataTypeTok{fill =}\NormalTok{ SID74)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom\_path}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ long, }\DataTypeTok{y =}\NormalTok{ lat, }\DataTypeTok{group =}\NormalTok{ group), }\DataTypeTok{colour =} \StringTok{"grey"}\NormalTok{, }\DataTypeTok{size =} \FloatTok{0.25}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_fill\_gradientn}\NormalTok{(}\DataTypeTok{limits =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{60}\NormalTok{), }\DataTypeTok{colours =} \KeywordTok{brewer.pal}\NormalTok{(}\DataTypeTok{n =} \DecValTok{5}\NormalTok{, }\StringTok{"Reds"}\NormalTok{), }
     \DataTypeTok{guide =} \StringTok{"colourbar"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_x\_continuous}\NormalTok{(}\DataTypeTok{name =} \StringTok{"Longitude"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_y\_continuous}\NormalTok{(}\DataTypeTok{name =} \StringTok{"Latitude"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{fill =} \StringTok{"SIDS 1974"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{coord\_map}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/spatial01-fig-1.pdf}
\caption{\label{fig:spatial01}Map of North Carolina, USA showing the
number of sudden infant death syndrome cases, by county for 1974.}
\end{figure}

\textbf{Point maps}

For this example we will used the \texttt{epi.incin} data set included
with \texttt{epiR}. Between 1972 and 1980 an industrial waste
incinerator operated at a site about 2 kilometres southwest of the town
of Coppull in Lancashire, England. Addressing community concerns that
there were greater than expected numbers of laryngeal cancer cases in
close proximity to the incinerator Diggle
(\protect\hyperlink{ref-diggle:1990}{1990}) conducted a study
investigating risks for laryngeal cancer, using recorded cases of lung
cancer as controls. The study area is 20 km x 20 km in size and includes
location of residence of patients diagnosed with each cancer type from
1974 to 1983.

Load the \texttt{epi.incin} data set and create negative and positive
labels for each point location. We don't have a boundary map for these
data so we'll use \texttt{spatstat} to create a convex hull around the
points and dilate the convex hull by 1000 metres as a proxy boundary.

Create an observation window for the data as \texttt{dat.w} and a
\texttt{ppp} object for plotting:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(spatstat)}

\KeywordTok{data}\NormalTok{(epi.incin); dat.df \textless{}{-}}\StringTok{ }\NormalTok{epi.incin}
\NormalTok{dat.df}\OperatorTok{$}\NormalTok{status \textless{}{-}}\StringTok{ }\KeywordTok{factor}\NormalTok{(dat.df}\OperatorTok{$}\NormalTok{status, }\DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{), }\DataTypeTok{labels =} \KeywordTok{c}\NormalTok{(}\StringTok{"Neg"}\NormalTok{, }\StringTok{"Pos"}\NormalTok{))}
\KeywordTok{names}\NormalTok{(dat.df)[}\DecValTok{3}\NormalTok{] \textless{}{-}}\StringTok{ "Status"}

\NormalTok{dat.w \textless{}{-}}\StringTok{ }\KeywordTok{convexhull.xy}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ dat.df[,}\DecValTok{1}\NormalTok{], }\DataTypeTok{y =}\NormalTok{ dat.df[,}\DecValTok{2}\NormalTok{])}
\NormalTok{dat.w \textless{}{-}}\StringTok{ }\KeywordTok{dilation}\NormalTok{(dat.w, }\DataTypeTok{r =} \DecValTok{1000}\NormalTok{)}
\NormalTok{dat.ppp \textless{}{-}}\StringTok{ }\KeywordTok{ppp}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ dat.df[,}\DecValTok{1}\NormalTok{], }\DataTypeTok{y =}\NormalTok{ dat.df[,}\DecValTok{2}\NormalTok{], }\DataTypeTok{marks =} \KeywordTok{factor}\NormalTok{(dat.df[,}\DecValTok{3}\NormalTok{]), }\DataTypeTok{window =}\NormalTok{ dat.w)}
\end{Highlighting}
\end{Shaded}

Create a SpatialPolygonsDataFrame from \texttt{dat.w}:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{coords \textless{}{-}}\StringTok{ }\KeywordTok{matrix}\NormalTok{(}\KeywordTok{c}\NormalTok{(dat.w}\OperatorTok{$}\NormalTok{bdry[[}\DecValTok{1}\NormalTok{]]}\OperatorTok{$}\NormalTok{x, dat.w}\OperatorTok{$}\NormalTok{bdry[[}\DecValTok{1}\NormalTok{]]}\OperatorTok{$}\NormalTok{y), }\DataTypeTok{ncol =} \DecValTok{2}\NormalTok{, }\DataTypeTok{byrow =} \OtherTok{FALSE}\NormalTok{)}
\NormalTok{pol \textless{}{-}}\StringTok{ }\KeywordTok{Polygon}\NormalTok{(coords, }\DataTypeTok{hole =} \OtherTok{FALSE}\NormalTok{)}
\NormalTok{pol \textless{}{-}}\StringTok{ }\KeywordTok{Polygons}\NormalTok{(}\KeywordTok{list}\NormalTok{(pol),}\DecValTok{1}\NormalTok{)}
\NormalTok{pol \textless{}{-}}\StringTok{ }\KeywordTok{SpatialPolygons}\NormalTok{(}\KeywordTok{list}\NormalTok{(pol))}
\NormalTok{pol.spdf \textless{}{-}}\StringTok{ }\KeywordTok{SpatialPolygonsDataFrame}\NormalTok{(}\DataTypeTok{Sr =}\NormalTok{ pol, }\DataTypeTok{data =} \KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{id =} \DecValTok{1}\NormalTok{), }\DataTypeTok{match.ID =} \OtherTok{TRUE}\NormalTok{)}
\NormalTok{pol.map \textless{}{-}}\StringTok{ }\KeywordTok{fortify}\NormalTok{(pol.spdf)}
\CommentTok{\#\textgreater{} Regions defined for each Polygons}
\end{Highlighting}
\end{Shaded}

Plot the data as a point map:

\begin{Shaded}
\begin{Highlighting}[]

\KeywordTok{ggplot}\NormalTok{() }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_point}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ dat.df, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ xcoord, }\DataTypeTok{y =}\NormalTok{ ycoord, }\DataTypeTok{colour =}\NormalTok{ Status, }\DataTypeTok{shape =}\NormalTok{ Status)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{geom\_polygon}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ pol.map, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ long, }\DataTypeTok{y =}\NormalTok{ lat, }\DataTypeTok{group =}\NormalTok{ group), }\DataTypeTok{col =} \StringTok{"black"}\NormalTok{, }
     \DataTypeTok{fill =} \StringTok{"transparent"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{scale\_colour\_manual}\NormalTok{(}\DataTypeTok{values =} \KeywordTok{c}\NormalTok{(}\StringTok{"blue"}\NormalTok{, }\StringTok{"red"}\NormalTok{)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{scale\_shape\_manual}\NormalTok{(}\DataTypeTok{values =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{16}\NormalTok{)) }\OperatorTok{+}
\StringTok{  }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Easting (m)"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Northing (m)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Status"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{coord\_equal}\NormalTok{() }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{theme\_bw}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{epiR_descriptive_files/figure-latex/spatial02-fig-1.pdf}
\caption{\label{fig:spatial02}Point map showing the place of residence
of individuals diagnosed with laryngeal cancer (Pos) and lung cancer
(Neg), Copull Lancashire, UK, 1972 to 1980.}
\end{figure}

\hypertarget{measures-of-association}{%
\subsection{Measures of association}\label{measures-of-association}}

An important task in epidemiology is to quantify the strength of
association between exposures and outcomes. In this context the term
`exposure' is taken to mean a variable whose association with the
outcome is to be estimated.

Exposures can be harmful, beneficial or both harmful and beneficial
(e.g.~if an immunisable disease is circulating, exposure to immunising
agents helps most recipients but may harm those who experience adverse
reactions). The term `outcome' is used to describe all the possible
results that may arise from exposure to a causal factor or from
preventive or therapeutic interventions (Porta, Greenland, and Last
\protect\hyperlink{ref-porta_et_al:2008}{2008}).

In this section we outline describe how \texttt{epiR} can be used to
compute the various measures of association used in epidemiology notably
the risk ratio, odds ratio, attributable risk, attributable fraction,
population attributable risk and population attributable fraction.
Examples are provided to demonstrate how the package can be used to deal
with exposure-outcome data in various formats.

Some preliminary comments. The \texttt{epi.2by2} function in
\texttt{epiR} requires an object of class \texttt{table} as input. This
vignette has been written assuming the reader routinely formats their 2
by 2 table data with the outcome status as columns and exposure status
as rows. If this is not the case the argument
\texttt{outcome\ =\ "as.columns"} (the default) can be changed to
\texttt{outcome\ =\ "as.rows"}.

\textbf{Direct entry of cell frequencies}

Create a 2 by 2 table by keying in the cell frequencies.

A cross sectional study investigating the relationship between dry cat
food (DCF) and feline lower urinary tract disease (FLUTD) was conducted
(Willeberg \protect\hyperlink{ref-willeberg:1977}{1977}). Counts of
individuals in each group were as follows. DCF-exposed cats (cases,
non-cases) 13, 2163. Non DCF-exposed cats (cases, non-cases) 5, 3349.
Enter these data directly into R as a matrix:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{flutd.tab \textless{}{-}}\StringTok{ }\KeywordTok{matrix}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{13}\NormalTok{,}\DecValTok{2163}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{3349}\NormalTok{), }\DataTypeTok{nrow =} \DecValTok{2}\NormalTok{, }\DataTypeTok{byrow =} \OtherTok{TRUE}\NormalTok{)}
\KeywordTok{rownames}\NormalTok{(flutd.tab) \textless{}{-}}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"DF+"}\NormalTok{, }\StringTok{"DF{-}"}\NormalTok{); }\KeywordTok{colnames}\NormalTok{(flutd.tab) \textless{}{-}}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"FLUTD+"}\NormalTok{, }\StringTok{"FLUTD{-}"}\NormalTok{)}
\NormalTok{flutd.tab \textless{}{-}}\StringTok{ }\KeywordTok{as.table}\NormalTok{(flutd.tab); flutd.tab}
\CommentTok{\#\textgreater{}     FLUTD+ FLUTD{-}}
\CommentTok{\#\textgreater{} DF+     13   2163}
\CommentTok{\#\textgreater{} DF{-}      5   3349}
\end{Highlighting}
\end{Shaded}

Calculate the prevalence ratio, odds ratio, attributable prevalence, the
attributable prevalence in the population, the attributable fraction in
the exposed and the attributable fraction in the population using
\texttt{epi.2by2}:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{epi.2by2}\NormalTok{(}\DataTypeTok{dat =}\NormalTok{ flutd.tab, }\DataTypeTok{method =} \StringTok{"cross.sectional"}\NormalTok{, }\DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{, }
   \DataTypeTok{units =} \DecValTok{100}\NormalTok{, }\DataTypeTok{outcome =} \StringTok{"as.columns"}\NormalTok{)}
\CommentTok{\#\textgreater{}              Outcome +    Outcome {-}      Total        Prevalence *        Odds}
\CommentTok{\#\textgreater{} Exposed +           13         2163       2176               0.597     0.00601}
\CommentTok{\#\textgreater{} Exposed {-}            5         3349       3354               0.149     0.00149}
\CommentTok{\#\textgreater{} Total               18         5512       5530               0.325     0.00327}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} Point estimates and 95\% CIs:}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{} Prevalence ratio                             4.01 (1.43, 11.23)}
\CommentTok{\#\textgreater{} Odds ratio                                   4.03 (1.43, 11.31)}
\CommentTok{\#\textgreater{} Attrib prevalence *                          0.45 (0.10, 0.80)}
\CommentTok{\#\textgreater{} Attrib prevalence in population *            0.18 ({-}0.02, 0.38)}
\CommentTok{\#\textgreater{} Attrib fraction in exposed (\%)              75.05 (30.11, 91.09)}
\CommentTok{\#\textgreater{} Attrib fraction in population (\%)           54.20 (3.61, 78.24)}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{}  Test that OR = 1: chi2(1) = 8.177 Pr\textgreater{}chi2 = 0.00}
\CommentTok{\#\textgreater{}  Wald confidence limits}
\CommentTok{\#\textgreater{}  CI: confidence interval}
\CommentTok{\#\textgreater{}  * Outcomes per 100 population units}
\end{Highlighting}
\end{Shaded}

The prevalence of FLUTD in DCF exposed cats was 4.01 (95\% CI 1.43 to
11.23) times greater than the prevalence of FLUTD in non-DCF exposed
cats.

In DCF exposed cats, 75\% of FLUTD was attributable to DCF (95\% CI 30\%
to 91\%). Fifty-four percent of FLUTD cases in this cat population were
attributable to DCF (95\% CI 4\% to 78\%).

\textbf{Data frame with one row per observation}

For this example we use the low infant birth weight data presented by
Hosmer and Lemeshow (\protect\hyperlink{ref-hosmer_lemeshow:2000}{2000})
and available in the \texttt{MASS} package in R. The \texttt{birthwt}
data frame has 189 rows and 10 columns. The data were collected at
Baystate Medical Center, Springfield, Massachusetts USA during 1986.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(MASS)}
\NormalTok{bwt \textless{}{-}}\StringTok{ }\NormalTok{birthwt; }\KeywordTok{head}\NormalTok{(bwt)}
\CommentTok{\#\textgreater{}    low age lwt race smoke ptl ht ui ftv  bwt}
\CommentTok{\#\textgreater{} 85   0  19 182    2     0   0  0  1   0 2523}
\CommentTok{\#\textgreater{} 86   0  33 155    3     0   0  0  0   3 2551}
\CommentTok{\#\textgreater{} 87   0  20 105    1     1   0  0  0   1 2557}
\CommentTok{\#\textgreater{} 88   0  21 108    1     1   0  0  1   2 2594}
\CommentTok{\#\textgreater{} 89   0  18 107    1     1   0  0  1   0 2600}
\CommentTok{\#\textgreater{} 91   0  21 124    3     0   0  0  0   0 2622}
\end{Highlighting}
\end{Shaded}

Each row of this data set represents data for one mother. We're
interested in the association between \texttt{smoke} (the mother's
smoking status during pregnancy) and \texttt{low} (delivery of a baby
less than 2.5 kg bodyweight).

Its important that the table you present to \texttt{epi.2by2} is in the
correct format: Disease positives in the first column, disease negatives
in the second column, exposure positives in the first row and exposure
negatives in the second row. If we run the \texttt{table} function on
the \texttt{bwt} data the output table is in the wrong format:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{low.tab \textless{}{-}}\StringTok{ }\KeywordTok{table}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{smoke, bwt}\OperatorTok{$}\NormalTok{low, }\DataTypeTok{dnn =} \KeywordTok{c}\NormalTok{(}\StringTok{"Smoke"}\NormalTok{, }\StringTok{"Low BW"}\NormalTok{)); low.tab}
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  0  1}
\CommentTok{\#\textgreater{}     0 86 29}
\CommentTok{\#\textgreater{}     1 44 30}
\end{Highlighting}
\end{Shaded}

There are two approaches for fixing this problem. For the first aproach
we ask R to switch the order of the rows and columns:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{low.tab \textless{}{-}}\StringTok{ }\KeywordTok{table}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{smoke, bwt}\OperatorTok{$}\NormalTok{low, }\DataTypeTok{dnn =} \KeywordTok{c}\NormalTok{(}\StringTok{"Smoke"}\NormalTok{, }\StringTok{"Low BW"}\NormalTok{))}
\NormalTok{low.tab \textless{}{-}}\StringTok{ }\NormalTok{low.tab[}\DecValTok{2}\OperatorTok{:}\DecValTok{1}\NormalTok{,}\DecValTok{2}\OperatorTok{:}\DecValTok{1}\NormalTok{]; low.tab}
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  1  0}
\CommentTok{\#\textgreater{}     1 30 44}
\CommentTok{\#\textgreater{}     0 29 86}
\end{Highlighting}
\end{Shaded}

The second approach is to set the exposure variable and the outcome
variable as a factor and to define the levels of each factor using
\texttt{levels\ =\ c(1,0)}:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bwt}\OperatorTok{$}\NormalTok{low \textless{}{-}}\StringTok{ }\KeywordTok{factor}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{low, }\DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{0}\NormalTok{))}
\NormalTok{bwt}\OperatorTok{$}\NormalTok{smoke \textless{}{-}}\StringTok{ }\KeywordTok{factor}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{smoke, }\DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{0}\NormalTok{))}
\NormalTok{bwt}\OperatorTok{$}\NormalTok{race \textless{}{-}}\StringTok{ }\KeywordTok{factor}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{race, }\DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Now generate the 2 by 2 table. Exposure (rows) = \texttt{smoke}, outcome
(columns) = \texttt{low}:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{low.tab \textless{}{-}}\StringTok{ }\KeywordTok{table}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{smoke, bwt}\OperatorTok{$}\NormalTok{low, }\DataTypeTok{dnn =} \KeywordTok{c}\NormalTok{(}\StringTok{"Smoke"}\NormalTok{, }\StringTok{"Low BW"}\NormalTok{)); low.tab}
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  1  0}
\CommentTok{\#\textgreater{}     1 30 44}
\CommentTok{\#\textgreater{}     0 29 86}
\end{Highlighting}
\end{Shaded}

Compute the odds ratio for smoking and delivery of a low birth weight
baby:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{epi.2by2}\NormalTok{(}\DataTypeTok{dat =}\NormalTok{ low.tab, }\DataTypeTok{method =} \StringTok{"cohort.count"}\NormalTok{, }\DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{, }
   \DataTypeTok{units =} \DecValTok{100}\NormalTok{, }\DataTypeTok{outcome =} \StringTok{"as.columns"}\NormalTok{)}
\CommentTok{\#\textgreater{}              Outcome +    Outcome {-}      Total        Inc risk *        Odds}
\CommentTok{\#\textgreater{} Exposed +           30           44         74              40.5       0.682}
\CommentTok{\#\textgreater{} Exposed {-}           29           86        115              25.2       0.337}
\CommentTok{\#\textgreater{} Total               59          130        189              31.2       0.454}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} Point estimates and 95\% CIs:}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{} Inc risk ratio                               1.61 (1.06, 2.44)}
\CommentTok{\#\textgreater{} Odds ratio                                   2.02 (1.08, 3.78)}
\CommentTok{\#\textgreater{} Attrib risk *                                15.32 (1.61, 29.04)}
\CommentTok{\#\textgreater{} Attrib risk in population *                  6.00 ({-}4.33, 16.33)}
\CommentTok{\#\textgreater{} Attrib fraction in exposed (\%)               37.80 (5.47, 59.07)}
\CommentTok{\#\textgreater{} Attrib fraction in population (\%)            19.22 ({-}0.21, 34.88)}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{}  Test that OR = 1: chi2(1) = 4.924 Pr\textgreater{}chi2 = 0.03}
\CommentTok{\#\textgreater{}  Wald confidence limits}
\CommentTok{\#\textgreater{}  CI: confidence interval}
\CommentTok{\#\textgreater{}  * Outcomes per 100 population units}
\end{Highlighting}
\end{Shaded}

The odds of having a low birth weight child for smokers is 2.02 (95\% CI
1.08 to 3.78) times greater than the odds of having a low birth weight
child for non-smokers.

We're concerned that the mother's race may confound the association
between low birth weight and delivery of a low birth weight baby.
Stratify the 2 by 2 table by race:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{low.stab \textless{}{-}}\StringTok{ }\KeywordTok{table}\NormalTok{(bwt}\OperatorTok{$}\NormalTok{smoke, bwt}\OperatorTok{$}\NormalTok{low, bwt}\OperatorTok{$}\NormalTok{race, }\DataTypeTok{dnn =} \KeywordTok{c}\NormalTok{(}\StringTok{"Smoke"}\NormalTok{, }\StringTok{"Low BW"}\NormalTok{, }\StringTok{"Race"}\NormalTok{))}
\NormalTok{low.stab}
\CommentTok{\#\textgreater{} , , Race = 1}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  1  0}
\CommentTok{\#\textgreater{}     1 19 33}
\CommentTok{\#\textgreater{}     0  4 40}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} , , Race = 2}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  1  0}
\CommentTok{\#\textgreater{}     1  6  4}
\CommentTok{\#\textgreater{}     0  5 11}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} , , Race = 3}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{}      Low BW}
\CommentTok{\#\textgreater{} Smoke  1  0}
\CommentTok{\#\textgreater{}     1  5  7}
\CommentTok{\#\textgreater{}     0 20 35}
\end{Highlighting}
\end{Shaded}

Compute the crude odds ratio and the Mantel-Haenszel adjusted odds
ratio. \texttt{epi.2by2} automatically calculates the Mantel-Haenszel
odds ratio and risk ratio when it is presented with stratified
contingency tables.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rval \textless{}{-}}\StringTok{ }\KeywordTok{epi.2by2}\NormalTok{(}\DataTypeTok{dat =}\NormalTok{ low.stab, }\DataTypeTok{method =} \StringTok{"cohort.count"}\NormalTok{, }\DataTypeTok{conf.level =} \FloatTok{0.95}\NormalTok{, }
   \DataTypeTok{units =} \DecValTok{100}\NormalTok{, }\DataTypeTok{outcome =} \StringTok{"as.columns"}\NormalTok{)}
\KeywordTok{print}\NormalTok{(rval)}
\CommentTok{\#\textgreater{}              Outcome +    Outcome {-}      Total        Inc risk *        Odds}
\CommentTok{\#\textgreater{} Exposed +           30           44         74              40.5       0.682}
\CommentTok{\#\textgreater{} Exposed {-}           29           86        115              25.2       0.337}
\CommentTok{\#\textgreater{} Total               59          130        189              31.2       0.454}
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} }
\CommentTok{\#\textgreater{} Point estimates and 95\% CIs:}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{} Inc risk ratio (crude)                       1.61 (1.06, 2.44)}
\CommentTok{\#\textgreater{} Inc risk ratio (M{-}H)                         2.15 (1.29, 3.58)}
\CommentTok{\#\textgreater{} Inc risk ratio (crude:M{-}H)                   0.75}
\CommentTok{\#\textgreater{} Odds ratio (crude)                           2.02 (1.08, 3.78)}
\CommentTok{\#\textgreater{} Odds ratio (M{-}H)                             3.09 (1.49, 6.39)}
\CommentTok{\#\textgreater{} Odds ratio (crude:M{-}H)                       0.66}
\CommentTok{\#\textgreater{} Attrib risk (crude) *                        15.32 (1.61, 29.04)}
\CommentTok{\#\textgreater{} Attrib risk (M{-}H) *                          22.17 (1.41, 42.94)}
\CommentTok{\#\textgreater{} Attrib risk (crude:M{-}H)                      0.69}
\CommentTok{\#\textgreater{} {-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}{-}}
\CommentTok{\#\textgreater{}  M{-}H test of homogeneity of RRs: chi2(2) = 3.862 Pr\textgreater{}chi2 = 0.15}
\CommentTok{\#\textgreater{}  M{-}H test of homogeneity of ORs: chi2(2) = 2.800 Pr\textgreater{}chi2 = 0.25}
\CommentTok{\#\textgreater{}  Test that M{-}H adjusted OR = 1:  chi2(2) = 9.413 Pr\textgreater{}chi2 = 0.00}
\CommentTok{\#\textgreater{}  Wald confidence limits}
\CommentTok{\#\textgreater{}  M{-}H: Mantel{-}Haenszel; CI: confidence interval}
\CommentTok{\#\textgreater{}  * Outcomes per 100 population units}
\end{Highlighting}
\end{Shaded}

The Mantel-Haenszel test of homogeneity of the strata odds ratios is not
significant (chi square test statistic 2.800; df 2; p-value = 0.25) so
we accept the null hypothesis and conclude that the odds ratios for each
strata of race are the same. After accounting for the confounding effect
of race, the odds of having a low birth weight child for smokers is 3.09
(95\% CI 1.49 to 6.39) times that of non-smokers.

\hypertarget{references}{%
\subsection*{References}\label{references}}
\addcontentsline{toc}{subsection}{References}

\hypertarget{refs}{}
\begin{cslreferences}
\leavevmode\hypertarget{ref-cdc:2006}{}%
Centers for Disease Control and Prevention. 2006. \emph{Principles of
Epidemiology in Public Health Practice: An Introduction to Applied
Epidemiology and Biostatistics}. Book. Atlanta, Georgia: Centers for
Disease Control; Prevention.

\leavevmode\hypertarget{ref-diggle:1990}{}%
Diggle, PJ. 1990. ``A point process modeling approach to raised
incidence of a rare phenomenon in the vicinity of a prespecified
point.'' \emph{Journal of the Royal Statistical Society Series A} 153:
349--62.

\leavevmode\hypertarget{ref-feychting_et_al:1998}{}%
Feychting, M, B Osterlund, and A Ahlbom. 1998. ``Reduced cancer
incidence among the blind.'' \emph{Epidemiology} 9: 490--94.

\leavevmode\hypertarget{ref-hosmer_lemeshow:2000}{}%
Hosmer, DW, and S Lemeshow. 2000. \emph{Applied Logistic Regression}.
London: Jon Wiley; Sons Inc.

\leavevmode\hypertarget{ref-porta_et_al:2008}{}%
Porta, M, S Greenland, and JM Last. 2008. \emph{A Dictionary of
Epidemiology}. London: Oxford University Press.

\leavevmode\hypertarget{ref-willeberg:1977}{}%
Willeberg, P. 1977. ``Animal disease information processing:
Epidemiologic analyses of the feline urologic syndrome.'' \emph{Acta
Veterinaria Scandinavica} 64: 1--48.
\end{cslreferences}

\end{document}
