#Exercise on t-tests and confidence intervals

 

#At the lectures we looked at two data examples -- age of mineral samples and bone mineral density.

#We will in this exercise see how the computations for these examples may be done in R.

#We will illustrate direct calculations of the quantities involved as well as the use of a special command for t-tests and confidence intervals.

 

 

 

#One sample: Age of minerals (cf. slide 3 from the lectures)

 

#Start by reading the data into R. This may be done by the command:

age=c(249, 254, 243, 268, 253, 269, 287, 241, 273, 306, 303, 280, 260, 256, 278, 344, 304, 283, 310)

 

#Compute mean and standard deviation:

mean(age)

sd(age)

# Check that you get the same result as in the lectures (cf slide 3)

 

# Compute the 97.5% percentile of the t-distribution with 18 degrees of freedom:

qt(0.975,18)

 

# Compute lower and upper limit of the 95% confidence interval:

mean(age) - qt(0.975,18)*(sd(age)/sqrt(19))      # lower limit

mean(age) + qt(0.975,18)*(sd(age)/sqrt(19))     # upper limit

# Check that you get the same result as in the lectures (cf slide 12)

 

# Compute t-statistic:

tstat=(mean(age)-265)/(sd(age)/sqrt(19))       #t-statistic

tstat

 

# Compute p-value:

1-pt(tstat,18)

 

# Check that you get the same result as in the lectures (cf slide 15)

 

 

# Use the command "t.test" to compute the confidence interval (this gives a two-sided test):

t.test(age,mu=265)

 

# Use the command "t.test" to compute a one-sided test (this gives a one-sided confidence interval):

t.test(age,alternative="greater",mu=265)

 

# Check that you get the same results as above.

 

 

#Two samples: Bone mineral density (cf. slide 16 from the lectures)

 

#Start by reading the data into R. This may be done by the command:

cont=c(0.228, 0.207, 0.234, 0.220, 0.217, 0.228, 0.209, 0.221, 0.204, 0.220, 0.203, 0.219, 0.218, 0.245, 0.210)

treat=c(0.250, 0.237, 0.217, 0.206, 0.247, 0.228, 0.245, 0.232, 0.267, 0.261, 0.221, 0.219, 0.232, 0.209, 0.255)

 

# Find the means and standard deviations, and check that you get the same results as in the lectures (slide 17)

 

# Use the command "t.test" to compute the confidence interval, t-statistic and p-value:

t.test(treat, cont , var.equal=T)

 

# Make sure that you understand the output from the command and check that you get the same results as in the lectures (slides 17 and 18)

 

 

# Optional: Use the formulas given on slide 17 to compute the pooled standard deviation, the standard error of the effect of treatment, and the 95% confidence interval.

 

# Optional: Use the formulas given on slide 18 to compute the t-statistic and the p-value.