z-test for the Population Mean
load("time.RData")
hist(time$time1,xlab="Time Exercising Per Week", main="Sample 1")

hist(time$time2,xlab="Time Exercising Per Week", main="Sample 2")

hist(time$time3,xlab="Time Exercising Per Week", main="Sample 3")

hist(time$time4,xlab="Time Exercising Per Week", main="Sample 4")

n=5:15
z=(550-500)/(100/sqrt(n))
pvalue=1-pnorm(z)
significance=c(pvalue<0.05)
data.frame(n,z,pvalue,significance)
## n z pvalue significance
## 1 5 1.118034 0.13177624 FALSE
## 2 6 1.224745 0.11033568 FALSE
## 3 7 1.322876 0.09293837 FALSE
## 4 8 1.414214 0.07864960 FALSE
## 5 9 1.500000 0.06680720 FALSE
## 6 10 1.581139 0.05692315 FALSE
## 7 11 1.658312 0.04862721 TRUE
## 8 12 1.732051 0.04163226 TRUE
## 9 13 1.802776 0.03571173 TRUE
## 10 14 1.870829 0.03068441 TRUE
## 11 15 1.936492 0.02640376 TRUE
load("Pregnancy.RData")
z.test = function(x, sig, mu0, alt="greater") {
mu = mean(x);
n = length(x);
z = (mu-mu0)/(sig/sqrt(n));
if (alt=="less"){p = pnorm(z)}
else {
if (alt=="two.sided"){p = 2*(1-pnorm(abs(z)))}
else {p = 1-pnorm(z)}
}
paste("mean = ",mu,"n = ",n,", z = ",z,", p-value = ",round(p,5))
}
z.test(x=pregnancy$length, sig=16, mu0=266, alt="less")
## [1] "mean = 259.68 n = 25 , z = -1.975 , p-value = 0.02413"
t-test for the Population Mean
load("drinks.RData")
t.test(drinks$drinks.per.week,mu = 4.73, alternative = "two.sided")
##
## One Sample t-test
##
## data: drinks$drinks.per.week
## t = -1.8275, df = 74, p-value = 0.07165
## alternative hypothesis: true mean is not equal to 4.73
## 95 percent confidence interval:
## 3.064735 4.801932
## sample estimates:
## mean of x
## 3.933333