Skip to main content

Section 12.3 Testing Hypothesis on Expectation

Let us consider the variable "dif.mpg" that contains the difference in fuel consumption between highway and city conditions. This variable was considered in Chapter ChapterΒ 11. Examine the distribution of this variable:
dif.mpg <- cars$highway.mpg - cars$city.mpg
summary(dif.mpg)
plot(table(dif.mpg))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   5.000   6.000   5.532   7.000  11.000
Figure 12.3.1.
In the first expression we created the variable "dif.mpg" that contains the difference in miles-per-gallon. The difference is computed for each car type between highway driving conditions and urban driving condition. The summary of this variable is produced in the second expression. Observe that the values of the variable range between 0 and 11, with 50% of the distribution concentrated between 5 and 7. The median is 6 and the mean is 5.532. The last expression produces the bar plot of the distribution. It turns out that the variable "dif.mpg" obtains integer values.
In this section we test hypotheses regarding the expected difference in fuel consumption between highway and city conditions.
Energy is required in order to move cars. For heavier cars more energy is required. Consequently, one may conjecture that milage per gallon for heavier cars is less than the milage per gallon for lighter cars.
The relation between the weight of the car and the difference between the milage-per-gallon in highway and city driving conditions is less clear. On the one hand, urban traffic involves frequent changes in speed in comparison to highway conditions. One may presume that this change in speed is a cause for reduced efficiency in fuel consumption. If this is the case then one may predict that heavier cars, which require more energy for acceleration, will be associated with a bigger difference between highway and city driving conditions in comparison to lighter cars.
One the other hand, heavier cars do less miles per gallon overall. The difference between two smaller numbers (the milage per gallon in highway and in city conditions for heavier cars) may tend to be smaller than the difference between two larger numbers (the milage per gallon in highway and in city conditions for lighter cars). If this is the case then one may predict that heavier cars will be associated with a smaller difference between highway and city driving conditions in comparison to lighter cars.
The average difference between highway and city conditions is approximately 5.53 for all cars. Divide the cars into two groups of equal size: One group is composed of the heavier cars and the other group is composed of the lighter cars. We will examine the relation between the weight of the car and difference in miles per gallon between the two driving conditions by testing hypotheses separately for each weight group
 1 
In the next chapters we will consider more direct ways for comparing the effect of one variable (curb.weight in this example) on the distribution of another variable (dif.mpg in this example). Here, instead, we investigate the effect indirectly by the investigation of hypotheses on the expectation of the variable dif.mpg separately for heavier cars and for lighter cars.
. For each such group we start by testing the two-sided hypothesis \(H_1:\Expec(X) \not = 5.53\text{,}\) where \(X\) is the difference between highway and city miles-per-gallon in cars that belong to the given weight group. After carrying the test for the two-sided alternative we will discuss results of the application of tests for one-sided alternatives.
We start by the definition of the weight groups. The variable "curb.weight" measures the weight of the cars in the data frame "cars". Let us examine the summary of the content of this variable:
summary(cars$curb.weight)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1488    2145    2414    2556    2935    4066
Half of the cars in the data frame weigh less than 2,414 lb and half the cars weigh more. The average weight of a car is 2,556 lb. Let us take 2,414 as a threshold and denote cars below this weight as "light" and cars above this threshold as "heavy":
heavy <- cars$curb.weight > 2414
table(heavy)
## heavy
## FALSE  TRUE 
##   103   102
The variable "heavy" indicates for each car type whether its weight is above or below the threshold weight of 2,414 lb. The variable is composed of a sequence with as many components as the number of observations in the data frame "cars" (\(n = 205\)). Each component is a logical value: "TRUE" if the car is heavier than the threshold and "FALSE" if it is not. When we apply the function "table" to this sequence we get that 102 of the cars are heavier than the threshold and 103 are not so.
We would like to apply the \(t\)-test first to the subset of all cars with weight above 2,414 lb (cars that are associated with the value "TRUE" in the variable "heavy"), and then to all cars with weights not exceeding the threshold (cars associated with value "FALSE"). In the past we showed that one may address components of a sequence using its position in the sequence. Here we demonstrate an alternative approach for addressing specific locations by using a sequence with logical components.
In order to illustrate this second approach consider the two sequences:
w <- c(5,3,4,6,2,9)
d <- c(13,22,0,12,6,20)
Say we want to select the components of the sequence "d" in all the locations where the components of the sequence "w" obtain values larger than 5. Consider the code:
w > 5
d[w > 5]
## [1] FALSE FALSE FALSE  TRUE FALSE  TRUE
## [1] 12 20
The expression "w > 5" is a sequence of logical components, with the value "TRUE" at the positions where "w" is above the threshold and the value "FALSE" at the positions where "w" is below the threshold. We may use the sequence with logical components as an index to the sequence of the same length "d". The relevant expression is "d[w > 5]". The output of this expression is the sub-sequence of elements from "d" that are associated with the "TRUE" values of the logical sequence. Indeed, "TRUE" values are present at the 4th and the 6th positions of the logical sequence. Consequently, the output of the expression "d[w > 5]" contains the 4th and the 6th components of the sequence "d".
The operator "!", when applied to a logical value, reverses the value. A "TRUE" becomes "FALSE" and a "FALSE" becomes "TRUE". Consider the code:
!(w > 5)
d[!(w > 5)]
## [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE
## [1] 13 22  0  6
Observe that the sequence "!(w > 5)" obtains a value of "TRUE" at the positions where "w" is less or equal to 5. Consequently, the output of the expression "d[!(w > 5)]" are all the values of "d" that are associated with components of "w" that are less or equal to 5.
The variable "dif.mpg" contains data on the difference in miles-per-gallon between highway and city driving conditions for all the car types. The sequence "heavy" identifies the car types with curb weight above the threshold of 2,414 lb. The components of this sequence are logical with the value "TRUE" at positions associated with the heavier car types and the "FALSE" at positions associated with the lighter car types. Observe that the output of the expression "dif.mpg[heavy]" is the subsequence of differences in miles-per-gallon for the cars with curb weight above the given threshold. We apply the function "t.test" to this expression in order to conduct the \(t\)-test on the expectation of the variable "dif.mpg" for the heavier cars:
t.test(dif.mpg[heavy],mu=5.53)
## 
##  One Sample t-test
## 
## data:  dif.mpg[heavy]
## t = -1.5385, df = 101, p-value = 0.127
## alternative hypothesis: true mean is not equal to 5.53
## 95 percent confidence interval:
##  4.900198 5.609606
## sample estimates:
## mean of x 
##  5.254902
The target population are the heavier car types. Notice that we test the null hypothesis that expected difference among the heavier cars is equal to 5.53 against the alternative hypothesis that the expected difference among heavier cars is not equal to 5.53. The null hypothesis is not rejected at the 5% significance level since the \(p\)-value, which is equal to 0.1735, is larger than 0.05. Consequently, based on the data at hand, we cannot conclude that the expected difference in miles-per-gallon for heavier cars is significantly different than the average difference for all cars.
Observe also that the estimate of the expectation, the sample mean, is equal to 5.254902, with a confidence interval of the form \([4.900198, 5.609606]\text{.}\)
Next, let us apply the same test to the lighter cars. The expression "dif.mpg[!heavy]" produces the subsequence of differences in miles-per-gallon for the cars with curb weight below the given threshold. The application of the function "t.test" to this subsequence gives:
t.test(dif.mpg[!heavy],mu=5.53)
## 
##  One Sample t-test
## 
## data:  dif.mpg[!heavy]
## t = 1.9692, df = 102, p-value = 0.05164
## alternative hypothesis: true mean is not equal to 5.53
## 95 percent confidence interval:
##  5.528002 6.083649
## sample estimates:
## mean of x 
##  5.805825
Again, the null hypothesis is not rejected at the 5% significance level since a \(p\)-value of 0.05164 is still larger than 0.05. However, unlike the case for heavier cars where the \(p\)-value was undeniably larger than the threshold. In this example it is much closer to the threshold of 0.05. Consequently, we may almost conclude that the expected difference in miles-per-gallon for lighter cars is significantly different than the average difference for all car.
Why did we not reject the null hypothesis for the heavier cars but almost did so for the lighter cars? Both tests are based on the \(T\) statistic, which measures the ratio between the deviation of the sample average from its expectation under the null, divided by the estimate of the standard deviation of the sample average. The value of this statistic is "t = -1.5385" for heavier cars and it is "t = 1.9692" for lighter cars, an absolute value of about 25% higher.
The deviation of the sample average for the heavier cars and the expectation under the null is \(5.254902 - 5.53 = -0.275098\text{.}\) On the other hand, the deviation of the sample average for the lighter cars and the expectation under the null is \(5.805825 - 5.53 = 0.275825\text{.}\) The two deviations are practically equal to each other in the absolute value.
The estimator of the standard deviation of the sample average is \(S/\sqrt{n}\text{,}\) where \(S\) is the sample standard deviation and \(n\) is the sample size. The sample sizes, 103 for lighter cars and 102 for heavier cars, are almost equal. Therefore, the reason for the difference in the values of the \(T\) statistics for both weight groups must be differences in the sample standard deviations. Indeed, when we compute the sample standard deviation for lighter and heavier cars
 2 
The function "tapply" applies the function that is given as its third argument (the function "sd" in this case) to each subset of values of the sequence that is given as its first argument (the sequence "dif.mpg" in the current application). The subsets are determined by the levels of the second argument (the sequence "heavy" in this case). The output is the sample standard deviation of the variable "dif.mpg" for lighter cars (the level "FALSE") and for heavier cars (the level "TRUE").
we get that the standard deviation for lighter cars (1.421531) is much smaller than the standard deviation for heavier cars (1.805856):
tapply(dif.mpg,heavy,sd)
##    FALSE     TRUE 
## 1.421531 1.805856
The important lesson to learn from this exercise is that simple minded notion of significance and statistical significance are not the same. A simple minded assessment of the discrepancy from the null hypothesis will put the evidence from the data on lighter cars and the evidence from the data on heavier cars on the same level. In both cases the estimated value of the expectation is the same distance away from the null value.
However, statistical assessment conducts the analysis in the context of the sampling distribution. The deviation of the sample average from the expectation is compared to the standard deviation of the sample average. Consequently, in statistical testing of hypothesis a smaller deviation of the sample average from the expectation under the null may be more significant than a larger one if the sampling variability of the former is much smaller than the sampling variability of the later.
Let us proceed with the demonstration of the application of the \(t\)-test by the testing of one-sided alternatives in the context of the lighter cars. One may test the one-sided alternative \(H_1:\Expec(X) > 5.53\) that the expected value of the difference in miles-per-gallon among cars with curb weight no more than 2,414 lb is greater than 5.53 by the application of the function "t.test" to the data on lighter cars. This data is the output of the expression "dif.mpg[!heavy]". As before, we specify the null value of the expectation by the introduction of the expression "mu=5.53". The fact that we are interested in the testing of the specific alternative is specified by the introduction of a new argument of the form: "alternative=greater". The default value of the argument "alternative" is "two.sided", which produces a test of a two-sided alternative. By changing the value of the argument to "greater" we produce a test for the appropriate one-sided alternative:
t.test(dif.mpg[!heavy],mu=5.53,alternative="greater")
## 
##  One Sample t-test
## 
## data:  dif.mpg[!heavy]
## t = 1.9692, df = 102, p-value = 0.02582
## alternative hypothesis: true mean is greater than 5.53
## 95 percent confidence interval:
##  5.573323      Inf
## sample estimates:
## mean of x 
##  5.805825
The value of the test statistic (t = 1.9692) is the same as for the test of the two-sided alternative and so is the number of degrees of freedom associated with the statistic (df = 102). However, the \(p\)-value is smaller (p-value = 0.02582), compared to the \(p\)-value in the test for the two-sided alternative (p-value = 0.05164). The \(p\)-value for the one-sided test is the probability under the sampling distribution that the test statistic obtains vales larger than the observed value of 1.9692. The \(p\)-value for the two-sided test is twice that figure since it involves also the probability of being less than the negative of the observes value.
The estimated value of the expectation, the sample average, is unchanged. However, instead of producing a confidence interval for the expectation the report produces a one-sided confidence interval of the form \([5.573323, \infty)\text{.}\) Such an interval corresponds to the smallest value that the expectation may reasonably obtain on the basis of the observed data.
Finally, consider the test of the other one-sided alternative \(H_1:\Expec(X) < 5.53\text{:}\)
t.test(dif.mpg[!heavy],mu=5.53,alternative="less")
## 
##  One Sample t-test
## 
## data:  dif.mpg[!heavy]
## t = 1.9692, df = 102, p-value = 0.9742
## alternative hypothesis: true mean is less than 5.53
## 95 percent confidence interval:
##      -Inf 6.038328
## sample estimates:
## mean of x 
##  5.805825
The alternative here is determined by the expression "alternative=less". The \(p\)-value is equal to 0.9742, which is the probability that the test statistic obtains values less than the observed value of 1.9692. Clearly, the null hypothesis is not rejected in this test.