library(kableExtra)
library(tidyverse)
library(stargazer)
data(mtcars)
mtcars[1:5,] %>%
kable() %>%
kable_styling(bootstrap_options = c("striped","bordered"), full_width = F)
| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
ave_mpg <- mtcars %>%
group_by(am) %>%
summarise(ave_mpg = mean(mpg))
automatic = ave_mpg[ave_mpg$am==0,'ave_mpg']
manual = ave_mpg[ave_mpg$am==1,'ave_mpg']
mtcars %>%
mutate(Type = ifelse(am == 0,'Automatic','Manual')) %>%
ggplot(mapping = aes(x=Type,y=mpg)) +
geom_boxplot(outlier.colour="black", outlier.shape=16,
outlier.size=2, notch=FALSE)
Average MPG for manual is 24.3923077 and average MPG for automatic is 17.1473684. Manual is better for MPG.
H0: Average MPG of manual and automatic are the same
H1: Manual is better for MPG
auto <- mtcars[mtcars$am == 0,]
manual <- mtcars[mtcars$am == 1,]
t.test(auto$mpg, manual$mpg)
##
## Welch Two Sample t-test
##
## data: auto$mpg and manual$mpg
## t = -3.7671, df = 18.332, p-value = 0.001374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.280194 -3.209684
## sample estimates:
## mean of x mean of y
## 17.14737 24.39231
P-value = 0.00137 < 0.05. Reject null hypothesis and conclude that manual is better for MPG
model1 <- lm(mpg~am,data=mtcars)
model2 <- lm(mpg~am + wt + hp,data=mtcars)
stargazer(model1,model2,digits=4, type = "html", header = FALSE)
| Dependent variable: | ||
| mpg | ||
| (1) | (2) | |
| am | 7.2449*** | 2.0837 |
| (1.7644) | (1.3764) | |
| wt | -2.8786*** | |
| (0.9050) | ||
| hp | -0.0375*** | |
| (0.0096) | ||
| Constant | 17.1474*** | 34.0029*** |
| (1.1246) | (2.6427) | |
| Observations | 32 | 32 |
| R2 | 0.3598 | 0.8399 |
| Adjusted R2 | 0.3385 | 0.8227 |
| Residual Std. Error | 4.9020 (df = 30) | 2.5375 (df = 28) |
| F Statistic | 16.8603*** (df = 1; 30) | 48.9600*** (df = 3; 28) |
| Note: | p<0.1; p<0.05; p<0.01 | |
Model 1 explains 36% of the variance and model 2 explains 84% of the variance.
According to model 2, keeping everything else constant, manual transmissions have 2.08 MPG than automatic