Tip:
Highlight text to annotate it
X
The function in the last video is used to report the mean and
the standard error.
Suppose I wanted to also report a 95% confidence
interval for the mean?
Here I'll construct a confidence interval.
The qt function is used to identify the 97.5 quantile of
the T-distribution with n minus 1 degrees of freedom.
There might be some temptation to concatenate the confidence
interval with the current results being reported from
the function.
In some instances, this might be fine, but it isn't a very
clear way to organize the results.
For example, what if another user mistakenly thought the
first two values were the confidence interval and the
last two were the mean and the standard error?
That could be disastrous.
When multiple objects are being returned from a
function, it's usually helpful to return them in a list.
But even now, things might still be a little bit unclear.
For this reason, it's useful to add names to each element
of the list.
And before I forget, I should update
the name of the function.
I'm going to name it GetCI for Get Confidence Interval.
There's one more change I want to make before moving on--
making the function slightly more general by allowing for a
different confidence level.
I'm going to do this using the second argument called level.
Since the 95% confidence level is so commonly used, I'll
specify a 0.95 confidence level as the default.
Next, I need to make some slight adjustments to the body
of the function to make use of this new argument.
I can compute the upper percentile for the confidence
interval, then provide this as a substitute for 0.975.
Now I can run the function without specifying a level to
get a standard 95% confidence level.
Or if I want a different confidence level, say, 99%, I
can easily make that request.
Suppose the user input a value for a level of 99 rather than
0.99 for the level argument.
In this application, it would be nice if the function failed
elegantly and communicated why there was an error rather than
simply reporting warning.
This will be the top of the next video.