The Boston Housing Dataset

Overview

For the second lab, we will be using a dataset containing information collected by the U.S Census Service concerning housing in the area of Boston Mass. The dataset was colelcted by the U.S Census Service concerning housing in the area of Boston Mass, posted in statlib Archive, and has been used extensively throughout in benchmarking algorithms. The dataset itself is small, with only 506 cases recorded.

The data was originally published by Harrison, D. and Rubinfeld, D.L. Hedonic prices and the demand for clean air, J. Environ. Economics & Management, vol.5, 81-102, 1978.

Dataset Attributes

There are 14 variables in this dataset, people often interested in two prototasks: nox, in which the nitrous oxide level is to be predicted; and price (medv), in which the median value of a home is to be predicted.

There are 13 attributes for each house of the dataset. They are:

  1. crim - per capita crime rate by town
  2. zn - proportion of residential land zoned for lots over 25,000 sq.ft.
  3. indus - industry, proportion of non-retail business acres per town.
  4. chas - Charles River dummy variable (1 if tract bounds river; 0 otherwise)
  5. nox - nitric oxides concentration (parts per 10 million)
  6. rm - the average number of rooms among homes in the neighborhood
  7. age - proportion of owner-occupied units built prior to 1940
  8. dis - weighted distances to five Boston employment centres
  9. rad - index of accessibility to radial highways
  10. tax - full-value property-tax rate per $10,000
  11. ptratio - pupil-teacher ratio by town
  12. lstat - percentage of population considered lower status (working poor)
  13. medv - Median value of owner-occupied homes in $1000’s

Kaggle, an online data science community, has done many analyses on this dataset.

Summarizing the dataset in R

Using the str() function, we can list out the variables and their data type in the Boston dataset.

str(Boston)
'data.frame':   506 obs. of  13 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 ...
 $ 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 ...

The ISLR2 library contains the Boston data set, which records medv (the median house value) for 506 census tracts in Boston. We will seek to predict medv using 12 predictors such as rmvar (average number of rooms per house), age (average age of houses), and lstat (percentage of households with low socioeconomic status).

head(Boston)
crim zn indus chas nox rm age dis rad tax ptratio lstat medv
0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 4.98 24.0
0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 9.14 21.6
0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 4.03 34.7
0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 2.94 33.4
0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 5.33 36.2
0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 5.21 28.7

Subsetting the Boston Dataset

Say you’d like to retrieve all of the samples with a house age less than 10 years, that is, taking out all rows satisfying age < 10. To do so, we can run the following command:

# Retrieving subset of data where age < 10
Boston[Boston$age < 10,]
crim zn indus chas nox rm age dis rad tax ptratio lstat medv
42 0.12744 0 6.91 0 0.448 6.770 2.9 5.7209 3 233 17.9 4.84 26.6
43 0.14150 0 6.91 0 0.448 6.169 6.6 5.7209 3 233 17.9 5.81 25.3
44 0.15936 0 6.91 0 0.448 6.211 6.5 5.7209 3 233 17.9 7.44 24.7
71 0.08826 0 10.81 0 0.413 6.417 6.6 5.2873 4 305 19.2 6.72 24.2
73 0.09164 0 10.81 0 0.413 6.065 7.8 5.2873 4 305 19.2 5.52 22.8
74 0.19539 0 10.81 0 0.413 6.245 6.2 5.2873 4 305 19.2 7.54 23.4
75 0.07896 0 12.83 0 0.437 6.273 6.0 4.2515 5 398 18.7 6.78 24.1
194 0.02187 60 2.93 0 0.401 6.800 9.9 6.2196 1 265 15.6 5.03 31.1
215 0.28955 0 10.59 0 0.489 5.412 9.8 3.5875 4 277 18.6 29.55 23.7
244 0.12757 30 4.93 0 0.428 6.393 7.8 7.0355 6 300 16.6 5.19 23.7
252 0.21409 22 5.86 0 0.431 6.438 8.9 7.3967 7 330 19.1 3.59 24.8
253 0.08221 22 5.86 0 0.431 6.957 6.8 8.9067 7 330 19.1 3.53 29.6
254 0.36894 22 5.86 0 0.431 8.259 8.4 8.9067 7 330 19.1 3.54 42.8
# Viewing the current distribution of house age in Boston 
hist(Boston$age)