The last time we did something in R is in Lesson 83. Since then, we have been learning the hypothesis testing framework. In Lesson 84, we first made friends with the hypothesis tests — the concept of needing evidence beyond a reasonable doubt. In Lesson 85, we learned the framework — the elements of hypothesis testing that provide us with a systematic way of setting up the test and using data to verify the hypothesis.

In Lessons 86, 87, and 88, we learned the one-sample hypothesis test on proportion, mean, and the variance, respectively. Then, in Lesson 89, we learned the one-sample sign test as a complement to the one-sample hypothesis test on the mean, which can account for outliers as well. Last week (Lesson 90), we dived into the bootstrap-based method where we relaxed the assumptions on the null distribution. The bootstrap-based process provides us the flexibility to run the test on various statistics of interest.

Today, let’s pause and look at how to perform these hypothesis tests in R. Brace yourself for an R drive.

To help us with this lesson today, we will make use of the eight data points on 2003 Ford Focus vehicle mileage that Joe used in Lesson 89.

The Basic Steps

Step1: Get the data

You can get the data from here.

Step 2: Create a new folder on your computer

Let us call this folder “lesson91”.

Step 3: Create a new code in R

Create a new code for this lesson. “File >> New >> R script”. Save the code in the same folder “lesson91” using the “save” button or by using “Ctrl+S”. Use .R as the extension — “lesson91_code.R”

Step 4: Choose your working directory

“lesson91” is the folder where we stored the code. Use “setwd(“path”)” to set the path to this folder. Execute the line by clicking the “Run” button on the top right.

setwd("path to your folder")

Step 5: Read the data into R workspace

Since the sample data is small enough, instead of saving the data in a text file and reading it into R workspace, we can directly input the data into the workspace as follows:

## Data Entry ## # FORD MPG - reported # x = c(21.7, 29, 28.1, 31.5, 24, 21.5, 28.7, 29)

The Baseline for Null Hypotheses

Let’s run the hypothesis tests on the mean, standard deviation, and the proportion. For this, we have to make some modest assumptions.

For the mean, we will use the EPA specified rating of 26.2 MPG. MPG.

For comparing the standard deviation, we are unsure of the baseline standard deviation from the EPA rating. However, they provide us with a range that is between 22 MPG and 32 MPG. If we can assume that the range is covered by six standard deviations, i.e., , we can get an estimate for baseline . In this case, MPG.

Further, let’s also assume that one in eight cars will usually have a mileage less than the minimum rating of 22 MPG.

# Compare to Baseline mu = 26 rangex = c(22,32) sigma = diff(range_ford)/6 n = length(x) p = 1/n

Hypothesis Test on the Mean

For the hypothesis test on the mean, we learned three methods, the t-test, the sign-test, and the bootstrap-based test.

Let’s start with the t-test. For the t-test, the null and the alternate hypothesis are:

MPG

MPG

The null distribution is a T-distribution with (n-1) degrees of freedom. The test statistic is . We have to verify how likely it is to see a value as large as in the null distribution.

Let’s compute the test statistic.

#T-Test # xbar = mean(x) s = sd(x) to = (xbar-mu)/(s/sqrt(n))

> to [1] 0.5173082

The test statistic is 0.517.

For a selected rejection rate of we have to compute the p-value corresponding to this test statistic, or the critical value on the T-distribution.

alpha = 0.05 pvalue_ttest = pt(to,df=(n-1)) tcritical = qt(alpha,df=(n-1))

When you execute these lines, you will see that the p-value is 0.69, and the critical value from the t-distribution is -1.89. The “pt” function computed from a T-distribution with user-specified degrees of freedom. For our case, df = 7. The “qt” function computes the quantile corresponding to a probability of 5%, i.e., from a T-distribution with user-specified degrees of freedom.

> pvalue_ttest [1] 0.6895594 > tcritical [1] -1.894579

Since the p-value is greater than 0.05, or since , we cannot reject the null hypothesis.

In R, there is a function to run this test — “t.test“

We could have used the function directly on the sample data x.

# using t-Test function in R # t.test(x,alternative = "less", mu = mu, conf.level = 0.95)

One Sample t-test data: x t = 0.51731, df = 7, p-value = 0.6896 alternative hypothesis: true mean is less than 26 95 percent confidence interval: -Inf 29.20539 sample estimates: mean of x 26.6875

It provides and the p-value as the outputs from which we can decide on the null hypothesis.

Next, let’s look at how to conduct the sign-test in R. The null and the alternative hypothesis for the sign-test are

If is true, about half of the values of sample X will be greater than 26.2 MPG, and about half of them will be negative. If is true, more than half of the values of sample X will be less than 26.2 MPG, i.e., the sample shows low mileages — significantly less than 26.2 MPG.

To run this test, we should compute , the test statistic that determines the number of values exceeding 26.2 MPG.

#Sign-Test splus = length(which(x>mu))

> splus [1] 5

is 5.

Under the null hypothesis, follows a binomial distribution with a probability of 0.5. Using this assumption, we compute the p-value using the “binomial.test” function in R.

# Using Binom Test function in R binom.test(splus,n,p=0.5,alternative = "less")

Exact binomial test data: splus and n number of successes = 5, number of trials = 8, p-value = 0.8555 alternative hypothesis: true probability of success is less than 0.5 95 percent confidence interval: 0.0000000 0.8888873 sample estimates: probability of success 0.625

The test provides the p-value, i.e., from a binomial distribution with n = 8 and p = 0.5.

Since the p-value is greater than 0.05, the rejection rate, we cannot reject the null hypothesis.

Lastly, we will check out the bootstrap-based test on the mean. For the bootstrap-based one-sided test, the null hypothesis and the alternate hypothesis are

Use the following lines to set up and run the test.

#Bootstrap Test N = 10000 xmean_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xmean_null[i,1] = mean(xboot) } hist(xmean_null,col="pink",xlab="Mean of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(mu,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xmean_null,0.95)),col="black",lwd=2,lty=2) pvalue_bootstrap = length(which(xmean_null>mu))/N

> pvalue_bootstrap [1] 0.7159

In the loop, we are executing the “sample” function to draw a bootstrap-replicate from the original data. Then, from this bootstrap-replicate, we compute the mean. Repeated such sampling and computation of the bootstrap-replicated mean statistic forms the null distribution. From this null distribution, we calculate the p-value, i.e., the proportion of the null distribution that exceeds the baseline of 26.2 MPG.

Since the p-value (0.716) is greater than a 5% rejection rate, we cannot reject the null hypothesis.

We can also observe that the code provides a way to visualize the null distribution and the basis value of 26.2 MPG. If the basis value is so far out on the null distribution of that less than 5% of the bootstrap-replicated test statistics are greater than , we would have rejected the null hypothesis.

Hypothesis Test on the Variance

For the hypothesis test on the variance, we learned two methods, the Chi-square test, and the bootstrap-based test.

Let’s first look at the Chi-square test. The null and alternate hypothesis is as follows:

The alternate hypothesis is two-sided. Deviation in either direction (less than or greater than) will reject the null hypothesis.

If you can recall from Lesson 88, is our test statistic, , which we will verify against a Chi-square distribution with degrees of freedom.

# Chi-square Test chi0 = ((n-1)*s^2)/sigma^2 pvalue_chi0 = pchisq(chi0,df=(n-1)) chilimit_right = qchisq(0.975,df=(n-1)) chilimit_left = qchisq(0.025,df=(n-1))

The “pchisq” function computes from a Chi-square distribution with user-specified degrees of freedom, df = 7. The “qchisq” function computes the quantile corresponding to a specified rate of rejection. Since we are conducting a two-sided test, we calculate the limiting value on the left tail and the right tail.

> pvalue_chi0 [1] 0.9999914

The p-value is 0.999. Since it is greater than 0.975, we reject the null hypothesis based on the two-sided test.

> chi0 [1] 35.60715 > chilimit_left [1] 1.689869 > chilimit_right [1] 16.01276

The lower and the upper bound from the null distribution are 1.69 and 16.01, respectively. Whereas, the test statistic is 35.60, well beyond the upper bound acceptable value. Hence we reject the null hypothesis.

Now, let’s look at how to run a bootstrap-based test for the standard deviation. For the bootstrap-based two-sided test, the null hypothesis and the alternate hypothesis are

Use the following lines to set up and run the test.

# Bootstrap Test for Standard Deviation N = 10000 xsd_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xsd_null[i,1] = sd(xboot) } hist(xsd_null,col="pink",xlab="Standard Deviation of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(sigma,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xsd_null,0.025),quantile(xsd_null,0.975)),col="black",lwd=2,lty=2) pvalue_bootstrap = length(which(xsd_null>sigma))/N

> pvalue_bootstrap [1] 0.9747

As before, in the loop, we are executing the “sample” function to draw a bootstrap-replicate from the original data. Then, from this bootstrap-replicate, we compute the standard deviation. Repeated such sampling and computation of the bootstrap-replicated standard deviation statistic forms the null distribution for . From this null distribution, we calculate the p-value, i.e., the proportion of the null distribution that exceeds the baseline of 1.67 MPG.

Since the p-value (0.975) is greater than or equal to 0.975, ( rejection rate), we reject the null hypothesis.

The code provides a way to visualize the null distribution and the basis value of 1.67 MPG. The basis value is far left on the null distribution. Almost 97.5% of the bootstrap-replicated test statistics are greater than — we reject the null hypothesis.

Hypothesis Test on the Proportion

For the hypothesis test on the proportion, we can employ the binomial distribution (parametric) as the null distribution, or use the bootstrap-based method (non-parametric) to generate the null distribution. For the parametric approach, the null and the alternative hypothesis are

We are considering a one-sided alternative since the departure in one direction (greater than) is sufficient to reject the null hypothesis.

From a sample of eight cars, the null distribution is the probability distribution of observing any number of cars having a mileage of less than 22 MPG. In other words, out of the eight cars, we could have 0, 1, 2, 3, …, 8 cars to have a mileage less than 22 MPG, the lower bound specified by EPA.

The null distribution is the distribution of the probability of observing these outcomes. In a Binomial null distribution with n = 8 and p = 1/8, what is the probability of getting 0, 1, 2, …, 8? and if more than an acceptable number (as seen in the null distribution) is seen, we reject the null hypothesis.

We can generate and plot that null distribution in R using the following lines.

## Hypothesis Test on the Proportion ncars = length(which(x<22)) plot(0:n,dbinom(0:n,n,prob=p),type="o",xlab="Number of cars with MPG less than the range",ylab="P(X=x)",font=2,font.lab=2) x_at_alpha = qbinom(0.95,n,prop) # approx quantile for 5% rejection cord.x <- c(x_at_alpha,seq(x_at_alpha,n),x_at_alpha) cord.y <- c(0, dbinom(x_at_alpha:n,n,prob=p), 0) polygon(cord.x,cord.y,col="pink") points(ncars,0.002,pch=15,col="blue",cex=2) pval = sum(dbinom(ncars:n,n,prob=p))

In the first line, we are computing the number of cars that have a mileage of less than 22 MPG, which is the test statistic. We observe this to be two cars.

> ncars [1] 2

In the next few lines, we are plotting the null distribution as computed from a binomial distribution with n = 8 and p = 1/8, and visually showing the region of rejection using the “polygon” function.

Then, we compute the p-value as the probability of observing two or more cars having a mileage less than 22 MPG.

> pval [1] 0.2636952

Since the p-value is greater than 0.05, we cannot reject the null hypothesis (based on the limited sample) that one in eight cars (2003 Ford Focus) will have a mileage of less than 22 MPG.

Let’s wrap up the day by running the bootstrap test on the proportion. For the bootstrap-based hypothesis test on the proportion, the null and the alternate hypothesis are

If we get a large proportion of the null distribution to be greater than 1/8, we will reject the null hypothesis.

Use the following lines to execute the test.

# Bootstrap Test on Proportion N = 10000 xprop_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xprop_null[i,1] = length(which(xboot<22))/n } hist(xprop_null,col="pink",xlab="Proportion of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(prop,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xprop_null,0.95)),col="black",lwd=2,lty=2) pval=length(which(xprop_null>prop))/N

> pval [1] 0.631

We are executing the “sample” function N = 10,000 times to draw bootstrap-replicates from the original data. Then, for each bootstrap-replicate, we compute the proportion that is less than 22 MPG. The 10,000 bootstrap-replicated proportions forms the null distribution for . From this null distribution, we calculate the p-value, i.e., the proportion of the null distribution that exceeds the baseline of

Since the p-value (0.631) is less than 0.95, ( rejection rate), we cannot reject the null hypothesis. 63% of the bootstrap-replicated proportion is greater than the baseline. If more than 95% of the bootstrap-replicated proportion is greater than the baseline, we would have rejected the null hypothesis. This scenario would have unfolded if the original sample had several cars with less than a mileage of 22 MPG. The bootstrap replicates would show them, in which case the proportion would be much greater than 1/8 in many replicates, and overall, the p-value would exceed 0.95.

Take the next two weeks to digest all the one-sample hypothesis tests, including how to execute the tests in R. In two weeks, we will move on to the two-sample hypothesis tests.

If you find this useful, please like, share and subscribe.

You can also follow me on Twitter @realDevineni for updates on new lessons.