library(corrplot)
## Warning: package 'corrplot' was built under R version 4.6.1
## corrplot 0.95 loaded
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.1
data(mtcars)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
There is nothing strange about the data at this point.
dim(mtcars)
## [1] 32 11
help(mtcars)
## starting httpd help server ... done
hist(mtcars$disp)
###1.2
plot(mtcars$mpg,mtcars$hp)
I am presenting a scatter plot of mpg vs. hp.
##1.3 Interesting Pattern
boxplot(mpg ~ am, data = mtcars,
names = c("Automatic", "Manual"),
main = "MPG by type of transmission", ylab = "MPG")
Manual cars are getting significantly better gas mileage than automatic
cars.
##1.4 Variables Correlated to MPG
cor(mtcars)[, "mpg"]
## mpg cyl disp hp drat wt qsec
## 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.4186840
## vs am gear carb
## 0.6640389 0.5998324 0.4802848 -0.5509251
plot(mtcars$wt, mtcars$mpg,
main = "MPG vs weight", xlab = "Weight", ylab = "MPG")
Weight, Cylinder, Displacement, and Horsepower all have the largest
correlation with MPG but they are all negative correlations.
##1.5 Missing Data
sum(is.na(mtcars))
## [1] 0
colSums(is.na(mtcars))
## mpg cyl disp hp drat wt qsec vs am gear carb
## 0 0 0 0 0 0 0 0 0 0 0
There are no missing values.
##1.6 Checking for Outliers
boxplot(mtcars$mpg, main = "mpg")
boxplot(mtcars$disp, main = "disp")
boxplot(mtcars$hp, main = "hp")
boxplot(mtcars$drat, main = "drat")
boxplot(mtcars$wt, main = "wt")
boxplot(mtcars$qsec, main = "qsec")
boxplot(mtcars$carb, main = "carb")
There are 4 outliers. They are in: hp, wt, qsec, carb.
##1.7 Standardization
mtcars$hp_rs <- (mtcars$hp - min(mtcars$hp)) / (max(mtcars$hp) - min(mtcars$hp))
max(mtcars$hp_rs)
## [1] 1
min(mtcars$hp_rs)
## [1] 0
##1.8 Winsorize
limits <- quantile(mtcars$wt, prob = c(0.05, 0.95))
limits
## 5% 95%
## 1.73600 5.29275
mtcars$wt_win <- mtcars$wt
mtcars$wt_win[mtcars$wt_win < limits[1]] <- limits[1]
mtcars$wt_win[mtcars$wt_win > limits[2]] <- limits[2]
max(mtcars$wt_win)
## [1] 5.29275
min(mtcars$wt_win)
## [1] 1.736
The new max is 5.293 adn the new min is 1.736