Table of Contents
To perform a one sample and two sample Z-tests in R, the following steps can be followed:
1. Load the necessary packages: The first step is to load the required packages for conducting the Z-tests, such as “stats” and “DescTools”.
2. Create a dataset: Next, create a dataset or import an existing dataset into R.
3. Specify the null and alternative hypotheses: Based on the research question, specify the null and alternative hypotheses for the Z-tests.
4. Conduct the one sample Z-test: Use the “z.test” function from the “DescTools” package to perform the one sample Z-test. This function requires the sample data, the population mean, and the population standard deviation as inputs.
5. Conduct the two sample Z-test: Similarly, use the “z.test” function to perform the two sample Z-test. This function requires the two sample data sets as inputs.
6. Interpret the results: Finally, interpret the results obtained from the Z-tests by comparing the p-value to the chosen significance level. A p-value less than the significance level indicates that the null hypothesis can be rejected.
Perform One Sample & Two Sample Z-Tests in R
You can use the z.test() function from the BSDA package to perform one sample and two sample z-tests in R.
This function uses the following basic syntax:
z.test(x, y, alternative='two.sided', mu=0, sigma.x=NULL, sigma.y=NULL,conf.level=.95)where:
- x: values for the first sample
- y: values for the second sample (if performing a two sample z-test)
- alternative: the alternative hypothesis (‘greater’, ‘less’, ‘two.sided’)
- mu: mean under the null or mean difference (in two sample case)
- sigma.x: population standard deviation of first sample
- sigma.y: population standard deviation of second sample
- conf.level: confidence level to use
The following examples shows how to use this function in practice.
Example 1: One Sample Z-Test in R
Suppose the IQ in a certain population is normally distributed with a mean of μ = 100 and standard deviation of σ = 15.
A scientist wants to know if a new medication affects IQ levels, so she recruits 20 patients to use it for one month and records their IQ levels at the end of the month.
The following code shows how to perform a one sample z-test in R to determine if the new medication causes a significant difference in IQ levels:
library(BSDA)
#enter IQ levels for 20 patients
data = c(88, 92, 94, 94, 96, 97, 97, 97, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 115)
#perform one sample z-test
z.test(data, mu=100, sigma.x=15)
One-sample z-Test
data: data
z = 0.90933, p-value = 0.3632
alternative hypothesis: true mean is not equal to 100
95 percent confidence interval:
96.47608 109.62392
sample estimates:
mean of x
103.05
The test statistic for the one sample z-test is 0.90933 and the corresponding p-value is 0.3632.
Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis.
Thus, we conclude that the new medication does not significantly affect IQ level.
Example 2: Two Sample Z-Test in R
Suppose the IQ levels among individuals in two different cities are known to be normally distributed each with population standard deviations of 15.
A scientist wants to know if the mean IQ level between individuals in city A and city B are different, so she selects a of 20 individuals from each city and records their IQ levels.
library(BSDA)#enter IQ levels for 20 individuals from each city
cityA = c(82, 84, 85, 89, 91, 91, 92, 94, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 114)
cityB = c(90, 91, 91, 91, 95, 95, 99, 99, 108, 109,
109, 114, 115, 116, 117, 117, 128, 129, 130, 133)
#perform two sample z-test
z.test(x=cityA, y=cityB, mu=0, sigma.x=15, sigma.y=15)
Two-sample z-Test
data: cityA and cityB
z = -1.7182, p-value = 0.08577
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-17.446925 1.146925
sample estimates:
mean of x mean of y
100.65 108.80
The test statistic for the two sample z-test is -1.7182 and the corresponding p-value is 0.08577
Since this p-value is not less than .05, we do not have sufficient evidence to reject the null hypothesis.
Thus, we conclude that the mean IQ level is not significantly different between the two cities.
The following tutorials explain how to perform other common statistical tests in R:
Cite this article
stats writer (2024). How can I perform one sample and two sample Z-tests in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-one-sample-and-two-sample-z-tests-in-r/
stats writer. "How can I perform one sample and two sample Z-tests in R?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-one-sample-and-two-sample-z-tests-in-r/.
stats writer. "How can I perform one sample and two sample Z-tests in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-one-sample-and-two-sample-z-tests-in-r/.
stats writer (2024) 'How can I perform one sample and two sample Z-tests in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-one-sample-and-two-sample-z-tests-in-r/.
[1] stats writer, "How can I perform one sample and two sample Z-tests in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I perform one sample and two sample Z-tests in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
