#Load the dataset
seeds = read.csv('seeds.csv')
seeds
#ViolinPlot
#library(ggplot2)
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Length, fill = as.factor(Type))) +
geom_violin() +
labs(
title = "Violinplot of Kernel Length by Type",
x = "Type",
y = "Kernel Length",
caption = "Source: Iskulghar"
)
#theme_minimal()
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Width, fill = as.factor(Type))) +
geom_violin() +
labs(
title = "Violinplot of Kernel Width by Type",
x = "Type",
y = "Kernel Width",
caption = "Source: Iskulghar"
)
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Groove, fill = as.factor(Type))) +
geom_violin() +
labs(
title = "Violinplot of Kernel Groove by Type",
x = "Type",
y = "Kernel Groove",
caption = "Source: Iskulghar"
)
#Boxplot
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Length, fill = as.factor(Type))) +
geom_boxplot() +
labs(
title = "Boxplot of Kernel Length by Type",
x = "Type",
y = "Kernel Length",
caption = "Source: Iskulghar"
)
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Width, fill = as.factor(Type))) +
geom_boxplot() +
labs(
title = "Boxplot of Kernel widthth by Type",
x = "Type",
y = "Kernel widthth",
caption = "Source: Iskulghar"
)
ggplot(data = seeds, aes(x = as.factor(Type), y = Kernel.Groove, fill = as.factor(Type))) +
geom_boxplot() +
labs(
title = "Violinplot of Kernel Groove by Type",
x = "Type",
y = "Kernel Groove",
caption = "Source: Iskulghar"
)
#Interactive plot
library(plotly)
fig = seeds %>%
plot_ly(y = ~Kernel.Length, type = 'violin')
fig
NA
fig = seeds %>%
plot_ly(y = ~Kernel.Width, type = 'violin')
fig
fig = seeds %>%
plot_ly(y = ~Perimeter, type = 'violin')
fig
plot_ly(seeds, y = ~Kernel.Length, type = 'box')
plot_ly(seeds, y = ~Kernel.Width, type = 'box')
plot_ly(seeds, y = ~Perimeter, type = 'box')
cor(seeds$Kernel.Length, seeds$Kernel.Width)
[1] 0.8626799
cor(seeds$Kernel.Width, seeds$Kernel.Width)
[1] 1
cor_matrix = cor(seeds[ ,1:7])
cor_matrix
Area Perimeter Compactness Kernel.Length Kernel.Width Asymmetry.Coeff Kernel.Groove
Area 1.0000000 0.9944373 0.6098875 0.9511258 0.9709796 -0.222820450 0.862727206
Perimeter 0.9944373 1.0000000 0.5318407 0.9729442 0.9454957 -0.211034252 0.889543418
Compactness 0.6098875 0.5318407 1.0000000 0.3740113 0.7621670 -0.329400178 0.227016463
Kernel.Length 0.9511258 0.9729442 0.3740113 1.0000000 0.8626799 -0.169704426 0.932124102
Kernel.Width 0.9709796 0.9454957 0.7621670 0.8626799 1.0000000 -0.253122410 0.748227942
Asymmetry.Coeff -0.2228204 -0.2110343 -0.3294002 -0.1697044 -0.2531224 1.000000000 -0.003349395
Kernel.Groove 0.8627272 0.8895434 0.2270165 0.9321241 0.7482279 -0.003349395 1.000000000
library(ggcorrplot)
ggcorrplot(cor_matrix)
ggcorrplot(cor_matrix, type = "lower")
ggcorrplot(cor_matrix,
type = "lower",
colors = c("purple", "white", "red"),
lab = TRUE)
library(GGally)
pairs_plot= ggpairs(seeds, aes(colour = as.factor(Type)))+
theme_grey(base_size = 14) + # Adjust the base font size
theme(axis.text = element_text(size = 18), # Adjust axis text size
axis.title = element_text(size = 20))
pairs_plot
# Adjust axis title size
ggsave("pairs_plot_500dpi.png", plot=pairs_plot, height = 10, width = 15)
library(stats)
seeds_pca = prcomp(seeds[ , -8], scale = TRUE, center = TRUE)
seeds_pca
Standard deviations (1, .., p=7):
[1] 2.24310190 1.09504983 0.82232374 0.26115727 0.13715851 0.07290793 0.02850049
Rotation (n x k) = (7 x 7):
PC1 PC2 PC3 PC4 PC5 PC6 PC7
Area -0.4444453 -0.02689510 0.02541939 0.19763627 -0.19751625 -0.42905925 -0.734121454
Perimeter -0.4415674 -0.08298812 -0.06019769 0.30034829 -0.16637062 -0.47503082 0.671539178
Compactness -0.2779150 0.52535969 0.63238211 -0.34000075 0.32627308 -0.13947927 0.073104145
Kernel.Length -0.4241261 -0.20007500 -0.21443932 0.25098045 0.76832639 0.28042137 -0.046369104
Kernel.Width -0.4329366 0.11572453 0.21512380 0.20373132 -0.46870015 0.70024261 0.037006864
Asymmetry.Coeff 0.1157433 -0.72122562 0.67513087 0.09326364 0.04045161 -0.01691821 0.003839455
Kernel.Groove -0.3866993 -0.37788656 -0.21853787 -0.80120818 -0.12333864 0.03920887 0.034960897
summary(seeds_pca)
Importance of components:
PC1 PC2 PC3 PC4 PC5 PC6 PC7
Standard deviation 2.2431 1.0950 0.8223 0.26116 0.13716 0.07291 0.02850
Proportion of Variance 0.7188 0.1713 0.0966 0.00974 0.00269 0.00076 0.00012
Cumulative Proportion 0.7188 0.8901 0.9867 0.99644 0.99912 0.99988 1.00000
pc_12 = data.frame(seeds_pca$x[ , 1:3])
head(pc_12)
pc_12_class = cbind(pc_12, Target = seeds$Type)
pc_12_class
library(factoextra)
fviz_eig(seeds_pca, xlab = "Principal Component", ylab = "Percentage of explained variance",addlabels = TRUE)
fviz_pca_var(seeds_pca,
col.var = "contrib",gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE)
library("corrplot")
var = get_pca_var(seeds_pca)
corrplot(var$cos2)
#seeds$Type_numeric = as.numeric(seeds$Type)+
fviz_pca_ind(seeds_pca,
geom.ind = "point",
#Target = seeds$Type,
col.ind = seeds$Type,
addEllipses = TRUE)
Error in `train_continuous()`:
! Discrete value supplied to a continuous scale
Backtrace:
1. base (local) `<fn>`(x)
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. base::lapply(data, scales_train_df, scales = npscales)
6. ggplot2 (local) FUN(X[[i]], ...)
...
10. ggplot2 (local) train_df(..., self = self)
11. self$train(df[[aesthetic]])
12. ggplot2 (local) train(..., self = self)
13. self$range$train(x)
14. scales::train_continuous(x, self$range)
kmeans_result = kmeans(seeds[ , 1:7], centers = 3 )
kmeans_result
K-means clustering with 3 clusters of sizes 60, 63, 76
Cluster means:
Area Perimeter Compactness Kernel.Length Kernel.Width Asymmetry.Coeff Kernel.Groove
1 18.71967 16.29950 0.8847450 6.209883 3.721283 3.616267 6.063867
2 14.80794 14.53714 0.8798778 5.594016 3.296063 2.719162 5.212587
3 12.00987 13.29947 0.8522934 5.236461 2.880421 4.577118 5.085329
Clustering vector:
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 3 2 2 2 2 2 2 2 2 1 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[53] 2 2 2 2 2 3 3 3 3 2 2 2 2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
[105] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 2 2 2 1 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[157] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Within cluster sum of squares by cluster:
[1] 183.4582 160.8177 203.7342
(between_SS / total_SS = 78.7 %)
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss" "size"
[8] "iter" "ifault"
library(cluster)
clusplot(seeds, kmeans_result$cluster)
#Classification
library(lattice)
library(e1071)
library(caret)
train_ix = createDataPartition(seeds$Type, p = 0.8, list = FALSE)
train_data = seeds[train_ix, ]
test_data = seeds[-train_ix, ]
train_data
test_data
svm_model = svm(Type ~ Area+Perimeter+Compactness+Kernel.Length+Kernel.Width+Asymmetry.Coeff+Kernel.Groove, data = train_data, kernel = "linear")
test_data[24, ]
predict(svm_model, newdata = test_data[24,-8])
122
1.907449
# Generate predictions
predictions = predict(svm_model, newdata = test_data)
# Convert predictions to factor with levels of test_data$Type
predictions = factor(predictions, levels = levels(test_data$Type))
# Convert test_data$Type to factor if it's not already
test_data$Type <- factor(test_data$Type, levels = levels(predictions))
# Create confusion matrix
conf_matrix = confusionMatrix(predictions, test_data$Type)
conf_matrix
Confusion Matrix and Statistics
Reference
Prediction 1 2 3
1 0 0 0
2 0 0 0
3 0 0 0
Overall Statistics
Accuracy : NaN
95% CI : (NA, NA)
No Information Rate : NA
P-Value [Acc > NIR] : NA
Kappa : NaN
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: 1 Class: 2 Class: 3
Sensitivity NA NA NA
Specificity NA NA NA
Pos Pred Value NA NA NA
Neg Pred Value NA NA NA
Prevalence NaN NaN NaN
Detection Rate NaN NaN NaN
Detection Prevalence NaN NaN NaN
Balanced Accuracy NA NA NA
cm = as.data.frame(conf_matrix$table)
ggplot(cm, aes(Prediction, Reference, fill = Freq)) +
geom_tile() +
geom_text(aes(label = Freq)) +
scale_fill_gradient(low="purple", high="red")