For this lab you should submit, on Blackboard, your .Rmd
and .docx
-files at the end of the lab hour.
Test objects and confidence interval values
We will learn how to perform different tests in the next couple of weeks.
All of the functions we will learn produce a test object in R.
Test objects all share some features.
Suppose we have assigned the name test
to a particular test object, then
Attribute | Contains |
---|---|
test$statistic |
The value of the test statistic |
test$p.value |
The \(p\)-value of the test |
test$conf.int |
The confidence interval for the test |
test$alternative |
The alternative hypothesis type (upper / lower / two-tailed) of the test |
test$method |
The test type |
Many tests expect a key/value pair of columns to allow the ~
way of writing test commands.
Task Load the dataset mpg
using data(mpg)
Task Create a new dataset mpg.cty.hwy
using gather
that contains a key column type
and a value column mpg
with the columns cty
and hwy
from the original data.
Task Construct a test object using
t.cty.hwy = t.test(mpg ~ type, data=mpg.cty.hwy)
Task Write out the test object itself (using a code block with just the test object name itself alone on a line). Describe the printout and what information you can read from it.
Task Using the $
syntax in the table above, extract the p-value and confidence interval from the test.
For some functions we have been using, we need a data.frame
. The library broom
contains functions for converting various objects into data.frame
s. Most useful is the function tidy
that converts most things into a data.frame
.
Task Load the library broom
and use the function tidy
to create a data.frame
from t.cty.hwy
.
Bootstrap confidence intervals
Recall that we can use bootstrap to get empirical distributions of sample statistics. This can be used to create a special kind of confidence intervals directly from the data.
For instance, one might do the following to get a 10% confidence interval for the mean city milage:
cty.mean = do(1000)*mean(~cty, data=mpg %>% sample_n(50))
cty.boot.ci = quantile(~mean, probs=c(.05, .95), data=cty.mean)
Task Change the above code to find a 5% confidence interval for the mean highway mileage.