#file.choose()
bicis <- read.csv("/Users/davidcavazos/Desktop/rentadebicis.csv")
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
summary(bicis)
## 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
regresion <- lm(rentas_totales ~ hora + dia + mes + año + estacion + dia_de_la_semana + sensacion_termica + humedad + velocidad_del_viento, data = bicis)
summary(regresion)
##
## Call:
## lm(formula = rentas_totales ~ hora + dia + mes + año + estacion +
## dia_de_la_semana + sensacion_termica + humedad + velocidad_del_viento,
## data = bicis)
##
## Residuals:
## Min 1Q Median 3Q Max
## -304.52 -93.34 -27.52 61.53 648.27
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.662e+05 5.495e+03 -30.246 < 2e-16 ***
## hora 7.733e+00 2.070e-01 37.356 < 2e-16 ***
## dia 3.925e-01 2.481e-01 1.582 0.113735
## mes 1.009e+01 1.672e+00 6.035 1.64e-09 ***
## año 8.264e+01 2.732e+00 30.254 < 2e-16 ***
## estacion -8.030e+00 5.144e+00 -1.561 0.118567
## dia_de_la_semana 5.270e-01 6.792e-01 0.776 0.437775
## sensacion_termica 6.183e+00 1.692e-01 36.551 < 2e-16 ***
## humedad -2.123e+00 7.870e-02 -26.973 < 2e-16 ***
## velocidad_del_viento 6.121e-01 1.773e-01 3.452 0.000559 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 141.7 on 10876 degrees of freedom
## Multiple R-squared: 0.3889, Adjusted R-squared: 0.3884
## F-statistic: 769.2 on 9 and 10876 DF, p-value: < 2.2e-16
regresion <- lm(rentas_totales ~ hora + mes + año + sensacion_termica + humedad + velocidad_del_viento, data = bicis)
summary(regresion)
##
## Call:
## lm(formula = rentas_totales ~ hora + mes + año + sensacion_termica +
## humedad + velocidad_del_viento, data = bicis)
##
## 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 <- data.frame(hora=11.54, mes=1:12, año=2013, sensacion_termica=23.66, humedad=61.89,velocidad_del_viento=12.799)
predict(regresion,datos)
## 1 2 3 4 5 6 7 8
## 273.6001 281.1738 288.7475 296.3213 303.8950 311.4687 319.0424 326.6161
## 9 10 11 12
## 334.1898 341.7635 349.3372 356.9110
#file.choose()
df <- read.csv("/Users/davidcavazos/Desktop/HousePriceData.csv")
summary(df)
## Observation Dist_Taxi Dist_Market Dist_Hospital
## Min. : 1.0 Min. : 146 Min. : 1666 Min. : 3227
## 1st Qu.:237.0 1st Qu.: 6477 1st Qu.: 9367 1st Qu.:11302
## Median :469.0 Median : 8228 Median :11149 Median :13189
## Mean :468.4 Mean : 8235 Mean :11022 Mean :13091
## 3rd Qu.:700.0 3rd Qu.: 9939 3rd Qu.:12675 3rd Qu.:14855
## Max. :932.0 Max. :20662 Max. :20945 Max. :23294
##
## Carpet Builtup Parking City_Category
## Min. : 775 Min. : 932 Length:905 Length:905
## 1st Qu.: 1317 1st Qu.: 1579 Class :character Class :character
## Median : 1478 Median : 1774 Mode :character Mode :character
## Mean : 1511 Mean : 1794
## 3rd Qu.: 1654 3rd Qu.: 1985
## Max. :24300 Max. :12730
## NA's :7
## Rainfall House_Price
## Min. :-110.0 Min. : 1492000
## 1st Qu.: 600.0 1st Qu.: 4623000
## Median : 780.0 Median : 5860000
## Mean : 786.9 Mean : 6083992
## 3rd Qu.: 970.0 3rd Qu.: 7200000
## Max. :1560.0 Max. :150000000
##
library(dplyr)
count(df, Parking, sort=TRUE)
## Parking n
## 1 Open 355
## 2 Not Provided 225
## 3 Covered 184
## 4 No Parking 141
count(df, City_Category, sort=TRUE)
## City_Category n
## 1 CAT B 351
## 2 CAT A 320
## 3 CAT C 234
# ¿Cuántos NA tengo en la base de datos?
sum(is.na(df))
## [1] 7
# ¿Cuántos NA tengo por variable?
sapply(df, function(x) sum(is.na(x)))
## Observation Dist_Taxi Dist_Market Dist_Hospital Carpet
## 0 0 0 0 7
## Builtup Parking City_Category Rainfall House_Price
## 0 0 0 0 0
# Eliminar todos los NA
df <- na.omit(df)
# Eliminar registro del precio atípico
df <- df[df$House_Price<12000000,]
# Eliminar registro de Rainfall negativo
df <- df[df$Rainfall>=0,]
#Gráficas
boxplot(df$House_Price, horizontal = TRUE)
regresion2 <- lm(House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital + Carpet + Builtup + Parking + City_Category + Rainfall, data=df)
summary(regresion2)
##
## Call:
## lm(formula = House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital +
## Carpet + Builtup + Parking + City_Category + Rainfall, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3572009 -800792 -65720 761534 4401585
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.599e+06 3.672e+05 15.246 < 2e-16 ***
## Dist_Taxi 3.009e+01 2.682e+01 1.122 0.2622
## Dist_Market 1.285e+01 2.081e+01 0.618 0.5370
## Dist_Hospital 4.864e+01 3.008e+01 1.617 0.1062
## Carpet -7.997e+02 3.476e+03 -0.230 0.8181
## Builtup 1.339e+03 2.901e+03 0.462 0.6444
## ParkingNo Parking -6.040e+05 1.389e+05 -4.348 1.53e-05 ***
## ParkingNot Provided -4.924e+05 1.235e+05 -3.988 7.22e-05 ***
## ParkingOpen -2.632e+05 1.126e+05 -2.338 0.0196 *
## City_CategoryCAT B -1.877e+06 9.598e+04 -19.554 < 2e-16 ***
## City_CategoryCAT C -2.890e+06 1.059e+05 -27.300 < 2e-16 ***
## Rainfall -1.175e+02 1.550e+02 -0.758 0.4484
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1222000 on 884 degrees of freedom
## Multiple R-squared: 0.5007, Adjusted R-squared: 0.4945
## F-statistic: 80.58 on 11 and 884 DF, p-value: < 2.2e-16
regresion2 <- lm(House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital + Carpet + Builtup + Parking + City_Category + Rainfall, data=df)
summary(regresion2)
##
## Call:
## lm(formula = House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital +
## Carpet + Builtup + Parking + City_Category + Rainfall, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3572009 -800792 -65720 761534 4401585
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.599e+06 3.672e+05 15.246 < 2e-16 ***
## Dist_Taxi 3.009e+01 2.682e+01 1.122 0.2622
## Dist_Market 1.285e+01 2.081e+01 0.618 0.5370
## Dist_Hospital 4.864e+01 3.008e+01 1.617 0.1062
## Carpet -7.997e+02 3.476e+03 -0.230 0.8181
## Builtup 1.339e+03 2.901e+03 0.462 0.6444
## ParkingNo Parking -6.040e+05 1.389e+05 -4.348 1.53e-05 ***
## ParkingNot Provided -4.924e+05 1.235e+05 -3.988 7.22e-05 ***
## ParkingOpen -2.632e+05 1.126e+05 -2.338 0.0196 *
## City_CategoryCAT B -1.877e+06 9.598e+04 -19.554 < 2e-16 ***
## City_CategoryCAT C -2.890e+06 1.059e+05 -27.300 < 2e-16 ***
## Rainfall -1.175e+02 1.550e+02 -0.758 0.4484
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1222000 on 884 degrees of freedom
## Multiple R-squared: 0.5007, Adjusted R-squared: 0.4945
## F-statistic: 80.58 on 11 and 884 DF, p-value: < 2.2e-16
datos <- data.frame(Dist_Taxi=8278, Dist_Market=16251, Dist_Hospital=13857, Carpet=1455, Builtup=1764, Parking="Covered", City_Category="CAT A", Rainfall=390)
predict(regresion2,datos)
## 1
## 7883860