\documentclass{article} \usepackage[dvips]{graphics} \begin{document} \title{ Midterm 1 2007} \author{$<$Solutions$>$} \date{$<$Oct. 1, 2007$>$} \maketitle \section{Instructions}\label{s:intro} Produce a \LaTeX\ document formatted like this one. Include this paragraph as it appears here. \section{ More Joy of \LaTeX} \subsection{Math} \[ \left [ \begin{array}{cc} a & b \\ c & d \end{array} \right ] \] \subsection{Tables} \begin{table}[h] \begin{center} \begin{tabular}{lrr} Name & Units & Price \\ \hline Heather & 3 & 3.75 \\ Roger & 12 & 15.00 \\ Pham & 5 & 6.25 \\ \hline \end{tabular} \end{center} \caption{Purchases} \end{table} \section{R functions} Recall that the {\it geometric mean} of a set of strictly positive values is obtained by exponentiating the average of the natural logs of all the values. That is, the geometric mean of a set of positive values $y_1$, $y_2$, $\ldots,$, $y_n$ is \[ exp\left ( \frac{ \sum{log(y_i)}}{n} \right ) \] Write an R function that will accept a numeric vector, test that all elements in the vector are positive, and if so, return the geometric mean of the values in the vector. The dataset called {\tt rivers} is built into R. To get a description of the dataset, enter \begin{verbatim} > help(rivers) \end{verbatim} Use your function to calculate the geometric mean of the data in this dataset. Include in your exam document the code for your function, your command line to apply your function to the {\tt rivers} data, and the resulting output. <<>>= harmmean <- function(x) { if(!is.numeric(x) || any(x < 0) ) stop("need vector of all positive numbers") else length(x) / sum( 1/x) } harmmean(rivers) @ \section{The bootstrap} <<>>= harmmean2 <- function(x, ind) { length(x) / sum( 1 / x[ind] ) } library(boot) boot.river <- boot(rivers, harmmean2, 1000) boot.river boot.ci(boot.river) @ The bootstrap analysis suggests that the point estimate is almost unbiased, in which case bias-correction would not be appropriate. \end{document}