library(readxl)
mydata <- read_excel("~/Desktop/Apartments.xlsx")
View(mydata)
mydata<-as.data.frame(mydata)
head(mydata)
## Age Distance Price Parking Balcony
## 1 7 28 1640 0 1
## 2 18 1 2800 1 0
## 3 7 28 1660 0 0
## 4 28 29 1850 0 1
## 5 18 18 1640 1 1
## 6 28 12 1770 0 1
mydata$ParkingF<-factor(mydata$Parking, levels = c(1,0), labels = c("Parking","No parking"))
mydata$BalconyF<-factor(mydata$Balcony, levels = c(1,0), labels = c("Balcony","No balcony"))
head(mydata)
## Age Distance Price Parking Balcony ParkingF BalconyF
## 1 7 28 1640 0 1 No parking Balcony
## 2 18 1 2800 1 0 Parking No balcony
## 3 7 28 1660 0 0 No parking No balcony
## 4 28 29 1850 0 1 No parking Balcony
## 5 18 18 1640 1 1 Parking Balcony
## 6 28 12 1770 0 1 No parking Balcony
t.test(mydata$Price, mu = 1900, alternative = "two.sided")
##
## One Sample t-test
##
## data: mydata$Price
## t = 2.9022, df = 84, p-value = 0.004731
## alternative hypothesis: true mean is not equal to 1900
## 95 percent confidence interval:
## 1937.443 2100.440
## sample estimates:
## mean of x
## 2018.941
#The mean is not equal to 1900, the mean of Price is 2018.941
fit1<-lm(Price~Age,data = mydata)
summary(fit1)
##
## Call:
## lm(formula = Price ~ Age, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -623.9 -278.0 -69.8 243.5 776.1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2185.455 87.043 25.108 <2e-16 ***
## Age -8.975 4.164 -2.156 0.034 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 369.9 on 83 degrees of freedom
## Multiple R-squared: 0.05302, Adjusted R-squared: 0.04161
## F-statistic: 4.647 on 1 and 83 DF, p-value: 0.03401
#The estimate of the regression coefficient for the predictor variable “Age” is -8.975. This means that for each unit increase in the Age of an apartment, we expect the Price of the apartment to decrease by approximately $8.975, holding all other variables constant.Since p-value =0.034 (p<0.05), the regression coefficient is stattistically significant
#The coefficient of determination is 0.05302. This indicates that only 5.203% of total variability of Price can be explained by independant variable - Age. Since p-value<0.05, we can state that the coefficient of determination is greater than 0.
sqrt(summary(fit1)$r.squared)
## [1] 0.230255
#The linear correlation between age and price is direct and equals to 23.0255%
library(car)
## Loading required package: carData
scatterplotMatrix(mydata[c("Price","Age","Distance")], smooth = FALSE)
#No of the scateplots whosing the relationship between explanatory
variables show strong relationship betweenn them, so it can be stated
that there is no multicolinearity.
fit2<-lm(Price~Age + Distance,data = mydata)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -603.23 -219.94 -85.68 211.31 689.58
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2460.101 76.632 32.10 < 2e-16 ***
## Age -7.934 3.225 -2.46 0.016 *
## Distance -20.667 2.748 -7.52 6.18e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 286.3 on 82 degrees of freedom
## Multiple R-squared: 0.4396, Adjusted R-squared: 0.4259
## F-statistic: 32.16 on 2 and 82 DF, p-value: 4.896e-11
vif(fit2)
## Age Distance
## 1.001845 1.001845
mean(vif(fit2))
## [1] 1.001845
#Since vif is close to 1, there is no strong correlation between explanatory variables, so no multicolinearity
mydata$StResid<-round(rstandard(fit2),3)
mydata$StResid
## [1] -0.665 1.783 -0.594 0.754 -1.073 -0.778 -0.302 -0.787 0.455 1.241 1.551 -0.353 -1.499 -0.469 0.292 -1.000
## [17] -0.645 0.577 -0.812 -1.381 -0.371 1.576 -0.283 -0.440 1.571 0.628 1.550 -1.071 -0.516 0.256 0.889 0.454
## [33] 2.051 -0.663 -1.261 -1.189 -1.005 2.577 1.091 0.279 -1.052 -0.103 0.252 0.117 -0.329 1.478 -0.053 -1.009
## [49] 0.747 -0.683 0.393 -0.861 -2.152 -1.101 1.445 -0.328 1.601 1.655 -0.550 0.333 1.783 -0.594 0.754 -1.073
## [65] -0.778 -0.302 -0.787 0.455 1.241 1.551 -0.353 -1.499 -0.469 0.292 -1.000 -0.645 -0.329 1.478 -0.053 -1.009
## [81] 0.747 -0.683 0.393 -0.861 -0.053
mydata$CooksD<-round(cooks.distance(fit2),3)
mydata$CooksD
## [1] 0.007 0.030 0.006 0.008 0.005 0.005 0.001 0.004 0.001 0.017 0.020 0.001 0.013 0.002 0.001 0.006 0.004 0.003 0.010
## [20] 0.012 0.002 0.061 0.001 0.001 0.034 0.004 0.023 0.005 0.003 0.001 0.030 0.002 0.069 0.005 0.008 0.012 0.007 0.320
## [39] 0.038 0.002 0.005 0.000 0.001 0.000 0.003 0.019 0.000 0.022 0.004 0.003 0.004 0.003 0.066 0.012 0.104 0.000 0.032
## [58] 0.037 0.001 0.001 0.030 0.006 0.008 0.005 0.005 0.001 0.004 0.001 0.017 0.020 0.001 0.013 0.002 0.001 0.006 0.004
## [77] 0.003 0.019 0.000 0.022 0.004 0.003 0.004 0.003 0.000
head(mydata[order(-mydata$CooksD), c("Price","CooksD")], 6)
## Price CooksD
## 38 2180 0.320
## 55 1740 0.104
## 33 2790 0.069
## 53 1760 0.066
## 22 2540 0.061
## 39 2400 0.038
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_0<-mydata %>%
filter(!Price %in% c(2180,1740,2790,1760,2540,2400))
mydata$StdFittedValues<-scale(fit2$fitted.values)
library(car)
scatterplot(y=mydata$StResid, x=mydata$StdFittedValues, ylab = "Standardized residuals", xlab = "Standardized fitted values", boxplots = FALSE, regLine = FALSE, smooth = FALSE)
#There is no heteroskedasticity
mydata$StResid<-round(rstandard(fit2),3)
hist(mydata$StResid, xlab = "Standardized residuals", ylab = "Frequency", main = "Histogram of standardized residuals")
shapiro.test(mydata$StResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StResid
## W = 0.95303, p-value = 0.003645
#Standard residuals are not normally distributed
fit2<-lm(Price~Age + Distance,data = mydata)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -603.23 -219.94 -85.68 211.31 689.58
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2460.101 76.632 32.10 < 2e-16 ***
## Age -7.934 3.225 -2.46 0.016 *
## Distance -20.667 2.748 -7.52 6.18e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 286.3 on 82 degrees of freedom
## Multiple R-squared: 0.4396, Adjusted R-squared: 0.4259
## F-statistic: 32.16 on 2 and 82 DF, p-value: 4.896e-11
#The estimate of the regression coefficient for the predictor variable “Age” is -9.793. This means that for each unit increase in the Age of an apartment, we expect the Price of the apartment to decrease by approximately $9.793, holding all other variables constant.Since p-value <0.05, the regression coefficient is stattistically significant
#The estimate of the regression coefficient for the predictor variable “Age” is -23.604. This means that for each unit increase in the Age of an apartment, we expect the Price of the apartment to decrease by approximately $23.604, holding all other variables constant.Since p-value <0.05, the regression coefficient is stattistically significant
#The coefficient of determination is 0.5382. This indicates that only 53.82% of total variability of Price can be explained by independant variables - Age and Distance. Since p-value<0.05, we can state that the coefficient of determination is greater than 0.
sqrt(summary(fit2)$r.squared)
## [1] 0.6629887
#The linear correlation between independant variables and dependant ones is direct and equals to 73.366458%
fit3<-lm(Price~Age + Distance + ParkingF + BalconyF,data = mydata)
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingF + BalconyF, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -459.92 -200.66 -57.48 260.08 594.37
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2499.770 80.645 30.997 < 2e-16 ***
## Age -6.799 3.110 -2.186 0.03172 *
## Distance -18.045 2.758 -6.543 5.28e-09 ***
## ParkingFNo parking -196.168 62.868 -3.120 0.00251 **
## BalconyFNo balcony -1.935 60.014 -0.032 0.97436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 273.7 on 80 degrees of freedom
## Multiple R-squared: 0.5004, Adjusted R-squared: 0.4754
## F-statistic: 20.03 on 4 and 80 DF, p-value: 1.849e-11
anova(fit2,fit3)
## Analysis of Variance Table
##
## Model 1: Price ~ Age + Distance
## Model 2: Price ~ Age + Distance + ParkingF + BalconyF
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 82 6720983
## 2 80 5991088 2 729894 4.8732 0.01007 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Model2 improves the fit compared to Model 1 by the decrease in RSS. Withe the confidence level of 90%, we can state that the second model fits the data better (p<0.1)
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingF + BalconyF, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -459.92 -200.66 -57.48 260.08 594.37
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2499.770 80.645 30.997 < 2e-16 ***
## Age -6.799 3.110 -2.186 0.03172 *
## Distance -18.045 2.758 -6.543 5.28e-09 ***
## ParkingFNo parking -196.168 62.868 -3.120 0.00251 **
## BalconyFNo balcony -1.935 60.014 -0.032 0.97436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 273.7 on 80 degrees of freedom
## Multiple R-squared: 0.5004, Adjusted R-squared: 0.4754
## F-statistic: 20.03 on 4 and 80 DF, p-value: 1.849e-11
#Apartments with no parking facilities tend to have by $142.085 lower prices compared to those with parking facilities.
#Apartments with no balcony tend to have by $7.028 higher prices than those with
#Alternative Hypothesis (H1): There is a significant linear relationship between the explanatory variables and the dependant variable.
fitted_values <- fitted(fit3)