This tutorial uses the diamonds data from the previous pages. Run the following line of code if you haven’t already called it in.

sparkly<-read.csv("https://mooredata.weebly.com/uploads/1/6/0/9/16090314/diamonds.csv") #loads data directly from website     

Test for Means

The t.test() function in R carries out a hypothesis test as well as a confidence interval for 1 mean or the difference in 2 means.

Confidence Interval for One Mean

To get a confidence interval for mean price of diamonds:

t.test(sparkly$price)
## 
##  One Sample t-test
## 
## data:  sparkly$price
## t = 228.95, df = 53939, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  3899.132 3966.467
## sample estimates:
## mean of x 
##    3932.8

Notice the default is a 95% interval. It’s easy to change the confidence level using the conf.level argument.

t.test(sparkly$price,conf.level=0.99)  #a 99% interval
## 
##  One Sample t-test
## 
## data:  sparkly$price
## t = 228.95, df = 53939, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  3888.552 3977.047
## sample estimates:
## mean of x 
##    3932.8

Hypothesis Test for One Mean

Notice the t.test() function also gave the results for a hypothesis test, with the default of a non-directional alternative hypothesis.
Take a quick look at the arguments of the function:

t.test(x, y = NULL, alternative = c(“two.sided”, “less”, “greater”), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, …)

  • The mu= statement indicates the value for your null hypothesis. The default value is 0.
  • The default alternative hypothesis is alternative=“two.sided”. To get a directional alternative, use alternative=“greater” or alternative=“less”.

If we wanted to test for evidence that mean diamond price for all diamonds is less than $4,000.00 use the following:

t.test(sparkly$price,alternative="less",mu=4000) 
## 
##  One Sample t-test
## 
## data:  sparkly$price
## t = -3.9121, df = 53939, p-value = 4.58e-05
## alternative hypothesis: true mean is less than 4000
## 95 percent confidence interval:
##      -Inf 3961.054
## sample estimates:
## mean of x 
##    3932.8

Notice that the output confirms we tested the alternative that mean price is less than $4,000.00.

Difference in Means: Independent Samples

The t.test() function can take 2 samples rather than 1. If we want to test whether the mean price for premium diamonds - mean price for Very Good diamonds is less than $1,000.00, that is the alternative mu_p - mu_vg < 1000, simply insert the 2 datasets as the first 2 entries of the t.test() function and change the alternative and null hypothesized value arguments to match our hytpohesis.

premPrice<-sparkly$price[sparkly$cut=="Premium"]
veryGoodPrice<-sparkly$price[sparkly$cut=="Very Good"]
t.test(premPrice,veryGoodPrice,alternative="less",mu=1000)
## 
##  Welch Two Sample t-test
## 
## data:  premPrice and veryGoodPrice
## t = -7.7163, df = 25844, p-value = 6.203e-15
## alternative hypothesis: true difference in means is less than 1000
## 95 percent confidence interval:
##      -Inf 687.2347
## sample estimates:
## mean of x mean of y 
##  4584.258  3981.760

The p-value returned is very tiny, so there is overwhelming evidence (P < 0.001) to conclude the alternative. When assuming the populations variances are equal, use the var.equal=TRUE argument.

Mean Difference: Paired t-test

If your data is paired, use the t.test() function with paired=TRUE argument, or subtract the 2 datasets and do a one sample t-test on the differences.

Normal QQplots

qqnorm(sparkly$price)
qqline(sparkly$price)  #adds a line if you want it

Wow, that QQplot is not linear. The S-shape shows the right skew in the price distribution. Thank goodness for the CLT!
To demonstrate the CLT, we can pretend our price column (which is highly right-skewed) is the population, take a bunch of samples and graph the resulting sample means. The CLT says that as long as our sample size is big enough, the distribution of sample means should be a normal distribution.

n<-100
reps<-1000
sampMat<-matrix(rep(0,n*reps),ncol=reps)

for(i in 1:reps){
  sampMat[,i]<-sample(sparkly$price,n)
}
par(mfrow=c(1,2))  #setting grpahics window to have 1 row and 2 columns to hold 2 plots
hist(sparkly$price,main="Price Distribution")
hist(apply(sampMat,2,mean),main="Distribution of Sample Means")

par(mfrow=c(1,1))  #resetting the graphics window to have 1 row and 1 column

Confidence Interval and Test for p (population proportion)

Confidence Interval and Hypothesis Test for One Proportion

The confidence interval and hypothesis test you have probably learned in a previous textbook is based on the Wald method and probably returned a Z test statistic. That method likely used the sample proportion in the standard error calculation. The prop.test() function in R uses the null hypothesized value of p, which is 0.5 by default. To change this, use the p= argument. Also, it returns a Chi-square test statistic, which is just the square of the Z statistic you will get if you hand calculate (p_hat - p_not)/SQRT(p_not*(1-p_not)/n).
We’ll use prop.test(Y,n,correct=FALSE) with correct=FALSE so that the algorithm won’t use continuity correction.

numPrem <- length(sparkly$cut[sparkly$cut=="Premium"])  #number of premium diamonds
n <- length(sparkly$cut)  #total number of diamonds
prop.test(numPrem, n, correct=FALSE)  #tests alternative the true proportion is different from 0.5
## 
##  1-sample proportions test without continuity correction
## 
## data:  numPrem out of n, null probability 0.5
## X-squared = 12880, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.2520090 0.2593717
## sample estimates:
##        p 
## 0.255673
prop.test(numPrem, n, correct=FALSE, p=0.25, alternative="greater")  #tests true proportion is greater than 0.25
## 
##  1-sample proportions test without continuity correction
## 
## data:  numPrem out of n, null probability 0.25
## X-squared = 9.2583, df = 1, p-value = 0.001172
## alternative hypothesis: true p is greater than 0.25
## 95 percent confidence interval:
##  0.2525957 1.0000000
## sample estimates:
##        p 
## 0.255673

Confidence Interval and hypothesis test for the difference in 2 population proportions

We’ll use prop.test() again. Suppose we have 2 different offers of a discount. 75 customers had offer 1 and 44 took it, while 80 customers had offer 2 and 50 took it.

prop.test(c(44,50),c(75,80),correct=FALSE) #test whether p1 is different from p2
## 
##  2-sample test for equality of proportions without continuity
##  correction
## 
## data:  c(44, 50) out of c(75, 80)
## X-squared = 0.23833, df = 1, p-value = 0.6254
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.1921983  0.1155316
## sample estimates:
##    prop 1    prop 2 
## 0.5866667 0.6250000

Chi-square Test

mat<-matrix(c(44,31,50,30),nrow=2)  #creating a matrix with 1st column offer 1 (take/don't take) and 2nd column offer 2
prop.test(c(44,50),c(75,80),correct=FALSE)  #previous example
## 
##  2-sample test for equality of proportions without continuity
##  correction
## 
## data:  c(44, 50) out of c(75, 80)
## X-squared = 0.23833, df = 1, p-value = 0.6254
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.1921983  0.1155316
## sample estimates:
##    prop 1    prop 2 
## 0.5866667 0.6250000
chisq.test(mat,correct=FALSE)  #does the same test, but does not return a confidence interval
## 
##  Pearson's Chi-squared test
## 
## data:  mat
## X-squared = 0.23833, df = 1, p-value = 0.6254

Tips

<Previous | Next>