Instalar paquetes y llamar librerías

#install.packages("rpart")
library(rpart)
#install.packages("rpart.plot")
library(rpart.plot)

Importar la base de datos

HousePricing <- read.csv("/Users/marceloreyesp/Desktop/Negocios/6to Semestre LIT/Clase Codigos/Clase Codigo R/HousePriceData.csv")

Entender la base de datos

summary(HousePricing)
##   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  
## 
str(HousePricing)
## '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 ...
head(HousePricing)
##   Observation Dist_Taxi Dist_Market Dist_Hospital Carpet Builtup      Parking
## 1           1      9796        5250         10703   1659    1961         Open
## 2           2      8294        8186         12694   1461    1752 Not Provided
## 3           3     11001       14399         16991   1340    1609 Not Provided
## 4           4      8301       11188         12289   1451    1748      Covered
## 5           5     10510       12629         13921   1770    2111 Not Provided
## 6           6      6665        5142          9972   1442    1733         Open
##   City_Category Rainfall House_Price
## 1         CAT B      530     6649000
## 2         CAT B      210     3982000
## 3         CAT A      720     5401000
## 4         CAT B      620     5373000
## 5         CAT B      450     4662000
## 6         CAT B      760     4526000

Crear árbol de decisión

HousePricing <- HousePricing[,-1] # Eliminar columna Observation
HousePricing <- subset(HousePricing, House_Price < 100000000) 
HousePricing$Parking <- as.factor(HousePricing$Parking)
HousePricing$City_Category <- as.factor(HousePricing$City_Category)

arbol_house <- rpart(House_Price ~ ., data = HousePricing)
rpart.plot(arbol_house)

prp(arbol_house, extra = 1, prefix = "$")

Conclusiones

En conclusión, al eliminar el valor atípico (“outlier”), el árbol de decisión revela que la Ubicación (City_Category) es el factor más determinante para el precio de las viviendas:

  1. Categoría A (CAT A): Son las propiedades más valiosas, con un precio promedio de $7.4 millones.
  2. Categoría B (CAT B): Tienen un valor intermedio. Dentro de este grupo, la Distancia al Taxi es clave; las casas más cercanas a paradas de taxi (< 11km) valen considerablemente más ($6.5M vs $5.3M).
  3. Categoría C (CAT C): Son las propiedades más económicas, con un precio promedio de $4.5 millones.

En resumen, la categoría de la ciudad define el rango de precio base, y la conectividad (taxis) ayuda a diferenciar el valor en zonas intermedias.