Το dataset το οποίο επιλέκτηκε για την εργασία αφορά μια πληθώρα σπιτιών, καθώς και στοιχεία για αυτά, τα οποιά επηρεάζουν την τιμή πώλησης τους. Το συγκεκριμένο σετ δεδομένων έχει δημιουργηθεί για την μελέτη απλής και πολυμεταβλητής γραμμικής παλινδρόμησης. Αποτελείται από 414 εγγραφές και 8 μεταβλητές.
Αποτελείται από ένα αρχείο με το όνομα Real_estate.csv
Link Dataset: https://www.kaggle.com/datasets/quantbruce/real-estate-price-prediction
Το συγκεκριμένο dataset καθότι δεν είχε χωριστεί σε test και train σετ από τον δημιουργό θα πρέπει να χωριστεί. Αυτό θα το κάνουμε με την παρακάτω συνάρτηση:
set.seed(578)
total_rows <- nrow(df)
## χωρίζουμε 80% test και train 20%
train_indices <- sample(1:total_rows, size = 0.8 *total_rows)
train_data <- df[train_indices, ]
test_data <- df[-train_indices, ]
cat("Το dataset έχει στο test set ", nrow(test_data), " και στο train set ", nrow(train_data))
## Το dataset έχει στο test set 83 και στο train set 331
print(summary(train_data))
## No X1 transaction date X2 house age
## Min. : 1.0 Min. :2013 Min. : 0.00
## 1st Qu.:105.5 1st Qu.:2013 1st Qu.:10.40
## Median :212.0 Median :2013 Median :16.20
## Mean :209.4 Mean :2013 Mean :18.32
## 3rd Qu.:314.5 3rd Qu.:2013 3rd Qu.:29.35
## Max. :414.0 Max. :2014 Max. :43.80
## X3 distance to the nearest MRT station X4 number of convenience stores
## Min. : 23.38 Min. : 0.000
## 1st Qu.: 289.32 1st Qu.: 1.000
## Median : 492.23 Median : 4.000
## Mean :1098.24 Mean : 4.073
## 3rd Qu.:1470.45 3rd Qu.: 6.000
## Max. :6488.02 Max. :10.000
## X5 latitude X6 longitude Y house price of unit area
## Min. :24.93 Min. :121.5 Min. : 7.60
## 1st Qu.:24.96 1st Qu.:121.5 1st Qu.: 27.50
## Median :24.97 Median :121.5 Median : 38.40
## Mean :24.97 Mean :121.5 Mean : 37.83
## 3rd Qu.:24.98 3rd Qu.:121.5 3rd Qu.: 46.10
## Max. :25.01 Max. :121.6 Max. :117.50
ggplot(data = train_data, aes(x= `Y house price of unit area`))+
geom_histogram(bins = 30, fill = "#3498DB", color = "white") +
labs(title = "Κατανομή τιμών σπιτιού ανά τ.μ.",
x = "Τιμή ανά τ.μ.",
y = "Συχνότητα")
ggplot(data = train_data, aes(x= `X6 longitude`, y = `X5 latitude`, color = "#3498DB"))+
geom_point(size = 4, alpha = 0.7) +
labs(title = "Τοποθεσία",
x = "Longitude",
y = "Latitude")
ggplot(data = train_data, aes(x= `Y house price of unit area`, y = `X3 distance to the nearest MRT station`, color = "#3498DB"))+
geom_point(size = 4, alpha = 0.7) +
labs(title = "Τιμή σε σχέση με την απόσταση από το MRT",
x = "Τιμή ανά τ.μ.",
y = "Απόσταση από τον κοντινότερο σταθμό MRT")
ggplot(data = train_data, aes(x= `Y house price of unit area`, y = `X2 house age`, color = "#3498DB"))+
geom_point(size = 4, alpha = 0.7) +
labs(title = "Τιμή σε σχέση με την ηλικία του σπιτιού",
x = "Τιμή ανά τ.μ.",
y = "Ηλικία σπιτιού")
ggplot(data = train_data, aes(x = factor(`X4 number of convenience stores`), y = `Y house price of unit area`)) +
geom_boxplot(alpha = 0.7) +
labs(title = "Τιμή ανά τ.μ. σε σχέση με τον αριθμό καταστημάτων",
x = "Αριθμός καταστημάτων",
y= "Τιμή ανά τ.μ.")
Σε αυτό το σημείο θέλοντας να δούμε το πόσο επηρεάζει η κάθε μεταβλητή ξεχωριστά την πρόγνωση.
m <- lm(`Y house price of unit area` ~ `X1 transaction date`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X1 transaction date`,
## data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.857 -9.862 1.088 8.341 79.043
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -6647.123 5398.009 -1.231 0.219
## `X1 transaction date` 3.321 2.681 1.238 0.216
##
## Residual standard error: 13.59 on 329 degrees of freedom
## Multiple R-squared: 0.00464, Adjusted R-squared: 0.001615
## F-statistic: 1.534 on 1 and 329 DF, p-value: 0.2164
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.004639954
ggplot(data = train_data, aes(`X1 transaction date`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
m <- lm(`Y house price of unit area` ~ `X2 house age`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X2 house age`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.881 -11.060 1.397 7.599 78.278
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 41.22139 1.39031 29.649 < 2e-16 ***
## `X2 house age` -0.18517 0.06426 -2.882 0.00422 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 13.45 on 329 degrees of freedom
## Multiple R-squared: 0.02462, Adjusted R-squared: 0.02165
## F-statistic: 8.304 on 1 and 329 DF, p-value: 0.004216
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.02461797
ggplot(data = train_data, aes(`X2 house age`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station`,
## data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.193 -5.974 -1.309 4.352 73.716
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.5624847 0.7414383 61.45 <2e-16
## `X3 distance to the nearest MRT station` -0.0070420 0.0004406 -15.98 <2e-16
##
## (Intercept) ***
## `X3 distance to the nearest MRT station` ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.22 on 329 degrees of freedom
## Multiple R-squared: 0.4371, Adjusted R-squared: 0.4354
## F-statistic: 255.4 on 1 and 329 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.437063
ggplot(data = train_data, aes(`X3 distance to the nearest MRT station`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
m <- lm(`Y house price of unit area` ~ `X4 number of convenience stores`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X4 number of convenience stores`,
## data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.199 -6.645 -1.684 5.298 87.594
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.3271 1.0635 25.70 <2e-16 ***
## `X4 number of convenience stores` 2.5786 0.2119 12.17 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.31 on 329 degrees of freedom
## Multiple R-squared: 0.3105, Adjusted R-squared: 0.3084
## F-statistic: 148.1 on 1 and 329 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.3104563
ggplot(data = train_data, aes(`X4 number of convenience stores`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
m <- lm(`Y house price of unit area` ~ `X5 latitude`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X5 latitude`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -37.548 -7.285 -1.534 5.723 76.257
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -14700.62 1274.07 -11.54 <2e-16 ***
## `X5 latitude` 590.27 51.03 11.57 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.48 on 329 degrees of freedom
## Multiple R-squared: 0.2891, Adjusted R-squared: 0.287
## F-statistic: 133.8 on 1 and 329 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.289137
ggplot(data = train_data, aes(`X5 latitude`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
m <- lm(`Y house price of unit area` ~ `X6 longitude`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X6 longitude`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.426 -5.753 -0.353 6.102 80.943
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -55008.86 5048.58 -10.9 <2e-16 ***
## `X6 longitude` 452.94 41.54 10.9 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.67 on 329 degrees of freedom
## Multiple R-squared: 0.2654, Adjusted R-squared: 0.2632
## F-statistic: 118.9 on 1 and 329 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.2654348
ggplot(data = train_data, aes(`X6 longitude`,`Y house price of unit area`)) +
geom_point() +
geom_abline(aes(intercept = coef(m)[1],
slope = coef(m)[2]), color = "red")
Στο στάδιο αυτό προσθέτουμε μια, μία τις μεταβλητές για να δούμε πως μεγιστοποιούμε το R τετράγωνο.
Από τα παραπάνω ξέρουμε πως τα υψηλότερα δύο R τετράγωνο είναι στις μεταβλητές “distance to the nearest MRT station” και “X4 number of convenience stores”. Συνεπώς ξεκινούμε με αυτόν τον συνδιασμό.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -36.274 -5.631 -1.427 3.941 78.584
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.1469153 1.4877099 26.314 < 2e-16
## `X3 distance to the nearest MRT station` -0.0054450 0.0005354 -10.170 < 2e-16
## `X4 number of convenience stores` 1.1446915 0.2326216 4.921 1.37e-06
##
## (Intercept) ***
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.878 on 328 degrees of freedom
## Multiple R-squared: 0.4758, Adjusted R-squared: 0.4726
## F-statistic: 148.8 on 2 and 328 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.4757647
Αφού το R τετράγωνο αυξήθηκε τότε μπορούμε να συνεχίζουμε προσθέτοντας την επόμενη μεγαλύτερη μεταβλητή που είναι η “latitude”.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.606 -5.208 -1.322 4.385 77.733
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.727e+03 1.308e+03 -4.378 1.62e-05
## `X3 distance to the nearest MRT station` -4.366e-03 5.756e-04 -7.585 3.48e-13
## `X4 number of convenience stores` 1.006e+00 2.285e-01 4.400 1.47e-05
## `X5 latitude` 2.309e+02 5.239e+01 4.407 1.42e-05
##
## (Intercept) ***
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude` ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.612 on 327 degrees of freedom
## Multiple R-squared: 0.5052, Adjusted R-squared: 0.5006
## F-statistic: 111.3 on 3 and 327 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.5051612
Το R τετράγωνο αυξήθηκε, συνεπώς κρατάμε την μεταβήτή “latitude”. Στην συνέχεια προσθέτουμε την μεταβλητή με την αμέσως μεγαλύτερη R τετράγωνο, η οποία είναι η “longitude”.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude` + `X6 longitude`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude` + `X6 longitude`,
## data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33.610 -5.200 -1.305 4.383 77.719
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.589e+03 7.711e+03 -0.725 0.469
## `X3 distance to the nearest MRT station` -4.378e-03 8.889e-04 -4.926 1.34e-06
## `X4 number of convenience stores` 1.005e+00 2.298e-01 4.375 1.64e-05
## `X5 latitude` 2.308e+02 5.307e+01 4.349 1.83e-05
## `X6 longitude` -1.111e+00 6.089e+01 -0.018 0.985
##
## (Intercept)
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude` ***
## `X6 longitude`
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.626 on 326 degrees of freedom
## Multiple R-squared: 0.5052, Adjusted R-squared: 0.4991
## F-statistic: 83.2 on 4 and 326 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.5051617
Από το παραπάνω βλέπουμε ότι η τιμή του R squared οριακά παρέμεινε σταθερή. Για δοκιμαστικούς λόγους δοκιμάζουμε να προσθέσουμε την επόμενη μεγαλύτερη μεταβλητή “house age”, γνωρίζοντας πως πιθανώς το “longitude” να πρέπει να αφαιρεθεί.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude` + `X6 longitude` + `X2 house age`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude` + `X6 longitude` +
## `X2 house age`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -34.285 -5.217 -1.462 3.847 76.358
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.396e+03 7.427e+03 -0.727 0.468
## `X3 distance to the nearest MRT station` -4.160e-03 8.572e-04 -4.853 1.89e-06
## `X4 number of convenience stores` 1.112e+00 2.223e-01 5.004 9.22e-07
## `X5 latitude` 2.530e+02 5.129e+01 4.932 1.30e-06
## `X6 longitude` -7.229e+00 5.866e+01 -0.123 0.902
## `X2 house age` -2.299e-01 4.473e-02 -5.140 4.76e-07
##
## (Intercept)
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude` ***
## `X6 longitude`
## `X2 house age` ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.272 on 325 degrees of freedom
## Multiple R-squared: 0.5424, Adjusted R-squared: 0.5353
## F-statistic: 77.03 on 5 and 325 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.5423575
Είναι προφανές πως αυξήθηκε σημαντικά η τιμή του R τετράγωνο, και άρα κρατάμε την τιμή “house age”. Δοκιμαστικά προσθέτουμε την περισσευούμενη μεταβλητή “transaction date”.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude` + `X6 longitude` + `X2 house age` + `X1 transaction date`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude` + `X6 longitude` +
## `X2 house age` + `X1 transaction date`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.515 -5.435 -1.002 3.873 74.947
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.385e+04 7.920e+03 -1.749 0.08127
## `X3 distance to the nearest MRT station` -4.504e-03 8.563e-04 -5.259 2.63e-07
## `X4 number of convenience stores` 1.082e+00 2.201e-01 4.915 1.41e-06
## `X5 latitude` 2.400e+02 5.094e+01 4.710 3.67e-06
## `X6 longitude` -2.186e+01 5.825e+01 -0.375 0.70765
## `X2 house age` -2.391e-01 4.436e-02 -5.389 1.37e-07
## `X1 transaction date` 5.245e+00 1.836e+00 2.857 0.00455
##
## (Intercept) .
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude` ***
## `X6 longitude`
## `X2 house age` ***
## `X1 transaction date` **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.171 on 324 degrees of freedom
## Multiple R-squared: 0.5536, Adjusted R-squared: 0.5453
## F-statistic: 66.97 on 6 and 324 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.5536056
Αυξήθηκε η τιμή R Square, οπότε την κρατάμε την μεταβλητή “transaction date”. Δοκιμαστικά αφαιρούμε την μεταβλητή “longitude”.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude` + `X2 house age` + `X1 transaction date`, data = train_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude` + `X2 house age` +
## `X1 transaction date`, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.429 -5.399 -1.251 3.720 75.235
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.646e+04 3.792e+03 -4.341 1.90e-05
## `X3 distance to the nearest MRT station` -4.259e-03 5.549e-04 -7.676 1.94e-13
## `X4 number of convenience stores` 1.089e+00 2.190e-01 4.974 1.06e-06
## `X5 latitude` 2.429e+02 5.027e+01 4.831 2.09e-06
## `X2 house age` -2.386e-01 4.429e-02 -5.388 1.37e-07
## `X1 transaction date` 5.184e+00 1.826e+00 2.839 0.00481
##
## (Intercept) ***
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude` ***
## `X2 house age` ***
## `X1 transaction date` **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.159 on 325 degrees of freedom
## Multiple R-squared: 0.5534, Adjusted R-squared: 0.5465
## F-statistic: 80.55 on 5 and 325 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(train_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το παραπάνω είναι: ", R_Squared)
## R^2 για το παραπάνω είναι: 0.5534115
Δεδομένου πως η μειώση της τιμής ήταν αμελλητέα, τότε αφαιρείται η μεταβλητή “longitude”.
Ξέρουμε πλέον πως το μοντέλο γραμμικής παλινδρόμησης έχει εξίσωση: \[Y = -1646 - 0,004259*`X3 distance to the nearest MRT station` + 1,089*`X4 number of convenience stores` + 242,9*`X5 latitude` - 0,2386*`X2 house age` + 5,184*`X1 transaction date`\]
Αφού βρήκαμε την εξίσωση, μπορούμε να την δοκιμάσουμε για το test set.
m <- lm(`Y house price of unit area` ~ `X3 distance to the nearest MRT station` + `X4 number of convenience stores` + `X5 latitude` + `X2 house age` + `X1 transaction date`, data = test_data)
summary(m)
##
## Call:
## lm(formula = `Y house price of unit area` ~ `X3 distance to the nearest MRT station` +
## `X4 number of convenience stores` + `X5 latitude` + `X2 house age` +
## `X1 transaction date`, data = test_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17.4395 -4.5467 0.1477 4.4279 17.4833
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.007e+04 6.050e+03 -1.664 0.100219
## `X3 distance to the nearest MRT station` -4.993e-03 1.029e-03 -4.851 6.28e-06
## `X4 number of convenience stores` 1.242e+00 3.416e-01 3.636 0.000498
## `X5 latitude` 1.412e+02 9.099e+01 1.552 0.124864
## `X2 house age` -4.437e-01 8.041e-02 -5.519 4.42e-07
## `X1 transaction date` 3.271e+00 2.872e+00 1.139 0.258182
##
## (Intercept)
## `X3 distance to the nearest MRT station` ***
## `X4 number of convenience stores` ***
## `X5 latitude`
## `X2 house age` ***
## `X1 transaction date`
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.405 on 77 degrees of freedom
## Multiple R-squared: 0.7254, Adjusted R-squared: 0.7076
## F-statistic: 40.68 on 5 and 77 DF, p-value: < 2.2e-16
SSE <- sum(m$residuals^2)
SSR <- sum((fitted(m) - mean(test_data$`Y house price of unit area`))^2)
SST <- SSR + SSE
R_Squared <- 1 - (SSE/SST)
cat("R^2 για το test data είναι: ", R_Squared)
## R^2 για το test data είναι: 0.7254159
Δεδομένου πως το σκορ R Squared για το test set είναι μεγαλύτερο του train test, σημαίνει πως το μοντέλο δεν έχει κάνει overfit, δουλέυει σωστά και έχει ικανοποιητικό σκορ (0.72).