The Complete Guide: Hypothesis Testing in R (2024)

A hypothesis test is a formal statistical test we use to reject or fail to reject some statistical hypothesis.

This tutorial explains how to perform the following hypothesis tests in R:

  • One sample t-test
  • Two sample t-test
  • Paired samples t-test

We can use the t.test() function in R to perform each type of test:

#one sample t-testt.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, …)

where:

  • x, y: The two samples of data.
  • alternative: The alternative hypothesis of the test.
  • mu: The true value of the mean.
  • paired: Whether to perform a paired t-test or not.
  • var.equal: Whether to assume the variances are equal between the samples.
  • conf.level: The confidence level to use.

The following examples show how to use this function in practice.

Example 1: One Sample t-test in R

A one sample t-test is used to test whether or not the mean of a population is equal to some value.

For example, suppose we want to know whether or not the mean weight of a certain species of some turtle is equal to 310 pounds. We go out and collect a simple random sample of turtles with the following weights:

Weights: 300, 315, 320, 311, 314, 309, 300, 308, 305, 303, 305, 301, 303

The following code shows how to perform this one sample t-test in R:

#define vector of turtle weightsturtle_weights <- c(300, 315, 320, 311, 314, 309, 300, 308, 305, 303, 305, 301, 303)#perform one sample t-testt.test(x = turtle_weights, mu = 310)One Sample t-testdata: turtle_weightst = -1.5848, df = 12, p-value = 0.139alternative hypothesis: true mean is not equal to 31095 percent confidence interval: 303.4236 311.0379sample estimates:mean of x 307.2308 

From the output we can see:

  • t-test statistic: -1.5848
  • degrees of freedom:12
  • p-value:0.139
  • 95% confidence interval for true mean:[303.4236, 311.0379]
  • mean of turtle weights:307.230

Since the p-value of the test (0.139) is not less than .05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that the mean weight of this species of turtle is different from 310 pounds.

Example 2: Two Sample t-test in R

A two sample t-test is used to test whether or not the means of two populations are equal.

For example, suppose we want to know whether or not the mean weight between two different species of turtles is equal. To test this, we collect a simple random sample of turtles from each species with the following weights:

Sample 1: 300, 315, 320, 311, 314, 309, 300, 308, 305, 303, 305, 301, 303

Sample 2: 335, 329, 322, 321, 324, 319, 304, 308, 305, 311, 307, 300, 305

The following code shows how to perform this two sample t-test in R:

#define vector of turtle weights for each samplesample1 <- c(300, 315, 320, 311, 314, 309, 300, 308, 305, 303, 305, 301, 303)sample2 <- c(335, 329, 322, 321, 324, 319, 304, 308, 305, 311, 307, 300, 305)#perform two sample t-testt.test(x = sample1, y = sample2)Welch Two Sample t-testdata: sample1 and sample2t = -2.1009, df = 19.112, p-value = 0.04914alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -14.73862953 -0.03060124sample estimates:mean of x mean of y 307.2308 314.6154 

From the output we can see:

  • t-test statistic: -2.1009
  • degrees of freedom:19.112
  • p-value:0.04914
  • 95% confidence interval for true mean difference: [-14.74, -0.03]
  • mean of sample 1 weights: 307.2308
  • mean of sample 2 weights:314.6154

Since the p-value of the test (0.04914) is less than .05, we reject the null hypothesis.

This means we have sufficient evidence to say that the mean weight between the two species is not equal.

Example 3: Paired Samples t-test in R

A paired samples t-test is used to compare the means of two samples when each observation in one sample can be paired with an observation in the other sample.

For example, suppose we want to know whether or not a certain training program is able to increase the max vertical jump (in inches) of basketball players.

To test this, we may recruit a simple random sample of 12 college basketball players and measure each of their max vertical jumps. Then, we may have each player use the training program for one month and then measure their max vertical jump again at the end of the month.

The following data shows the max jump height (in inches) before and after using the training program for each player:

Before: 22, 24, 20, 19, 19, 20, 22, 25, 24, 23, 22, 21

After: 23, 25, 20, 24, 18, 22, 23, 28, 24, 25, 24, 20

The following code shows how to perform this paired samples t-test in R:

#define before and after max jump heightsbefore <- c(22, 24, 20, 19, 19, 20, 22, 25, 24, 23, 22, 21)after <- c(23, 25, 20, 24, 18, 22, 23, 28, 24, 25, 24, 20)#perform paired samples t-testt.test(x = before, y = after, paired = TRUE)Paired t-testdata: before and aftert = -2.5289, df = 11, p-value = 0.02803alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -2.3379151 -0.1620849sample estimates:mean of the differences -1.25

From the output we can see:

  • t-test statistic: -2.5289
  • degrees of freedom:11
  • p-value:0.02803
  • 95% confidence interval for true mean difference: [-2.34, -0.16]
  • mean difference between before and after: -1.25

Since the p-value of the test (0.02803) is less than .05, we reject the null hypothesis.

This means we have sufficient evidence to say that the mean jump height before and after using the training program is not equal.

Additional Resources

Use the following online calculators to automatically perform various t-tests:

One Sample t-test Calculator
Two Sample t-test Calculator
Paired Samples t-test Calculator

The Complete Guide: Hypothesis Testing in R (2024)
Top Articles
Challah Bread Recipe - House of Nash Eats
Southern Tennessee Onions Casserole Recipe - Easy
Best Pre Med Schools U.s. News
Culver's Flavor Of The Day Ann Arbor
Sarah Bustani Boobs
Boost Mobile 69Th Ashland
Pbr Oklahoma Baseball
Rick Lee Oaklawn Park Picks Today
Realidades 2 Capitulo 2B Answers
Kcrubicon
U-Haul Customer Service & Support
Target Nytimes
Kitchen Song Singer Violet Crossword
Tyreek Hill admits some regrets but calls for officer who restrained him to be fired | CNN
Inside the Rise and Fall of Toys ‘R’ Us | HISTORY
Shore Lodge Webcam
Stephjc Forum
35 Best Anime Waifus Of All Time: The Ultimate Ranking – FandomSpot
Starfield PC, XSX | GRYOnline.pl
Christian Hogue co*ck
Metv Plus Schedule Today Near Texas
Elmira Star Gazette Obit
Lucio Surf Code
Winnie The Pooh Sewing Meme
G Data IS lastet 16 GB RAM vollständig aus
321 Flea Market Gastonia Nc
Gw2 Blue Prophet Shard
Emuaid Lawsuit
Used Drift Boats For Sale Craigslist
Southeast Ia Craigslist
Shaleback Hollow Location
Movies123 Avatar 2
Road Conditions Riverton Wy
20 Fantastic Things To Do In Nacogdoches, The Oldest Town In Texas
Best Boxing Gyms Near Me
Classic Buttermilk Pancakes
Strange World Showtimes Near Amc Hoffman Center 22
Boostmaster Lin Yupoo
From Iceland — Northern Comfort: A Turbulent Ride Of Comedy
Natick Mall Directory Map
Nahant Magic Seaweed
Ts Massage San Jose Ca
Ccga Address
Mekala - Jatland Wiki
Currently Confined Coles County
Craigslist Ft Meyers
Carter Williamson Jay Ok
Ava Kayla And Scarlet - Mean Bitches Humiliate A Beta
Mpbn Schedule
Christian Publishers Outlet Rivergate
Sdn Michigan State Osteopathic 2023
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6512

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.