# How to do one-sample t-test and Z-test as part of hypothesis testing in R, Python, and Google Spreadsheet

# What is hypothesis testing?

Hypothesis testing is a statistical method using inference to decide whether the data we have is enough to support our hypothesis about something, against the population parameter. Too hard to understand? Me too! 😅

In human language, it’s a test to prove whether our sample data can help us decide whether our hypothesis is proven or not. Still too hard to understand? Well, let’s use an example! 😏

## What is one-sample and two-sample hypothesis testing?

A one-sample hypothesis test is used to test whether a sample or population mean is significantly different than some hypothesized value. This means, we only have one dataset and we will compare it to an exact value, not comparing it to another dataset.

A two-sample hypothesis test is used to test whether a two-sample has a significantly different sample or population mean. This means we can have two datasets in our hypothesis, which could be from one source (dependent) or different sources (independent).

## Example: Students’ grades at Hogwarts

Kautzar is a wizard at Hogwarts, the Sorting Hat placed him in the House of Gryffindor. As the headmaster, Professor Dumbledore asked him to do data analysis, it’s related to students’ capability to do some difficult spells. For example, Kautzar claimed that he can do the “*Expecto patronum” *spell 13 times in a row without fail, when he tried to show it to you after 5 times in a row without fail, suddenly Professor Dumbledore called him to his office which is why he can’t continue proving his claim. Would anyone believe Kautzar’s claim? 😅

Anyway, due to the number of Hogwarts students quite a lot, Kautzar did some sampling to get the Potion class scores from students, which is taught by Professor Snape. Luckily, Kautzar has a job as a data analyst freelancer on a website made by Muggle, that’s why he is quite confident that he can help Professor Dumbledore. 😜

## Difference between t-test and Z-test.

In statistics, to do hypothesis testing, there are two types of tests, which is the t-test and the Z-test. How to know which one to use? Quite simple.

- If the sample size is equal to or less than 30
**AND**the population parameter is unknown, the appropriate test is a t-test. - If the sample size is more than 30
**OR**the population parameter is known, the appropriate test is a Z-test.

**The population parameter mentioned above is standard deviation or variance**, which is obvious because standard deviation is simply the squared root of variance.

# One-sample t-test

Kautzar wanted to know if the Hogwarts students got an average score of 75 in the Potion class (the score interval is 0 to 100). He randomly measured 25 students, the sample mean is 77.12 and the sample standard deviation is 11.58. **Can he prove his hypothesis that the average score is more than 75 at the α = 0.05 level of significance?**

**First, set the hypotheses.**

**Then, calculate the t-statistic using this formula.**

**Due to the sample count being 25 and the population parameter being unknown, we have to use a t-test.** Let’s try to calculate the t-statistic using R, Python, and Google Spreadsheet. We will use hogwarts1.csv which is generated by me. 😃

## Calculate the t-statistic using R

`# read the file into "df"`

df <- read.csv("hogwarts1.csv")

# look at the first 5 rows

head(df)

# calculate the mean of the score

mean(df$score)

# calculate the standard deviation of the score

sd(df$score)

# t-test

t.test(df$score, mu = 75, alternative = "greater", conf.level = 0.95)

## Calculate the t-statistic using Python

`# import pandas `

import pandas as pd

# read the data into df

df = pd.read_csv("hogwarts1.csv")

# look at the first 5 rows of df

df.head()

# calculate mean of score

df['score'].mean()

# import statistics

import statistics as st

# calculate the standard deviation of the score

st.stdev(df['score'])

# import scipy.stats as ss

import scipy.stats as ss

# one sample t-test

ss.ttest_1samp(a = df, popmean = 75, alternative = "greater")

## Calculate the t-statistic using Google Spreadsheet

**One-sample t-test: Conclusion**

As we can see, using R, Python, or Google Spreadsheet, the result is the same, which is:

- t-stat: 0.9153, p-value: 0.1845
- Decision rule: If the p-value is less than or equal to α = 0.05, then reject the null hypothesis (H0). Otherwise, do not reject the null hypothesis.
- Decision fact: Because the p-value: 0.1845 is higher than α = 0.05, then we do not reject the null hypothesis (H0), which means
**we do not have enough evidence to say the average score is higher than 75.**— Yay we did our first hypothesis testing in this case! 😉

**One-sample t-test: Alternative method**

- If we want to do the right-sided test, in R and Python please use “greater”. In Google Spreadsheets please use “T.DIST.RT”.
- If we want to do the two-sided test, in R and Python please use “two.sided”. In Google Spreadsheets please use “T.DIST.2T”.
- If we want to do the left-sided test, in R and Python please use “less”. In Google Spreadsheets please use “T.DIST”.

# One-sample Z-test

On the next day, Kautzar got more samples from the students. This time, he randomly measured 60 students, the sample mean is 77.5167 and the population standard deviation is equal to the sample standard deviation which is 14.2288. After learning yesterday’s hypothesis, he lowered his expectation, this time he wanted to know that the average score is 74. **Can he prove his hypothesis that the average score is more than 74 at the α = 0.05 level of significance?**

**First, set the hypotheses.**

**Then, calculate the Z-statistic using this formula.**

The Z follows a normal distribution with a mean of 0 and a standard deviation of 1. This means we need to ensure the data is normally distributed before using Z-test. In this case, we are assuming that hogwarts2.csv is normally distributed due to the number of samples being more than 30.

**Due to the sample count being 60 and the population parameter is known, we have to use a Z-test.** Let’s try to calculate the Z-statistic using R, Python, and Google Spreadsheet. We will use hogwarts2.csv which is generated by me. 😃

## Calculate the Z-statistic using R

`# read the file into "df"`

df <- read.csv("hogwarts2.csv")

# look at the first 5 rows

head(df)

# calculate the mean of the score

mean(df$score)

# calculate the standard deviation of the score

sd(df$score)

# install.packages, skip if already installed

## install.packages("BSDA")

# load BSDA (Basic Statistics and Data Analysis)

library(BSDA)

# one sample Z test

z.test(x = df$score, alternative = "greater", mu = 74, sigma.x = 14.2288, conf.level = 0.95)

## Calculate the Z-statistic using Python

`# import pandas `

import pandas as pd

# read the data into df

df = pd.read_csv("hogwarts2.csv")

# look at the first 5 rows of df

df.head()

# calculate mean of score

df['score'].mean()

# import statistics

import statistics as st

# calculate the standard deviation of the score

st.stdev(df['score'])

# import ztest

from statsmodels.stats.weightstats import ztest as ztest

# one sample Z-test

ztest(df, value = 74, alternative = "larger")

## Calculate the Z-statistic using Google Spreadsheet

**One-sample Z-test: Conclusion**

As we can see, using R, Python, or Google Spreadsheet, the result is the same, which is:

- Z-stat: 1.9144, p-value: 0.0278
- Decision rule: If the p-value is less than or equal to α = 0.05, then reject the null hypothesis (H0). Otherwise, do not reject the null hypothesis.
- Decision fact: Because the p-value: 0.0278 is lower than α = 0.05, then we do reject the null hypothesis (H0), which means
**we do have enough evidence to say the average score is higher than 74.**— Yay we did our second hypothesis testing! 😉

**One-sample Z-test: Alternative method**

- If we want to do the right-sided test, in R please use “greater” and in Python use “larger”.
- If we want to do the left-sided test, in R please use “less” and in Python use “smaller”.
- If we want to do the two-sided test, in R please use “two.sided” and in Python use “two-sided”.
- If we want to do the one-sided test in Google Spreadsheet please use “Z.TEST”.

Additional information:

- In R, using the current package and version, we can use different standard deviations between the sample and population.
- In Python, using the current package and version, the assumption is the standard deviations between the sample and population are equal.
- In Google Spreadsheet, using the current version, I don’t find any function to calculate a two-sided one-sample Z-test directly.
- In my humble opinion, if you need to do Z-test,
**please use R for now, it has more flexibility to fulfill your needs.**

# What did we learn so far?

- Hypothesis testing using t-test and z-test, also when to use it.
- In this article, we focus on one-sample test only. The two-test sample will be explored in another post.
- We can do all the tests using R, Python, and Google Spreadsheet with the same output.
- For Z-test, I suggest using R due to the flexibility to fulfill our needs.

# Conclusion

In my current job as a data analytics professional, hypothesis testing is very useful when we want to know how effective the changes or treatments that we did to customers, is it have significant effect or not? Usually, in A/B test experiments, we do this hypothesis test A LOT.

Even when it’s not an experiment, hypothesis testing will tell us better decision-making rather than only calculating the difference. It’s called inferential statistics for some reason, so it can help us decide with scientific proof.

## Thank you for reading!

Hi, thanks for coming to my Medium, I think my next post will be continuing this article, to share about two-sample hypothesis testing using R, Python, and Google Spreadsheet, which are the most common tools to be used.

I am learning to write, mistakes are unavoidable, even when I try my best. If you find any problems/mistakes, please let me know!