# Importar la base de datos de csv

data <- read.csv("C:\\Users\\eveyu\\Downloads\\HousePriceData.csv")
# Usar file.choose()

Entender la base de datos

str(data)
## 'data.frame':    905 obs. of  10 variables:
##  $ Observation  : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Dist_Taxi    : int  9796 8294 11001 8301 10510 6665 13153 5882 7495 8233 ...
##  $ Dist_Market  : int  5250 8186 14399 11188 12629 5142 11869 9948 11589 7067 ...
##  $ Dist_Hospital: int  10703 12694 16991 12289 13921 9972 17811 13315 13370 11400 ...
##  $ Carpet       : int  1659 1461 1340 1451 1770 1442 1542 1261 1090 1030 ...
##  $ Builtup      : int  1961 1752 1609 1748 2111 1733 1858 1507 1321 1235 ...
##  $ Parking      : chr  "Open" "Not Provided" "Not Provided" "Covered" ...
##  $ City_Category: chr  "CAT B" "CAT B" "CAT A" "CAT B" ...
##  $ Rainfall     : int  530 210 720 620 450 760 1030 1020 680 1130 ...
##  $ House_Price  : int  6649000 3982000 5401000 5373000 4662000 4526000 7224000 3772000 4631000 4415000 ...
summary(data)
##   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  
## 

Ubicación de outlier

which(data$House_Price == 150000000)
## [1] 348

Generar el Modelo

regresion <- lm(House_Price~Carpet+factor(Parking)+factor(City_Category), data=data)

summary(regresion)
## 
## Call:
## lm(formula = House_Price ~ Carpet + factor(Parking) + factor(City_Category), 
##     data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4328758 -1137271   -81123  1181788 12154351 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 -9.100e+05  1.956e+05  -4.653 3.77e-06 ***
## Carpet                       5.794e+03  7.541e+01  76.836  < 2e-16 ***
## factor(Parking)No Parking   -4.712e+05  2.026e+05  -2.325   0.0203 *  
## factor(Parking)Not Provided -2.575e+05  1.808e+05  -1.424   0.1547    
## factor(Parking)Open         -1.145e+05  1.655e+05  -0.692   0.4892    
## factor(City_Category)CAT B  -2.045e+06  1.404e+05 -14.569  < 2e-16 ***
## factor(City_Category)CAT C  -2.995e+06  1.554e+05 -19.272  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1800000 on 891 degrees of freedom
##   (7 observations deleted due to missingness)
## Multiple R-squared:  0.8765, Adjusted R-squared:  0.8757 
## F-statistic:  1054 on 6 and 891 DF,  p-value: < 2.2e-16

Notas/Ajustes al modelo

# Dist_Taxi, Dist_Market, Dist_Hospital, Rainfall fueron las variables que removí del modelo ya que no contaban con significancia estadística, lo que permitió simplificar el modelo.

#Las variables Carpet y Builtup, muestran un efecto de multicolinealidad ya que miden lo mismo, por lo tanto se eliminó "Builtup".

Predicciones

datos_nuevos <- data.frame(Carpet=1659, Parking="Open", City_Category="CAT B")

predict(regresion, datos_nuevos)
##       1 
## 6543342

Conclusiones

# El modelo es altamente significativo (p-value menor al 5%) con un poder explicativo del 87%, lo cual demuestra un muy buen desempeño.

# Sobre los coeficientes:
# Carpet: existe una relación significativa y positiva entre esta variable y el precio, indicando que a medida que incrementa una unidad de área el precio de la casa aumenta. Esto tiene sentido ya que mientras más grande es una propiedad mayor valor tiene.
# Tipo de estacionamiento: el precio de las casas se ve altamente influenciado por el tipo de estacionamiento que tiene, encontrando que las casas sin estacionamiento o con estacionamiento abierto disminuyen su precio.

# En conclusión, el precio de las casas se ve influenciado por el espacio, el tipo de estacionamiento y categoría de ciudad donde se encuentra.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.