R Homework Assignment: Working with RMarkdown and LATEX

.pdf

School

Utah State University *

*We aren’t endorsed by this school

Course

5050

Subject

Statistics

Date

Jun 9, 2024

Type

pdf

Pages

10

Uploaded by DoctorLightningSpider

Stat 5050: Introduction to R Fall 2021 Name: INSERT YOUR NAME HERE Homework Assignment 04 70 Points — Due Friday 10/1/2021 (via Canvas by 11:59pm) General Instructions For this fourth homework assignment, you have to work with RMarkdown or knitr/Sweave. You can create your own RMarkdown (.Rmd) file, based on files from class and from Homework 1, copy the question numbers and the answer options into your .Rmd file, and knit that file into a pdf file. Alternatively (and much easier!!!), use this .Rnw file as a template, just fill in the answers into the provided spaces, and knit into a pdf file. Only the final resulting pdf file (from .Rmd or .Rnw) has to be submitted via Canvas. As previously stated, I would like to encourage potential and current MS and PhD students to work with .Rnw and L A T E X instead of .Rmd. You need to learn how to write R code that is easily readable for others. There exists the Google’s R Style Guide (provided as a pdf here in Canvas) that summarizes rules for good R style. This style guide closely resembles the far more detailed Tidyverse Style Guide . These rules are accessible at https://style.tidyverse.org/ . In particular, make sure that you always have a space after a comma and that you consistently use the same type of assignment operator, ideally <- . Look at the examples in these style guides and follow that style whenever you write your own R code from now on. Do not forget to replace my name and include your name instead! In all question parts, show your R code and the results! 1
(i) (20 Points) Family Data Revisited: In the following exercises, try to write your code to be as general as possible so that it would still work if the family had 27 members in it or if the variables were in a different order in the data frame. Show your R code and the final results produced from within R for all question parts! (a) (3 Points) Copy the family data set for this homework from Canvas into your local folder for this homework. Then load the hw04_familyDF.rda data set into R. Show the “objects” that have been loaded. Is the first “object” that is listed a data frame? The R output should be TRUE or FALSE. Search for help if you don’t recall how to check whether something is a data frame. Answer: # Place your answer here (b) (4 Points) The NHANES survey used different cut-off values for men and women when classifying them as overweight. Suppose that a man is classified as obese if his bmi exceeds 26 and a woman is classified as obese if her bmi exceeds 25. Write a logical expression to create a logical vector, called OW.NHANES, that is TRUE if a member of the family is obese and FALSE otherwise. Display its content. Answer: # Place your answer here (c) (4 Points) Here is an alternative way to create the same vector that introduces some useful functions and ideas. We first create a numeric vector called OW.limit that is 26 for each male in the family and 25 for each female in the family. To do this, we create a vector of length 2, called OW.val, where the first element is 26 and second element is 25. Then we create the OW.limit vector by subsetting OW.val by position, where the positions are the numeric values in the gender variable (i.e., use as.numeric to coerce the factor vector to a numeric vector). Notice that we can “subset” a vector of length 2 by a much longer vector: 2
# Note that this code chunk is not executed because eval=FALSE. # Change to eval=TRUE once you have answered the previous question parts. OW.val <- 26 : 25 OW.limit <- OW.val[ as.numeric (family $ gender)] OW.limit Finally, use OW.limit and the bmi vector in family to create the desired logical vector, and call it OW.NHANES2. Display its content. Compare with your results from part (b) via the any function. Did you get the intended result? If not, check your R code again! Answer: # Place your answer here (d) (4 Points) Use the vector OW.limit and each person’s height to find the weight that they would have if their bmi was right at the limit (26 for men and 25 for women). Call this weight OW.weight and display its content. To do this, start with the formula bmi = (weight / 2.2) / (2.54 / 100 * height)^2 and re-express it in terms of weight (i.e., weight = ... ). Answer: # Place your answer here (e) (5 Points) Create the following plot of actual weight (on the vertical axis) against the weight at which they would be overweight (on the horizontal axis). If you get an error when you run this code, check whether you are using the correct variable names in your code earlier on. # Note that this code chunk is not executed because eval=FALSE. # Change to eval=TRUE once you have answered the previous question parts. # # Make sure that your graph appears in your output! plot (OW.weight, family $ weight, xlab = "Minimum Weight for Overweight" , 3
xlim = c ( 100 , 220 ), # !!! ylab = "Observed Weight" , ylim = c ( 100 , 220 )) # !!! abline ( a = 0 , b = 1 ) abline adds a straight line (here with y-intercept a = 0 and slope b = 1) to the plot. Note that this is not the regression line! Thus, points that fall exactly on the line belong to individuals where the observed weight exactly qualifies to be overweight. Points above the line represent individuals who are overweight, and points below the line represent individuals who are not overweight. We can easily count in the plot how many points are above the line and how many points are below the line, but we want that R does this counting for us! So, write two R expressions that do this counting for us and display their results. Answer: # Number of points above the line # Place your answer here # Number of points below the line # Place your answer here 4
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help