{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
# Importar los paquetes necesarios
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:car':
##
## logit
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
data(mpg, package = "ggplot2")
print(shapiro.test(mpg$hwy))
##
## Shapiro-Wilk normality test
##
## data: mpg$hwy
## W = 0.95885, p-value = 2.999e-06
print(shapiro.test(mpg$cty))
##
## Shapiro-Wilk normality test
##
## data: mpg$cty
## W = 0.95679, p-value = 1.744e-06
print(shapiro.test(mpg$displ))
##
## Shapiro-Wilk normality test
##
## data: mpg$displ
## W = 0.9408, p-value = 3.936e-08
correlation_matrix <- cor(mpg[, c("displ", "cty", "hwy")])
print(correlation_matrix)
## displ cty hwy
## displ 1.000000 -0.7985240 -0.7660200
## cty -0.798524 1.0000000 0.9559159
## hwy -0.766020 0.9559159 1.0000000
lm_cty <- lm(cty ~ displ, data = mpg)
print(summary(lm_cty))
##
## Call:
## lm(formula = cty ~ displ, data = mpg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.3109 -1.4695 -0.2566 1.1087 14.0064
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 25.9915 0.4821 53.91 <2e-16 ***
## displ -2.6305 0.1302 -20.20 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.567 on 232 degrees of freedom
## Multiple R-squared: 0.6376, Adjusted R-squared: 0.6361
## F-statistic: 408.2 on 1 and 232 DF, p-value: < 2.2e-16
lm_hwy <- lm(hwy ~ displ + cty, data = mpg)
print(summary(lm_hwy))
##
## Call:
## lm(formula = hwy ~ displ + cty, data = mpg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3124 -1.2423 0.0053 1.0296 4.1243
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.15145 1.21271 0.949 0.343
## displ -0.03432 0.14791 -0.232 0.817
## cty 1.32914 0.04490 29.602 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.756 on 231 degrees of freedom
## Multiple R-squared: 0.9138, Adjusted R-squared: 0.913
## F-statistic: 1224 on 2 and 231 DF, p-value: < 2.2e-16
print(ggplot(mpg, aes(x = displ, y = cty)) +
geom_point() +
geom_smooth(method = "lm") +
labs(title = "Dispersión entre cilindrada y millas por galón en ciudad",
x = "Cilindrada del motor (litros)",
y = "Millas por galón en ciudad"))
## `geom_smooth()` using formula = 'y ~ x'
print(plot(lm_hwy, which = 1))
## NULL
data(mpg, package = "ggplot2")
print(head(mpg))
## # A tibble: 6 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…