\documentclass[11pt]{article} \usepackage{graphics} \usepackage{natbib} \usepackage{Sweave} \begin{document} \title{ STAT:5400 Midterm 1, 2015} \author{Solutions} \date{\today} \maketitle \section{Instructions}\label{s:intro} Produce a \LaTeX\ document formatted like this one. You do not have to copy any of the instructions into your document. Just make the sections and subsections with the same names and numbers that I have here, as well as retaining any numbered lists. Use labels and references instead of typing in numbers of equations, figures, tables, or sections. You must work in a Linux environment. Use CSG Linux Desktop to log into one of the computers in 346 SH. Use Sweave or knitr to insert your code and output into your document. Submit your .Rnw file, .bib file, and the final .pdf file into the ICON dropbox. If your .Rnw file will not compile, upload it and a text file containing code and output from running R interactively. I will give substantial partial credit. Before you leave, check with me to make sure I have received your exam files in ICON. Type Section~\ref{sec:latex} exactly as shown below. Use Bib\TeX\ to include the citation. Note that you can get the Bib\TeX\ citation for an R package by loading the package and then entering \begin{verbatim} citation("") \end{verbatim} \section{ \LaTeX\ } \label{sec:latex} The multivariate normal probability density function for a $d$-dimensional random vector ${\bf x}$ is: \[ f({\bf x} | \mbox{\boldmath $\mu$}, \ \Sigma) \ = \ \frac{1}{ (2 \pi) ^ {d/2} |\Sigma| ^ {1/2} } exp \left ( - \frac { [{\bf x}-\mbox{\boldmath$\mu$}] ^ T \Sigma^{-1} [{\bf x}-\mbox{\boldmath$\mu$}] } { 2 } \right ) \] Random draws from this density may be generated using the {\tt rmnorm} function in the {\tt mnormt} package \citep{mnormt}. \section{R} You do not have to type any text in this section. Simply number the questions and include the correct R code and output for each. \begin{enumerate} \item Generate the sequence consisting of eight 4s, then seven 6s, and finally nine 3s. Store the numbers obtained, in order, in the columns of a 6 by 4 matrix. <<>>= y <- rep.int( c(4,6,3), c(8,7,9) ) m <- matrix( y, nrow = 6) m @ \item Generate 1000 random draws from a Gamma density with shape parameter 3 and rate parameter 0.6. Produce a boxplot of the result. Write your code in such a way that I would be able to reproduce your results exactly. <>= set.seed(365) g <- rgamma(1000, 3, 0.6) boxplot(g) @ \item A trapezoid is a quadrilateral with two parallel sides called its bases. The formula for the area of a trapezoid is \[ A = \frac{ (a + b) h }{2} \] where $a$ and $b$ are the lengths of the two bases and $h$ is the height. Write an R function to compute the areas of trapezoids. Your function should accept three arguments -- the lengths of the two bases and the height. It should check that each argument is numeric, positive, and of length 1. If any arguments are invalid, it should print an error message and return ``NA." Otherwise it should return the area of the trapezoid. Show the code for your function and the results of running it with two sets of arguments: \begin{itemize} \item [] 3,6,2 \item [] c(36,24), 5, -2 \end{itemize} <<>>= traparea <- function(a,b,h) { args <- list(a,b,h) checks <- lapply( args, function(q) is.numeric(q) && q > 0 && length(q)==1 ) if( !all(unlist(checks))) { print("Invalid input.") area <- NA } else { area <- (a + b) * h / 2 } area } traparea(3,6,2) traparea( c(36,24), 5, -2 ) @ \end{enumerate} \bibliographystyle{apalike} \bibliography{mid1} \end{document}