\documentclass[letterpaper,9pt,twoside,printwatermark=false]{pinp}

%% Some pieces required from the pandoc template
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Use the lineno option to display guide line numbers if required.
% Note that the use of elements such as single-column equations
% may affect the guide line number alignment.

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}

% The geometry package layout settings need to be set here...
\geometry{layoutsize={0.95588\paperwidth,0.98864\paperheight},%
          layouthoffset=0.02206\paperwidth,%
		  layoutvoffset=0.00568\paperheight}

\definecolor{pinpblue}{HTML}{185FAF}  % imagecolorpicker on blue for new R logo
\definecolor{pnasbluetext}{RGB}{101,0,0} %



\title{Assignment 2 - Histograms, Medians, Means, Boxplots and Standard
Deviation. Due September 21, 11:59pm 2018}

\author[a]{EPIB607 - Inferential Statistics}

  \affil[a]{Fall 2018, McGill University}

\setcounter{secnumdepth}{5}

% Please give the surname of the lead author for the running footer
\leadauthor{Bhatnagar and Hanley}

% Keywords are not mandatory, but authors are strongly encouraged to provide them. If provided, please include two to five keywords, separated by the pipe symbol, e.g:
 \keywords{  Histograms |  Means |  Medians |  Boxplots |  Standard Deviation |  Summary statistics |  mosaic package  }  

\begin{abstract}
The first step in understanding data is to hear what the data say, to
``let the data speak for themselves''. Numbers speak clearly only when
we help them speak by organizing, displaying, and summarizing. In this
assignment you will explore how to visualize your data and summarize it
using summary statistics. All graphs and calculations are to be
completed in an R Markdown document using the provided template. You are
free to choose any function from any package to complete the assignment.
Concise answers will be rewarded. Be brief and to the point. Please
submit both the compiled HTML report and the source file (.Rmd) to
myCourses by September 21, 2018, 11:59pm. Both HTML and .Rmd files
should be saved as `IDnumber\_LastName\_FirstName\_EPIB607\_A2'.
\end{abstract}

\dates{This version was compiled on \today}
\doi{\url{https://sahirbhatnagar.com/EPIB607/}}

\pinpfootercontents{Assignment 2 due Sepetember 21, 2018 by 11:59pm}

\begin{document}

% Optional adjustment to line up main text (after abstract) of first page with line numbers, when using both lineno and twocolumn options.
% You should only change this length when you've finalised the article contents.
\verticaladjustment{-2pt}

\maketitle
\thispagestyle{firststyle}
\ifthenelse{\boolean{shortarticle}}{\ifthenelse{\boolean{singlecolumn}}{\abscontentformatted}{\abscontent}}{}

% If your first paragraph (i.e. with the \dropcap) contains a list environment (quote, quotation, theorem, definition, enumerate, itemize...), the line after the list may have some extra indentation. If this is the case, add \parshape=0 to the end of the list environment.


\section*{Template}\label{template}
\addcontentsline{toc}{section}{Template}

The \texttt{.Rmd} template for Assignment 2 is available
\href{https://github.com/sahirbhatnagar/EPIB607/raw/master/assignments/a2/a2_template.Rmd}{here}

\section*{\texorpdfstring{The \texttt{mosaic} package
(optional)}{The mosaic package (optional)}}\label{the-mosaic-package-optional}
\addcontentsline{toc}{section}{The \texttt{mosaic} package (optional)}

The \texttt{mosaic} package provides a consistent and user-friendly
interface for descriptive statistics, plots and inference. You may find
it useful to complete an interactive tutorial on its plotting functions.
(note: this is optional and will not be counted for any marks). First
install the following packages:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"learnr"}\NormalTok{,}\StringTok{"mosaic"}\NormalTok{), }\DataTypeTok{dependencies =} \OtherTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Then, from RStudio, run the following command which will open a new page
in your web broswer:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{learnr}\OperatorTok{::}\KeywordTok{run_tutorial}\NormalTok{(}\StringTok{"introduction"}\NormalTok{, }\DataTypeTok{package =} \StringTok{"ggformula"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

An advanced tutorial on customizing plots is available also:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{learnr}\OperatorTok{::}\KeywordTok{run_tutorial}\NormalTok{(}\StringTok{"refining"}\NormalTok{, }\DataTypeTok{package =} \StringTok{"ggformula"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\section{Age-structures of Populations, then and
now}\label{age-structures-of-populations-then-and-now}

The 1911 census of Ireland was taken on April 2nd 1911 and was released
to the public in 1961. Follow
\href{http://www.census.nationalarchives.ie/help/about19011911census.html}{this
link} for further details on the census. James Hanley (JH) has scrapped
the data for Dublin, collected the age-frequency distribtion by gender
and provided you with a
\href{https://github.com/sahirbhatnagar/EPIB607/raw/master/data/age_sex_frequencies_ireland.csv}{three
column .csv file} which looks like this:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cens <-}\StringTok{ }\KeywordTok{read.csv}\NormalTok{(}\StringTok{"https://github.com/sahirbhatnagar/EPIB607/raw/master/data/age_sex_frequencies_ireland.csv"}\NormalTok{)}
\KeywordTok{head}\NormalTok{(cens)}
\end{Highlighting}
\end{Shaded}

\begin{ShadedResult}
\begin{verbatim}
#    Gender Age Freq
#  1   Male   0 5332
#  2   Male   1 4570
#  3   Male   2 4979
#  4   Male   3 4789
#  5   Male   4 4884
#  6   Male   5 4787
\end{verbatim}
\end{ShadedResult}

The \texttt{Age} column represents the age in 1911. The \texttt{Freq}
column gives the frequency of the number of people for a given age and
\texttt{Gender}.

\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\tightlist
\item
  What was the ealiest year of birth for (i) males and (ii) females ?
\item
  Create a suitable visulaization of this data and then comment on any
  patterns you see and give reasons for these patterns. Your choice
  should leverage all the information provided in the data and be
  influeced by the message that you are trying to convey. Be sure to
  include an informative title and figure caption.
\item
  Calculate the mean age, the standard deviation (SD), and the
  quartiles: \(Q_{25}, Q_{50} (median), Q_{75}\) separately for males
  and females.
\item
  The orignial census cards have been scanned are available online.
  \href{http://www.census.nationalarchives.ie/reels/nai000230598/}{This
  one in particular} is quite famous. Why?
\end{enumerate}

\section{Number of Authors}\label{number-of-authors}

Fletcher et al. (1979, NEJM 301:180-183) studied the characteristics of
612 randomly selected articles published in the NEJM, JAMA, and the
Lancet since 1946. Two characteristics examined were the number of
authors per article and the number of students studied in each article:

\begin{longtable}[]{@{}llll@{}}
\toprule
Year & No. articles studied & No. Authors Mean (SD) & No. subjects
Median\tabularnewline
\midrule
\endhead
1946 & 151 & 2.0 (1.4) & 25\tabularnewline
1956 & 149 & 2.3 (1.6) & 36\tabularnewline
1966 & 157 & 2.8 (1.2) & 16\tabularnewline
1976 & 155 & 4.9 (7.3) & 30\tabularnewline
\bottomrule
\end{longtable}

\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\tightlist
\item
  Why report median number of subjects (instead of average)?
\item
  In 1976, can the standard deviation of 7.3 really be larger than the
  mean of 4.9? Explain.
\item
  In light of (a) and (b), how would you present the data in this table?
\end{enumerate}

\section{Cancer Deaths in the US}\label{cancer-deaths-in-the-us}

The American Cancer Society (ACS) recently published age-adjusted cancer
death rates by cancer site and gender at this
\href{https://www.cancer.org/research/cancer-facts-statistics/all-cancer-facts-figures/cancer-facts-figures-2017.html}{link}.

\begin{enumerate}
\def\labelenumi{\alph{enumi})}
\tightlist
\item
  In the figure
  \href{https://www.cancer.org/content/dam/cancer-org/research/cancer-facts-and-statistics/annual-cancer-facts-and-figures/2017/trends-in-age-adjusted-cancer-death-rates-by-site-males-us-1930-2014.pdf}{Trends
  in Age-adjusted Cancer Death Rates by Site, Males, US, 1930-2014
  (PDF)} list the scales being used and the variable that has been
  mapped onto them.
\item
  Briefly comment on what you like about the figure, and what could have
  been improved (e.g.~aesthetics, labels, use of color)
\item
  The data used to make the figures has also been provided on the ACS
  website in Excel spreasheets:
  \href{https://www.cancer.org/content/dam/cancer-org/research/cancer-facts-and-statistics/annual-cancer-facts-and-figures/2017/age-adjusted-cancer-death-rates-males-1930-2014.xlsx}{{[}males{]}},
  \href{https://www.cancer.org/content/dam/cancer-org/research/cancer-facts-and-statistics/annual-cancer-facts-and-figures/2017/age-adjusted-cancer-death-rates-females-1930-2014.xlsx}{{[}females{]}}.
  Once you have downloaded the spreadsheets, the following code can be
  used to combine both datasets:
\end{enumerate}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(dplyr)}
\NormalTok{males <-}\StringTok{ }\NormalTok{readxl}\OperatorTok{::}\KeywordTok{read_xlsx}\NormalTok{(}\StringTok{"~/Downloads/age-adjusted-cancer-death-rates-males-1930-2014.xlsx"}\NormalTok{, }
                           \DataTypeTok{skip =} \DecValTok{1}\NormalTok{, }\DataTypeTok{n_max =} \DecValTok{85}\NormalTok{) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Gender =} \StringTok{"Male"}\NormalTok{)  }
\NormalTok{females <-}\StringTok{ }\NormalTok{readxl}\OperatorTok{::}\KeywordTok{read_xlsx}\NormalTok{(}\StringTok{"~/Downloads/age-adjusted-cancer-death-rates-females-1930-2014.xlsx"}\NormalTok{, }
                             \DataTypeTok{skip =} \DecValTok{1}\NormalTok{, }\DataTypeTok{n_max =} \DecValTok{85}\NormalTok{) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Gender =} \StringTok{"Female"}\NormalTok{)}
\NormalTok{cancer_rates <-}\StringTok{ }\NormalTok{dplyr}\OperatorTok{::}\KeywordTok{bind_rows}\NormalTok{(}\KeywordTok{list}\NormalTok{(females,males))}
\KeywordTok{head}\NormalTok{(cancer_rates)}
\end{Highlighting}
\end{Shaded}

\begin{ShadedResult}
\begin{verbatim}
#  # A tibble: 6 x 13
#     Year Stomach `Colon and Rect~ `Pancreas‡` `Lung and Bronc~ Breast
#    <dbl>   <dbl>            <dbl>       <dbl>            <dbl>  <dbl>
#  1  1930    35.2             27.1        3.82             2.58   30.1
#  2  1931    33.8             27.7        4.12             2.63   30.6
#  3  1932    33.7             28.5        4.50             2.84   30.9
#  4  1933    32.5             28.7        4.27             2.96   30.8
#  5  1934    31.6             29.7        4.36             3.08   31.6
#  6  1935    31.4             30.2        4.67             3.54   31.3
#  # ... with 7 more variables: `Uterus†` <dbl>, `Liver‡` <dbl>,
#  #   Gender <chr>, `Liver†` <dbl>, `Pancreas†` <dbl>, Prostate <dbl>,
#  #   Leukemia <dbl>
\end{verbatim}
\end{ShadedResult}

In order to make the data ready for plotting, we need to \emph{melt} it
first:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(tidyr)}
\NormalTok{cancer_rates_melt <-}\StringTok{ }\NormalTok{tidyr}\OperatorTok{::}\KeywordTok{gather}\NormalTok{(cancer_rates, }\DataTypeTok{key =} \StringTok{"Site"}\NormalTok{, }\DataTypeTok{value =} \StringTok{"Rate"}\NormalTok{, }\OperatorTok{-}\NormalTok{Year, }\OperatorTok{-}\NormalTok{Gender)}
\KeywordTok{head}\NormalTok{(cancer_rates_melt, }\DataTypeTok{n =} \DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{ShadedResult}
\begin{verbatim}
#  # A tibble: 10 x 4
#      Year Gender Site     Rate
#     <dbl> <chr>  <chr>   <dbl>
#   1  1930 Female Stomach  35.2
#   2  1931 Female Stomach  33.8
#   3  1932 Female Stomach  33.7
#   4  1933 Female Stomach  32.5
#   5  1934 Female Stomach  31.6
#   6  1935 Female Stomach  31.4
#   7  1936 Female Stomach  30.9
#   8  1937 Female Stomach  29.2
#   9  1938 Female Stomach  28.9
#  10  1939 Female Stomach  26.9
\end{verbatim}
\end{ShadedResult}

Plot the data using a graph of your choice with the goal of comparing
males vs.~females. (you might consider using the function
\texttt{gf\_line} from the \texttt{ggformula} package). Briefly comment
on your comparison. Be sure to include an informative title and figure
caption.

\section{Does breast-feeding weaken
bones?}\label{does-breast-feeding-weaken-bones}

Breast-feeding mothers secrete calcium into their milk. Some of the
calcium may come from their bones, so mothers may lose bone mineral
content. Researchers compared 47 breast-feeding women with 22 women of
similar age who were neither pregnant nor lactating. They measured the
percent change in the mineral content of the women's spines over three
months. The data can be read into R:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{boneloss <-}\StringTok{ }\KeywordTok{read.csv}\NormalTok{(}\StringTok{"https://github.com/sahirbhatnagar/EPIB607/raw/master/data/boneloss.csv"}\NormalTok{)}
\KeywordTok{head}\NormalTok{(boneloss)}
\end{Highlighting}
\end{Shaded}

\begin{ShadedResult}
\begin{verbatim}
#     type mineral_loss
#  1 Other          2.4
#  2 Other          0.0
#  3 Other          0.9
#  4 Other         -0.2
#  5 Other          1.0
#  6 Other          1.7
\end{verbatim}
\end{ShadedResult}

A negative value of \texttt{mineral\_loss} indicates a loss in mineral
content. Do the data show distinctly greater bone mineral loss among the
breast-feeding women? Use descriptive statistics, plots and/or tables to
justify your conclusion.

\section{Food in America}\label{food-in-america}

Vox published a list of
\href{http://www.vox.com/a/explain-food-america}{Charts that explain
food in America}. There are 40 maps, charts, and graphs that show where
our food and drink comes from and how we eat it.

Pick your best and least favorite graphic, and briefly explain why using
the taxonomy we learned in
\href{https://docs.google.com/presentation/d/1wXgcTzcRKl_leGRfNZjWWPkjwJSTlZSXBCl-fFuLEaE/edit?usp=sharing}{class}
and provide a link to the figure. For example, this link:
\href{https://www.vox.com/a/explain-food-america\#list-21}{Figure 18}
was created using the following code:
\texttt{{[}Figure\ 18{]}(https://www.vox.com/a/explain-food-america\#list-21)}

%\showmatmethods


\bibliography{pinp}
\bibliographystyle{jss}



\end{document}

