\documentclass[twocolumn]{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\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}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: {\bf \Large Work only on this sheet} (on both sides, if
needed).  MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\bf Important note:}  Remember that in problems calling for R code, you
are allowed to use any built-in R function, e.g. {\bf choose()}, {\bf
sum()}, etc.  

{\bf 1.} (5) The R analog of C++'s "." symbol that indicates member
variables for a class instance (e.g. {\bf x.y} is the member variable
{\bf y} in an instance x of some C++ class) is
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 2.} (15) Write a single R statement that vectorizes the code

\begin{lstlisting}
propgt15 <- 0
for (i in 1:length(x))
   if (x[i] > 1.5) propgt15 <- propgt15 + 1
propgt15 <- propgt1/length(x)
\end{lstlisting}

{\bf 3.} Write brief ONE-LINE explanations as to why each of the
following statements is wrong:

\begin{itemize}

\item [(a)] (10) ``Say X has a N(0,1) distribution.  Then P(X = t) is
$
\frac{1}{\sqrt{2\pi}} \exp{(-0.5 t^2)}
$.

\item [(b)] (10) ``In the baseball data analysis, p.286, we are assuming
that weight is equal to c + d $\times$ height, for some c and d.''

\item [(c)] (10) ``In the testing rule on p.224 top, 0.05 is interpreted
as the probability that $H_0$ is true.''

\end{itemize}

{\bf 4.} (20) Consider this simple Markov model of the reliability of a
two-component system.  The state is the number of machines currently up,
thus 0, 1 or 2.  If both machines are currently up, there is a
probability p that one of them goes down in the next time step, and 
probability 1-p that both stay up.  If exactly one is up, there is
probability q that it goes down in the next time step, and probability
1-q that it stays up.  Finally, any machine is that is currently down
will have probability r of being repaired in the next time step, and
probably 1-r of staying down.  If both machines are down, their two
repair processes are independent.

Write an R function {\bf machinemodel()} that returns the $\pi$ vector
for this chain, with input parameters {\bf p}, {\bf q} and {\bf r}.  You
should of course call the function {\bf findpi1()}, given in the book.

Note:  In your electronic file, put your entire {\bf machinemodel()} on
a single line, with appropriate braces and semicolons, e.g.

\begin{lstlisting}
> minmax <- function(x) {mn <- min(x); mx <- max(x); c(mn,mx)} 
> minmax(c(12,5,13))
[1]  5 13
\end{lstlisting}

{\bf 5.} (15) Suppose we have a random sample of size 25 from a
beta-distributed population (Sec. 5.5.6) with $\alpha = 0.5$ and $\beta
= 0.2$, and we compute the sample mean $\overline{X}$.  Find the
approximate (but not simulated) value of $P(\overline{X} > 0.72)$.
Note:  Your R expression may be somewhat complex.  Make doubly sure that
you've got it right, e.g. that your parentheses match up right.

{\bf 6.} (15) What is the name of the quantity returned by the following
function?

\begin{lstlisting}
f <- function(y,x) {
   lmout <- lm(y ~ x)
   # get the beta-hats
   betahat <- lmout$coef
   pred <- cbind(1,x) %*% betahat
   tmp <- cor(pred,y)
   tmp^2
}
\end{lstlisting}

{\bf Solutions:}

{\bf 1.} \$

{\bf 2.}

\begin{lstlisting}
propgt15 <- mean(x > 1.5)
\end{lstlisting}

{\bf 3.a}  For contin. distributions, P(X = t) = 0.

{\bf 3.b}  It is mean weight, not weight.

{\bf 3.c}  $H_0$ is either true or not (through all lines of the
``notebook,'' no probability to it).

{\bf 4.}

\begin{lstlisting}
machinemodel <- function(p,q,r) { 
   m <- matrix(rep(0,9),nrow=3)
   r1 <- 1-r
   q1 <- 1-q
   m[1,1] <- r1^2
   m[1,2] <- 2*r*r1
   m[2,1] <- q*r1
   m[2,2] <- q*r+q1*r1
   m[3,2] <- p
   m[,3] <- 1-m[,1]-m[,2]
   findpi1(m)
}
\end{lstlisting}

{\bf 5.}

\begin{lstlisting}
1 - pnorm(0.72, mean=0.5/0.7, sd=sqrt(0.1/(25*0.7^2*1.7)))
\end{lstlisting}

{\bf 6.} $R^2$

\end{document}

n <- 25
1 - pnorm(0.72, mean=0.5/0.7, sd=sqrt(0.1/(n*0.7^2*1.7)))
# check
samples <- matrix(rbeta(n*1000,0.5,0.2),ncol=n)
xbars <- apply(samples,1,mean)
mean(xbars > 0.72)

