Our aim is to predict house values. Before we begin to do any analysis, we should always check whether the dataset has missing value or not, we do so by typing:
taiwan_real_estate <- read.csv("real_estates.csv",row.names=1)
attach(taiwan_real_estate)
any(is.na(taiwan_real_estate))
## [1] FALSE
Let’s take a look at structure of the data set:
glimpse(taiwan_real_estate)
## Rows: 414
## Columns: 6
## $ house.age <dbl> 32.0, 19.5, 13.3, 13.3, 5.0, 7.1, …
## $ distance.to.the.nearest.MRT.station <dbl> 84.87882, 306.59470, 561.98450, 56…
## $ number.of.convenience.stores <int> 10, 9, 5, 5, 5, 3, 7, 6, 1, 3, 1, …
## $ latitude <dbl> 24.98298, 24.98034, 24.98746, 24.9…
## $ longitude <dbl> 121.5402, 121.5395, 121.5439, 121.…
## $ house.price.of.unit.area <dbl> 37.9, 42.2, 47.3, 54.8, 43.1, 32.1…
Let’s simplify variables’ names:
taiwan_real_estate <- taiwan_real_estate %>%
rename(house_age_years = house.age, price_twd_msq = house.price.of.unit.area,
n_convenience = number.of.convenience.stores,
dist_to_mrt_m = distance.to.the.nearest.MRT.station)
We can also perform binning for “house_age_years”:
#perform binning with specific number of bins
taiwan_real_estate<-taiwan_real_estate %>% mutate(house_age_cat = cut(house_age_years, breaks=c(0,15,30,45),include.lowest = T,
right = F))
Prepare a heatmap with correlation coefficients on it:
library(corrplot)
## corrplot 0.92 loaded
M<-cor(taiwan_real_estate[,1:6])
corrplot(M, method = 'number') # colorful number
Draw a scatter plot of n_convenience vs. price_twd_msq:
ggplot(taiwan_real_estate, aes(x = n_convenience, y = price_twd_msq)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "blue") +
labs(title = "Scatter plot of n_convenience vs. price_twd_msq with Trend Line",
x = "Number of Convenience Stores",
y = "House Price of Unit Area")
## `geom_smooth()` using formula = 'y ~ x'
Draw a scatter plot of house_age_years vs. price_twd_msq:
ggplot(taiwan_real_estate, aes(x = house_age_years, y = price_twd_msq)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "blue") +
labs(title = "Scatter plot of house_age_years vs. price_twd_msq with Trend Line",
x = "House Age (years)",
y = "House Price of Unit Area")
## `geom_smooth()` using formula = 'y ~ x'
Draw a scatter plot of distance to nearest MRT station vs. price_twd_msq:
Plot a histogram of price_twd_msq with 10 bins, facet the plot so each house age group gets its own panel:
ggplot(taiwan_real_estate, aes(x = price_twd_msq)) +
geom_histogram(bins = 10, fill = "blue", color = "black") +
facet_wrap(~house_age_cat) +
labs(title = "Histogram of Price per Square Meter Faceted by House Age Group",
x = "House Price of Unit Area",
y = "Frequency")
Summarize to calculate the mean, sd, median etc. house price/area by house age:
summary_stats <- taiwan_real_estate %>%
group_by(house_age_cat) %>%
summarise(
mean_price = mean(price_twd_msq, na.rm = TRUE),
sd_price = sd(price_twd_msq, na.rm = TRUE),
median_price = median(price_twd_msq, na.rm = TRUE),
min_price = min(price_twd_msq, na.rm = TRUE),
max_price = max(price_twd_msq, na.rm = TRUE),
count = n()
)
print(summary_stats)
## # A tibble: 3 × 7
## house_age_cat mean_price sd_price median_price min_price max_price count
## <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 [0,15) 41.8 14.2 42.6 7.6 118. 190
## 2 [15,30) 32.6 11.4 32.9 11.2 59.6 129
## 3 [30,45] 37.7 12.8 38.3 12.2 78.3 95
Run a linear regression of price_twd_msq vs. best, but only 1 predictor (We will compare 2 of them for analysis):
model_n_convenience <- lm(price_twd_msq ~ n_convenience, data = taiwan_real_estate)
summary_n_convenience <- summary(model_n_convenience)
model_dist_to_mrt_m <- lm(price_twd_msq ~ dist_to_mrt_m, data = taiwan_real_estate)
summary_dist_to_mrt_m <- summary(model_dist_to_mrt_m)
comparison <- data.frame(
Model = c("n_convenience", "dist_to_mrt_m"),
Coefficient = c(summary_n_convenience$coefficients[2, 1], summary_dist_to_mrt_m$coefficients[2, 1]),
Std_Error = c(summary_n_convenience$coefficients[2, 2], summary_dist_to_mrt_m$coefficients[2, 2]),
t_value = c(summary_n_convenience$coefficients[2, 3], summary_dist_to_mrt_m$coefficients[2, 3]),
R_squared = c(summary_n_convenience$r.squared, summary_dist_to_mrt_m$r.squared),
Adj_R_squared = c(summary_n_convenience$adj.r.squared, summary_dist_to_mrt_m$adj.r.squared)
)
print(comparison) # The best will be dist_to_mrt_m
## Model Coefficient Std_Error t_value R_squared Adj_R_squared
## 1 n_convenience 2.637653463 0.1868289689 14.11801 0.3260466 0.3244108
## 2 dist_to_mrt_m -0.007262052 0.0003925495 -18.49971 0.4537543 0.4524284
We start by displaying the statistical summary of the model using the R function summary():
summary(model_dist_to_mrt_m) # Printing that table
##
## Call:
## lm(formula = price_twd_msq ~ dist_to_mrt_m, data = taiwan_real_estate)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.396 -6.007 -1.195 4.831 73.483
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.8514271 0.6526105 70.26 <2e-16 ***
## dist_to_mrt_m -0.0072621 0.0003925 -18.50 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.07 on 412 degrees of freedom
## Multiple R-squared: 0.4538, Adjusted R-squared: 0.4524
## F-statistic: 342.2 on 1 and 412 DF, p-value: < 2.2e-16
You can access lots of different aspects of the regression object. To see what’s inside, use names():
names(model_dist_to_mrt_m)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
Inside the model we can see that there are the necessary elements within the model to analyze and interpret the regression results.
Considering the summary results above and based on the R-squared value (0.4538), the dist_to_mrt_m predictor model explains about 45.38% of the variation in price per square meter, indicating moderate prediction accuracy.
Model diagnostics:
par(mfrow = c(2, 2))
plot(model_dist_to_mrt_m)
Let’s briefly go through each chart from top to bottom and left to right to describe shorthand what we see.
Residuals vs Fitted: we see that almost most points are scattered around the line, indicating possible nonlinearity or heteroscedasticity.
Q-Q Residuals: we can easily notice that the beginning and the end have deviations from the diagonal line, indicating non-normality of the residuals distribution.
Scale-Location: in this graph, you can see that the points are scattered quite randomly, although there are some right on the line. Despite these some points, we can say that we have a constant variance.
Residuals vs Leverage: here we can see a few points with high leverage and large residuals, in particular point 271, which is a significant outlier and has a large impact on the model. Such points can highly distort the model results and require attention.
To summarize, the graphs show possible problems with heteroskedasticity and non-normality of residuals, as well as indicate the presence of outliers that may affect the model.
Create the diagnostic plots using ggfortify:
library(ggfortify)
autoplot(model_dist_to_mrt_m)
Outliers and high levarage points:
plot(model_dist_to_mrt_m, 5)
Influential values:
# Cook's distance
plot(model_dist_to_mrt_m, 4)
or just plot all of diagnostic plots together:
autoplot(model_dist_to_mrt_m, which = 1:6, label.size = 3)
Discussion:
Cook’s Distance: We can see in the graph that points 271, 250, and 149 have the highest Cook’s distance value, indicating that they have a significant impact on model fit.
Cook’s Distance vs Leverage: Again, points 271, 149 and 250 stand out as having high values of leverage and Cook’s distance, confirming their significant influence on the model.
It is noticeable in the graphs that points 271, 250 and 149 have high values of Cook’s distance and leverage, indicating their significant influence on the model.
We begin by splitting the dataset into two parts, training set and testing set. In this example we will randomly take 75% row in this dataset and put it into the training set, and other 25% row in the testing set:
smp_size<-floor(0.75*nrow(taiwan_real_estate))
set.seed(12)
train_ind<-sample(seq_len(nrow(taiwan_real_estate)), size=smp_size)
train<-taiwan_real_estate[train_ind, ]
test<-taiwan_real_estate[-train_ind, ]
1st comment: floor() is used to return the largest integer value which is not greater than an individual number, or expression.
2nd comment: set.seed() is used to set the seed of R’s random number generator, this function is used so results from this example can be recreated easily.
Now we have our training set and testing set.
Generally, selecting variables for linear regression is a debatable topic.
There are many methods for variable selecting, namely, forward stepwise selection, backward stepwise selection, etc, some are valid, some are heavily criticized.
I recommend this document: https://www.stat.cmu.edu/~cshalizi/mreg/15/lectures/26/lecture-26.pdf and Gung’s comment: https://stats.stackexchange.com/questions/20836/algorithms-for-automatic-model-selection/20856#20856 if you want to learn more about variable selection process.
If our goal is prediction, it is safer to include all predictors in our model, removing variables without knowing the science behind it usually does more harm than good!!!
We begin to create our multiple linear regression model:
model2 <- lm(price_twd_msq ~ ., data = train)
summary(model2)
##
## Call:
## lm(formula = price_twd_msq ~ ., data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -34.009 -4.953 -1.296 4.461 75.042
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.861e+03 7.542e+03 -0.379 0.704717
## house_age_years -4.209e-01 1.223e-01 -3.442 0.000659 ***
## dist_to_mrt_m -4.558e-03 8.681e-04 -5.251 2.86e-07 ***
## n_convenience 9.826e-01 2.287e-01 4.297 2.34e-05 ***
## latitude 2.505e+02 5.375e+01 4.660 4.74e-06 ***
## longitude -2.755e+01 6.032e+01 -0.457 0.648221
## house_age_cat[15,30) -1.089e+00 1.916e+00 -0.568 0.570124
## house_age_cat[30,45] 5.789e+00 3.577e+00 1.618 0.106655
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.331 on 302 degrees of freedom
## Multiple R-squared: 0.5676, Adjusted R-squared: 0.5575
## F-statistic: 56.62 on 7 and 302 DF, p-value: < 2.2e-16
We can observe from the important aspects that our model explains about 56.76% of the variation of price_twd_msq. The influence of house_age_years, dist_to_mrt_m, n_convenience and latitude variables are significant while the influence of longitude variables and house age categories are insignificant. The standard error of the residuals shows the average deviation of the predicted values from the actual values.
Looking at model summary, we see that variables Intercept, longitude, house_age_cat[15,30) and house_age_cat[30,45] are insignificant, so let’s estimate the model without those variables:
model3 <- lm(price_twd_msq ~ house_age_years + dist_to_mrt_m + n_convenience + latitude, data = train)
summary(model3)
##
## Call:
## lm(formula = price_twd_msq ~ house_age_years + dist_to_mrt_m +
## n_convenience + latitude, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -34.782 -5.448 -1.616 4.269 75.310
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.079e+03 1.356e+03 -4.481 1.05e-05 ***
## house_age_years -2.546e-01 4.762e-02 -5.347 1.76e-07 ***
## dist_to_mrt_m -4.692e-03 5.988e-04 -7.837 7.78e-14 ***
## n_convenience 1.006e+00 2.303e-01 4.367 1.73e-05 ***
## latitude 2.452e+02 5.432e+01 4.514 9.10e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.459 on 305 degrees of freedom
## Multiple R-squared: 0.5513, Adjusted R-squared: 0.5454
## F-statistic: 93.67 on 4 and 305 DF, p-value: < 2.2e-16
There are many standards researchers apply for deciding whether a VIF is too large. In some domains, a VIF over 2 is worthy of suspicion. Others set the bar higher, at 5 or 10. Others still will say you shouldn’t pay attention to these at all. Ultimately, the main thing to consider is that small effects are more likely to be “drowned out” by higher VIFs, but this may just be a natural, unavoidable fact with your model.
## house_age_years dist_to_mrt_m n_convenience latitude
## 1.021076 1.965314 1.623361 1.513424
## Variables with VIF > 5 may indicate multicollinearity issues.
Finally we test our model on test dataset:
## Mean Absolute Error (MAE): 5.752221
## Mean Squared Error (MSE): 54.56405
## Root Mean Squared Error (RMSE): 7.386748
We can observe that the model tells us relatively accurately about prices and their approximate changes, but there are some outliers, especially for low price values, where the predicted values differ significantly from the actual values. Overall, we see a positive correlation between the predicted and actual values, indicating that the model is capturing the underlying trend in the data quite well.
We have acceptable prediction accuracy, however, there are outliers that indicate the need to further improve our model.
Best subset and stepwise (forward, backward, both) techniques of variable selection can be used to come up with the best linear regression model for the dependent variable medv.
## Subset selection object
## Call: regsubsets.formula(price_twd_msq ~ ., data = train, nvmax = NULL)
## 7 Variables (and intercept)
## Forced in Forced out
## house_age_years FALSE FALSE
## dist_to_mrt_m FALSE FALSE
## n_convenience FALSE FALSE
## latitude FALSE FALSE
## longitude FALSE FALSE
## house_age_cat[15,30) FALSE FALSE
## house_age_cat[30,45] FALSE FALSE
## 1 subsets of each size up to 7
## Selection Algorithm: exhaustive
## house_age_years dist_to_mrt_m n_convenience latitude longitude
## 1 ( 1 ) " " "*" " " " " " "
## 2 ( 1 ) " " "*" "*" " " " "
## 3 ( 1 ) "*" "*" " " "*" " "
## 4 ( 1 ) "*" "*" "*" "*" " "
## 5 ( 1 ) "*" "*" "*" "*" " "
## 6 ( 1 ) "*" "*" "*" "*" " "
## 7 ( 1 ) "*" "*" "*" "*" "*"
## house_age_cat[15,30) house_age_cat[30,45]
## 1 ( 1 ) " " " "
## 2 ( 1 ) " " " "
## 3 ( 1 ) " " " "
## 4 ( 1 ) " " " "
## 5 ( 1 ) " " "*"
## 6 ( 1 ) "*" "*"
## 7 ( 1 ) "*" "*"
## Start: AIC=1638.47
## price_twd_msq ~ 1
##
## Df Sum of Sq RSS AIC
## + dist_to_mrt_m 1 27754.0 33057 1451.5
## + n_convenience 1 18298.4 42512 1529.5
## + longitude 1 17543.7 43267 1535.0
## + latitude 1 16685.2 44125 1541.0
## + house_age_cat 2 5118.7 55692 1615.2
## + house_age_years 1 2037.6 58773 1629.9
## <none> 60811 1638.5
##
## Step: AIC=1451.52
## price_twd_msq ~ dist_to_mrt_m
##
## Df Sum of Sq RSS AIC
## + n_convenience 1 1821.6 31235 1435.9
## + latitude 1 1793.8 31263 1436.2
## + house_age_years 1 1773.7 31283 1436.4
## + house_age_cat 2 1666.5 31390 1439.5
## <none> 33057 1451.5
## + longitude 1 27.3 33029 1453.3
##
## Step: AIC=1435.94
## price_twd_msq ~ dist_to_mrt_m + n_convenience
##
## Df Sum of Sq RSS AIC
## + house_age_years 1 2123.9 29111 1416.1
## + house_age_cat 2 2113.2 29122 1418.2
## + latitude 1 1388.9 29846 1423.8
## <none> 31235 1435.9
## + longitude 1 6.3 31229 1437.9
##
## Step: AIC=1416.11
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years
##
## Df Sum of Sq RSS AIC
## + latitude 1 1822.97 27288 1398.1
## + house_age_cat 2 871.12 28240 1410.7
## <none> 29111 1416.1
## + longitude 1 32.98 29078 1417.8
##
## Step: AIC=1398.07
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude
##
## Df Sum of Sq RSS AIC
## + house_age_cat 2 972.81 26315 1390.8
## <none> 27288 1398.1
## + longitude 1 8.02 27280 1400.0
##
## Step: AIC=1390.81
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## + longitude 1 18.161 26297 1392.6
## Start: AIC=1392.6
## price_twd_msq ~ house_age_years + dist_to_mrt_m + n_convenience +
## latitude + longitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## - longitude 1 18.16 26315 1390.8
## <none> 26297 1392.6
## - house_age_cat 2 982.95 27280 1400.0
## - house_age_years 1 1031.51 27329 1402.5
## - n_convenience 1 1607.54 27905 1409.0
## - latitude 1 1891.31 28189 1412.1
## - dist_to_mrt_m 1 2400.71 28698 1417.7
##
## Step: AIC=1390.81
## price_twd_msq ~ house_age_years + dist_to_mrt_m + n_convenience +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## - house_age_cat 2 972.8 27288 1398.1
## - house_age_years 1 1035.7 27351 1400.8
## - n_convenience 1 1625.4 27941 1407.4
## - latitude 1 1924.7 28240 1410.7
## - dist_to_mrt_m 1 4348.8 30664 1436.2
## Start: AIC=1638.47
## price_twd_msq ~ 1
##
## Df Sum of Sq RSS AIC
## + dist_to_mrt_m 1 27754.0 33057 1451.5
## + n_convenience 1 18298.4 42512 1529.5
## + longitude 1 17543.7 43267 1535.0
## + latitude 1 16685.2 44125 1541.0
## + house_age_cat 2 5118.7 55692 1615.2
## + house_age_years 1 2037.6 58773 1629.9
## <none> 60811 1638.5
##
## Step: AIC=1451.52
## price_twd_msq ~ dist_to_mrt_m
##
## Df Sum of Sq RSS AIC
## + n_convenience 1 1821.6 31235 1435.9
## + latitude 1 1793.8 31263 1436.2
## + house_age_years 1 1773.7 31283 1436.4
## + house_age_cat 2 1666.5 31390 1439.5
## <none> 33057 1451.5
## + longitude 1 27.3 33029 1453.3
## - dist_to_mrt_m 1 27754.0 60811 1638.5
##
## Step: AIC=1435.94
## price_twd_msq ~ dist_to_mrt_m + n_convenience
##
## Df Sum of Sq RSS AIC
## + house_age_years 1 2123.9 29111 1416.1
## + house_age_cat 2 2113.2 29122 1418.2
## + latitude 1 1388.9 29846 1423.8
## <none> 31235 1435.9
## + longitude 1 6.3 31229 1437.9
## - n_convenience 1 1821.6 33057 1451.5
## - dist_to_mrt_m 1 11277.2 42512 1529.5
##
## Step: AIC=1416.11
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years
##
## Df Sum of Sq RSS AIC
## + latitude 1 1823.0 27288 1398.1
## + house_age_cat 2 871.1 28240 1410.7
## <none> 29111 1416.1
## + longitude 1 33.0 29078 1417.8
## - house_age_years 1 2123.9 31235 1435.9
## - n_convenience 1 2171.8 31283 1436.4
## - dist_to_mrt_m 1 10564.4 39676 1510.1
##
## Step: AIC=1398.07
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude
##
## Df Sum of Sq RSS AIC
## + house_age_cat 2 972.8 26315 1390.8
## <none> 27288 1398.1
## + longitude 1 8.0 27280 1400.0
## - n_convenience 1 1706.1 28994 1414.9
## - latitude 1 1823.0 29111 1416.1
## - house_age_years 1 2557.9 29846 1423.8
## - dist_to_mrt_m 1 5494.4 32783 1452.9
##
## Step: AIC=1390.81
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## + longitude 1 18.2 26297 1392.6
## - house_age_cat 2 972.8 27288 1398.1
## - house_age_years 1 1035.7 27351 1400.8
## - n_convenience 1 1625.4 27941 1407.4
## - latitude 1 1924.7 28240 1410.7
## - dist_to_mrt_m 1 4348.8 30664 1436.2
##
## Call:
## lm(formula = price_twd_msq ~ dist_to_mrt_m + n_convenience +
## house_age_years + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
##
## Call:
## lm(formula = price_twd_msq ~ house_age_years + dist_to_mrt_m +
## n_convenience + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
##
## Call:
## lm(formula = price_twd_msq ~ dist_to_mrt_m + n_convenience +
## house_age_years + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
## Start: AIC=1638.47
## price_twd_msq ~ 1
##
## Df Sum of Sq RSS AIC
## + dist_to_mrt_m 1 27754.0 33057 1451.5
## + n_convenience 1 18298.4 42512 1529.5
## + longitude 1 17543.7 43267 1535.0
## + latitude 1 16685.2 44125 1541.0
## + house_age_cat 2 5118.7 55692 1615.2
## + house_age_years 1 2037.6 58773 1629.9
## <none> 60811 1638.5
##
## Step: AIC=1451.52
## price_twd_msq ~ dist_to_mrt_m
##
## Df Sum of Sq RSS AIC
## + n_convenience 1 1821.6 31235 1435.9
## + latitude 1 1793.8 31263 1436.2
## + house_age_years 1 1773.7 31283 1436.4
## + house_age_cat 2 1666.5 31390 1439.5
## <none> 33057 1451.5
## + longitude 1 27.3 33029 1453.3
##
## Step: AIC=1435.94
## price_twd_msq ~ dist_to_mrt_m + n_convenience
##
## Df Sum of Sq RSS AIC
## + house_age_years 1 2123.9 29111 1416.1
## + house_age_cat 2 2113.2 29122 1418.2
## + latitude 1 1388.9 29846 1423.8
## <none> 31235 1435.9
## + longitude 1 6.3 31229 1437.9
##
## Step: AIC=1416.11
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years
##
## Df Sum of Sq RSS AIC
## + latitude 1 1822.97 27288 1398.1
## + house_age_cat 2 871.12 28240 1410.7
## <none> 29111 1416.1
## + longitude 1 32.98 29078 1417.8
##
## Step: AIC=1398.07
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude
##
## Df Sum of Sq RSS AIC
## + house_age_cat 2 972.81 26315 1390.8
## <none> 27288 1398.1
## + longitude 1 8.02 27280 1400.0
##
## Step: AIC=1390.81
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## + longitude 1 18.161 26297 1392.6
##
## Call:
## lm(formula = price_twd_msq ~ dist_to_mrt_m + n_convenience +
## house_age_years + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
## Start: AIC=1392.6
## price_twd_msq ~ house_age_years + dist_to_mrt_m + n_convenience +
## latitude + longitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## - longitude 1 18.16 26315 1390.8
## <none> 26297 1392.6
## - house_age_cat 2 982.95 27280 1400.0
## - house_age_years 1 1031.51 27329 1402.5
## - n_convenience 1 1607.54 27905 1409.0
## - latitude 1 1891.31 28189 1412.1
## - dist_to_mrt_m 1 2400.71 28698 1417.7
##
## Step: AIC=1390.81
## price_twd_msq ~ house_age_years + dist_to_mrt_m + n_convenience +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## - house_age_cat 2 972.8 27288 1398.1
## - house_age_years 1 1035.7 27351 1400.8
## - n_convenience 1 1625.4 27941 1407.4
## - latitude 1 1924.7 28240 1410.7
## - dist_to_mrt_m 1 4348.8 30664 1436.2
##
## Call:
## lm(formula = price_twd_msq ~ house_age_years + dist_to_mrt_m +
## n_convenience + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
## Start: AIC=1638.47
## price_twd_msq ~ 1
##
## Df Sum of Sq RSS AIC
## + dist_to_mrt_m 1 27754.0 33057 1451.5
## + n_convenience 1 18298.4 42512 1529.5
## + longitude 1 17543.7 43267 1535.0
## + latitude 1 16685.2 44125 1541.0
## + house_age_cat 2 5118.7 55692 1615.2
## + house_age_years 1 2037.6 58773 1629.9
## <none> 60811 1638.5
##
## Step: AIC=1451.52
## price_twd_msq ~ dist_to_mrt_m
##
## Df Sum of Sq RSS AIC
## + n_convenience 1 1821.6 31235 1435.9
## + latitude 1 1793.8 31263 1436.2
## + house_age_years 1 1773.7 31283 1436.4
## + house_age_cat 2 1666.5 31390 1439.5
## <none> 33057 1451.5
## + longitude 1 27.3 33029 1453.3
## - dist_to_mrt_m 1 27754.0 60811 1638.5
##
## Step: AIC=1435.94
## price_twd_msq ~ dist_to_mrt_m + n_convenience
##
## Df Sum of Sq RSS AIC
## + house_age_years 1 2123.9 29111 1416.1
## + house_age_cat 2 2113.2 29122 1418.2
## + latitude 1 1388.9 29846 1423.8
## <none> 31235 1435.9
## + longitude 1 6.3 31229 1437.9
## - n_convenience 1 1821.6 33057 1451.5
## - dist_to_mrt_m 1 11277.2 42512 1529.5
##
## Step: AIC=1416.11
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years
##
## Df Sum of Sq RSS AIC
## + latitude 1 1823.0 27288 1398.1
## + house_age_cat 2 871.1 28240 1410.7
## <none> 29111 1416.1
## + longitude 1 33.0 29078 1417.8
## - house_age_years 1 2123.9 31235 1435.9
## - n_convenience 1 2171.8 31283 1436.4
## - dist_to_mrt_m 1 10564.4 39676 1510.1
##
## Step: AIC=1398.07
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude
##
## Df Sum of Sq RSS AIC
## + house_age_cat 2 972.8 26315 1390.8
## <none> 27288 1398.1
## + longitude 1 8.0 27280 1400.0
## - n_convenience 1 1706.1 28994 1414.9
## - latitude 1 1823.0 29111 1416.1
## - house_age_years 1 2557.9 29846 1423.8
## - dist_to_mrt_m 1 5494.4 32783 1452.9
##
## Step: AIC=1390.81
## price_twd_msq ~ dist_to_mrt_m + n_convenience + house_age_years +
## latitude + house_age_cat
##
## Df Sum of Sq RSS AIC
## <none> 26315 1390.8
## + longitude 1 18.2 26297 1392.6
## - house_age_cat 2 972.8 27288 1398.1
## - house_age_years 1 1035.7 27351 1400.8
## - n_convenience 1 1625.4 27941 1407.4
## - latitude 1 1924.7 28240 1410.7
## - dist_to_mrt_m 1 4348.8 30664 1436.2
## df AIC
## model_forward 8 2272.556
## model.backward 8 2272.556
## model.step 8 2272.556
##
## Call:
## lm(formula = price_twd_msq ~ dist_to_mrt_m + n_convenience +
## house_age_years + latitude + house_age_cat, data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.913 -5.098 -1.227 4.551 75.387
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6.250e+03 1.337e+03 -4.673 4.46e-06 ***
## dist_to_mrt_m -4.274e-03 6.040e-04 -7.076 1.03e-11 ***
## n_convenience 9.871e-01 2.282e-01 4.326 2.06e-05 ***
## house_age_years -4.217e-01 1.221e-01 -3.453 0.000633 ***
## latitude 2.521e+02 5.356e+01 4.708 3.82e-06 ***
## house_age_cat[15,30) -1.024e+00 1.909e+00 -0.536 0.592043
## house_age_cat[30,45] 5.851e+00 3.570e+00 1.639 0.102303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.319 on 303 degrees of freedom
## Multiple R-squared: 0.5673, Adjusted R-squared: 0.5587
## F-statistic: 66.2 on 6 and 303 DF, p-value: < 2.2e-16
From Best subset regression and stepwise selection (forward, backward, both), we see that all methods consistently selected the same model with the predictors dist_to_mrt_m, n_convenience, house_age_years, latitude, and house_age_cat[15,30), indicating their significant influence on the price per square meter.
Models are compared based on adjusted r square, AIC, BIC criteria for in-sample performance and mean square prediction error (MSPE) for out-of-sample performance.
Finally, we can check the Out-of-sample Prediction or test error (MSPE):
Please check how function ?cv.glm works.
We will just extract from this object created by cv.glm command - the raw cross-validation estimate of prediction error.
## Linear Regression
##
## 310 samples
## 5 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 280, 281, 279, 279, 279, 278, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 9.064416 0.5838067 6.474888
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
## Cross-validation estimate of prediction error: 82.16364
## Manual cross-validation estimate of prediction error: 89.47849
## Based on AIC criteria, the best model is Forward
## We need to check out-of-sample MSPE for both models. Based on out-of-sample prediction error, model Forward is the best.
To summarize, we can say that based on the AIC criterion and MSPE prediction error estimation, for both of them the forward selection model was chosen as the best model in our conditions.
These results are satisfactory and in line with our expectations, given the research and work done on the data.