The property market in Kuala Lumpur is full of opportunities. However, it can be hard to navigate with little to no experience. This project aims to build a model to predict the rental price of property based on the attributes.
#Include all library required
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
library(dplyr)
library(stringr)
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.2
library(caret)
## Warning: package 'caret' was built under R version 4.3.2
library(Metrics)
## Warning: package 'Metrics' was built under R version 4.3.2
library(e1071)
## Warning: package 'e1071' was built under R version 4.3.2
library(rpart)
library(nnet)
#Read data file and store in data frame
data = read.csv("rental.csv")
#Check column/rows and data type
glimpse(data)
## Rows: 19,991
## Columns: 14
## $ ads_id <int> 100323185, 100203973, 100323128, 100191767, 9702…
## $ prop_name <chr> "The Hipster @ Taman Desa", "Segar Courts", "Pan…
## $ completion_year <dbl> 2022, NA, NA, 2020, NA, NA, NA, 2018, 2014, NA, …
## $ monthly_rent <chr> "RM 4 200 per month", "RM 2 300 per month", "RM …
## $ location <chr> "Kuala Lumpur - Taman Desa", "Kuala Lumpur - Che…
## $ property_type <chr> "Condominium", "Condominium", "Apartment", "Apar…
## $ rooms <chr> "5", "3", "3", "2", "1", "3", "3", "1", "2", "3"…
## $ parking <dbl> 2, 1, NA, 1, 1, 1, 2, 1, 1, 1, NA, 2, 1, 1, NA, …
## $ bathroom <dbl> 6, 2, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, …
## $ size <chr> "1842 sq.ft.", "1170 sq.ft.", "650 sq.ft.", "743…
## $ furnished <chr> "Fully Furnished", "Partially Furnished", "Fully…
## $ facilities <chr> "Minimart, Gymnasium, Security, Playground, Swim…
## $ additional_facilities <chr> "Air-Cond, Cooking Allowed, Washing Machine", "A…
## $ region <chr> "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur", …
# Check if there is any NA value
colSums(is.na(data))
## ads_id prop_name completion_year
## 0 0 9185
## monthly_rent location property_type
## 0 0 0
## rooms parking bathroom
## 0 5702 6
## size furnished facilities
## 0 0 0
## additional_facilities region
## 0 0
# Check if there is any empty string value
colSums(data=="")
## ads_id prop_name completion_year
## 0 948 NA
## monthly_rent location property_type
## 2 0 0
## rooms parking bathroom
## 6 NA NA
## size furnished facilities
## 0 5 2209
## additional_facilities region
## 5948 0
# Check if there is any zero value
colSums(data==0)
## ads_id prop_name completion_year
## 0 0 NA
## monthly_rent location property_type
## 0 0 0
## rooms parking bathroom
## 0 NA NA
## size furnished facilities
## 0 0 0
## additional_facilities region
## 0 0
#Remove and row that has N/A value
data = data[complete.cases(data), ]
colSums(is.na(data))
## ads_id prop_name completion_year
## 0 0 0
## monthly_rent location property_type
## 0 0 0
## rooms parking bathroom
## 0 0 0
## size furnished facilities
## 0 0 0
## additional_facilities region
## 0 0
#Extract size numbers only and put in new column: size_sqft
data = data %>%
mutate(size_sqft = as.numeric(str_replace_all(size, "\\D", "")))
#Extract Kuala Lumpur/Selangor out from location and put in new column: location_extracted
data = data %>%
mutate(location_extracted = str_split(location, " - ", simplify = TRUE)[, 2])
#Checking for KTM/LRT string present in additional facilities and create new column to indicate
data$nearby_railways = ifelse(str_detect(data$additional_facilities, "\\bNear KTM/LRT\\b"), 'yes', 'no')
#Extract numbers only from monthly_rent and put in new column: monthly_rent_rm
data = data %>%
mutate(monthly_rent_rm = as.numeric(str_replace_all(monthly_rent, "\\D", "")))
#Changing rooms column data type to numeric
data$rooms = as.numeric(as.character(data$rooms))
#Removing all unneccesary column
data = data[, !(names(data) %in% c('ads_id', 'prop_name', 'facilities', 'additional_facilities', 'monthly_rent', 'size', 'location'))]
#Upper and lower limit for monthly_rent_rm and size_sqft to remove outliers
data = subset(data, monthly_rent_rm > 100 & monthly_rent_rm < 10000)
data = subset(data, size_sqft > 50 & size_sqft < 3000)
#Creating price range column to group them for classification
data$price_range = cut(data$monthly_rent_rm, breaks = quantile(data$monthly_rent_rm, probs = c(0, 0.25, 0.5, 0.75, 1)),
labels = c("Low", "Mid-Low", "Mid-High", "High"), include.lowest = TRUE)
# Check if there is any NA value
colSums(is.na(data))
## completion_year property_type rooms parking
## 0 0 0 0
## bathroom furnished region size_sqft
## 0 0 0 0
## location_extracted nearby_railways monthly_rent_rm price_range
## 0 0 0 0
# Check if there is any empty string value
colSums(data=="")
## completion_year property_type rooms parking
## 0 0 0 0
## bathroom furnished region size_sqft
## 0 0 0 0
## location_extracted nearby_railways monthly_rent_rm price_range
## 0 0 0 0
# Check if there is any zero value
colSums(data==0)
## completion_year property_type rooms parking
## 0 0 0 0
## bathroom furnished region size_sqft
## 0 0 0 0
## location_extracted nearby_railways monthly_rent_rm price_range
## 0 0 0 0
#Check column/rows and data type
glimpse(data)
## Rows: 8,170
## Columns: 12
## $ completion_year <dbl> 2022, 2020, 2018, 2014, 2022, 2018, 2017, 2020, 202…
## $ property_type <chr> "Condominium", "Apartment", "Service Residence", "A…
## $ rooms <dbl> 5, 2, 1, 2, 4, 1, 3, 3, 3, 2, 2, 2, 3, 2, 4, 4, 3, …
## $ parking <dbl> 2, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, …
## $ bathroom <dbl> 6, 2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 2, …
## $ furnished <chr> "Fully Furnished", "Partially Furnished", "Fully Fu…
## $ region <chr> "Kuala Lumpur", "Kuala Lumpur", "Kuala Lumpur", "Ku…
## $ size_sqft <dbl> 1842, 743, 700, 750, 1100, 700, 1009, 950, 950, 650…
## $ location_extracted <chr> "Taman Desa", "Sentul", "Ampang", "Segambut", "Seta…
## $ nearby_railways <chr> "no", "yes", "yes", "yes", "yes", "yes", "no", "yes…
## $ monthly_rent_rm <dbl> 4200, 1700, 1550, 1400, 2000, 1500, 1300, 3200, 320…
## $ price_range <fct> High, Mid-High, Mid-Low, Mid-Low, Mid-High, Mid-Low…
#Histogram for all quantitative columns
with(data, hist(parking))
with(data, hist(rooms))
with(data, hist(bathroom))
with(data, hist(completion_year))
with(data, hist(size_sqft))
with(data, hist(monthly_rent_rm))
#Getting all quantitative columns
cols = c("monthly_rent_rm", "size_sqft", "rooms", "parking", "bathroom", "completion_year")
head(data[,cols])
## monthly_rent_rm size_sqft rooms parking bathroom completion_year
## 1 4200 1842 5 2 6 2022
## 4 1700 743 2 1 2 2020
## 8 1550 700 1 1 1 2018
## 9 1400 750 2 1 1 2014
## 12 2000 1100 4 2 2 2022
## 13 1500 700 1 1 1 2018
#Normalization for all quantitative columns
scaler = preProcess(data[, cols], method=c("center", "scale"))
data[, cols] = predict(scaler, data[, cols])
head(data[,cols]
)
## monthly_rent_rm size_sqft rooms parking bathroom completion_year
## 1 3.09122048 3.0815561 2.8478368 0.9533845 7.6626962 1.0493931
## 4 -0.08900634 -0.7108203 -0.8204667 -0.8221230 0.1408618 0.7568307
## 8 -0.27981994 -0.8592026 -2.0432346 -0.8221230 -1.7395968 0.4642683
## 9 -0.47063355 -0.6866650 -0.8204667 -0.8221230 -1.7395968 -0.1208566
## 12 0.29262088 0.5210982 1.6250689 0.9533845 0.1408618 1.0493931
## 13 -0.34342448 -0.8592026 -2.0432346 -0.8221230 -1.7395968 0.4642683
#Spliting dataset to training and testing data
set.seed(999)
data_split = createDataPartition(data$monthly_rent_rm, p=0.8, list=FALSE)
train_data = data[data_split, ]
test_data = data[-data_split, ]
#Displaying the number of rows
message(
"Initial: ", nrow(data), " rows.\n",
"Train: ", nrow(train_data), " rows (or ", round((nrow(train_data)/nrow(data))*100, 2), "% of ", nrow(data), " rows).\n",
"Test: ", nrow(test_data), " rows (or ", round((nrow(test_data)/nrow(data))*100, 2), "% of ", nrow(data), " rows)."
)
## Initial: 8170 rows.
## Train: 6538 rows (or 80.02% of 8170 rows).
## Test: 1632 rows (or 19.98% of 8170 rows).
#Put inputs and outputs into different variable
X_train = train_data[, 1:10]
reg_train = train_data[, "monthly_rent_rm"]
clas_train = train_data[, "price_range"]
X_test = test_data[-data_split, 1:10]
reg_test = test_data[-data_split, "monthly_rent_rm"]
clas_test = test_data[-data_split, "price_range"]
#Linear Regression
reg1_model = lm(monthly_rent_rm~., data=train_data %>% select(-price_range))
reg1_pred = predict(reg1_model, X_test)
message(
"The root mean squared error is ", round(rmse(trunc(reg_test), trunc(reg1_pred)), 4), ".\n",
"The accuracy is ", round(accuracy(trunc(reg_test), trunc(reg1_pred)) * 100, 2), "%."
)
## The root mean squared error is 0.4428.
## The accuracy is 83.33%.
#Support Vector Regression
reg2_model = svm(monthly_rent_rm~., data=train_data %>% select(-price_range))
reg2_pred = predict(reg2_model, X_test)
message(
"The root mean squared error is ", round(rmse(trunc(reg_test), trunc(reg2_pred)), 4), ".\n",
"The accuracy is ", round(accuracy(trunc(reg_test), trunc(reg2_pred)) * 100, 2), "%."
)
## The root mean squared error is 0.4644.
## The accuracy is 85.29%.
#Logistic Regression
clas1_model = multinom(price_range~., data=train_data %>% select(-monthly_rent_rm))
## # weights: 452 (336 variable)
## initial value 9063.592533
## iter 10 value 6677.957991
## iter 20 value 6122.962646
## iter 30 value 5589.968021
## iter 40 value 5340.996926
## iter 50 value 5167.661704
## iter 60 value 5118.860021
## iter 70 value 5109.673784
## iter 80 value 5105.798242
## iter 90 value 5102.700304
## iter 100 value 5100.592895
## final value 5100.592895
## stopped after 100 iterations
clas1_pred = predict(clas1_model, X_test)
confusionMatrix(clas1_pred, clas_test)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Low Mid-Low Mid-High High
## Low 87 20 3 0
## Mid-Low 23 33 11 2
## Mid-High 4 15 32 6
## High 2 4 11 53
##
## Overall Statistics
##
## Accuracy : 0.6699
## 95% CI : (0.6142, 0.7224)
## No Information Rate : 0.3791
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.5481
##
## Mcnemar's Test P-Value : 0.5304
##
## Statistics by Class:
##
## Class: Low Class: Mid-Low Class: Mid-High Class: High
## Sensitivity 0.7500 0.4583 0.5614 0.8689
## Specificity 0.8789 0.8462 0.8996 0.9306
## Pos Pred Value 0.7909 0.4783 0.5614 0.7571
## Neg Pred Value 0.8520 0.8354 0.8996 0.9661
## Prevalence 0.3791 0.2353 0.1863 0.1993
## Detection Rate 0.2843 0.1078 0.1046 0.1732
## Detection Prevalence 0.3595 0.2255 0.1863 0.2288
## Balanced Accuracy 0.8145 0.6522 0.7305 0.8997
#Naive Bayes Classification
clas2_model = naiveBayes(price_range~., data=train_data %>% select(-monthly_rent_rm))
clas2_pred = predict(clas2_model, X_test)
confusionMatrix(clas2_pred, clas_test)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Low Mid-Low Mid-High High
## Low 88 19 8 0
## Mid-Low 24 36 14 2
## Mid-High 2 14 26 17
## High 2 3 9 42
##
## Overall Statistics
##
## Accuracy : 0.6275
## 95% CI : (0.5706, 0.6818)
## No Information Rate : 0.3791
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.4873
##
## Mcnemar's Test P-Value : 0.1826
##
## Statistics by Class:
##
## Class: Low Class: Mid-Low Class: Mid-High Class: High
## Sensitivity 0.7586 0.5000 0.45614 0.6885
## Specificity 0.8579 0.8291 0.86747 0.9429
## Pos Pred Value 0.7652 0.4737 0.44068 0.7500
## Neg Pred Value 0.8534 0.8435 0.87449 0.9240
## Prevalence 0.3791 0.2353 0.18627 0.1993
## Detection Rate 0.2876 0.1176 0.08497 0.1373
## Detection Prevalence 0.3758 0.2484 0.19281 0.1830
## Balanced Accuracy 0.8083 0.6645 0.66181 0.8157
For regression model, Support Vector Regression model has highest
accuracy.
For classification model, Naive Bayes Classification model has highest
accuracy.
Overall, regression models performed better than classification
models.