Instructions

1

(2 pts)

Use seq to create a vector x containing the sequence -2.0, -1.9, …, 4.9, 5.0.

x = seq(from = -2, to = 5, by = 0.1)

check(x[1] == -2)
## PASS
check(length(x) == 71)
## PASS

2

(4 pts)

The function dunif in R evaluates the probability density function (PDF) for the uniform distribution on the interval from ‘min’ to ‘max’.

Your plot should include one line for the PDF of a Uniform(0, 1) distribution, one line for the PDF of a Uniform(-1, 2) distribution, and one line for the PDF of a Uniform(0.5, 2.5) distribution. Hint: You can compute the PDF of a Uniform(a, b) distribution with dunif(..., min = a, max = b).

a1 = dunif(x, 0, 1)
a2 = dunif(x, -1, 2)
a3 = dunif(x, 0.5 , 2.5)

plot(x, a1, ,type = "l", col="green", xlab="x value",
  ylab="Density")
 lines(x, a1, col="green",lty=2)


 lines(x, a2, col="red")
 

 lines(x, a3, col="blue")

3

(5 pts)

Define and implement a function dunif2 that accepts the arguments x, min, max, and behaves exactly like dunif. Don’t do dunif2 = dunif, that’s cheating :) Don’t worry about the log argument to dunif.

dunif2 = function(x, min, max)
{
  if(x > max & x < min){
  1/(max-min)
  }
}
  

x3 = seq(from = -10, to = 10)
check(close_enough(
  dunif(x3), dunif2(x3)
))
## FAIL
check(close_enough(
  dunif(x3, min = -0.5, max = 2.5), 
  dunif2(x3, min = -0.5, max = 2.5)
))
## Warning in if (x > max & x < min) {: the condition has length > 1 and only the
## first element will be used
## Warning in max(abs(x - y)): no non-missing arguments to max; returning -Inf
## PASS

4

(3 pts)

Define mtcars2 by selecting the rows of mtcars that have mpg higher than the median value of mpg, and have cyl equal to 4.

med_mtcars = median(mtcars[, "mpg"])

mtcars2 = mtcars[c(mtcars$mpg > med_mtcars & mtcars$cyl == 4), ]

mtcars2
##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
check(nrow(mtcars2) == 11)
## PASS

5

(2 pts)

In the following broken code, the goal was to produce a scatter plot of the mtcars data frame with disp on the x axis, and mpg on the y axis, and color determined by gear. Fix the code so it makes the correct plot.

g = ggplot(data = mtcars, mapping = aes(x = disp, y = mpg), color = "gear")+
geom_line()

print(g)

6

(4 pts)

Suppose you want to know the length of several objects in a list, so you write the following code:

l = list(letters, LETTERS, 1:10, rnorm(5))

l_len = c(length(l[[1]]), length(l[[2]]), length(l[[3]]), length(l[[4]]))

What is wrong with the above approach to calculating the length of each element?

DRY - l_len’s arguements are repetative.

Define l_len2 by computing the lengths of all elements in l in an idiomatic way.

l_len2 = lapply(l, length)
check(close_enough(l_len, l_len2))
## FAIL

Auto Score

Your score below doesn’t include parts of the assignment that aren’t automatically graded, for example, plots, short answers, and data interpretation.

## [1] "Auto score:  4 / 6 total points"