R LAB THREE: Inferences for One Population Mean

Topics: confidence intervals; one sample z intervals/tests; one sample t intervals/tests

R textbook: Sections 5.1, 5.2

Data: Daily energy expenditure for lean and obese women data (energy.txt) posted on the class webpage.

1) Read the data from the webpage into RStudio

As shown in the picture, click “From Web URL…” and type/paste the link of the data file, which is http://www.sci.csueastbay.edu/~sfan/SubPages/CSUteach/st3502/labs/energy.txt

2) Inference for the mean expenditure (ignoring stature group for now)

> summary(energy$expend) Min. 1st Qu. Median Mean 3rd Qu. Max. 6.130 7.660 8.595 8.979 9.900 12.790

 One sample t method

R code:

#normaility checking qqnorm(energy$expend) qqline(energy$expend,col=2) shapiro.test(energy$expend) t.test(energy$expend,mu=8.5,alternative="two.sided",conf.level = .90)

R output: Shapiro-Wilk normality test data: energy$expend W = 0.95418, p-value = 0.3812

1 One Sample t-test data: energy$expend t = 1.3238, df = 21, p-value = 0.1998 alternative hypothesis: true mean is not equal to 8.5

90 percent confidence interval: 8.356339 9.601843 sample estimates: mean of x 8.979091

Note: The four R functions for t distribution with degree of freedom of df dt(x, df) returns the t probability density at x pt(q, df) returns the t cumulative probability at q, P[t < q] qt(p, df) returns the (p*100)th percentile of t rt(n, df) returns n random numbers generated from t distribution

Power and sample size calculation: see R document (help(power.t.test))

# Ott, 6th ed., eg. 5.11 power.t.test(delta=.1,sd=.225,sig.level=.05,power=.99,type="one.sample",alternative="one.sided") # Ott, 6th ed., eg. 5.8 power.t.test(n=50,delta=15,sd=35.2,sig.level=.01,type="one.sample",alternative="one.sided")

 One sample z method

R does not have a function for z method and thus we need to calculate the confidence interval and z test statistic ourselves. Yet, we can use R to find statistics, such as sample mean and sample S.D., critical value and p value.

Note that if n is greater than 200, z method is almost the same as t method.

 Wilcoxon signed rank test

R code: wilcox.test(energy$expend,mu=8.5) R output:

Wilcoxon signed rank test with continuity correction data: energy$expend V = 159, p-value = 0.2988 alternative hypothesis: true location is not equal to 8.5

2