Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (34 sloc) 1.38 KB

One-Sample Z-Test

R doesn't have a built-in function to conduct the one-sample z-test. Fortunately the math is simple enough for us to carry out the test using only a few functions. Let start with same data.

mu0 = 12 #The hypothesized value
sigma = 2.6 #The population standard deviation
xbar = 13.5 #The sample mean
n = 50 #The sample size

To calculate the test statistics, we can just directly use the test statistics formula

test.stat = (xbar - mu0)/(sigma/sqrt(n))
## [1] 4.079462

To get the critical value, we can use the qnorm() function (since we now assume the standard normal distribution):

crit.val = qnorm(0.05, lower.tail = F)
## [1] 1.644854

The argument lower.tail = F is to tell R that we want to find the critical value which gives a probability of 0.05 on the upper tail. If we were to do a lower-tailed test, the command will be qnorm(0.05, lower.tail = T) (or by default, lower.tail is TRUE if there is no input for this argument).

To obtain the p-value, we can use the pnorm() function:

p.val = pnorm(test.stat, lower.tail = F)
## [1] 2.257e-05

If this were a two-tailed test, remember we need to multiply the p-value by 2:

p.val2 = 2*pnorm(abs(test.stat), lower.tail = F) #If we were to do a two-tailed test
## [1] 4.514001e-05
You can’t perform that action at this time.