Tutorial Exercises Week 6
Question 1
Write an R function that calculates f(x) in the equation below:
f(x) = \frac{e^x}{1+e^x}
- What is f(-2)?
- What is f(0)?
- What is f(2)?
In each case provide your answer rounded to 4 decimal places.
Question 2
Plot the function
f(x) = x^3 - 12x^2
in the interval [-6, 13].
At what values of x are the extreme points of this function? Both extreme points are integers (whole numbers).
Hint: Add the layers theme_minimal() + scale_x_continuous(breaks = -6:13)
to add more line verticial breaks to help see where the extreme points are.
Question 3
Use R to find the minimum of the following function:
f(x)=x^2 - 3x - 12
- What is the minimizing value of x?
- What value does the function achieve at its minimum?
Question 4
Use R to find the maximum of the following function:
f(x)=100+2x-x^2
What value of x maximizes this function?
Question 5
Create a function in R with two arguments x and z which does the following:
f(x,z)= \begin{cases} x+z & \text{ if } x > z\\ x-z & \text{ if } x = z\\ xz & \text{ if } x < z\\ \end{cases}
What is the value of the function at the following values:
- x=2 and z=3.
- x=2 and z=2.
- x=3 and z=2.
Question 6
Download and read in the file rotterdam-airbnb.csv.
Using the data create a factor variable called n_beds
according to:
"1"
if bedrooms = 1."2"
if bedrooms = 2."3+
if bedrooms > 2.
Create a bar plot of the variable n_beds
. Which describes the shape of the bar plot?
- Most listings have 1 bedroom. Listings with 3+ bedrooms are relatively rare.
- Most listings have 3+ bedrooms. Listings with 1 bedroom are relatively rare.
- Most listings have 2 bedrooms. Listings with 1 bedroom are relatively rare.
Question 7
Using the same dataset as the previous question, create a factor variable called affordability
which takes the following values:
"Cheap"
if the price is below 120"Expensive"
if the price is above 250."Affordable"
otherwise (between 120-250).
Set the levels of the factor to go "Cheap"
, then "Affordable"
, then "Expensive"
.
Create a bar plot of the variable n_beds
from the previous question with colors filling the bars that represent the affordability.
Which category of n_beds
contains the most listings labelled as "Expensive"
?
- 1
- 2
- 3+