\[\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.
\(\E[mpg] = 20\)
\(\E[cyl] = 5\)
\(\E[disp] = 150\)
\(\E[hp] = 150\)
\(\E[drat] = 4\)
\(\E[wt] = 3\)
\(\E[qsec] = 10\)
\(\E[vs] = 0.5\)
\(\E[am] = 0.5\)
\(\E[gear] = 4\)
\(\E[carb] = 4\)
Rules:
You cannot load any external packages, and you cannot use any kind of pvalue
function (if there is one…)
The maximum amount of code that you can send me is 10 lines
To win
You must email me your code brantly.callaway@uga.edu
I’ll run exactly the code that you send me, and if your code gets all 11 p-values correct, then you 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