mydata <- read.table("./Housing.csv", header=TRUE, sep=",") #Importing dataset
mydata <- mydata[, c(1, 2, 7)] #Keeping variables I will use for regression
mydata$ID <- seq(1, nrow(mydata)) #Creating new variable called ID
set.seed(1)
mydata <- mydata[sample(nrow(mydata), 300), ] #Random sample of 300 units.
mydata$guestroom <- factor(mydata$guestroom,
levels = c("no", "yes"),
labels = c("no", "yes"))
head(mydata) #Showing 6 units
## price area guestroom ID
## 129 5873000 5500 yes 129
## 509 2590000 4400 no 509
## 471 3010000 5076 no 471
## 299 4200000 5800 no 299
## 270 4375000 3900 no 270
## 187 5110000 11410 no 187
EXPLAINING DATA: Unit of observation - one house Sample size - 300 units Variables: Price - price of the house expressed in dollars Area - the area of house in square feet guestroom - whether house has a guestroom
The data set was taken from kaggle.com, the data set is called Housing prices and has 545 units. I made a sample of 300 units.
summary(mydata)
## price area guestroom ID
## Min. : 1750000 Min. : 1836 no :248 Min. : 1.0
## 1st Qu.: 3386250 1st Qu.: 3518 yes: 52 1st Qu.:147.2
## Median : 4217500 Median : 4515 Median :288.5
## Mean : 4637405 Mean : 5084 Mean :283.0
## 3rd Qu.: 5600000 3rd Qu.: 6360 3rd Qu.:418.2
## Max. :13300000 Max. :15600 Max. :545.0
The median price is 4217500 usd, media area is 4515 square feet. There are 248 houses without guestroom and 52 houses with guestroom in the sample. Maximum price is 13300000 usd and minimum price is 1750000 usd. Minimum Area is 1836 square feet and maximum area is 15600 square feet.
In the regression model i will be analyzing the effect that size of a house (area) and having a guestroom has on the price of a house. I believe that a relationship will be positive so bigger area means higher prices and having a guestroom will also mean higher price.
ASSUMPTIONS: - Linearity in parameters (I WILL DO SCATTERPLOT) - Expected value of errors equals to 0 (WE INCLUDED ALL OF RELEVANT VARIABLES IN CORRECT FORM AND INCLUSION OF REGRESSION CONSTANT, SO WE ASSUME ASSAMPTION IS MET) - Homoskedasticity needs to be met - Errors are independent (MET - UNIT IS OBSERVED ONLY ONCE) - No perfect multiculinarity (MET BECAUSE THERE IS ONLY ONE EXPLANATORY NUMERICAL VARIABLE)
THERE ARE ALSO ADDITIONAL ASSUMPTIONS Normal Distribution of errors - even if violated we can ignore since our sample is big enough - 300 units. Number of units must be greater than a number of variables - this is met
ADDITIONAL REQUIREMENTS Dependent variable is numerical, explanatory variables are numerical or dichotomous. Explanatory varible must vary.
library(car)
## Loading required package: carData
scatterplot(price ~ area,
ylab = "Price of a house in €",
xlab = "Area of a house in square feet",
smooth = FALSE,
data = mydata)
LINEARITY is met as we can see in scatterplot.
fit <- lm(price ~ area,
data = mydata)
We will check homoscedasticity
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 : price
## Variables: fitted values of price
##
## Test Summary
## -------------------------------
## DF = 1
## Chi2 = 58.36665
## Prob > Chi2 = 2.175479e-14
H0: Homoscedasticity is met. H1: Homoscedasticity is violated. P value is less then 0.1%, so we reject H0. Homoscedasticity is violated. Since we rejected H0, I will later perform White’s robust correction.
mydata$StdFitted <- scale(fit$fitted.values)
library(car)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
We can see in the scatterplot that homoscedasticity is violated.
Checking distribution of errors
mydata$StdResid <- round(rstandard(fit), 3)
mydata$CooksD <- round(cooks.distance(fit), 3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
There are some units that fell out of +/- 3 range so those units will
have to be removed. We will preform Shapiro test to check the if errors
are normally distributed.
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.94419, p-value = 3.096e-09
H0:Errors are normally distributed. H1:Errors are not normally distributed.
We reject H0 since P value is less than 0.1%. Since we rejected H0 normality is violated, but we have big sample (300 units), so we can ignore the violation.
hist(mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histrogram of Cooks distances")
We will remove the units where gaps can be seen.
head(mydata[order(- mydata$StdResid),], 10)
## price area guestroom ID StdFitted StdResid CooksD
## 1 13300000 7420 no 1 1.09753485 4.955 0.091
## 2 12250000 8960 no 2 1.82118523 3.889 0.111
## 14 9240000 3500 no 14 -0.74448431 3.372 0.030
## 19 8890000 4600 yes 19 -0.22759118 2.856 0.014
## 24 8645000 4560 yes 24 -0.24638729 2.709 0.013
## 16 9100000 6000 no 16 0.43027281 2.623 0.014
## 13 9310000 6550 no 13 0.68871937 2.615 0.017
## 33 8295000 4880 no 33 -0.09601838 2.400 0.010
## 20 8855000 6420 no 20 0.62763200 2.357 0.013
## 15 9240000 7800 no 15 1.27609793 2.246 0.022
head(mydata[order(mydata$StdResid),], 10)
## price area guestroom ID StdFitted StdResid CooksD
## 404 3500000 12944 no 404 3.6932782 -2.858 0.210
## 126 5943000 15600 no 126 4.9413401 -2.005 0.187
## 212 4900000 12900 no 212 3.6726024 -1.926 0.094
## 402 3500000 9500 no 402 2.0749328 -1.902 0.033
## 474 3003000 8050 no 474 1.3935736 -1.833 0.017
## 461 3087000 8100 no 461 1.4170688 -1.792 0.016
## 545 1750000 3850 no 545 -0.5800183 -1.529 0.005
## 484 2940000 6615 no 484 0.7192631 -1.492 0.006
## 543 1750000 3620 no 543 -0.6880960 -1.469 0.005
## 355 3780000 8400 no 355 1.5580396 -1.426 0.012
I would remove units 1,2 14 since STD residuals lie outside +/- 3 range.
head(mydata[order(- mydata$CooksD),], 10)
## price area guestroom ID StdFitted StdResid CooksD
## 404 3500000 12944 no 404 3.6932782 -2.858 0.210
## 126 5943000 15600 no 126 4.9413401 -2.005 0.187
## 2 12250000 8960 no 2 1.8211852 3.889 0.111
## 212 4900000 12900 no 212 3.6726024 -1.926 0.094
## 1 13300000 7420 no 1 1.0975348 4.955 0.091
## 402 3500000 9500 no 402 2.0749328 -1.902 0.033
## 187 5110000 11410 no 187 2.9724472 -1.377 0.032
## 14 9240000 3500 no 14 -0.7444843 3.372 0.030
## 15 9240000 7800 no 15 1.2760979 2.246 0.022
## 225 4760000 10240 no 225 2.4226609 -1.287 0.019
We will also remove 404, 126, 2, 212, 1 - since there are gaps between those units in terms of Cooks Distances.
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 %in% c(1, 2, 14, 404, 126, 212))
Since homoscedasticity is violated I will do White’s robust correction. First I will include only Area and then I will add Guestrooms into the formula.
library(estimatr)
## Warning: package 'estimatr' was built under R version 4.3.2
fit1 <- lm_robust(price ~ area,
se_type = "HC1",
data = mydata)
summary(fit1)
##
## Call:
## lm_robust(formula = price ~ area, data = mydata, se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 2279576 210125.37 10.849 2.904e-23 1866024.2 2693129 292
## area 459 46.79 9.809 8.089e-20 366.9 551 292
##
## Multiple R-squared: 0.2903 , Adjusted R-squared: 0.2878
## F-statistic: 96.22 on 1 and 292 DF, p-value: < 2.2e-16
sqrt(summary(fit1)$r.squared)
## [1] 0.5387636
H0: Ro^2 is equal to 0. H1: Ro^2 is greater than 0.
I reject H0 at P value 2.2e-16. Data shows that there is at least some impact of area to the price of the house.
Now I will explain some coefficients. Estimated coefficient is 459. That means that if area increases by 1 square feet, then the price of a house on average increases by 459 dollars. Multiple R-Squared is 0.2903. That means that 29.03 % of variability of the price of a house can be explained by variability of area.
Now I will include also guestroom as a categorical varaible.
fit2 <- lm_robust(price ~ area + guestroom,
data = mydata)
summary(fit2)
##
## Call:
## lm_robust(formula = price ~ area + guestroom, data = mydata)
##
## Standard error type: HC2
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) 2281340.5 198021.57 11.521 1.493e-25 1891604.4 2671076.5 291
## area 417.2 46.55 8.963 3.923e-17 325.6 508.8 291
## guestroomyes 1166213.3 210571.85 5.538 6.831e-08 751776.4 1580650.2 291
##
## Multiple R-squared: 0.3612 , Adjusted R-squared: 0.3568
## F-statistic: 78.75 on 2 and 291 DF, p-value: < 2.2e-16
sqrt(summary(fit2)$r.squared)
## [1] 0.6009651
H0: Ro^2 is equal to 0. H1: Ro^2 is greater than 0.
I reject H0 at p value 2.2e-16. I assume that area of the house and having or not having a guestroom have some impact on the price of the house.
Estimated coefficients: area - 417.2. If area increases by 1 square feet, price of a house increases on average by 417.2 dollars. Given the area, houses that have a guestroom have on average higher price by 1166213.3 dollars, compared to the houses without guestroom. Multiple R squared is 0.3612 - 36.12 of variability of price can be explained by the variability of area and having or not having a guestroom.
COMPARING MODELS
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.3.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
waldtest(fit1, fit2, test = "F")
## Wald test
##
## Model 1: price ~ area
## Model 2: price ~ area + guestroom
## Res.Df Df F Pr(>F)
## 1 292
## 2 291 1 30.673 6.831e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
H0: Delta Ro^2 is equal to 0. H1: Delta Ro^2 is greater than 0.
Based on calculated P value (6.831e-08), I reject H0. Model with area and guestroom is better than model where only area is included.
After conducting the analysis I can assume that area of the house and whetever the house has a guestroom can explain the differences in the prices. Calculated P value was less than 0.01% (2.2e-16) so we reject H0 (Ro^2 is equal to 0).