library(readr)
# install.packages("rpart")
library(rpart)
# install.packages("rpart.plot")
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.4.3
titanic <- read_csv
HousePriceData <- read_csv("C:/Users/robie/Downloads/HousePriceData.csv")
## Rows: 905 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Parking, City_Category
## dbl (8): Observation, Dist_Taxi, Dist_Market, Dist_Hospital, Carpet, Builtup...
##
## ℹ 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.
# install.packages("rpart")
library(rpart)
# install.packages("rpart.plot")
library(rpart.plot)
#file.choose()
house <- read.csv("C:/Users/robie/Downloads/HousePriceData.csv")
summary(house)
## 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(house)
## '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(house)
## 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
house <- house[-348,]
house$Parking <- as.factor(house$Parking)
house$City_Category <- as.factor(house$City_Category)
str(house)
## 'data.frame': 904 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 : Factor w/ 4 levels "Covered","No Parking",..: 4 3 3 1 3 4 2 4 3 4 ...
## $ City_Category: Factor w/ 3 levels "CAT A","CAT B",..: 2 2 1 2 2 2 1 3 2 3 ...
## $ 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 ...
arbol_house <- rpart(House_Price~., data=house)
options(scipen = 999)
rpart.plot(arbol_house)
En conclusión, el árbol de decisión muestra que el precio de la vivienda está determinado principalmente por el tamaño de la propiedad, especialmente por el área construida (Builtup), siendo esta la variable con mayor influencia en el modelo. A medida que aumenta el tamaño construido y el área habitable (Carpet), el precio tiende a incrementarse significativamente. Además, la categoría de la ciudad (City_Category) también juega un papel importante, ya que las viviendas ubicadas en categorías más altas presentan valores mayores. Por otro lado, factores como la distancia a servicios (taxi, mercado y hospital) y el tipo de estacionamiento influyen en el precio, pero su impacto es secundario en comparación con las características estructurales. En general, el modelo evidencia que el mercado inmobiliario valora principalmente el tamaño y la ubicación de la vivienda como determinantes clave del precio.