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(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.5 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ stringr 1.4.0
## ✔ tidyr 1.2.0 ✔ forcats 0.5.1
## ✔ readr 2.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(janitor)
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(ggplot2)
getwd()
#file.choose()
bd <- read_csv("/Users/danieltrevino/Documents/Quinto Semestre TEC/Bootcamp de Programación/rentadebicis.csv")
## Rows: 10886 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (14): hora, dia, mes, aÒo, estacion, dia_de_la_semana, asueto, temperatu...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
resumen <- summary(bd)
resumen
## hora dia mes aÒo
## Min. : 0.00 Min. : 1.000 Min. : 1.000 Min. :2011
## 1st Qu.: 6.00 1st Qu.: 5.000 1st Qu.: 4.000 1st Qu.:2011
## Median :12.00 Median :10.000 Median : 7.000 Median :2012
## Mean :11.54 Mean : 9.993 Mean : 6.521 Mean :2012
## 3rd Qu.:18.00 3rd Qu.:15.000 3rd Qu.:10.000 3rd Qu.:2012
## Max. :23.00 Max. :19.000 Max. :12.000 Max. :2012
## estacion dia_de_la_semana asueto temperatura
## Min. :1.000 Min. :1.000 Min. :0.00000 Min. : 0.82
## 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:0.00000 1st Qu.:13.94
## Median :3.000 Median :4.000 Median :0.00000 Median :20.50
## Mean :2.507 Mean :4.014 Mean :0.02857 Mean :20.23
## 3rd Qu.:4.000 3rd Qu.:6.000 3rd Qu.:0.00000 3rd Qu.:26.24
## Max. :4.000 Max. :7.000 Max. :1.00000 Max. :41.00
## sensacion_termica humedad velocidad_del_viento
## Min. : 0.76 Min. : 0.00 Min. : 0.000
## 1st Qu.:16.66 1st Qu.: 47.00 1st Qu.: 7.002
## Median :24.24 Median : 62.00 Median :12.998
## Mean :23.66 Mean : 61.89 Mean :12.799
## 3rd Qu.:31.06 3rd Qu.: 77.00 3rd Qu.:16.998
## Max. :45.45 Max. :100.00 Max. :56.997
## rentas_de_no_registrados rentas_de_registrados rentas_totales
## Min. : 0.00 Min. : 0.0 Min. : 1.0
## 1st Qu.: 4.00 1st Qu.: 36.0 1st Qu.: 42.0
## Median : 17.00 Median :118.0 Median :145.0
## Mean : 36.02 Mean :155.6 Mean :191.6
## 3rd Qu.: 49.00 3rd Qu.:222.0 3rd Qu.:284.0
## Max. :367.00 Max. :886.0 Max. :977.0
plot(bd$temperatura, bd$rentas_totales, main = "Influencia de la Temperatura sobre las rentas totales", xlab = " Temperatura (C)", ylab = "Cantidad")
regresion <- lm(rentas_totales ~ hora + estacion + dia + mes + aÒo + estacion + dia_de_la_semana + asueto + temperatura + sensacion_termica + humedad + velocidad_del_viento, data = bd)
summary(regresion)
##
## Call:
## lm(formula = rentas_totales ~ hora + estacion + dia + mes + aÒo +
## estacion + dia_de_la_semana + asueto + temperatura + sensacion_termica +
## humedad + velocidad_del_viento, data = bd)
##
## Residuals:
## Min 1Q Median 3Q Max
## -305.52 -93.64 -27.70 61.85 649.10
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.661e+05 5.496e+03 -30.217 < 2e-16 ***
## hora 7.735e+00 2.070e-01 37.368 < 2e-16 ***
## estacion -7.774e+00 5.177e+00 -1.502 0.13324
## dia 3.844e-01 2.482e-01 1.549 0.12150
## mes 9.996e+00 1.682e+00 5.943 2.89e-09 ***
## aÒo 8.258e+01 2.732e+00 30.225 < 2e-16 ***
## dia_de_la_semana 4.393e-01 6.918e-01 0.635 0.52545
## asueto -4.864e+00 8.365e+00 -0.582 0.56089
## temperatura 1.582e+00 1.038e+00 1.524 0.12752
## sensacion_termica 4.748e+00 9.552e-01 4.971 6.76e-07 ***
## humedad -2.115e+00 7.884e-02 -26.827 < 2e-16 ***
## velocidad_del_viento 5.582e-01 1.809e-01 3.086 0.00203 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 141.7 on 10874 degrees of freedom
## Multiple R-squared: 0.3891, Adjusted R-squared: 0.3885
## F-statistic: 629.6 on 11 and 10874 DF, p-value: < 2.2e-16
# tres * = mucha relacion con lo que queremos predecir
# Adjusted R-squared: Cuantas veces predice las rentas el modelo
regresion <- lm(rentas_totales ~ hora + mes + aÒo + asueto + temperatura + sensacion_termica + humedad + velocidad_del_viento, data = bd)
summary(regresion)
##
## Call:
## lm(formula = rentas_totales ~ hora + mes + aÒo + asueto + temperatura +
## sensacion_termica + humedad + velocidad_del_viento, data = bd)
##
## Residuals:
## Min 1Q Median 3Q Max
## -310.30 -93.82 -28.26 61.41 648.88
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.661e+05 5.497e+03 -30.226 < 2e-16 ***
## hora 7.737e+00 2.070e-01 37.377 < 2e-16 ***
## mes 7.556e+00 4.209e-01 17.954 < 2e-16 ***
## aÒo 8.261e+01 2.732e+00 30.235 < 2e-16 ***
## asueto -7.535e+00 8.156e+00 -0.924 0.35553
## temperatura 1.606e+00 1.038e+00 1.548 0.12170
## sensacion_termica 4.716e+00 9.549e-01 4.939 7.98e-07 ***
## humedad -2.113e+00 7.874e-02 -26.834 < 2e-16 ***
## velocidad_del_viento 5.672e-01 1.806e-01 3.140 0.00169 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 141.7 on 10877 degrees of freedom
## Multiple R-squared: 0.3888, Adjusted R-squared: 0.3884
## F-statistic: 864.9 on 8 and 10877 DF, p-value: < 2.2e-16
#Modelo corregido
regresion <- lm(rentas_totales ~ hora + mes + aÒo + sensacion_termica + humedad + velocidad_del_viento, data = bd)
summary(regresion)
##
## Call:
## lm(formula = rentas_totales ~ hora + mes + aÒo + sensacion_termica +
## humedad + velocidad_del_viento, data = bd)
##
## Residuals:
## Min 1Q Median 3Q Max
## -308.60 -93.85 -28.34 61.05 648.09
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.662e+05 5.496e+03 -30.250 < 2e-16 ***
## hora 7.734e+00 2.070e-01 37.364 < 2e-16 ***
## mes 7.574e+00 4.207e-01 18.002 < 2e-16 ***
## aÒo 8.266e+01 2.732e+00 30.258 < 2e-16 ***
## sensacion_termica 6.172e+00 1.689e-01 36.539 < 2e-16 ***
## humedad -2.121e+00 7.858e-02 -26.988 < 2e-16 ***
## velocidad_del_viento 6.208e-01 1.771e-01 3.506 0.000457 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 141.7 on 10879 degrees of freedom
## Multiple R-squared: 0.3886, Adjusted R-squared: 0.3883
## F-statistic: 1153 on 6 and 10879 DF, p-value: < 2.2e-16
datos_nuevos <- data.frame(hora = 12, mes = 1:12, aÒo = 2013, sensacion_termica=24, humedad = 62, velocidad_del_viento = 13)
predict(regresion, datos_nuevos)
## 1 2 3 4 5 6 7 8
## 279.1478 286.7215 294.2952 301.8690 309.4427 317.0164 324.5901 332.1638
## 9 10 11 12
## 339.7375 347.3112 354.8849 362.4587
#obtenemos las rentas por hora de los meses
En esta actividad, observamos como se realiza un modelo de regresión lineal a partir de una base de datos, con el fin de generar prdicciones de alguna de las variables respecto a las demás, en este caso la renta de bicis respecto al clima, mes y otras variables de la base de datos.