Para resolver este ejercicio tendrán que apelar a lo que aprendieron en las clases anteriores pero también (y no menor) a la lógica y a la buena amiga internet (animense a googlear los nombres de los test, sus interpretaciones y todo lo que los pueda ayudar a completar el ejercicio wikipedia es un buen inicio)
Los datos se extrajeron de la revista Motor Trend US de 1974 y comprenden el consumo de combustible y 10 aspectos del diseño y el rendimiento de 32 automóviles (modelos de 1973-74).
Las variables son
mpg Millas/(US) galón
cyl Número de cilindros
disp Cilindrada (cu.in.)
hp Potencia bruta
drat Relación del eje trasero
wt Peso (1000 lbs)
qsec Tiempo de 1/4 de milla
vs Motor (0 = en forma de V, 1 = recto)
am Transmisión (0 = automática, 1 = manual)
gear Número de marchas adelante
carb Número de carburadores
La base se ve así
head(data,5)
## mpg cyl disp hp drat wt qsec vs am gear
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 V-shaped Manual 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 V-shaped Manual 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 Straigth Manual 4
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 Straigth Automatic 3
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 V-shaped Automatic 3
## carb
## Mazda RX4 4
## Mazda RX4 Wag 4
## Datsun 710 1
## Hornet 4 Drive 1
## Hornet Sportabout 2
Y está estructurada así:
str(data)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : Factor w/ 2 levels "V-shaped","Straigth": 1 1 2 2 1 2 1 2 2 2 ...
## $ am : Factor w/ 2 levels "Automatic","Manual": 2 2 2 1 1 1 1 1 1 1 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
Veamos una estadistica de resumen de esta base:
summary(data)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs am
## Min. :2.760 Min. :1.513 Min. :14.50 V-shaped:18 Automatic:19
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 Straigth:14 Manual :13
## Median :3.695 Median :3.325 Median :17.71
## Mean :3.597 Mean :3.217 Mean :17.85
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90
## Max. :4.930 Max. :5.424 Max. :22.90
## gear carb
## Min. :3.000 Min. :1.000
## 1st Qu.:3.000 1st Qu.:2.000
## Median :4.000 Median :2.000
## Mean :3.688 Mean :2.812
## 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :5.000 Max. :8.000
Hacemos algunos gráficos para ver:
library("ggpubr")
# grafico de densidad
ggdensity(data$mpg, fill = "lightgray")+
ggtitle("Millas por galon", "density plot")
# QQ plot
ggqqplot(data$mpg)+
ggtitle("Millas por galon", "QQ Plot")
#Boxplot
boxplot(data$mpg)
#correlacion de Pearson
cor.test(data$mpg, data$wt)
##
## Pearson's product-moment correlation
##
## data: data$mpg and data$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9338264 -0.7440872
## sample estimates:
## cor
## -0.8676594
Vamos a incluir un grafiquito para ayudarnos a intepretar
ggplot(data, aes(x=mpg, y=wt))+
geom_point()
ggplot(data, aes(x=mpg, y=wt, color=am))+
geom_point()+
geom_smooth(method = "lm")
t.test(data$mpg ~ data$vs)
##
## Welch Two Sample t-test
##
## data: data$mpg by data$vs
## t = -4.6671, df = 22.716, p-value = 0.0001098
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.462508 -4.418445
## sample estimates:
## mean in group V-shaped mean in group Straigth
## 16.61667 24.55714
Vamos a incluir un grafiquito para ayudarnos a intepretar
ggplot(data, aes(y=mpg, x=vs))+
geom_boxplot()
data %>% select(mpg, disp, hp, wt, am, vs)%>% tbl_summary(by=vs, missing = "no") %>%
add_p() %>%
add_overall()
| Characteristic | Overall, N = 321 | V-shaped, N = 181 | Straigth, N = 141 | p-value2 |
|---|---|---|---|---|
| mpg | 19.2 (15.4, 22.8) | 15.7 (14.8, 19.1) | 22.8 (21.4, 29.6) | <0.001 |
| disp | 196 (121, 326) | 311 (276, 360) | 121 (83, 162) | <0.001 |
| hp | 123 (96, 180) | 180 (156, 226) | 96 (66, 110) | <0.001 |
| wt | 3.33 (2.58, 3.61) | 3.57 (3.24, 3.84) | 2.62 (2.00, 3.21) | 0.001 |
| am | 0.3 | |||
| Automatic | 19 (59%) | 12 (67%) | 7 (50%) | |
| Manual | 13 (41%) | 6 (33%) | 7 (50%) | |
| 1 Median (IQR); n (%) | ||||
| 2 Wilcoxon rank sum test; Pearson's Chi-squared test | ||||