--- Onehack.us e-Learning Platform ---
- [Narrator] When you have a single quantitative variable
like age or how much time a person has spent on your website
and you want to compare that variable
to a single hypothesized value,
say a national average,
then a one-sample t-test is going to be your test choice.
Fortunately, this is really easy to do in R.
To demonstrate this, I'm going to load a few packages,
including the datasets package,
and then I'm going to come down
and get a little bit of information
about a dataset called quakes,
and this is about earthquakes in Fiji,
all of which had a value on the Richter scale,
I assume that's what it is,
greater than four,
and we can get the names of all the variables.
Now, they're listed off to the right,
but I'm going to just do quakes and then names.
So here are the ones that we want.
The only one I'm going to focus on right now
is mag for magnitude.
So what I'm going to do
is I'm going to take the quakes dataset.
I'm going to pull the magnitude vector.
By the way, I'm using pull because that gives me a vector,
which is what I need for some of my other analyses,
as opposed to select which returns a data frame.
And then I'm going to take a look at the beginning of it
with glimpse.
If I used print, it would show me all 1,000 pieces of data.
I just want to see the first few so I'm using glimpse,
and I'm going to save that into df for data frame.
So let me run that command.
And you can see over here we've got numbers
one through 1,000 and there's the actual values,
and here we are down here, the first few repeated as well.
Great.
Now, before you do numerical analyses,
it's always a good idea to do some graphs,
get some visualizations,
and get a feel for what's going on in your data.
For this example, I'm going to use base graphics from R
as opposed to GG plot two.
So I'm going to take df and feed it into hist for histogram.
And when I do that, you can see we've got
a histogram over here,
and because they said they don't even record these things
until they hit four,
it starts at four and you can see it tapers off,
which is really what you would expect.
And then we're going to get a box plot,
and that's important for seeing if we have outliers.
I'm going to make it sideways, horizontal,
so that the number scale is in the same direction
as the histogram.
And when I do that, you can see that we've got a median
of around 4.6, the middle 50% range from maybe 4.3 to 4.9,
and then we have a few outliers up on the high end.
I can get a summary table,
the descriptive statistics for this table also,
that give me the quartiles,
that's the minimum, or Q zero, the first quartile,
the median, which is also the second quartile, or Q2,
the third quartile and the max, which is quartile four,
as well as the mean,
so we have a mean of 4.62 for these.
So that's a quick look at our data,
and with that it's reasonable to go ahead
and start doing some statistical analyses,
and we can do the t-test.
R has a built-in t-test function.
It's actually one we're going to use
for a lot of different things 'cause it's generic
and it's powerful, and it's t.test.
And all I'm going to do is I'm going to take the data
that I've saved in data frame and feed it into t.test.
Now this works because I have just a single variable
in there and because it saves as a vector
and not a data frame.
So, this works.
And when I do that, let's zoom in on that,
here's the results that I get.
It says one-sample t-test.
It gives us a value of t of 362.76, which is enormous.
The degrees of freedom 999
'cause we have 1,000 observations,
and the p value has many, many zeros in front of it,
so we had to go to scientific notation here.
It's about 16 zeros.
But the problem is it's testing it
against a null hypothesis of zero.
Since the data doesn't even start
until we have a level of four,
that makes sense.
The 95% confidence interval for the mean
goes from 4.595 to 4.645.
It's actually really tight,
but with 1,000 observations that's not surprising,
and there's the actual sample mean.
Now, because we did a couple of silly things here,
we did a null hypothesis with a population value of zero,
which isn't even possible in the dataset,
we may want to make a few changes.
So this time I'm going to use the t.test function,
and I'm going to say let's do a directional hypothesis,
not that I need to,
but I want you to know that it's possible.
And all I have to do is say alternative
and in this case is equal to greater.
And then I can use mu,
which is the symbol for the population mean.
And in this case, I'm going to set it to 4.6,
which actually is pretty high considering the sample mean
is 4.6204, but let's run that t-test.
Now when I do that, we come down here
and you can see that the t value is much smaller.
Previously, it was like 360-something.
Here it's 1.6, the degrees of freedom is 999,
the p value is 0.05477.
So if you're taking a strict cutoff of 0.05 or less,
it doesn't make it, but it's really close,
which is amazing considering the sample mean
is only two-hundredths above our population value of 4.6
that we used for this one.
That's the true mean greater than 4.6.
And we have a confidence interval
that because it's directional it's one-sided.
It's as low as 4.599 and as high as infinity,
so we just barely dip below that.
So, there are easy ways to do t-tests.
You can do some alternatives,
you can do some modifications
if they suit your data and your questions,
but either way, it's really simple to get started
with inferential testing in R.