Teoría

lm() Es la funcion en R para ajustar modelos lineales Es el modelo estadístico mas básico que existe, y más facil de interpretar.
Para interpretarlo se usa la función R-cuadrada, que significa qué tan cerca están los datos de la regresión. Va de 0 a 1, donde 1 es el que el modelo explica toda la variabilidad.

Contexto

La empresa renta bicicletas y cuenta con regristros por horas de las que tienen rentadas. Se desea generar un modelo predictivo.

Instalar paquetes y llamar librerías

#install.packages("corrplot")
library(corrplot)
## corrplot 0.95 loaded

Subir archivo

# file.choose()
df <- read.csv("/Users/danaraesparzamacias/Desktop/rentadebicis.csv")

Entender base de datos

summary(df)
##       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
str(df)
## 'data.frame':    10886 obs. of  14 variables:
##  $ hora                    : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ dia                     : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ mes                     : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ año                     : int  2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
##  $ estacion                : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ dia_de_la_semana        : int  6 6 6 6 6 6 6 6 6 6 ...
##  $ asueto                  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ temperatura             : num  9.84 9.02 9.02 9.84 9.84 ...
##  $ sensacion_termica       : num  14.4 13.6 13.6 14.4 14.4 ...
##  $ humedad                 : int  81 80 80 75 75 75 80 86 75 76 ...
##  $ velocidad_del_viento    : num  0 0 0 0 0 ...
##  $ rentas_de_no_registrados: int  3 8 5 3 0 0 2 1 1 8 ...
##  $ rentas_de_registrados   : int  13 32 27 10 1 1 0 2 7 6 ...
##  $ rentas_totales          : int  16 40 32 13 1 1 2 3 8 14 ...
head(df)
##   hora dia mes  año estacion dia_de_la_semana asueto temperatura
## 1    0   1   1 2011        1                6      0        9.84
## 2    1   1   1 2011        1                6      0        9.02
## 3    2   1   1 2011        1                6      0        9.02
## 4    3   1   1 2011        1                6      0        9.84
## 5    4   1   1 2011        1                6      0        9.84
## 6    5   1   1 2011        1                6      0        9.84
##   sensacion_termica humedad velocidad_del_viento rentas_de_no_registrados
## 1            14.395      81               0.0000                        3
## 2            13.635      80               0.0000                        8
## 3            13.635      80               0.0000                        5
## 4            14.395      75               0.0000                        3
## 5            14.395      75               0.0000                        0
## 6            12.880      75               6.0032                        0
##   rentas_de_registrados rentas_totales
## 1                    13             16
## 2                    32             40
## 3                    27             32
## 4                    10             13
## 5                     1              1
## 6                     1              1
tail(df,10)
##       hora dia mes  año estacion dia_de_la_semana asueto temperatura
## 10877   14  19  12 2012        4                3      0       17.22
## 10878   15  19  12 2012        4                3      0       17.22
## 10879   16  19  12 2012        4                3      0       17.22
## 10880   17  19  12 2012        4                3      0       16.40
## 10881   18  19  12 2012        4                3      0       15.58
## 10882   19  19  12 2012        4                3      0       15.58
## 10883   20  19  12 2012        4                3      0       14.76
## 10884   21  19  12 2012        4                3      0       13.94
## 10885   22  19  12 2012        4                3      0       13.94
## 10886   23  19  12 2012        4                3      0       13.12
##       sensacion_termica humedad velocidad_del_viento rentas_de_no_registrados
## 10877            21.210      50              12.9980                       33
## 10878            21.210      50              19.0012                       28
## 10879            21.210      50              23.9994                       37
## 10880            20.455      50              26.0027                       26
## 10881            19.695      50              23.9994                       23
## 10882            19.695      50              26.0027                        7
## 10883            17.425      57              15.0013                       10
## 10884            15.910      61              15.0013                        4
## 10885            17.425      61               6.0032                       12
## 10886            16.665      66               8.9981                        4
##       rentas_de_registrados rentas_totales
## 10877                   185            218
## 10878                   209            237
## 10879                   297            334
## 10880                   536            562
## 10881                   546            569
## 10882                   329            336
## 10883                   231            241
## 10884                   164            168
## 10885                   117            129
## 10886                    84             88
correlacion <- cor(df)
corrplot(correlacion)

Generar el modelo

regresion <- lm(rentas_totales~., data=df) 
summary(regresion) 
## 
## Call:
## lm(formula = rentas_totales ~ ., data = df)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -5.059e-11 -1.600e-14  3.000e-15  2.100e-14  3.479e-11 
## 
## Coefficients:
##                            Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)              -4.281e-10  2.551e-11 -1.678e+01  < 2e-16 ***
## hora                      3.089e-14  9.793e-16  3.155e+01  < 2e-16 ***
## dia                       5.217e-15  1.106e-15  4.719e+00 2.40e-06 ***
## mes                      -3.943e-14  7.507e-15 -5.253e+00 1.52e-07 ***
## año                       2.125e-13  1.268e-14  1.676e+01  < 2e-16 ***
## estacion                  1.793e-15  2.306e-14  7.800e-02  0.93804    
## dia_de_la_semana         -6.018e-14  3.362e-15 -1.790e+01  < 2e-16 ***
## asueto                   -2.350e-13  3.772e-14 -6.231e+00 4.80e-10 ***
## temperatura               2.151e-14  4.624e-15  4.653e+00 3.31e-06 ***
## sensacion_termica        -6.997e-15  4.265e-15 -1.640e+00  0.10097    
## humedad                   2.946e-15  3.701e-16  7.962e+00 1.86e-15 ***
## velocidad_del_viento      2.358e-15  8.059e-16  2.926e+00  0.00344 ** 
## rentas_de_no_registrados  1.000e+00  1.720e-16  5.815e+15  < 2e-16 ***
## rentas_de_registrados     1.000e+00  5.225e-17  1.914e+16  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.309e-13 on 10872 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 6.902e+31 on 13 and 10872 DF,  p-value: < 2.2e-16

Ajustar el modelo

regresion2 <- lm(rentas_totales~hora+mes+año+humedad+sensacion_termica+ velocidad_del_viento, data=df) 
summary(regresion2) 
## 
## Call:
## lm(formula = rentas_totales ~ hora + mes + año + humedad + sensacion_termica + 
##     velocidad_del_viento, data = df)
## 
## 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 ***
## humedad              -2.121e+00  7.858e-02 -26.988  < 2e-16 ***
## sensacion_termica     6.172e+00  1.689e-01  36.539  < 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

Generar predicciones

datos_nuevos <- data.frame(hora=12, mes=1:12, año=2013, sensacion_termica=24, humedad=62, velocidad_del_viento=13)
predict(regresion2,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