\documentclass{article}
\usepackage{url}
\usepackage{natbib}
\usepackage{float}
% \usepackage{exercises}
\usepackage{nextpage}
\setcounter{tocdepth}{1}
\setlength{\voffset}{-2.5cm}
\setlength{\hoffset}{-0.5cm} \setlength{\textwidth}{14cm}
\setlength{\textheight}{23cm}
\title{Assignment 1, Math 3346, 2009}
\author{Lecturer: John Maindonald}
\usepackage{Sweave}
\begin{document}
\maketitle
\thispagestyle{empty}

This exercise will work with experimental control and treatment groups
in the data set \texttt{nswdemo}, and with non-experimental comparison
groups in the data sets \texttt{cps1} and \texttt{psdi1}.  All these
datasets are included in the \textit{DAAG} package.

Data in \texttt{nswdemo} are from a randomized trial that was designed to
assess whether a work training program helped the income prospects
of individuals who had a history of employment difficulties.
The remaining two datasets are comparison datsets from neighboring
areas.  They have been investigated, in the econometric literature,
for possible use as non-experimental controls.
\section{Preliminaries}
A first step is to attach the package \texttt{DAAG} that has all
the data:
\begin{Schunk}
\begin{Sinput}
> library(DAAG)
\end{Sinput}
\end{Schunk}

\subsection*{Creation of a Suitable Data Object}
Here, we will create one large dataframe that holds all the data.
(An alternative, described below, is to form a list in which each
separate group is a different list element.)
\begin{Schunk}
\begin{Sinput}
> ## Note the coding for the experimental control & treatment groups
> table(nswdemo$trt)
> ## Code the non-experimental controls as 2 (cps1) and 3 (psid1)
> cps1$trt <- rep(2, dim(cps1)[1])
> psid1$trt <- rep(3, dim(psid1)[1])
> allsets <- rbind(nswdemo, cps1, psid1)
> allsets$trt <- factor(c("exp-ctl", "exp-trt", "cf-cps1", "cf-psid1")
+                       [allsets$trt+1])
> ## Check that the numbers in the different groups make sense
> table(allsets$trt)
\end{Sinput}
\end{Schunk}

\subsection*{Counts of number of missing values}

The following creates functions that can be used for various counting tasks:
\begin{Schunk}
\begin{Sinput}
> ## Function that retrieves number of NAs for a single column
> countNAcol <- function(x)sum(is.na(x))
> ## Now use the function aggregate() to apply countNAcol() to
> ## allsets, indexed by trt
> aggregate(allsets, list(group = allsets$trt), FUN = countNAcol)
\end{Sinput}
\end{Schunk}

Notice that we can replace \texttt{countNAcol} by any other function
that returns a single value.  The following counts the number of zeros
in each of the columns \texttt{re74}, \texttt{re75} and \texttt{re78}.
\begin{Schunk}
\begin{Sinput}
> countzeros <- function(x)sum(!is.na(x) & x==0)
> aggregate(allsets[, c("re74", "re75", "re78")], list(group=allsets$trt), 
+           FUN=countzeros)
\end{Sinput}
\end{Schunk}

\paragraph{Alternative:}
The following alternatives use a further call to \texttt{sapply()},
in place of use of \texttt{aggregate()}:
\begin{itemize}
  \item Use \texttt{sapply()} with the \texttt{allsets} data frame
    that was created above:
\begin{Schunk}
\begin{Sinput}
> ## Function that retrieves number of NAs for each column of a data frame
> countlist <- function(z, statsfun=length)sapply(z, statsfun)
>   # Notice that statsfun has been given a default argument
> ## Apply this to the allsets data frame
> sapply(split(allsets, allsets$trt), FUN=countlist, statsfun=countNAcol)
\end{Sinput}
\end{Schunk}
Notice that, as used here, \texttt{sapply()} has three arguments.  The 
first is the list of data frames to which the function given as the 
second argument will, in turn, be applied.  The third argument,
\texttt{statsfun=countNAcol}, is passed to \texttt{countlist()},
thus supplying the second of its two arguments.

\item Create a list in which the data frame corresponding to each
  group is a separate list element:
\begin{Schunk}
\begin{Sinput}
> listsets <- list("cf-cps1"=cps1[,-1], "cf-psid1"=psid1[,-1],
+                  "exp-ctl"=subset(nswdemo, trt==0)[,-1],
+                  "exp-trt"=subset(nswdemo, trt==1)[,-1])
> ## Check that the numbers in the different groups make sense
> sapply(listsets, nrow)
> ## Now do the calculations with listsets
> sapply(listsets, FUN=countlist, statsfun=countNAcol)
\end{Sinput}
\end{Schunk}
\end{itemize}
Use of \texttt{sapply()} in this way is actually more general.
For example, a possible argument is \texttt{statsfun=range},
so that two values are returned.  The labeling is then less
informative than one might like!   

\section{Exercises}
Here then are the formal exercises:
\begin{enumerate}
\item Look up \texttt{help(nswdemo)}, \texttt{help(cps1)} and
  \texttt{help(psid1)}.  Try also
\begin{Schunk}
\begin{Sinput}
> str(nswdemo); str(cps1); str(psid1)
\end{Sinput}
\end{Schunk}
Use these sources of information to write brief notes on each of the
columns of data, noting whether columns should be treated as numeric
or categorical.  
\newline [1 mark]
\item[] Write brief notes documenting each of the functions 
  \texttt{countNA()}, \texttt{countzeros()} and \texttt{countlist()}
  \newline [2 marks]
\item For each column of the data and for each of the four
  groups, do the following:
  \begin{enumerate}
  \item Determine the number of missing values. 
    \newline[1 mark]
  \item Determine, for each of \texttt{re74}, \texttt{re75} and
    \texttt{re78}, the number and proportion that are
    zero.\newline[1 mark]
  \end{enumerate}
\item Now examine \texttt{re74}, comparing control and treatment data:
  \begin{enumerate}
  \item Compare the proportion of \texttt{NA}s between 
    the experimental control and treatment.
    \newline[$\frac{1}{2}$ mark]
  \item Compare the proportion of 0's in each of \texttt{re74}, \texttt{re75}
    and \texttt{re75} (obviously \texttt{NA}s have to 
    be excluded) between the four groups.\newline[$\frac{1}{2}$ mark]
  \item For columns that are numeric with more than two unique values,
    determine for each of the four groups the range of values.\newline
    [1 mark]
\end{enumerate}    
\item Provide graphs that conveniently summarise differences between
  the four groups, with respect to \texttt{age} and \texttt{re75}.
  Issues to consider, and on which you should comment, are:
\begin{itemize}
\item Is it best to examine separately i) comparisons with respect to
  number of zeros, and ii) distributions of non-zero values, rather
  than using one density plot for both? [Both approaches can be
  defended, depending however on the audience.]
  \item For \texttt{re75}, is a logarithmic scale preferable?
  \item If zeros are included in a probability density plot for the
    logged values, it will be necessary to add a small positive
    offset before taking logarithms.  What magnitude of offset is
    sensible?
\end{itemize}
NB: Marks will be given for layout, with a preference for a layout
that lays information out in a compact and readily comprehended form.\newline
[4 marks]
\item Use tables to summarise differences in categorical variables
between the two groups.\newline
[2 marks]
\item What are the major differences between the four groups,
as evident from examining columns one at a time?  Comment especially
on any differences in the pre-training variables, i.e., all except
\texttt{re78}.
\newline
[3 marks]
\item Do any differences in the pre-training variables have implications
  for the way that you might analyse the data, or the reliance that
  you might put on the results?\newline
[2 marks]
\item The aim of the study was to assess the effect of training.  Is
  it best to base the comparison between treatment and control group
  i) on \texttt{re78} alone, or ii) on \texttt{re78 - re75}?  Justify
  your answer.\newline [2 marks]
\end{enumerate}
\vspace*{3pt}

\noindent [\textbf{TOTAL: 20 marks}]

\subsection*{Due Date: August 25, 2009, 5pm}
In addition to any R code that may be included in the main document,
please provide the R code separately from the output.  Marks will be
subtracted if the R code is not provided.

Please provide assigments in a pdf file, either as hard copy or
emailed to john.maindonald@anu.edu.au
\end{document}

