Also added graphing for future analysis questions.
#Added graphing libraries for plotting:
library(ggpubr)
## Loading required package: ggplot2
library(broom)
library(ggplot2)
library(corrplot)
## corrplot 0.95 loaded
motortrend <- mtcars
datasets::mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Dataset and plotting libraries load without any issues.
?mtcars
## starting httpd help server ... done
Analysis: When reviewing the code-book, using knit allows you to see an in depth analysis of the 1974 Motor Trend Magazine about car details. You are able to see the data frame based upon 11 numeric variables for each of the 32 observations about each vehicle’s technical details.
mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
Analysis: The mtcars data set contains realistic values for each of the columns. The first column includes the character data type through all of the vehicles being showcased from the magazine. For the second column, it is represented by mpg through the float type. Cylinders is a standard int value and consists of vehicular data for this column. When looking into another float column, Displacement, the values can connect to the size of the car.
Horsepower contains regular integers for their output in performance with the type of vehicle. Looking into the rear axle ratio, this is a float type. Seeing the overall Weight of each vehicle can be related to pounds by the float variable with the whole number representing a thousand pounds and the decimal numbers afterwards being hundreds of pounds. For the 1/4 mile time, the data is similarly structured to the weight through seconds being the whole number and milliseconds being the decimal. The first column to feature a binary configuration is vs through having either a 0 or 1 for when the configuration is set to a specific v or s shape.
A similar situation occurs where a car could have an auto or manual transmission through the am column, with 1 meaning the vehicle was manual and then 0 being automatic. Number of forward gears is centered around being a standard integer through whole numbers not going beyond the maximum reported values of 5. The relation of gears and number of carburetors can be connected to sharing the whole number data type.
For the summary in mpg, the min in mpg is 10.40 while the max is set to 33.90. The median is centered around 19.20 with the mean average is somewhat higher through 20.09. The 1st quartile is set to 15.43 while the 3rd will be 22.80.
The number of cylinders have some connections to 4.000 through the min and 1st quartile sharing that value. Another relation with the same value consists of the 3rd quartile coupled with the max being 8.000. The median will equal 6.000 with the mean being somewhat higher with 6.188.
Inspecting the disp column allows us to see that the min is quite low with 71.1, with this value being surpassed by the 1st quartile being 120.8 and median being 196.3. The mean is quite high compared to the median with 230.7. The 3rd quartile is distanced at a large amount through 326.0 and the max will be 472.0.
Column hp begins for the summary with a min of 52.0 being succeeded by the 1st quartile of 96.5. The median will be closer to the higher end horsepower range of the era with 123.0 with the mean surpassing the value slightly with 146.7. For the 3rd quartile, it contains a high value of 180.0 with the max being a long shot compared to the previous numbers with 335.0.
Seeing the rear axle ratio data, I can see the values being quite close together through the min being 2.760 and the max including 4.930. The 1st quartile is set to 3.080 and median consisting of 3.695. The mean is somewhat smaller through having a value of 3.597. For the 3rd quartile, the number is also somewhat close to the median through 3.920.
Weight has a median of 3.325 that has been impacted by a heavy max of 5.424. The mean falls into the same situation through 3.217. This is similar through the previous dataset where the median surpasses the mean by a small amount. Min is quite low with 1.513 being followed by an ample increase through the 1st quartile being 2.581. 3rd quantile is closer to the mean with 3.610.
Overseeing the data for the quarter mile times, the min is most distant with 14.50 seconds. The other terms are close together from having values from 16-18 with the 1st quartile being 16.89. Both the mean and median report values that are decimals short of each other with 17.71 and 17.85 respectively. A close number to these float datatypes includes the 3rd quartile through being 18.90. The max will be somewhat distant to these values with a vehicle completing the course in 22.90 seconds.
In terms of shaping of the engine, due to the data being binary for each car row, the min, 1st quartile, and median will share the same value of 0 being the trademark v shape. The 3rd quartile and max is set at 1 due to being a straight-line. With multiple cars having 0’s and 1’s for their engine types, the mean will be set at a decimal of .4375.
Seeing the transmission types for each vehicle is similar to the above column through having binary type organization. The min, 1st quartile, and median all feature the same value of 0.0000, meaning an auto transmission. The max and 3rd quartile are shared in their values through being centered at 1.0000 with that value defining manual operation. The mean will be set at .4062 in terms of comparison through the engine types.
The gears column connect similarly to the real axle ratio’s data through being closely related to each other in float numeric. The max is set to 5.000 while the min will be 3.000. The median will be perfectly placed between these values at 4.000, with the first quartile being 3.000 and the mean being lower than the median in terms of the value of 3.688.
In order to inspect the data for the number of carburetors, the distance of max and min will be quite large with the min being 1.000 while the max will be 8.000. Sharing the number of 2.000 will occur through the 1st and median values. Mean will be somewhat higher with 2.812 with this number being surpassed at a higher amount through 4.000 in the 3rd quartile.
When looking into this data-set, numbers for each row also contain no NA values which is great to see. The header section is correctly formatted coupled with the vehicle rows that are connected to the column data. All of the data is realistic based upon the car data and the features they have.
ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line",
add.params = list(color = "#06276d"),
xlab = "Weight (Tons)", ylab = "Miles Per Gallon")
I utilized the scatter plot to understand correlations in terms of 2 columns being freely selected for the selected data-set and as such I saw connections based upon the arrangement of these columns for x or y. This graph is only an example to show a visualization that has helped me understand the data.
ggscatter(mtcars, x = "hp", y = "wt", add = "reg.line",
add.params = list(color = "#d8b023"),
xlab = "Horsepower", ylab = "Weight")
corr_matrix <- cor(mtcars)
print(corr_matrix)
## mpg cyl disp hp drat wt
## mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.68117191 -0.8676594
## cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.69993811 0.7824958
## disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.71021393 0.8879799
## hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.44875912 0.6587479
## drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.00000000 -0.7124406
## wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.71244065 1.0000000
## qsec 0.4186840 -0.5912421 -0.4336979 -0.7082234 0.09120476 -0.1747159
## vs 0.6640389 -0.8108118 -0.7104159 -0.7230967 0.44027846 -0.5549157
## am 0.5998324 -0.5226070 -0.5912270 -0.2432043 0.71271113 -0.6924953
## gear 0.4802848 -0.4926866 -0.5555692 -0.1257043 0.69961013 -0.5832870
## carb -0.5509251 0.5269883 0.3949769 0.7498125 -0.09078980 0.4276059
## qsec vs am gear carb
## mpg 0.41868403 0.6640389 0.59983243 0.4802848 -0.55092507
## cyl -0.59124207 -0.8108118 -0.52260705 -0.4926866 0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692 0.39497686
## hp -0.70822339 -0.7230967 -0.24320426 -0.1257043 0.74981247
## drat 0.09120476 0.4402785 0.71271113 0.6996101 -0.09078980
## wt -0.17471588 -0.5549157 -0.69249526 -0.5832870 0.42760594
## qsec 1.00000000 0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs 0.74453544 1.0000000 0.16834512 0.2060233 -0.56960714
## am -0.22986086 0.1683451 1.00000000 0.7940588 0.05753435
## gear -0.21268223 0.2060233 0.79405876 1.0000000 0.27407284
## carb -0.65624923 -0.5696071 0.05753435 0.2740728 1.00000000
corrplot(corr_matrix, method="number", type="upper", order="hclust",
tl.col="green", tl.srt=15)
ggplot(mtcars, aes(x = qsec, y = hp)) +
geom_point() +
labs(title = "Patterns of higher Quarter Mile times with horsepower", x = "Quarter Mile Time by sec", y = "Horsepower")
A trend I found was based on when looking into the amount of horsepower a vehicle has compared with it’s data, the trend-line is going upward through horsepower and some outliers in the over 5,000 pound range somewhat about 200 have impacted it’s upward direction. Most of the best cars in this category are between 3,000 and 4,000 pounds with these 4 vehicles achieving 230+ horsepower.
For correlations, I noticed that there is a high correlation with weight and displacement through it’s value being .89. Another high correlation consists of a value of .90 being from cylinders and displacement. Quarter mile times with the correlation value of .74 being connected with engine shape is also quite strong as well.
A pattern through comparing the times with horsepower with quarter mile times relates to most of the vehicles from this data-set are between 16 to 20.5 seconds while the values in horsepower are centered around 230-50.
corr_matrix <- cor(mtcars)
print(corr_matrix)
## mpg cyl disp hp drat wt
## mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.68117191 -0.8676594
## cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.69993811 0.7824958
## disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.71021393 0.8879799
## hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.44875912 0.6587479
## drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.00000000 -0.7124406
## wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.71244065 1.0000000
## qsec 0.4186840 -0.5912421 -0.4336979 -0.7082234 0.09120476 -0.1747159
## vs 0.6640389 -0.8108118 -0.7104159 -0.7230967 0.44027846 -0.5549157
## am 0.5998324 -0.5226070 -0.5912270 -0.2432043 0.71271113 -0.6924953
## gear 0.4802848 -0.4926866 -0.5555692 -0.1257043 0.69961013 -0.5832870
## carb -0.5509251 0.5269883 0.3949769 0.7498125 -0.09078980 0.4276059
## qsec vs am gear carb
## mpg 0.41868403 0.6640389 0.59983243 0.4802848 -0.55092507
## cyl -0.59124207 -0.8108118 -0.52260705 -0.4926866 0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692 0.39497686
## hp -0.70822339 -0.7230967 -0.24320426 -0.1257043 0.74981247
## drat 0.09120476 0.4402785 0.71271113 0.6996101 -0.09078980
## wt -0.17471588 -0.5549157 -0.69249526 -0.5832870 0.42760594
## qsec 1.00000000 0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs 0.74453544 1.0000000 0.16834512 0.2060233 -0.56960714
## am -0.22986086 0.1683451 1.00000000 0.7940588 0.05753435
## gear -0.21268223 0.2060233 0.79405876 1.0000000 0.27407284
## carb -0.65624923 -0.5696071 0.05753435 0.2740728 1.00000000
corrplot(corr_matrix, method="number", type="upper", order="hclust",
tl.col="green", tl.srt=30)
When looking into the data through the correlation matrix, I see how the strongest correlation possible consists of mpg and rear axle ratio. This is based from the larger positive number being more correlated. The most negative correlation in this graph consists of weight and mpg with -.87, which is an inverse relationship as more weight will usually mean less mpg.
nrow(mtcars)
## [1] 32
There is missing data based on the number of rows for the (##) column which contain all of the vehicles. In total there are 32 observations.
ggboxplot(mtcars, x = "cyl", y = "disp",
color = "Purple", palette = "jco",
add = "jitter", add.params = list(size = 0.6),
xlab = "Cylinders", ylab = "Displacement", legend = "none")
When comparing the cylinders by the amount of Displacement, I noticed that most of the outliers were located with 8 cylinders with a total of 7 of them. The 6 and 4 cylinder engines had 5 and 6 outliers respectively.
min_hp_rs <- min(mtcars$hp)
max_hp_rs <- max(mtcars$hp)
mtcars$hp_range_standardized <- (mtcars$hp - min_hp_rs) / (max_hp_rs - min_hp_rs)
summary(mtcars$hp_range_standardized)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.1572 0.2509 0.3346 0.4523 1.0000
After this code has been outputted, the max now shows 1.000 with the min being set to 0.000. 1st quartile changes to .1572 with the 3rd quartile being far from 1.0 through .4523. Both the median and mean also have the same connectivity with the quartiles in terms of the values being centered in .2509 to .3346.
mtcars_truncated <- mtcars[mtcars$wt <= 32, ]
summary(mtcars_truncated$wt)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.513 2.581 3.325 3.217 3.610 5.424
lower_bound_wt_win <- quantile(mtcars_truncated$wt, 0.05, na.rm = TRUE)
upper_bound_wt_win <- quantile(mtcars_truncated$wt, 0.95, na.rm = TRUE)
mtcars_truncated$wt[mtcars_truncated$wt < lower_bound_wt_win] <- lower_bound_wt_win
mtcars_truncated$wt[mtcars_truncated$wt > upper_bound_wt_win] <- upper_bound_wt_win
summary(mtcars_truncated[, "wt"])
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.736 2.581 3.325 3.222 3.610 5.293
The data for the wt column now shows the max being 5.293 and the min is now 1.736. When we utilize the confidence intervals for this column, the weight table will adapt to significance at the .95 and .05 level. Based on the adaptation, there was change in min by .223 pounds and max will shift by .131.