\documentclass[11pt]{article} \usepackage{graphics} \usepackage{natbib} \usepackage{url} \begin{document} \SweaveOpts{concordance=TRUE} \title{ STAT:5400 Midterm 1, 2018} \author{$<$Your Name$>$} \date{$<$date when you took exam$>$} \maketitle \section{ \LaTeX\ } \label{sec:latex} ``A system of measurement is a collection of units of measurement and rules relating them to each other... Systems of measurement in modern use include the metric system, the imperial system, and United States customary units." \citep{wiki:xxx} Table~\ref{mytab} lists conversions between U.S. customary units and the metric system. Section~\ref{Rstuff} develops an R function to compute the conversions. \begin{table}[h] \label{mytab} \begin{center} \begin{tabular}{cc} \hline U.S. & Metric \\ \hline 1 inch & 2.54 cm \\ 1 inch & 0.0254 m \\ 1 foot & 30.48 cm \\ 1 foot & 0.3048 m \\ 1 yard & 91.44 cm \\ 1 yard & 0.9144 m \\ \hline \end{tabular} \end{center} \caption{Converting U.S. customary length units to metric} \end{table} \newpage \section{R} \label{Rstuff} \begin{enumerate} \item Write an R function called {\tt US2metric} to convert measurements from U.S. customary units to metric system units. Your function should accept three arguments: \begin{itemize} \item the numeric value to be converted \item the abbreviation for the original U.S. customary units ( ``in", ``ft", or ``yd") \item the abbreviation for the metric units into which the value is to be converted (``cm", or ``m") \end{itemize} Your function should check that all arguments have valid values, should do the conversion, and should return a list containing the numeric value after conversion and the abbreviation for the final units. Include the code for your R function. <>= US2metric <- function( s, u1, u2) { if(!is.numeric(s) || !(u1 %in% c("in","ft","yd")) || !(u2 %in% c("cm","m") )) { print("Invalid input.") val = NA units <- u2 } else { units <- u2 if( u1 == "in") multiplier <- 2.54 else if(u1 == "ft") multiplier = 30.48 else if(u1 == "yd") multiplier = 91.44 if(u2 == "m") multiplier <- multiplier / 100 val <- s * multiplier } list(val = val, units = units) } @ \item Show the code and output for running your function with the following sets of arguments: \begin{enumerate} \item {\tt US2metric(5, "in", "cm")} \item {\tt US2metric("in", 5, "cm")} \item {\tt US2metric(5, "ft", "m")} <>= US2metric(5, "in", "cm") US2metric("in", 5, "cm") US2metric( 5, "ft", "m") @ \end{enumerate} \item Read the dataset ``Davis.txt" into an R object called {\tt myDavis}. You may either download it from the web and read it from the file, or read it directly from the web. Include your line of R code here. <>= myDavis <- read.table("http://homepage.divms.uiowa.edu/~kcowles/Datasets/Davis.txt", header = TRUE) @ \item Use a built-in R function to display the structure of the {\tt myDavis} object. The output should include the data type of each column. Include your line of R code and its output here. <>= str(myDavis) @ \item Briefly explain in words why the columns called {\tt rwgt} and {\tt rhgt} have a different data type from {\tt mwgt} and {\tt mhgt}. Insert your answer here. The {\tt rwgt} and {\tt rhgt} variables have some missing values, which are denoted with question marks in the data file. Therefore, R reads them as character variables rather than numeric and converts them to factors. \end{enumerate} \bibliographystyle{apalike} \bibliography{mid1} \end{document}