Title image Spring 2022

R Markdown

The purpose of this project is to introduce you to the R Markdown system. R Markdown is a method of creating web documents the let you display code, plots, and text in an integrated manner. It is an excellent way to present your work, because it enables someone looking at your results to understand exactly how they were generated.


Project Tasks

    Part 1: Setup

    Get ready to write some R code and explore what R Markdown can do.

    Open R Studio or a text editor and Create a new R Markdown file called project5.Rmd. R Studio will give you a dialog where you can enter a title, your name, and select an output format. Select HTML for the output format. Save your file to an appropriate working directory.

  1. The default R Markdown file includes the two built-in data sets cars, and pressure. If you are not using these data sets, you can remove those variables from the {r} statements. Your code blocks should go in between the expressions ```{r} and ``` as below.
    ```{r}
    # code goes here
    a <- 8
    b <- 5
    c <- a + b
    ```
  2. Part 2: Write a function to generate some data

    In this section, you will write a function that generates some noisy data that fits an approximate linear model. The function will take in an argument that is the number of data points to generate.

  3. In the first code block, assign to a variable generateData a function with one parameter, N, which is the number of data points to generate. Inside the function, do the following.
    1. Use the set.seed function to set the random seed to a value of your choice. Can you make the seed a parameter of the function with a default value?
    2. Assign to tmpx a vector of N numbers uniformly distributed betwen 0 and 100. You can use the runif function to do this.
    3. Assign to tmpy a linear function of tmpx with added noise. One way to implement this is to consider the equation:

      tmpy <- (tmpx + noise)*slope + intercept + noise

      You can generate a vector of uniform or normally distributed noise using runif or rnorm. Note that you have to generate the same number of noise values as there are values in tmpx.

    4. Use the data.frame function to create a data frame with two columns x and y that get the values in tmpx and tmpy, respectively. Have your function return the data frame.
  4. After you have defined the function, assign to a variable df the result of executing the function with the parameter 100.
  5. Part 3: Fit a linear model to the noisy data and plot it

    After the prior section, the variable df contains a column of data df$x and a column of data df$y. You can use the lm function to execute a linear regression and get the best fit line.

  6. Assign to a variable (e.g. model) the result of calling the linear model function lm with an argument that relates df$y to df$x (i.e. the input is df$y ~ df$x).
  7. Create a plot of df$x and df$y, then add the best fit line using the abline function. Using the capabilities you have learned in prior projects, make the plot use solid points (pch argument), and make the points and the line fit be different colors. Put the linear fit equation into the plot using the text function.
  8. In your markdown file, put some text around your code and your plot explaining what the code does and describing the plot.
  9. Part 4: Explore a data set of your choice

    Pick a data set and repeat the exercise above with new data, making it look nice using the Markdown capabilities.

  10. In your markdown file, and using a data set of your choice, read the data and execute a linear regression analysis of two of its columns. One possibility is AGE and WRKYEARS in the GSS 1991 data. Plot the two columns and the linear fit, making the graph look nice. Add some text around your code to describe the data set and discuss the results (i.e. do they make sense?).

    If you are using a package like foreign to read an SPSS file, you will have to include a library command in your R Markdown file. Likewise, you will also have to include the set working directory function, setwd, to specify where R should look for the file. You can figure out what these commands are by first unclicking and clicking the foreign package and copying the last command that appears. It should look something like the following (MacOS).

    library("foreign", lib.loc="/Library/Frameworks/R.framework/Versions/3.4/Resources/library")

    Likewise, you can use the menu to set your working directory to your source file and copy the setwd command that appears.

  11. View your markdown output in a browser, then use the browser's print capability to generate a pdf. Submit the pdf with your code. You may answer the questions below in your markdown file.

Report

Answer the following questions. Submit your answers as a plain text file or PDF in your handin directory. Put your name at the top of the file. No credit will be given for any other format or for a file without a name.


Turn In