0% found this document useful (0 votes)
13 views7 pages

Hypothesis Testing with R Tutorial

1) The document discusses hypothesis testing using R. Hypothesis testing helps determine if conclusions drawn from sample data can be applied to the overall population by analyzing if differences observed in samples are statistically significant or likely due to chance. 2) The key steps are defining the null and alternative hypotheses, such as whether two methods yield the same or different results. Common hypotheses include whether a sample mean equals or differs from a hypothesized value. 3) Functions like t.test() can be used to conduct one-sample or two-sample hypothesis tests in R. For a one-sample test example, t.test() found no significant difference between sample data and a hypothesized mean of 7.

Uploaded by

krisswu313
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Hypothesis Testing with R Tutorial

1) The document discusses hypothesis testing using R. Hypothesis testing helps determine if conclusions drawn from sample data can be applied to the overall population by analyzing if differences observed in samples are statistically significant or likely due to chance. 2) The key steps are defining the null and alternative hypotheses, such as whether two methods yield the same or different results. Common hypotheses include whether a sample mean equals or differs from a hypothesized value. 3) Functions like t.test() can be used to conduct one-sample or two-sample hypothesis tests in R. For a one-sample test example, t.test() found no significant difference between sample data and a hypothesized mean of 7.

Uploaded by

krisswu313
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hypothesis Testing using R

Pramudita Satria Palar, Vani Virdyawan, Ferryanto

2022-04-24

Introduction

In this tutorial, we will be testing a hypothesis using R. There are several hypothesis testing methods that
you can immediately use once you have R installed on your computer. You can use these functions for
various applications according to your needs (e.g., one sample or two sample difference test or normality
test). First, let’s return to the fundamental question: Why do we need hypothesis testing? Recall that the
sample we take from the population is limited in number, so we need to be careful in drawing conclusions
from our data. Hypothesis testing helps in answering these questions.
It should be noted that the question or hypothesis that you want to answer must be able to be answered
using a scientific method, for example: “Does students who take the Calculus course tutorial get a higher
score than those who do not take the tutorial?” (in a statistical sense). The output of the hypothesis test
is whether the difference that occurs is statistically significant or not. These results will help us in making
decisions later.

Defining hypotheses

First, we need to determine the null and alternative hypotheses in testing the hypothesis. Usually, though
not always, the null hypothesis is something that we want to reject. For example, if you create a manu-
facturing method that is better than the old method, you make the null hypothesis that the new and old
manufacturing methods perform the same; therefore, you want to reject the null hypothesis. An alternative
hypothesis is a hypothesis that you accept when you reject the null hypothesis because there is sufficient
evidence for you to do so. We will use the notation H0 for the null hypothesis and Ha for the alternative
hypothesis.
The following examples give you an idea of H0 and Ha :

• H0 : There is no significant difference between the results yielded by method A and method B.
• Ha : Method A yields higher output than method B.

Notice the word higher, which indicates that the hypothesis test above is one-sided. Similarly, you can also
define a two-sided hypothesis test, for example:

• H0 : Your monthly average wage is 1000$.


• Ha : Your monthly average wage is not 1000$.

The alternative hypothesis stated above is two-sided because your salary could be less or higher than 1000$.
The two possible outputs from hypothesis testing are as follows:

• A small p-value indicates strong evidence to reject H0 , so you reject H0 .

1
• A large p-value indicates weak evidence to reject H0 , so you fail to reject H0 .

The threshold of p-value where you decide between rejecting or failing to reject H0 will depend on the level
of significance that you want. Generally, the value 0.05 is commonly used in various communities. However,
some communities take even lower limits to be more confident in their decisions.
To give an initial illustration, we will try to compare two data sets from N (0, 1) and N (2, 1.5), each with 50
observations, and we will name them X1 and X2 .

5
0
y

−5

X1 X2

Figure 1: Boxplot of the two data that we will compare (blue filled circle shows the mean values)

The boxplot above shows that X2 has a higher mean than X1 , so we can say that X2 is higher on average
than X1 . But is the difference just a coincidence? Or because a certain effect causes X2 to be higher on
average than X1 ? Pay attention that some realizations from X2 have lower values than all realizations from
X1 . This is an example of a two-sample test in which we want to compare whether there is a significant
difference between the two samples or not. In this regard, the quantity that we want to compare is the
difference between the means of the data. A hypothesis test can also be a one-sample test, which we use to
investigate whether a mean value from one sample is consistent with a hypothesized value of the mean.
We will answer these questions with a t-test using the [Link]() function.

Using the function [Link]()

t-test with one sample

One-sample t-test usually tests one of the following pairs of null and alternative hypotheses

1. H0 : µ = µ0 , Ha : µ ̸= µ0
2. H0 : µ ≤ µ0 , Ha : µ < µ0

2
3. H0 : µ ≤ µ0 , Ha : µ > µ0

where µ0 is the value that we define for the null hypothesis.


We will start with a simple example by creating synthetic data generated from the normal distribution
N (7, 2) with 50 observations. In the context of the real world, of course, all you have are the results of your
observations. What you want to answer here is, for example, is it true that the true mean of this data is 7?
Or we can define our null hypothesis as follows; for example, is it true that the actual mean is less than 7.2?
Or is it true that the actual mean is greater than 7.2? We can write these three null hypotheses along with
their alternative hypotheses in the following format:

1. H0 : µ = 7, Ha : µ ̸= 7
2. H0 : µ ≤ 6.8, Ha : µ > 6.8
3. H0 : µ ≥ 7.2, Ha : µ < 7.2

The figure below shows a visualization of the data that we want to analyze in the form of a histogram. At
first glance, the data seems to have a mean number of µ = 7. Hypothesis testing will provide a formal answer
to this question.

Histogram of xs1
10
8
Frequency

6
4
2
0

6.6 6.8 7.0 7.2 7.4

xs1

Figure 2: Boxplot untuk t-test dengan satu sample

First, we will try using the following null hypothesis Ha : µ = 7, and then use the [Link]() function. To
perform a t-test with [Link](), it is recommended that you enter your minimum argument, i.e., mu (the
null hypothesis) and alternative (that you can enter with [Link],less, or greater). For example, if
your null hypothesis is H0 : µ = 7, you must enter [Link] in alternative. The results of the t-test will
be stored in a variable which we will name tresult.

3
[Link](5)
xs1 <- rnorm(50, mean=7,sd = 0.2)
tresult <- [Link](xs1, mu = 7, alternative="[Link]")

You can output the results of the t-test using the following syntax (or simply, just type tresult in your R
console)

tresult

##
## One Sample t-test
##
## data: xs1
## t = 0.42965, df = 49, p-value = 0.6693
## alternative hypothesis: true mean is not equal to 7
## 95 percent confidence interval:
## 6.952244 7.073730
## sample estimates:
## mean of x
## 7.012987

Pay attention to significant figures such as p-value and t. You need to look at the p-value and compare it
to the significance level that you already set. For example, if you set the significance level to 5 percent,
you will fail to reject the null hypothesis if the p-value obtained is less than 0.05. On the other hand, you
will reject the null hypothesis if the p-value is greater than 0.05. For the above case, you will get a p-value
greater than 0.05, which means you failed to reject the hypothesis that µ = 7. In other words, there is strong
evidence for rejecting µ = 7.
If you want to display the p-value and t in your console (e.g., for further calculations), you can type
tresult$[Link] and tresult$statistic as shown in the example below.

tresult$statistic

## t
## 0.4296464

tresult$[Link]

## [1] 0.6693364

Now, let’s try with other pairs of null and alternative hypothesis. For the pair of hypotheses H0 : µ ≤ 6.8,
Ha : µ > 6.8, then you must enter alternative="greater" in your console:

tresult_greater <- [Link](xs1,mu=6.8,alternative = "greater")


tresult_greater

##
## One Sample t-test
##
## data: xs1
## t = 7.0463, df = 49, p-value = 2.812e-09

4
## alternative hypothesis: true mean is greater than 6.8
## 95 percent confidence interval:
## 6.96231 Inf
## sample estimates:
## mean of x
## 7.012987

You will get a very small p-value. For example, with a significance level of 5%, you reject the null hypothesis
that H0 : µ ≤ 6.8 and accept the alternative hypothesis Ha : µ > 6.8. You can do the same for H0 : µ ≥ 7.2,
Ha : µ < 7.2. Please try using the alternative alternative="less" in your R console.

tresult_less <- [Link](xs1,mu=7.2,alternative = "less")


tresult_less

##
## One Sample t-test
##
## data: xs1
## t = -6.187, df = 49, p-value = 6.023e-08
## alternative hypothesis: true mean is less than 7.2
## 95 percent confidence interval:
## -Inf 7.063664
## sample estimates:
## mean of x
## 7.012987

Two-sample problem

Now we return to the two-sample question that we wanted to answer above. We will use [Link]() for two
samples problem with the following syntax: [Link](x,y) where x dan y are the two samples we want to
test. The plot below shows the histogram, normalized so that we have density in the ordinate, so as to
visualize the difference between the two data better.
In this regard, we want to test the difference between two means with the following null hypothesis:

H0 : µ1 = µ2

We first try using a confidence level of 0.95 by executing the code below:

[Link](5)
x1 = rnorm(100, mean = 0, sd = 1) # Data pertama
x2 = rnorm(100, mean = 1.2, sd = 2.5) # Data kedua

t2result <- [Link](x1,x2,[Link]=0.95)

The most important value for the results of the t-test above is the p-value, as it informs us for making a
decision. The most common limit for the significance level is α = 0.05, i.e., we reject the null hypothesis
when p < 0.05. The initial confidence level number set by [Link]() is 0.95, but you can adjust the [Link]
number to your liking (e.g., [Link]=0.99 to give a confidence level of 0.99).
Let us continue with more realistic data. The context of the following problem is to compare two catalysts to
see how they affect a specific chemical process. Catalyst 1 (let us name it cat_1) is a frequently used catalyst.
On the other hand, Catalyst 2 (let us call it cat_2) is cheaper and recommended for use if its performance

5
0.4
0.3
Density

0.2
0.1
0.0

−6 −4 −2 0 2 4 6

Data

Figure 3: Boxplot dua data yang akan kita bandingkan (lingkaran biru menunjukkan angka mean)

is roughly the same as that of Catalyst 1. The experiment is performed eight times to investigate whether
the second catalyst is actually better than the first catalyst. The data we want to analyze is shown in the
Table below, where num is the number of observations.

num cat_1 cat_2


1 91.50 89.19
2 94.18 90.95
3 92.18 90.46
4 95.39 93.21
5 91.79 97.19
6 89.07 97.04
7 94.72 91.07
8 89.21 92.75

We can write the observation results from the two types of catalysts using the function c to save the data in
a vector format. The same function, i.e., [Link]() will be used with little tweaks because we assume that
the two catalysts have the same value of unknown variance. We will add the argument [Link]=TRUE
to the function [Link](). Remember that mu=0 in the context of a two-sample test is for the difference
between two means

cat_1 <- c(91.50, 94.18, 92.18, 95.39, 91.79, 89.07, 94.72, 89.21) # Katalis 1
cat_2 <- c(89.19, 90.95, 90.46, 93.21, 97.19, 97.04, 91.07, 92.75) # katalis 2

t2resultc <- [Link](cat_1,cat_2,mu=0,[Link]=TRUE)

Let us show the result from the hypothesis test by typing t2resultc in our console:

6
t2resultc

##
## Two Sample t-test
##
## data: cat_1 and cat_2
## t = -0.35359, df = 14, p-value = 0.7289
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.373886 2.418886
## sample estimates:
## mean of x mean of y
## 92.2550 92.7325

We can clearly see that the p-value is higher than 0.05, which means that we fail to reject the null hypothesis.
It is worth noting that our null hypothesis is µ1 = µ2 ; in other words, µ1 − µ2 = 0. We can then infer that
the first and the second catalysts have the same performance. We can now say that the second catalyst
can be used as a cheaper alternative because the hypothesis test reveals that the difference (in terms of
performance) is not statistically significant.

Common questions

Powered by AI

Graphical representations like boxplots and histograms play a crucial role in visually summarizing and interpreting data distributions, variability, and central tendencies. In hypothesis testing, they help in identifying outliers, visualizing assumptions like normality, and comparing group means, which all contribute to framing sound hypotheses. A boxplot can succinctly display differences in group means, variability, and potential outliers, while histograms provide insight into data distribution, aiding the validation of test assumptions like normality required for t-tests .

The p-value measures the probability of observing test results at least as extreme as the results actually observed, under the assumption that the null hypothesis is true. A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis, leading to its rejection. Conversely, a large p-value suggests weak evidence against the null hypothesis, leading to a failure to reject it. Therefore, the p-value is critical in helping decide between rejecting or failing to reject the null hypothesis, based on a predetermined level of significance .

Setting a lower significance level, such as 0.01 instead of 0.05, reduces the Type I error probability—the risk of rejecting a true null hypothesis. This increase in stringency means stronger evidence is required to reject the null hypothesis, potentially leading to more cautious conclusions. However, it can also increase the risk of Type II errors, as more extreme evidence is needed for rejection. Thus, the choice reflects a trade-off between error types based on the context of the research .

The t.test() function in R is used to determine if there is a significant difference between the means of two data sets or if a sample mean is significantly different from a known value (one-sample test). It performs hypothesis testing using a t-distribution and requires arguments such as the sample data and the null hypothesis mean (mu). The function can handle one-sample, two-sample, and paired tests by setting parameters like 'alternative' (two.sided, less, greater) and 'var.equal' for equal variance assumption in two-sample tests. After execution, the function provides the t-statistic, degrees of freedom, p-value, and confidence intervals, helping in statistical decision-making for the hypothesis being tested .

Assuming equal variance is based on the premise that populations from which samples are drawn have the same variability. This assumption simplifies calculations and improves test power. In R, using the t.test() function, you can assume equal variance by setting the parameter var.equal = TRUE. To check this assumption, one might perform a preliminary test, like Levene's test, to compare the variances of the two groups. If the test confirms homogeneity of variance, the equal variance assumption is valid, facilitating the use of pooled variance in calculations .

Hypothesis testing aims to determine whether there is enough statistical evidence in a sample of data to reject a null hypothesis within an acceptable error margin. This process helps in making decisions about the data, ensuring the conclusions drawn are scientifically valid by quantifying the likelihood of observing the data if the null hypothesis were true .

A one-sample hypothesis test is sufficient when comparing the sample mean to a known or hypothesized population mean. For example, analyzing whether the average test score of a classroom differs from the national average requires a one-sample test. The expected outcome is either failing to reject the null hypothesis if the p-value is above the significance level, suggesting no significant difference, or rejecting it if the p-value falls below, indicating a significant deviation from the hypothesized mean .

A two-tailed hypothesis test is appropriate when we are interested in deviations from a hypothesized mean in either direction—greater or less than. For instance, if evaluating whether a drug has a different effect from a standard treatment, without a predefined direction of the expected effect, we set up: H0: µ = µ0 (no difference), and Ha: µ ≠ µ0 (a difference exists). This setup captures the possibility of the drug being either more or less effective than the standard treatment .

A 95% confidence interval provides a range of values that, with 95% certainty, includes the true population parameter being estimated. In hypothesis testing, especially with t-tests, it gives additional insight into the estimate of the mean difference: if the interval does not contain the value under the null hypothesis, it supports the rejection of the null hypothesis. It helps visualize the precision and reliability of the estimated effect size, guiding decisions beyond just the p-value .

In a one-sample t-test, hypothesis statements can be structured by comparing the sample mean to a hypothesized population mean. For example, potential null and alternative hypotheses include: H0: µ = µ0 and Ha: µ ≠ µ0 (two-sided test), H0: µ ≤ µ0 and Ha: µ > µ0 (right-tailed test), or H0: µ ≥ µ0 and Ha: µ < µ0 (left-tailed test). These setups allow tests against a specific value of interest in the population. For instance, testing if a mean is 7, against the alternative that it is not, forms a two-sided test: H0: µ = 7, Ha: µ ≠ 7 .

You might also like