'data.frame': 214 obs. of 10 variables:
$ RI : num 1.52 1.52 1.52 1.52 1.52 ...
$ Na : num 13.6 13.9 13.5 13.2 13.3 ...
$ Mg : num 4.49 3.6 3.55 3.69 3.62 3.61 3.6 3.61 3.58 3.6 ...
$ Al : num 1.1 1.36 1.54 1.29 1.24 1.62 1.14 1.05 1.37 1.36 ...
$ Si : num 71.8 72.7 73 72.6 73.1 ...
$ K : num 0.06 0.48 0.39 0.57 0.55 0.64 0.58 0.57 0.56 0.57 ...
$ Ca : num 8.75 7.83 7.78 8.22 8.07 8.07 8.17 8.24 8.3 8.4 ...
$ Ba : num 0 0 0 0 0 0 0 0 0 0 ...
$ Fe : num 0 0 0 0 0 0.26 0 0 0 0.11 ...
$ Type: Factor w/ 6 levels "1","2","3","5",..: 1 1 1 1 1 1 1 1 1 1 ...
# Summary of all variablessummary(Glass)
RI Na Mg Al
Min. :1.511 Min. :10.73 Min. :0.000 Min. :0.290
1st Qu.:1.517 1st Qu.:12.91 1st Qu.:2.115 1st Qu.:1.190
Median :1.518 Median :13.30 Median :3.480 Median :1.360
Mean :1.518 Mean :13.41 Mean :2.685 Mean :1.445
3rd Qu.:1.519 3rd Qu.:13.82 3rd Qu.:3.600 3rd Qu.:1.630
Max. :1.534 Max. :17.38 Max. :4.490 Max. :3.500
Si K Ca Ba
Min. :69.81 Min. :0.0000 Min. : 5.430 Min. :0.000
1st Qu.:72.28 1st Qu.:0.1225 1st Qu.: 8.240 1st Qu.:0.000
Median :72.79 Median :0.5550 Median : 8.600 Median :0.000
Mean :72.65 Mean :0.4971 Mean : 8.957 Mean :0.175
3rd Qu.:73.09 3rd Qu.:0.6100 3rd Qu.: 9.172 3rd Qu.:0.000
Max. :75.41 Max. :6.2100 Max. :16.190 Max. :3.150
Fe Type
Min. :0.00000 1:70
1st Qu.:0.00000 2:76
Median :0.00000 3:17
Mean :0.05701 5:13
3rd Qu.:0.10000 6: 9
Max. :0.51000 7:29
Looking at the distributions of the predictors, RI, K, Ca, Ba, and Fe appear to be right-skewed. Mg appears to be left-skewed, with most observations concentrated at the higher end of its range. Si also shows some left skewness. Na and Al appear more symmetric compared with the other predictors, although Al may have a slight right skew. Ba and Fe have many observations near zero and show particularly strong right skewness.
###Relationship between predictors
pairs(predictors)
round(cor(predictors), 2)
RI Na Mg Al Si K Ca Ba Fe
RI 1.00 -0.19 -0.12 -0.41 -0.54 -0.29 0.81 0.00 0.14
Na -0.19 1.00 -0.27 0.16 -0.07 -0.27 -0.28 0.33 -0.24
Mg -0.12 -0.27 1.00 -0.48 -0.17 0.01 -0.44 -0.49 0.08
Al -0.41 0.16 -0.48 1.00 -0.01 0.33 -0.26 0.48 -0.07
Si -0.54 -0.07 -0.17 -0.01 1.00 -0.19 -0.21 -0.10 -0.09
K -0.29 -0.27 0.01 0.33 -0.19 1.00 -0.32 -0.04 -0.01
Ca 0.81 -0.28 -0.44 -0.26 -0.21 -0.32 1.00 -0.11 0.12
Ba 0.00 0.33 -0.49 0.48 -0.10 -0.04 -0.11 1.00 -0.06
Fe 0.14 -0.24 0.08 -0.07 -0.09 -0.01 0.12 -0.06 1.00
##Interpretation:
The scatterplot matrix and correlation matrix show relationships among several predictors. The strongest relationship is between RI and Ca, with a correlation of 0.81, indicating a strong positive relationship. RI and Si have a moderate negative relationship with a correlation of -0.54. Other moderate relationships include Mg and Ba (-0.49), Mg and Al (-0.48), Al and Ba (0.48), and Mg and Ca (-0.44). Many of the remaining predictor pairs have relatively weak linear relationships.
b. Do there appear to be any outliers in the data? Are any predictors skewed?
Based on the boxplots, most predictors appear to contain potential outliers. RI, Na, Al, Si, K, Ca, Ba, and Fe have observations outside the boxplot whiskers, while Mg does not show obvious outliers. From the histograms, RI, K, Ca, Ba, and Fe are right-skewed, while Mg and Si show left skewness. Na and Al appear more symmetric compared with the other predictors.
c. Are there any relevant transformations of one or more predictors that might improve the classification model?
par(mfrow =c(2, 2))hist(Glass$K, main ="Original K")hist(sqrt(Glass$K), main ="Square Root")hist(log1p(Glass$K), main ="Log(1 + K)")hist(log(Glass$K +0.01), main ="Log(K + 0.01)")
par(mfrow =c(2, 2))hist(Glass$Ca, main ="Original Ca")hist(sqrt(Glass$Ca), main ="Square Root")hist(log1p(Glass$Ca), main ="Log(1 + Ca)")hist(log(Glass$Ca), main ="Log(Ca)")
par(mfrow =c(2, 2))hist(Glass$Ba, main ="Original Ba")hist(sqrt(Glass$Ba), main ="Square Root")hist(log1p(Glass$Ba), main ="Log(1 + Ba)")hist(log(Glass$Ba +0.01), main ="Log(Ba + 0.01)")
par(mfrow =c(2, 2))hist(Glass$Fe, main ="Original Fe")hist(sqrt(Glass$Fe), main ="Square Root")hist(log1p(Glass$Fe), main ="Log(1 + Fe)")hist(log(Glass$Fe +0.01), main ="Log(Fe + 0.01)")
##Interpretation:
Several predictors are skewed, so transformations may be considered. For Ca, square-root and log transformations reduce the long right tail, with a log transformation providing a more compressed distribution. K is also strongly right-skewed, but it contains many zero values, making a standard log transformation inappropriate. Square-root and log1p transformations can accommodate the zeros but do not completely remove the skewness. Ba and Fe contain a large number of zero values, so their skewness is partly caused by the concentration of observations at zero. Log transformations with an added constant preserve these observations but do not necessarily produce symmetric distributions. Therefore, transformations may be useful for some predictors, especially Ca, but their effect on classification performance should ultimately be evaluated during model training.
a. Investigate the frequency distributions for the categorical predictors. Are any of the distributions degenerate in the ways discussed earlier in this chapter?
The frequency distributions show that several categorical predictors are highly unbalanced. For example, mycelium is dominated by category 0, with 639 observations compared with only 6 observations in category 1. Other predictors such as sclerotia, leaf.malf, shriveling, and leaf.mild also show strong imbalance toward one category. In contrast, variables such as plant.stand and crop.hist have more balanced distributions across their categories. Therefore, some predictors appear to have near-zero variation and may provide limited information for prediction.
b. Roughly 18% of the data are missing. Are there particular predictors that are more likely to be missing? Is the pattern of missing data related to the classes?
There are particular predictors that are more likely to have missing values than others. The missing values are also not evenly distributed across the classes. Some classes, such as 2-4-d-injury, cyst-nematode, and herbicide-injury, have several predictors with 100% missing values, while phytophthora-rot has many predictors with about 77% missing values. Therefore, the pattern of missing data appears to be related to the classes.
C. Develop a strategy for handling missing data, either by eliminating predictors or imputation.
I would use imputation rather than eliminate predictors because removing predictors could result in the loss of useful information. Since the predictors are categorical, I used mode imputation, replacing each missing value with the most frequent observed category for that predictor. After imputation, there were no remaining missing values. However, because the missing-data pattern appears to be related to the disease classes, mode imputation should be used carefully, especially for predictors with high percentages of missing values.