library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.3.2
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.3.2
##
## Attaching package: 'ISLR2'
## The following objects are masked from 'package:ISLR':
##
## Auto, Credit
Qualitative predictors include: name.
mpgrange <- range(Auto$mpg)
cylrange <- range(Auto$cylinders)
horserange <- range(Auto$horsepower)
weightrange <- range(Auto$weight)
accrange <- range(Auto$acceleration)
yearrange <- range(Auto$year)
originrange <- range(Auto$origin)
print(paste('MPG Range:', mpgrange[2]-mpgrange[1]))
## [1] "MPG Range: 37.6"
print(paste('Cylinder Range:', cylrange[2]-cylrange[1]))
## [1] "Cylinder Range: 5"
print(paste('Horsepower Range:', horserange[2]-horserange[1]))
## [1] "Horsepower Range: 184"
print(paste('Weight Range:', weightrange[2]-weightrange[1]))
## [1] "Weight Range: 3527"
print(paste('Acceleration Range:', accrange[2]-accrange[1]))
## [1] "Acceleration Range: 16.8"
print(paste('Year Range:', yearrange[2]-yearrange[1]))
## [1] "Year Range: 12"
print(paste('Origin Range:', originrange[2]-originrange[1]))
## [1] "Origin Range: 2"
mpgm <- mean(Auto$mpg)
cylm <- mean(Auto$cylinders)
horm <- mean(Auto$horsepower)
weightm <- mean(Auto$weight)
accm <- mean(Auto$acceleration)
yearm <- mean(Auto$year)
originm <- mean(Auto$origin)
print(paste('MPG Mean:', mpgm))
## [1] "MPG Mean: 23.4459183673469"
print(paste('Cylinder Mean:', cylm))
## [1] "Cylinder Mean: 5.4719387755102"
print(paste('Horsepower Mean:', horm))
## [1] "Horsepower Mean: 104.469387755102"
print(paste('Weight Mean:', weightm))
## [1] "Weight Mean: 2977.58418367347"
print(paste('Acceleration Mean:', accm))
## [1] "Acceleration Mean: 15.5413265306122"
print(paste('Year Mean:', yearm))
## [1] "Year Mean: 75.9795918367347"
print(paste('Origin Mean:', originm))
## [1] "Origin Mean: 1.5765306122449"
The standard deviations include:
mpgms <- sd(Auto$mpg)
cylms <- sd(Auto$cylinders)
horms <- sd(Auto$horsepower)
weightms <- sd(Auto$weight)
accms <- sd(Auto$acceleration)
yearms <- sd(Auto$year)
originms <- sd(Auto$origin)
print(paste('MPG Standard Deviation:', mpgms))
## [1] "MPG Standard Deviation: 7.8050074865718"
print(paste('Cylinder Standard Deviation:', cylms))
## [1] "Cylinder Standard Deviation: 1.70578324745278"
print(paste('Horsepower Standard Deviation:', horms))
## [1] "Horsepower Standard Deviation: 38.4911599328285"
print(paste('Weight Standard Deviation:', weightms))
## [1] "Weight Standard Deviation: 849.402560042949"
print(paste('Acceleration Standard Deviation:', accms))
## [1] "Acceleration Standard Deviation: 2.75886411918808"
print(paste('Year Standard Deviation:', yearms))
## [1] "Year Standard Deviation: 3.68373654357783"
print(paste('Origin Standard Deviation:', originms))
## [1] "Origin Standard Deviation: 0.805518183418306"
Auto2 <- Auto[-c(10,85),]
Now that the 10th and 85th observations have been removed, lets look at the ranges, means, and standard deviations.
mpgrange <- range(Auto2$mpg)
cylrange <- range(Auto2$cylinders)
horserange <- range(Auto2$horsepower)
weightrange <- range(Auto2$weight)
accrange <- range(Auto2$acceleration)
yearrange <- range(Auto2$year)
originrange <- range(Auto2$origin)
print(paste('MPG Range:', mpgrange[2]-mpgrange[1]))
## [1] "MPG Range: 37.6"
print(paste('Cylinder Range:', cylrange[2]-cylrange[1]))
## [1] "Cylinder Range: 5"
print(paste('Horsepower Range:', horserange[2]-horserange[1]))
## [1] "Horsepower Range: 184"
print(paste('Weight Range:', weightrange[2]-weightrange[1]))
## [1] "Weight Range: 3527"
print(paste('Acceleration Range:', accrange[2]-accrange[1]))
## [1] "Acceleration Range: 16.8"
print(paste('Year Range:', yearrange[2]-yearrange[1]))
## [1] "Year Range: 12"
print(paste('Origin Range:', originrange[2]-originrange[1]))
## [1] "Origin Range: 2"
mpgm <- mean(Auto2$mpg)
cylm <- mean(Auto2$cylinders)
horm <- mean(Auto2$horsepower)
weightm <- mean(Auto2$weight)
accm <- mean(Auto2$acceleration)
yearm <- mean(Auto2$year)
originm <- mean(Auto2$origin)
print(paste('MPG Mean:', mpgm))
## [1] "MPG Mean: 23.494358974359"
print(paste('Cylinder Mean:', cylm))
## [1] "Cylinder Mean: 5.45897435897436"
print(paste('Horsepower Mean:', horm))
## [1] "Horsepower Mean: 104.069230769231"
print(paste('Weight Mean:', weightm))
## [1] "Weight Mean: 2972.46923076923"
print(paste('Acceleration Mean:', accm))
## [1] "Acceleration Mean: 15.5658974358974"
print(paste('Year Mean:', yearm))
## [1] "Year Mean: 76.0025641025641"
print(paste('Origin Mean:', originm))
## [1] "Origin Mean: 1.57948717948718"
mpgms <- sd(Auto2$mpg)
cylms <- sd(Auto2$cylinders)
horms <- sd(Auto2$horsepower)
weightms <- sd(Auto2$weight)
accms <- sd(Auto2$acceleration)
yearms <- sd(Auto2$year)
originms <- sd(Auto2$origin)
print(paste('MPG Standard Deviation:', mpgms))
## [1] "MPG Standard Deviation: 7.79519784772664"
print(paste('Cylinder Standard Deviation:', cylms))
## [1] "Cylinder Standard Deviation: 1.70047898117198"
print(paste('Horsepower Standard Deviation:', horms))
## [1] "Horsepower Standard Deviation: 38.1763305305051"
print(paste('Weight Standard Deviation:', weightms))
## [1] "Weight Standard Deviation: 848.512066998811"
print(paste('Acceleration Standard Deviation:', accms))
## [1] "Acceleration Standard Deviation: 2.73967162383015"
print(paste('Year Standard Deviation:', yearms))
## [1] "Year Standard Deviation: 3.67755557433291"
print(paste('Origin Standard Deviation:', originms))
## [1] "Origin Standard Deviation: 0.806522092344236"
pairs(mpg ~ displacement + horsepower + weight + acceleration,
data = Auto)
Looking at the matrix of scatterplots above, we can see a general negative correlation between mpg and displacement, mpg and horsepower, mpg and weight, and horsepower and acceleration. We can see a general positive correlation between, displacement and horsepower, displacement and weight, and horsepower and weight.
head(Boston)
## crim zn indus chas nox rm age dis rad tax ptratio lstat medv
## 1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 4.98 24.0
## 2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 9.14 21.6
## 3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 4.03 34.7
## 4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 2.94 33.4
## 5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 5.33 36.2
## 6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 5.21 28.7
The Boston Data Frame has 506 rows and 14 columns. Each row represents each observation or suburb, and the columns represent various factors that affect housing values.
pairs(crim ~ age + tax + medv + zn + chas,
data = Boston)
In the scatter plots above, there is not a lot of conclusions that are apparent.
pairs(crim ~ nox + rm + dis + rad + ptratio + lstat,
data = Boston)
Again, looking at the scatterplots, there are not a lot of visible correlations between factors. There is a general negative correlation between nox and dis, which is the nitrogen oxide concentration and the weighted mean of distances to five emlpyment centers. This can show that the closer you are to the employment centers, the higher the nitrogen oxide concentration, or vice versa.
Something similar exits with rm and lstat, which are the average numbers per dwelling and the lower status of the population (percent).
The per capita crime rate has a slight association with the age and median value of homes. The lower valued homes and the suburbs with a higher proportion of older homes tend to have a higher per capita crime rate.
crimerange <- range(Boston$crim)
taxrange <- range(Boston$tax)
ptrrange <- range(Boston$ptratio)
crimerange
## [1] 0.00632 88.97620
taxrange
## [1] 187 711
ptrrange
## [1] 12.6 22.0
print(paste('Crime Rate Range:', crimerange[2]-crimerange[1]))
## [1] "Crime Rate Range: 88.96988"
print(paste('Tax Rate Range:', taxrange[2]-taxrange[1]))
## [1] "Tax Rate Range: 524"
print(paste('Pupil-Teacher Ratio Range:', ptrrange[2]-ptrrange[1]))
## [1] "Pupil-Teacher Ratio Range: 9.4"
There seems to be a large range difference in each of these predictors. There are towns with significantly higher crime rates, tax rates, and Pupil-Teacher Ratios compared to some of their neighbors.
charlesriver <- Boston |>
filter(chas == 1)
count(charlesriver)
## n
## 1 35
There are 35 Census Tracts that bounds the river.
medptr <- median(Boston$ptratio)
print(paste('Median Pupil-Teacher Ratio:', medptr))
## [1] "Median Pupil-Teacher Ratio: 19.05"
The median PTR is above.
minooc <- min(Boston$medv)
minooc <- minooc*1000
print(paste('Lowest Median value of Owner-Occupied Homes:', minooc))
## [1] "Lowest Median value of Owner-Occupied Homes: 5000"
The lowest median value of Owner-occupied homes is above.
minvalue <- Boston |>
filter(medv == 5)
minvalue
## crim zn indus chas nox rm age dis rad tax ptratio lstat medv
## 1 38.3518 0 18.1 0 0.693 5.453 100 1.4896 24 666 20.2 30.59 5
## 2 67.9208 0 18.1 0 0.693 5.683 100 1.4254 24 666 20.2 22.98 5
There are two suburbs with the lowest median home value. One sits at about the middle of the crime rate range while one sits at the higher end. They also both sit on the higher part of the range for both the tax rate and the pupil-teacher ratios.
seven <- Boston |>
filter(rm >= 7)
eight <- Boston |>
filter(rm >= 8)
count(seven)
## n
## 1 64
count(eight)
## n
## 1 13
There are 64 suburbs with an average of more than 7 rooms per dwelling and 13 with an average of 8 or more.
eight
## crim zn indus chas nox rm age dis rad tax ptratio lstat medv
## 1 0.12083 0 2.89 0 0.4450 8.069 76.0 3.4952 2 276 18.0 4.21 38.7
## 2 1.51902 0 19.58 1 0.6050 8.375 93.9 2.1620 5 403 14.7 3.32 50.0
## 3 0.02009 95 2.68 0 0.4161 8.034 31.9 5.1180 4 224 14.7 2.88 50.0
## 4 0.31533 0 6.20 0 0.5040 8.266 78.3 2.8944 8 307 17.4 4.14 44.8
## 5 0.52693 0 6.20 0 0.5040 8.725 83.0 2.8944 8 307 17.4 4.63 50.0
## 6 0.38214 0 6.20 0 0.5040 8.040 86.5 3.2157 8 307 17.4 3.13 37.6
## 7 0.57529 0 6.20 0 0.5070 8.337 73.3 3.8384 8 307 17.4 2.47 41.7
## 8 0.33147 0 6.20 0 0.5070 8.247 70.4 3.6519 8 307 17.4 3.95 48.3
## 9 0.36894 22 5.86 0 0.4310 8.259 8.4 8.9067 7 330 19.1 3.54 42.8
## 10 0.61154 20 3.97 0 0.6470 8.704 86.9 1.8010 5 264 13.0 5.12 50.0
## 11 0.52014 20 3.97 0 0.6470 8.398 91.5 2.2885 5 264 13.0 5.91 48.8
## 12 0.57834 20 3.97 0 0.5750 8.297 67.0 2.4216 5 264 13.0 7.44 50.0
## 13 3.47428 0 18.10 1 0.7180 8.780 82.9 1.9047 24 666 20.2 5.29 21.9
Among the suburbs with an average of 8 rooms or more per dwelling, they all have very low crime rates, low tax rates, and very low pupil to teacher ratios.