Introduction

An environmental group wants to know if US cars use more gas than Japanese cars. To check this we compare the mean mpg of the two groups. The data has 35 US cars and 28 Japanese cars.

Data

The data is read directly from the link. The Japanese column has 7 empty cells because that sample is smaller, so those NA values are removed.

dat <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/US_Japanese_Cars.csv",
                fileEncoding = "UTF-8-BOM")

us <- dat$USCars
japan <- na.omit(dat$JapaneseCars)

length(us)
## [1] 35
length(japan)
## [1] 28
summary(us)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    9.00   14.00   15.00   15.97   18.00   28.00
sd(us)
## [1] 4.054668
summary(japan)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   18.00   24.00   27.00   26.75   31.00   35.00
sd(japan)
## [1] 4.70323

Question 1: Are the data normal?

qqnorm(us, main = "Normal Q-Q Plot: US Cars (mpg)")
qqline(us)

qqnorm(japan, main = "Normal Q-Q Plot: Japanese Cars (mpg)")
qqline(japan)

Comments

For the Japanese cars the points stay close to the line, so this group looks normal.

For the US cars the points are close to the line at the beginning, but the highest ones move up and away from it. So the US group does not look normal. A few US cars get much better mpg than the rest, and that is also why the mean (15.97) is higher than the median (15).

Question 2: Is the variance constant?

boxplot(us, japan,
        names = c("US", "Japan"),
        col = c("lightblue", "orange"),
        main = "Fuel Efficiency by Country of Manufacture",
        xlab = "Country of Manufacture",
        ylab = "Fuel Efficiency (mpg)")

Comments

The two boxes are not the same size. The Japanese box covers 7 mpg and the US box only 4 mpg, so the Japanese cars are more spread out. The US plot also has two dots on top, which are outliers, and Japan has none. So the variance does not look equal.

Japan has the higher values and also the bigger box. When that happens, taking the log of the data usually makes the two groups more similar.

Question 3: Log transform

log_us <- log(us)
log_japan <- log(japan)

Normal probability plots

qqnorm(log_us, main = "Normal Q-Q Plot: Log of US Cars (mpg)")
qqline(log_us)

qqnorm(log_japan, main = "Normal Q-Q Plot: Log of Japanese Cars (mpg)")
qqline(log_japan)

Side by side boxplots

boxplot(log_us, log_japan,
        names = c("US", "Japan"),
        col = c("lightblue", "orange"),
        main = "Log Fuel Efficiency by Country of Manufacture",
        xlab = "Country of Manufacture",
        ylab = "Log of Fuel Efficiency (log mpg)")

Comments on the differences

The US plot changed a lot. The points that were going up away from the line are now much closer to it, so the US group looks normal after the log. The Japanese plot looks almost the same, because it was already fine before.

The boxes changed too. Before, the Japanese box was about twice as tall as the US box. Now both boxes are almost the same size (0.251 and 0.256), so the variance looks equal.

The US group still has two outliers, but now they are closer to the rest of the data.

Both things we need for the test look fine now, so the log data is used from here on.

Question 4: Hypothesis test

Let mu1 be the mean of the log mpg of US cars and mu2 the mean of the log mpg of Japanese cars.

  • Null hypothesis, H0: mu1 = mu2
  • Alternative hypothesis, Ha: mu1 < mu2

This is a one sided two-sample t-test with pooled variance, using a significance level of 0.05. In R, var.equal = TRUE gives the pooled version and alternative = "less" makes it one sided.

t.test(log_us, log_japan, var.equal = TRUE, alternative = "less")
## 
##  Two Sample t-test
## 
## data:  log_us and log_japan
## t = -9.4828, df = 61, p-value = 6.528e-14
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##        -Inf -0.4366143
## sample estimates:
## mean of x mean of y 
##  2.741001  3.270957

a. Sample averages of the log mpg

mean(log_us)
## [1] 2.741001
mean(log_japan)
## [1] 3.270957

The sample average of the log mpg is 2.741 for the US cars and 3.271 for the Japanese cars.

b. Conclusions

The test gives t = -9.48 with 61 degrees of freedom and a p-value of almost zero (6.53e-14). The p-value is much smaller than 0.05, so we reject the null hypothesis.

This means the mean log mpg of US cars is lower than the mean log mpg of Japanese cars. In the sample the US cars got about 16 mpg and the Japanese cars about 27 mpg.

So the data supports what the environmental group thought.

Complete code

# read the data
dat <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/US_Japanese_Cars.csv",
                fileEncoding = "UTF-8-BOM")

# split the two samples and remove the empty cells
us <- dat$USCars
japan <- na.omit(dat$JapaneseCars)

length(us)
length(japan)

summary(us)
sd(us)

summary(japan)
sd(japan)

# question 1
qqnorm(us, main = "Normal Q-Q Plot: US Cars (mpg)")
qqline(us)

qqnorm(japan, main = "Normal Q-Q Plot: Japanese Cars (mpg)")
qqline(japan)

# question 2
boxplot(us, japan,
        names = c("US", "Japan"),
        col = c("lightblue", "orange"),
        main = "Fuel Efficiency by Country of Manufacture",
        xlab = "Country of Manufacture",
        ylab = "Fuel Efficiency (mpg)")

# question 3
log_us <- log(us)
log_japan <- log(japan)

qqnorm(log_us, main = "Normal Q-Q Plot: Log of US Cars (mpg)")
qqline(log_us)

qqnorm(log_japan, main = "Normal Q-Q Plot: Log of Japanese Cars (mpg)")
qqline(log_japan)

boxplot(log_us, log_japan,
        names = c("US", "Japan"),
        col = c("lightblue", "orange"),
        main = "Log Fuel Efficiency by Country of Manufacture",
        xlab = "Country of Manufacture",
        ylab = "Log of Fuel Efficiency (log mpg)")

# question 4
t.test(log_us, log_japan, var.equal = TRUE, alternative = "less")

mean(log_us)
mean(log_japan)