RQ: How do size and parking of real estate units affect the rent?

library(readxl)
mydata <- read_excel("~/Desktop/IMB/MVA/R homeworks/R homeworks/houses_Madrid.xlsx")
set.seed(1) 
mydata <- mydata[sample(nrow(mydata), 250), ]
mydata<- mydata[, -c(4)]
library(tidyr)
mydata <- drop_na(mydata)

#Renaming columns
colnames(mydata)[colnames(mydata) == "Rent price"] <- "Rent_price"
colnames(mydata)[colnames(mydata) == "Square meters"] <- "Size"

# Filter the dataset to keep only rows with 'rent_price' above 0
mydata <- mydata[mydata$`Rent_price` > 0, ]

#Factoring parking
mydata$Parking <- factor(mydata$Parking,
 levels = c("False", "True"),
 labels = c("False", "True"))

head(mydata)
## # A tibble: 6 × 4
##      ID Rent_price  Size Parking
##   <dbl>      <dbl> <dbl> <fct>  
## 1  4342       1779   116 False  
## 2 16968        621    53 False  
## 3  8525        964    82 False  
## 4 11204       2512   183 True   
## 5 13281       2504   236 True   
## 6 17693       1299    80 True

Data explanation and choice of explanatory variables

Unit of observation is a real estate unit in Madrid, sample size is 250.

Explanatory variables:

Estimate of rent = b0 + b1 * Size + b2 * Parking (Dummy)

Data source: Madrid real estate market (Kaggle - link: https://www.kaggle.com/datasets/mirbektoktogaraev/madrid-real-estate-market)

Descriptive statistics

summary(mydata)
##        ID          Rent_price          Size         Parking   
##  Min.   :   16   Min.   : 316.0   Min.   : 23.00   False:161  
##  1st Qu.: 4882   1st Qu.: 865.5   1st Qu.: 67.75   True : 63  
##  Median :10430   Median :1323.0   Median : 90.00              
##  Mean   :10505   Mean   :1393.4   Mean   :119.18              
##  3rd Qu.:15281   3rd Qu.:1850.0   3rd Qu.:146.00              
##  Max.   :21714   Max.   :2514.0   Max.   :499.00

Average rent is 1393.4 eur, while the minimum one is 316 eur. 75% of RE units are 146 square meters big and the rest of the units are bigger. 161 of real estate units do not have a parking, while 63 have it.

Checking assumptions and requirements

Linearity
#Scatterplot matrix
library(car)
## Loading required package: carData
scatterplotMatrix(mydata[ ,c(-1,-4)],
                  smooth = FALSE)

According to the scatterplot, there is no linearity between rent and size of the real estate. I will have to transform the variable using natural logarithm.

#Creating logarithm
mydata$lnSize <- log(mydata$Size)

scatterplotMatrix(mydata[c(2,5)],
                  smooth = FALSE)

#Estimating regression function
fit <- lm(Rent_price ~ lnSize + Parking,
          data = mydata)
Multicolinearity
vif(fit)
##   lnSize  Parking 
## 1.249918 1.249918
mean(vif(fit))
## [1] 1.249918

There are no issues with too strong multicolinearity (VIF < 5 and average VIF is around 1 ).

mydata$StdResid <- round(rstandard(fit),3)
mydata$CooksD <- cooks.distance(fit)
mydata$StdFitted <- scale(fit$fitted.values)

#Checking histograms
hist(mydata$StdResid,
     xlab = "Standard residual",
     ylab = "Frequency",
     main = "Histogram of Std residuals",
     breaks = seq(from = -5, to = 5, by = 1),
     col = "blue")

#Checking Shapiro for normality
shapiro.test(mydata$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$StdResid
## W = 0.94041, p-value = 6.287e-08

H0: Errors are normally distributed.

H1: Errors are not normally distributed.

BSD we reject H0 (p<0.001) and conclude that errors are not normally distributed.

hist(mydata$CooksD,
     xlab = "CooksD",
     ylab = "Frequency",
     main = "Histogram of Cooks distances",
     col = "lightgreen")

#Checking outliers and high influence units
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 8)
## # A tibble: 8 × 2
##      ID CooksD
##   <dbl>  <dbl>
## 1  9112 0.225 
## 2  6714 0.144 
## 3 14866 0.137 
## 4 12105 0.0995
## 5 13278 0.0454
## 6 12657 0.0277
## 7  2459 0.0226
## 8  9851 0.0205
head(mydata[order(-mydata$StdResid), c("ID", "StdResid")], 6)
## # A tibble: 6 × 2
##      ID StdResid
##   <dbl>    <dbl>
## 1  9539     2.54
## 2  3560     2.50
## 3  2501     2.38
## 4  2170     1.80
## 5  3393     1.78
## 6 14592     1.76
head(mydata[order(mydata$StdResid), c("ID", "StdResid")], 6)
## # A tibble: 6 × 2
##      ID StdResid
##   <dbl>    <dbl>
## 1  9112    -4.48
## 2  6714    -4.07
## 3 14866    -4.03
## 4 12105    -2.93
## 5  2459    -2.57
## 6 13278    -2.23

Outliers

3 units should be dropped (9112, 6714, 14866) -> they have Std. residuals below -3

High impact units

All the units have Cooks distances below 1, but some are very distant from the others so they should be removed.

#Removing outliers and high impact units
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mydata <- mydata %>%
  filter(ID != 9112)
mydata <- mydata %>%
  filter(ID != 6714)
mydata <- mydata %>%
  filter(ID != 14866)
mydata <- mydata %>%
  filter(ID != 12105)
mydata <- mydata %>%
  filter(ID != 13278)
fit <- lm(Rent_price ~ lnSize + Parking,
          data = mydata)

hist(mydata$StdResid,
     xlab = "Standard residual",
     ylab = "Frequency",
     main = "Histogram of Std residuals",
     breaks = seq(from = -5, to = 5, by = 1),
     col = "blue")

shapiro.test(mydata$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$StdResid
## W = 0.97684, p-value = 0.00115

H0: Errors are normally distributed.

H1: Errors are not normally distributed.

BSD we reject H0 (p=0.002) and conclude that errors are not normally distributed. But since we have a large sample, we do not have to worry about that.

hist(mydata$CooksD,
     xlab = "CooksD",
     ylab = "Frequency",
     main = "Histogram of Cooks distances",
     col = "lightgreen")

There are no outliers and high impact units anymore.

#Checking scatterplot
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
            ylab = "Standardized residuals",
            xlab = "Standardized fitted values",
            boxplots = FALSE,
            regLine = FALSE,
            smooth = FALSE)

Based on scatterplot between std. fitted values and std. residuals, linearity assumption and homoscedasticity are met.

Homoscedasticity
#Checking homoscedasticity with BP test
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                  Data                  
##  --------------------------------------
##  Response : Rent_price 
##  Variables: fitted values of Rent_price 
## 
##         Test Summary         
##  ----------------------------
##  DF            =    1 
##  Chi2          =    1.563644 
##  Prob > Chi2   =    0.2111325

H0: Variance is constant.

H1: Variance is not constant.

BSD we cannot reject H0. Homoscedasticity is not violated.

OTHER ASSUMPTIONS:

  • Assumption of error independence is met, because each unit is observed only once, I do not have panel data.
  • The assumption of number of units being greater than the number of estimated parameters n>k is met.
summary(fit)
## 
## Call:
## lm(formula = Rent_price ~ lnSize + Parking, data = mydata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1218.67  -292.49   -48.95   251.54  1134.79 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2582.24     229.44  -11.26   <2e-16 ***
## lnSize        863.14      51.11   16.89   <2e-16 ***
## ParkingTrue   134.91      62.18    2.17   0.0311 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 372 on 216 degrees of freedom
## Multiple R-squared:  0.6378, Adjusted R-squared:  0.6345 
## F-statistic: 190.2 on 2 and 216 DF,  p-value: < 2.2e-16

Explanation of partial regression coefficients

H0:beta1 = 0

H1: beta1 =/ 0

BSD we reject H0 at p<0.001, lnSize has a statistically significant effect on rent.

lnSize = 863.14 => If the size of the real estate unit increases by 1%, the rent on average increases by 8.6314 euros (p<0.001), assuming parking remains unchanged.

H0:beta2 = 0

H1: beta2 =/ 0

BSD we reject H0 at p=0.04, parking has a statistically significant effect on rent.

Parking = 134.91 => Given the size, real estate units with parking have on average rent higher by 134.91 eur compared to real estate units without it (p=0.004).

Coefficient of determination

63.78% of variability of rent price can be explained by linear effect of natural logaithm of size (lnSize) and parking.

F- statistics

H0: beta1 = beta2 = 0

H1: At least one is different than 0.

We reject H0 at p<0.001 and conclude that at least one of the variables (lnSize, Parking) impacts the rent.

Coefficient of multiple correlation

sqrt(summary(fit)$r.squared)
## [1] 0.7986457

Relationship between rent, ln of size and parking is strong.

Conclusion

BSD we found that size and parking have a statistically significant effect on the rent.