2.8 Exercises
devtools
, tidyverse
to your R installation. Once that is done, then install the package demodelr
from my github page.
phosphorous
in the demodelr
library? (Hint: try the command ?phosphorous
.)
geom_point()
) of the dataset phosphorous
in the demodelr
library. Be sure to label the axes.
Exercise 2.7 For this exercise you will do some plotting:
- Define a sequence (call this sequence \(x\)) that ranges between -12 to 12 with spacing of .05.
- Also define the variable \(y\) such that \(y=\sin(x)\).
- Make a scatter plot to graph \(y=\sin(x)\). Set the points to be red.
- 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 callednutrient
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\).
- If you haven’t already, make a scatterplot (
geom_point()
) of the datasetphosphorous
in the package library. Be sure to label the axes correctly. - Use your function
nutrient
from the previous exercise to make an initial guess fortheta
(\(\theta\)) that would be consistent with the data. You can evaluate your guess by plotting (withgeom_line()
) against the data. - Use guess and check to refine the value of \(\theta\) that seems to work best.
- 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.
- Explain (using your own words) what the function
runif(1,100,1000)
does. - 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.
- Create a new source file and save it as
myFunction.R
. - Type this code the file you created:
myInteger <- ceiling(runif(1, 100, 1000))
(This will declare a variablemyInteger
that you will work with in the following steps.) - 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. - Copy and paste your function into your homework document for evaluation.
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}\]
- Define a function in
R
that computes \(y\) for \(0 \leq x \leq 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}\]
- Define a function in
R
that computes \(r\) for \(0 \leq T \leq 30\). - Use
geom_line()
to generate a graph of \(r(T)\) over the interval \(0 \leq T \leq 30\).