library(ISLR)
# Membaca dataset Boston Housing
library(MASS)
data(Boston)
# Melihat dimensi dataset
dim(Boston)
## [1] 506 14
# Melihat struktur dataset
str(Boston)
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
# Melihat summary dataset
summary(Boston)
## crim zn indus chas
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000
## 1st Qu.: 0.08205 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000
## nox rm age dis
## Min. :0.3850 Min. :3.561 Min. : 2.90 Min. : 1.130
## 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02 1st Qu.: 2.100
## Median :0.5380 Median :6.208 Median : 77.50 Median : 3.207
## Mean :0.5547 Mean :6.285 Mean : 68.57 Mean : 3.795
## 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08 3rd Qu.: 5.188
## Max. :0.8710 Max. :8.780 Max. :100.00 Max. :12.127
## rad tax ptratio black
## Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32
## 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38
## Median : 5.000 Median :330.0 Median :19.05 Median :391.44
## Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67
## 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23
## Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90
## lstat medv
## Min. : 1.73 Min. : 5.00
## 1st Qu.: 6.95 1st Qu.:17.02
## Median :11.36 Median :21.20
## Mean :12.65 Mean :22.53
## 3rd Qu.:16.95 3rd Qu.:25.00
## Max. :37.97 Max. :50.00
# Melihat sebaran data menggunakan boxplot
boxplot(Boston, main="Boston Housing Data Boxplot")
# Melakukan prediksi harga rumah menggunakan model regresi linear
trainIndex <- sample(1:nrow(Boston), 400)
train <- Boston[trainIndex,]
test <- Boston[-trainIndex,]
fit <- lm(medv ~ ., data=train)
summary(fit)
##
## Call:
## lm(formula = medv ~ ., data = train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5432 -2.7864 -0.7542 1.6481 26.9498
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.376065 6.011239 5.053 6.72e-07 ***
## crim -0.096646 0.041000 -2.357 0.018912 *
## zn 0.045528 0.015842 2.874 0.004279 **
## indus -0.002147 0.068669 -0.031 0.975074
## chas 3.616143 0.992817 3.642 0.000307 ***
## nox -17.258724 4.293400 -4.020 7.00e-05 ***
## rm 4.382228 0.500702 8.752 < 2e-16 ***
## age -0.006951 0.015058 -0.462 0.644626
## dis -1.592781 0.232783 -6.842 3.07e-11 ***
## rad 0.321389 0.076902 4.179 3.62e-05 ***
## tax -0.012637 0.004351 -2.905 0.003888 **
## ptratio -0.805899 0.148221 -5.437 9.62e-08 ***
## black 0.009772 0.003038 3.216 0.001409 **
## lstat -0.482497 0.058782 -8.208 3.40e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.758 on 386 degrees of freedom
## Multiple R-squared: 0.7354, Adjusted R-squared: 0.7265
## F-statistic: 82.52 on 13 and 386 DF, p-value: < 2.2e-16
predicted_prices <- predict(fit, newdata=test)
plot(predicted_prices, test$medv, main="Predicted vs Actual House Prices", xlab="Predicted Price", ylab="Actual Price")
abline(0,1)
Dalam contoh ini, kita menggunakan dataset Boston Housing dari package MASS di R. Pertama, kita membaca dataset menggunakan fungsi data(). Kemudian, kita melihat dimensi dan struktur dataset menggunakan fungsi dim() dan str(). Selanjutnya, kita melihat summary dataset menggunakan fungsi summary(). Kemudian, kita melihat sebaran data menggunakan boxplot dengan fungsi boxplot(). Terakhir, kita melakukan prediksi harga rumah menggunakan model regresi linear dengan fungsi lm(). Kita memilih 400 sampel secara acak sebagai data pelatihan dan menggunakan sampel yang tersisa sebagai data uji. Hasil prediksi ditampilkan menggunakan fungsi plot().