(This article was first published on

Comparison of the sample mean with know population mean and standard deviation.**Statistic on aiR**, and kindly contributed to R-bloggers)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 his blog:**Statistic on aiR**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...