2.8 Exercises

Exercise 2.1 Create a folder on your computer and a project file where you will store all your R work for this textbook.
 
Exercise 2.2 Install the packages devtools, tidyverse to your R installation. Once that is done, then install the package demodelr from my github page.
 
Exercise 2.3 What are the variables listed in the dataset phosphorous in the demodelr library? (Hint: try the command ?phosphorous.)
 
Exercise 2.4 Make a scatterplot (geom_point()) of the dataset phosphorous in the demodelr library. Be sure to label the axes.

 

Exercise 2.5 Change the line plot of Wilson’s weight over time so the line is blue and the size is 4.

 

Exercise 2.6 Change the color of the scatterplot of Wilson’s weight over time to a either a hexadecimal color or a named color of your choice.

 

Exercise 2.7 For this exercise you will do some plotting:

  1. Define a sequence (call this sequence \(x\)) that ranges between -12 to 12 with spacing of .05.
  2. Also define the variable \(y\) such that \(y=\sin(x)\).
  3. Make a scatter plot to graph \(y=\sin(x)\). Set the points to be red.
  4. Make a line plot to graph \(y=\sin(x)\). Label the x-axis with your favorite book title. Label the y-axis with your favorite food to eat.

 

Exercise 2.8 An equation that relates a consumer’s nutrient content (denoted as \(y\)) to the nutrient content of food (denoted as \(x\)) is given by: \(\displaystyle y = c x^{1/\theta}\), where \(\theta \geq 1\) and \(c\) are both constants is a constant. Let’s just assume that \(c=1\) and the \(0 \leq x \leq 1\).

Write a function called nutrient that will make a sequence of y values for an input x and theta (\(theta\)). Then use that code to make a make a line plot (geom_line()) for five different values of \(\theta>1\), appropriately labeling all axes.

 

Exercise 2.9 Researchers measured the phosphorous content of Daphnia and its primary food source algae. This is the dataset phosphorous in the demodelr library.

Researchers believe that Daphnia has strict homeostatic regulation of the phosphorous in algae, and as such want to determine the value of \(\theta\) in the equation \(y= \displaystyle y = c x^{1/\theta}\). They have already determined that the value of \(c=1.737\).

  1. If you haven’t already, make a scatterplot (geom_point()) of the dataset phosphorous in the package library. Be sure to label the axes correctly.
  2. Use your function nutrient from the previous exercise to make an initial guess for theta (\(\theta\)) that would be consistent with the data. You can evaluate your guess by plotting (with geom_line()) against the data.
  3. Use guess and check to refine the value of \(\theta\) that seems to work best.
  4. Report your value of \(\theta\).

 

Exercise 2.10 For this exercise you will investigate some built-in functions. Remember you can learn more about a function by typing ?FUNCTION, where FUNCTION is the name.

  1. Explain (using your own words) what the function runif(1,100,1000) does.
  2. Explain (using your own words) what the function ceiling() does, showing an example of its use.
 

Exercise 2.11 For this exercise you write a sample function file.

  1. Create a new source file and save it as myFunction.R.
  2. Type this code the file you created: myInteger <- ceiling(runif(1, 100, 1000)) (This will declare a variable myInteger that you will work with in the following steps.)
  3. Determine a function in R that will compute a cumulative sum from 1 to the value of myInteger. Modify your file so that it also computes the cumulative sum and then source your file.
  4. Copy and paste your function into your homework document for evaluation.
 
Exercise 2.12 The Ebola outbreak in Africa in 2014 severely affected the country of Sierra Leone. A model for the number of deaths \(D\) due to ebola is given by the following equation: \[ D(t) = \frac{K \cdot N_{0} }{N_{0} + (K-N_{0}) \exp(-rt)}, \] where \(K = 3980\), \(N_{0}=5\) and \(r = 0.0234\). The variable \(t\) is in days. Use geom_line() to visualize this curve from \(0 \leq t \leq 700\).
 

Exercise 2.13 Consider the following piecewise function: \[\begin{equation} y = \begin{cases} x^2 & \text{ for } 0 \leq x < 1,\\ 2-x &\text{ for } 1 \leq x \leq 2 \\ \end{cases} \end{equation}\]

  1. Define a function in R that computes \(y\) for \(0 \leq x \leq 2\).
  2. Use geom_line() to generate a graph of \(y(x)\) over the interval \(0 \leq x \leq 2\).
 

Exercise 2.14 An insect’s development rate \(r\) depends on temperature \(T\) (degrees Celsius) according to the following equation: \[\begin{equation} r = \begin{cases} 0.1 & \text{ for } 17 \leq T < 27,\\ 0 &\text{ otherwise.} \end{cases} \end{equation}\]

  1. Define a function in R that computes \(r\) for \(0 \leq T \leq 30\).
  2. Use geom_line() to generate a graph of \(r(T)\) over the interval \(0 \leq T \leq 30\).