\[\newcommand{\E}{\mathbb{E}}\]

For today’s code challenge, we’ll use the data mtcars which comes loaded with R.

Calculate the p-values for testing the following hypotheses.

  1. \(\E[mpg] = 20\)

  2. \(\E[cyl] = 5\)

  3. \(\E[disp] = 150\)

  4. \(\E[hp] = 150\)

  5. \(\E[drat] = 4\)

  6. \(\E[wt] = 3\)

  7. \(\E[qsec] = 10\)

  8. \(\E[vs] = 0.5\)

  9. \(\E[am] = 0.5\)

  10. \(\E[gear] = 4\)

  11. \(\E[carb] = 4\)

Rules:

To win



Solution below…















n <- nrow(mtcars)
h0 <- c(20, 5, 150, 150, 4, 3, 10, 0.5, 0.5, 4, 4)
p <- c()
for (i in 1:ncol(mtcars)) {
  t <- sqrt(n)*(mean(mtcars[,i])-h0[i])/sqrt(var(mtcars[,i]))
  p[i] <- 2*pnorm(-abs(t))
}
round(p,6)
##  [1] 0.932214 0.000169 0.000229 0.784622 0.000020 0.209113 0.000000 0.483008
##  [9] 0.287870 0.016576 0.000032