[A, SfS] Chapter 6: Hypothesis Testing: 6.8: Tests & Confidence Intervals in R
Hypothesis Tests and Confidence Intervals in R
Hypothesis tests and confidence intervals in R
In this section, we will discuss how we can use built-in #\mathrm{R}# functions to conduct hypothesis tests and compute confidence intervals when given a set of data.
There are a few built-in functions in the #\mathrm{R}# statistics package that can carry out the hypothesis testing and compute confidence intervals for us. We did not mention these at first because it was important to first learn the computational mechanisms of these procedures. Also, some of these functions require raw data, and don't work if all you have is a summary of the data (e.g., mean and standard deviation).
We first consider the function #\mathtt{t.test()}#. This function can be used for the one-sample and two-sample t-tests, and also for the paired data t-test, and provides confidence intervals as well, but you must have the data vectors, not just summaries. If you only want a confidence interval, you can ignore the other output.
One-sample t-testTo perform a one-sample t-test using the #\mathtt{t.test()}# function, you should have a vector #X# of data measured on a continuous variable whose population mean is #\mu#.
If the alternative hypothesis is two-sided (e.g., #H_1:\mu \neq \mu_0#), you don't have to specify anything.If the alternative hypothesis is one-sided (e.g., #H_1: \mu < \mu_0# or #H_1:\mu > \mu_0#) you must either include the setting #\mathtt{alternative = “less”}# or #\mathtt{alternative = “greater”}#.
If #\mu_0# is a number other than zero, you must also specify this.
For example, if your null hypothesis is #H_0:\mu = 15# then include the setting #\mathtt{mu = 15}#.
The output will also include a #95\%# confidence interval for #\mu#. If you want a different confidence level, you can specify this also. For example, If you want a #99\%# confidence interval, include the setting #\mathtt{conf.level=0.99}#. If the alternative is either “less” or “greater” then the confidence interval will be one-sided. Otherwise, it will be two-sided.
#\text{}#
Two-sample t-test or Paired data t-testTo perform a two-sample t-test or a paired data t-test, you need a second vector #Y# of data measured on a continuous variable.
If you want a paired data t-test then #X# and #Y# must be of the same length, and you include the setting #\mathtt{paired = TRUE}#.
For two independent samples, if you want to assume that the two population variances are equal then you should include the setting #\mathtt{var.equal = TRUE}#.
In both of these situations, if you are conducting a one-sided test then it is necessary to enter the data vectors into the function in the correct order. If you have #H_1:\mu_X - \mu_Y > 0# then you should enter first #X# and the #Y#, with the #\mathtt{alternative = “greater”}# setting.
For paired data, if you have #H_1: \mu_D > 0# and the differences are based on #X - Y#, then you should enter first #X# and then #Y#, with the #\mathtt{alternative = “greater”}# and #\mathtt{paired = TRUE}# settings.
RThe following examples assume #X# and #Y# are data vectors that are already present in the #\mathrm{R}# workspace.
Testing #H_0: \mu \geq 100# against #H_1: \mu < 100#, with a #90\%# CI.
> t.test(X,alternative="less",mu=100,conf.level=0.90)
Paired data t-test of #H_0: \mu_D \leq 0# against #H_1: \mu_D > 0#, where the vectors #X# and #Y# have the same length.
> t.test(X,Y,paired=TRUE,alternative="greater")
Testing #H_0: \mu_X - \mu_Y = 0# against #H_1: \mu_X - \mu_Y \neq 0#, while assuming equal population variances.
> t.test(X,Y,var.equal=TRUE)
Note: For this and all the other tests described below, it is not necessary to spell out the words “alternative”, “less”, “greater”, TRUE or FALSE completely.
If you just provide the first letter of the word, #\mathrm{R}# will know what you intend. This protocol is true in general for #\mathrm{R}# functions.
As mentioned previously, you can add the following user-defined functions to your #\mathrm{R}# workspace if you need to compute the degrees of freedom #\nu# for a two-sample t-test when the population variances are not assumed equal:
DF = function(sd1,sd2,n1,n2){
return((sd1^2/n1+sd2^2/n2)^2/((sd1^2/n1)^2/(n1-1)+(sd2^2/n2)^2/(n2-1)))
}
For example, suppose #s_1 = 1.7#, #s_2 = 1.9#, #n_1 = 12# and #n_2 = 13#. Then
> DF(1.7,1.9,12,13)
would return the degrees of freedom #\nu#.
#\text{}#
Next we consider the #\mathtt{prop.test()}# function. This function is useful for conducting the one-sample and two-sample tests for population proportions, and obtaining confidence intervals. This function does not use the z-statistic, however. It uses the chi-square statistic, which is equivalent.
Moreover, the method used by this function for computing the standard deviation #\hat{p}# is not the same method we have presented in our course, so we will get slightly different values for the bounds of a confidence interval for #p# or for #p_1 - p_2#, and also slightly different values for the P-values in hypothesis tests about #p# or about #p_1 - p_2#, if we use #\mathtt{prop.test()}#.
One-sample Proportion TestTo perform a one-sample proportion test (e.g. #H_0: p=0.5# against #H_1: p \neq 0.5#), you must provide the number #X# of observed successes and the number #n# of observations.
If you have a one-sided alternative you must include the setting #\mathtt{alternative=“greater”}# or #\mathtt{alternative=“less”}#.
If you are comparing #p# to a number other than #0.5#, you must also specify this (e.g., #\mathtt{p=0.65}#).
The output will include a #95\%# confidence interval for #p#. This confidence interval will be truncated so that it remains between #0# and #1#. If you want a different confidence level, you can specify this also.
Something called the “Yates’ continuity correction” will be implemented unless you specify #\mathtt{correct=FALSE}#. we don't want this correction in this course, so you must always include this setting.
For example, if you want a #99\%# confidence interval, include the setting #\mathtt{conf.level=99}#. If the alternative is “less” or “greater” then the CI will be one-sided.
#\text{}#
Two-sample Proportion TestTo perform a two-sample proportion test (e.g., #H_0:p_1 - p_2 = 0# against #H_1 : p_1 - p_2 \neq 0#), you must provide two vectors. The first vector contains the number of observed successes #X_1# and #X_2# for each sample group, while the second vector contains the number of observations #n_1# and #n_2# for each sample group. In this case, it does not make sense to specify a value #p#, and you will get an error if you do.
In both of these situations, if you are conducting a one-sided test then it is necessary to enter the data vectors into the function in the correct order. If you have #H_1: p_1 - p_2 > 0# then you should enter first #X_1# and then #X_2#, with the #\mathtt{alternative = “greater”}# setting.
For example, if you want a #99\%# confidence interval, include the setting #\mathtt{conf.level=99}#. If the alternative is “less” or “greater” then the CI will be one-sided.
RThe following examples assume #X# and #Y# are data vectors that are already present in the #\mathrm{R}# workspace.
Testing #H_0: p \geq 0.65# against #H_1: p < 0.65#, with a #99\%# CI, based on a sample with #23# successes among #35# observations.
> prop.test(23,35,alternative="less",p=0.65,conf.level=0.99,correct=F)
Testing #H_0: p_1 - p_2 \leq 0# against #H_1: p_1 - p_2 > 0#, based on a sample with with #23# successes among #35# observations and a second sample with #17# successes out of #28# observations.
> prop.test(c(23,17),c(35,28),alternative="greater",correct=F)
If you don't have the numbers of successes and the sample sizes already computed, and just have columns #X# and #Y# of data consisting of #1#s and #0#s, then you can use the #\mathtt{sum()}# and #\mathtt{count()}# functions within the #\mathtt{prop.test()}# function.
> prop.test(sum(X),count(X),p=0.37,conf.level=0.90,correct=F)
> prop.test(c(sum(X),sum(Y)),c(count(X),count(Y)),alt="g",correct=F)
#\text{}#
Exact Binomial TestThe #\mathtt{binom.test()}# function performs the exact binomial test for the probability of a success, based on the number of successes in a sample from a population.
The instructions are the same as for #\mathtt{prop.test()}# above, but only in the one-sample situation, and there is no continuity correction.
This is preferred over #\mathtt{prop.test()}# with small sample sizes.
#\text{}#
Computing Power of a t-test using RThe #\mathtt{power.t.test()}# function can be used to compute the power for one- and two-sample t-tests, or determine the parameter values necessary to obtain a target power.
> power.t.test(n = 20, delta = 1.5, sig.level = 0.01. type = "one.sample", alternative = "two.sided", sd = 3)
To compute the power of a one-sided, two-sample t-test at the #0.05# (default) significance level when the sample sizes are #20# and #24#, the standard deviation is #1# (default) for both populations, and the effect size delta is #0.5#, use:
> power.t.test(n = c(20, 24), delta = 0.5, type = "two.sample", alternative = "one.sided")
To compute the sample size necessary to achieve a power of #0.90# in a one-sided, one-sample t-test at the #0.05# significance level with a standard deviation of #1#, use:
> power.t.test(power = .90, delta = 2, type = "one.sample", alternative = "one.sided")
To compute the effect size corresponding to a power of #0.80# in a one-sided, one-sample, t-test at the #0.10# significance level when the sample size is #53# and the standard deviation is #4#, use:
> power.t.test(power = .80, n = 53, type = "one.sample", alternative = "one.sided", sig.level = 0.10)
To compute the significance level corresponding to a power of #0.85# in a two-sided, one-sample t-test when the sample size is #26#, the effect size is #1#, and the standard deviation is #4#, use:
> power.t.test(power = .85, n = 26, type = "one.sample", alternative = "two.sided", sig.level = NULL)
Computing Power of a Proportion Test using RThe #\mathtt{power.prop.test()}# function works in a similar way to compute the power of a two-sample test for equality of proportions or to determine the parameter values necessary to obtain a target power.
> power.prop.test(n = 50, p1 = 0.50, p2 = 0.75, sig.level = 0.01, alternative = "two.sided")
To compute the sample size necessary for both groups to achieve a power of #0.80# in a two-sided, two-sample test of proportions at significance level #0.05# (default), use:
> power.prop.test(p1 = 0.50, p2 = 0.75, power = 0.80, alternative = "two.sided")
To compute the true population proportion of the second group necessary to achieve a power of #0.80# in a one-sided, two-sample test of proportions at the significance level #0.05# with both sample sizes equal to #50#, use:
> power.prop.test(n = 50, p1 = 0.50, power = 0.80, alternative = "one.sided")
To compute the effect size corresponding to a power of #0.80# in a one-sided, one-sample, t-test at the #0.10# significance level when the sample size is #53# and the standard deviation is #4#, use:
> power.prop.test(power = .80, n = 53, type = "one.sample", alternative = "one.sided", sig.level = 0.10)
Finally, to compute the significance level of a two-sided, two-sample test of proportions at a power of #0.90# when the sample sizes are both #50#, and the true population proportions are #0.50# and #0.75#, respectively, use:
> power.prop.test(n = 50, p1 = 0.50, p2 = 0.90, power = 0.90, alternative = "two.sided", sig.level = NULL)
Unfortunately, this function assumes both samples have the same sample size, and does not accommodate the one-sample setting.
Or visit omptest.org if jou are taking an OMPT exam.