#data(package = .packages(all.available = TRUE))
mydata <- force(mtcars)
head(mydata, 10)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Description:
mpg: Miles/(US) gallon (Ratio)
cyl: Number of
cylinders (Ordinal)
disp: Displacement (in cubic inches) (Ratio)
hp: Gross horsepower (Ratio)
drat: Rear axle ratio (Ratio)
wt: Weight (1000 lbs) (Ratio)
qsec: 1/4 mile time (in seconds)
(Ratio)
vs: Engine (0 = V-shaped, 1 = straight) (Nominal)
am:
Transmission (0 = automatic, 1 = manual) (Nominal)
gear: Number of
forward gears (Ordinal)
carb: Number of carburetors (Ordinal)
Unit of observation: 1 car
32 observations
Source: Henderson and Velleman (1981), Building multiple
regression models interactively. Biometrics, 37, 391–411.
———————-
Turning all the categorical variables into factors:
mydata$vsF <- factor(mydata$vs,
levels = c(0,1),
labels = c("V-shaped", "Straight"))
mydata$amF <- factor(mydata$am,
levels = c(0,1),
labels = c("Automatic", "Manual"))
Showing the descriptive statistics of cars’ fuel efficiency
between automatic and manual transmission:
describeBy(mydata$mpg, mydata$amF)
##
## Descriptive statistics by group
## group: Automatic
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 19 17.15 3.83 17.3 17.12 3.11 10.4 24.4 14 0.01 -0.8 0.88
## ------------------------------------------------------------------------------------------
## group: Manual
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 13 24.39 6.17 22.8 24.38 6.67 15 33.9 18.9 0.05 -1.46 1.71
It appears from this data that automatic transmission cars on average consume 17.15 mpg, meanwhile the manual transmission ones have an average consume of 24.39. The most efficient car is an automatic transmission one with 10.4 mpg and the most fuel consuming one is a manual transmission car with 33.9 mpg. Standard deviation is greater in manual transmission cars due to the fact that fuel efficiency in those cars depends by the driver driving style and timing.
Since we are interested in checking if there’s a difference between
two independent samples, (mpg of automatic and mpg of manual), we need
to check first if the assumptions are met:
Since the compared
variable mpg is numeric and equality of variances between groups is
always assumed, we have left to check for normality of the variable
between groups.
We begin with normality of the variable between
groups:
Automatic_t <- ggplot(mydata[mydata$amF == "Automatic", ], aes(x = mpg)) +
theme_linedraw()+
geom_histogram(stat = "bin", position = "stack", binwidth = 2.5, fill = "coral", color = "white" )+
ylab("Number of cars") +
xlab("Miles per US gallon") +
ggtitle("Automatic")
Manual_t <- ggplot(mydata[mydata$amF == "Manual", ], aes(x = mpg)) +
theme_linedraw()+
geom_histogram(stat = "bin", position = "stack", binwidth = 2.5, fill = "lightblue", color = "white" )+
ylab("Number of cars") +
xlab("Miles per US gallon") +
ggtitle("Manual")
ggarrange(Automatic_t, Manual_t,
ncol = 2, nrow = 1)
We can’t be sure if normality is met by watching the graphs alone,
we need to perform the Shapiro-Wilk test:
H0: the sample is normally
distributed
H1: the sample is not normally distributed
mydata %>%
group_by(amF) %>%
shapiro_test(mpg)
## # A tibble: 2 × 4
## amF variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Automatic mpg 0.977 0.899
## 2 Manual mpg 0.946 0.536
Based on sample data, we cannot reject H0 for both groups. The
corresponding p-values are 0.89 for Automatic cars and 0.53 for manual
cars.
Now we are going to perform the t test and we are going to
assume that there are variances between groups, (var.equal = FALSE), and
use the Welch correction.
Welch correction:
H0: true
difference in means between Automatic and Manual is equal to 0;
H1:
true difference in means between Automatic and Manual is NOT equal to
0.
t.test(mydata$mpg ~ mydata$amF,
paired = FALSE,
var.equal = FALSE,
alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: mydata$mpg by mydata$amF
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means between group Automatic and group Manual is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean in group Automatic mean in group Manual
## 17.14737 24.39231
effectsize::cohens_d(mydata$mpg ~ mydata$amF)
## Cohen's d | 95% CI
## --------------------------
## -1.48 | [-2.27, -0.67]
##
## - Estimated using pooled SD.
interpret_cohens_d(-1.41, rules = "sawilowsky2009")
## [1] "very large"
## (Rules: sawilowsky2009)
Based on sample data, we reject H0 at p-value 0.002. We can claim
that there are differences in the fuel consumption of automatic and
manual transmission cars, the automatic transmission is more fuel
efficient. The effect is very large.
To to a robustness check
we are going now to perform the non-parametric alternative test and
check the result. The test is the Wilcoxon signed rank sum test. The
hypothesis for such test are:
H0: The locations of distributions
are the same;
H1: The location of distributions are NOT the
same.
wilcox.test(mydata$mpg ~ mydata$amF,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "less")
##
## Wilcoxon rank sum test
##
## data: mydata$mpg by mydata$amF
## W = 42, p-value = 0.0008767
## alternative hypothesis: true location shift is less than 0
ggplot(mydata, aes(x = mpg, fill = amF)) +
geom_histogram(position = position_dodge(width = 1), binwidth = 3, colour = "white") +
scale_x_continuous(breaks = seq(0, 75, 5)) +
ylab("Number of cars") +
xlab("Miles per US gallon") +
labs(fill = "Transmission")
effectsize(wilcox.test(mydata$mpg ~ mydata$amF,
paired = FALSE,
correct = FALSE,
exact = FALSE,
alternative = "less"))
## r (rank biserial) | 95% CI
## ----------------------------------
## -0.66 | [-1.00, -0.42]
##
## - One-sided CIs: lower bound fixed at [-1.00].
Based on sample data, we reject H0 at p-value < 0.001, and conclude that there’s difference between automatic and manual transmission, the automatic transmission is more fuel efficient. The effect size based on the rules “funder2019” is very large.