1. Load package MASS

The dataset “Boston” comes with package MASS. When you load the package, it automatically loads the dataset as well.

library(MASS)   # load the package

# If not installed, use the command below
# install.packages('MASS', repos='https://cran.case.edu/')

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

dim(Boston)     # (num of row, num of col)
names(Boston)   # column names

?Boston         # see explanation for variables

#1  crim     per capita crime rate by town.
#2  zn       proportion of residential land zoned for lots over 25,000 sq.ft.
#3  indus    proportion of non-retail business acres per town.
#4  chas     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).
#5  nox      nitrogen oxides concentration (parts per 10 million).
#6  rm       average number of rooms per dwelling.
#7  age      proportion of owner-occupied units built prior to 1940.
#8  dis      weighted mean of 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 black    1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town.
#13 lstat    lower status of the population (percent).
#14 medv (response Y) median value of owner-occupied homes in $1000s.

# 13 lstat = lower status of the population (percent)
#      Proportion of population that is lower status
#      = 1/2 (proportion of adults without,
#      some high school education and proportion of male
#      workers classified as laborers). The logarithmic
#      specification implies that socioeconomic status
#      distinctions mean more in the upper brackets of
#      society than in the lower classes. Source: 1970 U. S. Census


2. Selecting a column of data

Boston[ ,1]         # 1st column
Boston[,"age"]      # column "age"
Boston$age          # column "age

age                 # you can't refer just by columnnames
Boston$age          # you have to use this


3. Attach() columns

plot(Boston$age, Boston$medv)   # plot age vs medv

attach(Boston)    # now you can use their col name as a variable

plot(age, medv)   # no need to say Boston$


4. Scatter plots of medv vs other predictors

# column "medv" is the respoinse variable.

# Scatter plots of Y vs each X
plot(age, medv)

plot(crim, medv)

plot(zn, medv)

plot(rad, medv)

plot(ptratio, medv)

plot(black, medv)

plot(chas, medv)

# Histogram of the respoonse variable
hist(medv)


5. Scatter Plots in a panel

#- Scatter plots
layout( matrix(1:6, 2, 3) )
plot(crim, medv)
plot(zn, medv)
plot(indus, medv)
plot(nox, medv)
plot(rm, medv)
plot(age, medv)

layout( matrix(1:6, 2, 3) )
plot(dis, medv)
plot(rad, medv)
plot(tax, medv)
plot(ptratio, medv)
plot(black, medv)
plot(lstat, medv)

layout(1)     # reset the panel


6. Correlaiton Plots

# Correlaiton Plots
pairs(Boston)   # shows scatterplot matrix (very large. hard to see)

cor(Boston)     # raw correlation matrix
# correlation plots with better graphics
library(corrplot)   # install.packages("corrplot")
## corrplot 0.95 loaded
corrplot(cor(Boston), method="number")  # cor matrix with number and color scheme

corrplot(cor(Boston))  # cor matrix with color scheme




COCE ONLY

##--------------------------------------
## 1. Load package MASS

library(MASS)   # load the package

# If not installed, use the command below
# install.packages('MASS', repos='https://cran.case.edu/')

head(Boston)    # see the data

Boston          # see the entire data

dim(Boston)     # (num of row, num of col)
names(Boston)   # column names

?Boston         # see explanation for variables

#1  crim     per capita crime rate by town.
#2  zn       proportion of residential land zoned for lots over 25,000 sq.ft.
#3  indus    proportion of non-retail business acres per town.
#4  chas     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).
#5  nox      nitrogen oxides concentration (parts per 10 million).
#6  rm       average number of rooms per dwelling.
#7  age      proportion of owner-occupied units built prior to 1940.
#8  dis      weighted mean of 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 black    1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town.
#13 lstat    lower status of the population (percent).
#14 medv (response Y) median value of owner-occupied homes in $1000s.

# 13 lstat = lower status of the population (percent)
#      Proportion of population that is lower status
#      = 1/2 (proportion of adults without,
#      some high school education and proportion of male
#      workers classified as laborers). The logarithmic
#      specification implies that socioeconomic status
#      distinctions mean more in the upper brackets of
#      society than in the lower classes. Source: 1970 U. S. Census


##--------------------------------------
## 2. Selecting a column of data


Boston[ ,1]         # 1st column
Boston[,"age"]      # column "age"
Boston$age          # column "age


age                 # you can't refer just by columnnames
Boston$age          # you have to use this



##--------------------------------------
## 3. Attach() columns

plot(Boston$age, Boston$medv)   # plot age vs medv

attach(Boston)    # now you can use their col name as a variable

plot(age, medv)   # no need to say Boston$



##--------------------------------------
## 4. Scatter plots of medv vs other predictors

# column "medv" is the respoinse variable.
# Scatter plots
plot(age, medv)
plot(crim, medv)
plot(zn, medv)
plot(rad, medv)
plot(ptratio, medv)
plot(black, medv)
plot(chas, medv)

# Histogram of the respoonse variable
hist(medv)




##--------------------------------------
## 5. Scatter Plots in a panel

# Scatter plots
layout( matrix(1:6, 2, 3) )
plot(crim, medv)
plot(zn, medv)
plot(indus, medv)
plot(nox, medv)
plot(rm, medv)
plot(age, medv)


layout( matrix(1:6, 2, 3) )
plot(dis, medv)
plot(rad, medv)
plot(tax, medv)
plot(ptratio, medv)
plot(black, medv)
plot(lstat, medv)
layout(1)     # reset the panel



##--------------------------------------
## 6. Correlaiton Plots

# Correlaiton Plots
pairs(Boston)   # shows scatterplot matrix (very large. hard to see)

cor(Boston)     # raw correlation matrix

# correlation plots with better graphics
library(corrplot)   # install.packages("corrplot")
corrplot(cor(Boston), method="number")  # cor matrix with number and color scheme
corrplot(cor(Boston))  # cor matrix with color scheme