# One sample Z-test

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Comparison of the sample mean with know population mean and standard deviation.**Statistic on aiR**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Suppose that 10 volunteers have done an intelligence test; here are the results obtained. The mean obtained at the same test, from the entire population is 75. You want to check if there is a statistically significant difference (with a significance level of 95%) between the means of the sample and the population, assuming that the sample variance is known and equal to 18.

65, 78, 88, 55, 48, 95, 66, 57, 79, 81

To solve this problem it is necessary to develop a

**one sample Z-test**. In R there isn’t a similar function, so we can create our function.

Recalling the formula for calculating the value of z, we will write this function:

$$Z=\frac{\overline{x}-\mu_0}{\frac{\sigma}{\sqrt{n}}}$$

z.test = function(a, mu, var){ zeta = (mean(a) - mu) / (sqrt(var / length(a))) return(zeta) }

We have built so the function

`z.test`

; it receives in input a vector of values (a), the mean of the population to perform the comparison (mu), and the population variance (var); it returns the value of zeta. Now apply the function to our problem.a = c(65, 78, 88, 55, 48, 95, 66, 57, 79, 81) z.test(a, 75, 18) [1] -2.832353

The value of zeta is equal to -2.83, which is higher than the critical value

`Zcv = 1.96`

, with `alpha = 0.05`

(**2-tailed test**). We conclude therefore that the mean of our sample is significantly different from the mean of the population.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Statistic on aiR**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.