\documentclass[twocolumn]{article}
% \documentclass{article}

\setlength{\oddsidemargin}{0.5in}
\setlength{\evensidemargin}{0.0in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

{\bf DIRECTIONS:} Write your solutions in a single {\bf .tex} file,
\underline{including} R code. Your {\bf .tar} package will consist of
that file, its output {\bf .pdf} file, and a separate file for each
problem requiring R code, each such file named in the form {\bf x.R},
where x is the problem number.  Name your {\bf .tar} file as you did in
the homework. Your submission must be in the {\bf 132groupquiz}
directory in {\bf handin}; it must be {\bf timestamped on or before
11:50 a.m.  NO LATE SUBMISSIONS; keep submitting the work you have, as
you go along, so that you at least have something turned in.}  You are
not necessarily expected to solve all the problems. 

{\bf 1.} (30) This problem involves the built-in dataset {\bf ToothGrowth}
in R.  Type

\begin{lstlisting}
> ToothGroth  # print data frame
> ?ToothGroth  # learn more about it 
\end{lstlisting}

to get acquainted.

Fit the model 

\begin{equation}
\textrm{mean length } = 
\beta_0 +
\beta_1 \textrm{ J} +
\beta_2 \textrm{ dosage}
\end{equation}

where $J$ is a dummy variable for OJ.

Do NOT include your code in a {\bf .R} file, but DO show ALL your code,
including data set up, and the output in your {\bf .tex} file.

Interpret the results.

{\bf 2.} (25) I flip a coin twice, getting $X$ heads.  I then flip it $X$
more times, with $Y$ heads among these flips.  Define $T = X + Y$.  Find 
$p_T(2)$, with a clean, clear derivtion.

{\bf 3.} (25) Write a function with ``declaration''

\begin{lstlisting}
pval <- function(x,p0)
\end{lstlisting}

that will return the approximate p-value for a significance test, as
follows.

Here {\bf x} is a vector of 0s and 1s, where for instance 1 could mean, Yes,
plan to vote for Smith, with 0 meaning No, won't vote for her.  We will
test the hypothesis

\begin{equation}
H_0: p = p_0
\end{equation}

where $p$ is the true (but unknown) population proportion of 1s.  The
alternate hypothesis is

\begin{equation}
H_A: p \neq p_0
\end{equation}

Example:

\begin{lstlisting}
> set.seed(168) 
> x <- sample(0:1,500,prob=c(0.2,0.8),
     replace=TRUE) 
> pval(x,0.75) 
[1] 0.009823275
\end{lstlisting}

{\bf 4.} (20) Write a function with ``declaration''

\begin{lstlisting}
qpqinv <- function(lmout)
\end{lstlisting}

where {\bf lmout} is an object of class {\bf "lm"} (i.e.\ the value
returned from a call to {\bf lm()}), which will return the matrix 
$(Q'Q)^{-1}$.

Hint:  Here you'll need to explore an actual R {\bf "lm"} object.

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.a}

\begin{lstlisting}
> summary(lm(len ~ .,data=tg))

Call:
lm(formula = len ~ ., data = tg)

Residuals:
   Min     1Q Median     3Q    Max 
-6.600 -3.700  0.373  2.116  8.800 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   5.5725     1.2824   4.345 5.79e-05 ***
supp          3.7000     1.0936   3.383   0.0013 ** 
dose          9.7636     0.8768  11.135 6.31e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.236 on 57 degrees of freedom
Multiple R-squared:  0.7038,    Adjusted R-squared:  0.6934 
F-statistic: 67.72 on 2 and 57 DF,  p-value: 8.716e-16
\end{lstlisting} 

Each increase of one unit of the supplement is \underline{estimated} to
result in an increase of 3.7 in \underline{mean} tooth length.  Orange
juice has an estimated impact of 9.76.  Statements should NOT be made
along the lines of ``Dose is more significant than OJ.''

{\bf 2.}

\begin{eqnarray}
p_T(2) &=& P(T = 2)  ~~ \textrm{  (def. of } p_Y\textrm{)} \\ 
&=& \sum_{i=0}^2 P(X = i, Y = 2 - i)  \textrm{  (2.5)} \\
&=& \sum_{i=0}^2 P(X = i) ~ P(Y = 2 - i)  \textrm{  (2.5)} \\
&=& \sum_{i=0}^2 C_{2,i} 0.5^2 \cdot C_{i,2-i} 0.5^i  \textrm{ (binom.
pmf)}
\end{eqnarray}

{\bf 3.}

\begin{lstlisting}
pval <- function(x,p0) {
   estp <- mean(x)
   n <- length(x)
   z <- (estp - p0) / sqrt(p0 * (1-p0) / n)
   2 * (1 - pnorm(abs(z)))
}
\end{lstlisting}

{\bf 4.} Lots of ways to do this, such as 

\begin{lstlisting}
qpqinv <- function(lmout) {
   n <- length(lmout$residuals)
   p1 <- lmout$rank
   s2 <- sum(lmout$residuals^2) / (n-p1)
   vcov(lmou) / s2
}
\end{lstlisting}

\end{document}

