Rishabh Kumar Raikwar

Research Question: What is the relationship between apartment prices and other variables such as size of apartment and neighbourhood ?

library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:Hmisc':
## 
##     describe
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:dplyr':
## 
##     recode
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
mydata <- read.csv("C:/Users/Rishabh/Documents/Homework 1/students/housing_price_dataset.csv" ,header = TRUE)
head(mydata)
##   ID SqFeet Bedrooms Neighborhood     Price
## 1 13   1327        5       Suburb  52612.49
## 2  4   1335        5       Suburb  96023.52
## 3 55   1531        5        Urban 103770.52
## 4 71   1951        5        Urban 108841.77
## 5 45   1382        5        Urban 110209.18
## 6 40   1048        5       Suburb 111288.54

Description of data set

Unit of observation: One apartment

Sample size: 80

ID: ID number

SqFeet: Size of apartment in square feet

Bedrooms: Number of bedrooms in apartment (Same for every apartment in the data)

Neighbourhood: Urban or Suburb

Price: Price of apartment in US Dollars

Data source: https://www.kaggle.com

#Creating factors
mydata$Neighbourhood_F <- factor(mydata$Neighborhood,
                        levels = c("Suburb","Urban"),
                        labels = c("Suburb","Urban"))
summary(mydata[,-c(1,3,4)])
##      SqFeet         Price        Neighbourhood_F
##  Min.   :1026   Min.   : 52613   Suburb:40      
##  1st Qu.:1612   1st Qu.:194659   Urban :40      
##  Median :1986   Median :247939                  
##  Mean   :1997   Mean   :243961                  
##  3rd Qu.:2483   3rd Qu.:292501                  
##  Max.   :2963   Max.   :420442

Explanotory Variables:

SqFeet:Area of apartment have a positive relationship with Price.

Neighbourhood_F: Apartment in Urban neighbourhood should be more costly than apartment in suburb.

Initial Regression function

price = α + β1 * SqFeet + β2 * Neighbourhood_F

where: ‘price’ is the price of the apartment, ‘α’ is the intercept, ‘β1’ is the coefficient for the ‘SqFeet’ variable,‘β2’ is the coefficient for the ‘Neighbourhood_F’ variable.

scatterplot(mydata$Price ~ mydata$SqFeet | mydata$Neighbourhood_F,
            smooth=FALSE,
            boxplots = FALSE,
            ylab = "Price in US Dollars",
            xlab= "Area of apartment in sq feet")

#### The scatterplot shows positive linear realationship between price and area of apartment.

fit <- lm(Price ~ SqFeet + Neighbourhood_F,
          data=mydata)
vif(fit)
##          SqFeet Neighbourhood_F 
##        1.002554        1.002554

VIF: All the values are almost equal to one, which means the fit does not have a problem with multi collinearity

# Checking outliers and unit with significant impact
mydata$StdResid <- round(rstandard(fit),3)
mydata$CooksD <- round(cooks.distance(fit),3)
hist(mydata$StdResid,
     xlab= "Standardized Residual",
     ylab= "Frequency",
     main = "Histogram showing standard residuals")

#### Since Standard Residuals are within +/- 3 , there are no outliers.

hist(mydata$CooksD,
     xlab= "Cooks Distance",
     ylab = "Frequency",
     main = "Histogram showing Cooks distance")

#### Gaps show unit with significant impact, they should be removed.

head(mydata[order(-mydata$CooksD),], 5)
##    ID SqFeet Bedrooms Neighborhood     Price Neighbourhood_F StdResid
## 49 61   1116        5        Urban 274360.47           Urban    2.219
## 1  13   1327        5       Suburb  52612.49          Suburb   -2.497
## 27 14   2750        5       Suburb 218974.55          Suburb   -1.886
## 80  2   2857        5       Suburb 420441.89          Suburb    1.667
## 60  8   1359        5       Suburb 292431.08          Suburb    1.866
##    CooksD
## 49  0.109
## 1   0.095
## 27  0.065
## 80  0.059
## 60  0.051
#Removing units with significant impact
removal <- c("13", "61","14","2")

mydata <- mydata %>%
  filter(!(ID %in% removal))
fit1 <- lm(Price ~ SqFeet + Neighbourhood_F,
           data = mydata)
mydata$StdResid <- round(rstandard(fit1), 3) 
mydata$CooksD <- round(cooks.distance(fit1), 3)

Checking Data again after removing units with significant impact

hist(mydata$CooksD,
     xlab= "Cooks Distance",
     ylab = "Frequency",
     main = "Histogram showing Cooks distance")

hist(mydata$StdResid,
     xlab= "Standardized Residual",
     ylab= "Frequency",
     main = "Histogram showing standard residuals")

#### Assumption about normality of standard residuals

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

H0: standard residuals are normally distributed

H1: standard residuals are not normally distributed

We cannot reject H0 at p value > 0.05

Homoscedasticity

mydata$StdFitted <- scale(fit1$fitted.values)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
            ylab = "Standardized residuals",
            xlab = "Standardized fitted values",
            boxplots = FALSE,
            regLine = FALSE,
            smooth = FALSE)

ols_test_breusch_pagan(fit1)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##               Data                
##  ---------------------------------
##  Response : Price 
##  Variables: fitted values of Price 
## 
##         Test Summary         
##  ----------------------------
##  DF            =    1 
##  Chi2          =    2.202206 
##  Prob > Chi2   =    0.1378134

We cannot reject H0 at p value > 0.05

All assumptions are met.

summary(fit1)
## 
## Call:
## lm(formula = Price ~ SqFeet + Neighbourhood_F, data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -121006  -31690    -620   28526   98197 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           65522.93   23607.01   2.776   0.0070 ** 
## SqFeet                   94.71      11.38   8.322 3.49e-12 ***
## Neighbourhood_FUrban -20453.98   11614.32  -1.761   0.0824 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 50340 on 73 degrees of freedom
## Multiple R-squared:  0.4899, Adjusted R-squared:  0.4759 
## F-statistic: 35.05 on 2 and 73 DF,  p-value: 2.141e-11

If we increase the the size of the apartment by 1 sq feet, the price on average increases by $94.71 assuming neighbourhood remains unchanged (p< 0.001)

We cannot say that neighbourhood of apartment affects the price (p > 0.05).

Multiple R-squared : 48.99% of variabilty in price of the apartment can be explained by variability in size of the apartment.

F-statistic H0: rho=0

H1: rho > 0

We reject H0 at p <0.001 and can say that this model explains some of the variability in the price of the apartment.