#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)
Warning: package ‘plotly’ was built under R version 4.3.2
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
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)
Warning: package ‘ggcorrplot’ was built under R version 4.3.2
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)
Warning: package ‘factoextra’ was built under R version 4.3.2Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
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")
Warning: package ‘corrplot’ was built under R version 4.3.2corrplot 0.92 loaded
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)
#Clustering
kmeans_result = kmeans(seeds[ , 1:7], centers = 3 )
kmeans_result
K-means clustering with 3 clusters of sizes 60, 76, 63
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 12.00987 13.29947 0.8522934 5.236461 2.880421 4.577118 5.085329
3 14.80794 14.53714 0.8798778 5.594016 3.296063 2.719162 5.212587
Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 2 3 3 3 2 3 3 2 2 3 3 3 3 3 3 3 3 1 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[53] 3 3 3 3 3 2 2 2 2 3 3 3 3 2 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 3 1 1 1 1 1 1 1
[105] 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 1 1 1 3 3 3 1 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[157] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Within cluster sum of squares by cluster:
[1] 183.4582 203.7342 160.8177
(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)
Warning: package ‘e1071’ was built under R version 4.3.2
library(caret)
Warning: package ‘caret’ was built under R version 4.3.2
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, -8]
predict(svm_model, newdata = test_data[24,-8])
114
1.584717
# 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)
Error in confusionMatrix.default(predictions, test_data$Type) :
The data must contain some levels that overlap the reference.
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")
#2. Using regression dataset
library(datasets)
admission = read.csv('US Admission.csv')
admission
admission = admission[, -1]
admission
#Plot linear regression
x = admission$Chance.of.Admit
y = admission$GRE.Score
plot(x, y)
x = admission$Chance.of.Admit
y = admission$TOEFL.Score
plot(x, y)
x = admission$Chance.of.Admit
y = admission$University.Rating
plot(x, y)
x = admission$Chance.of.Admit
y = admission$SOP
plot(x, y)
x = admission$Chance.of.Admit
y = admission$LOR
plot(x, y)
x = admission$Chance.of.Admit
y = admission$CGPA
plot(x, y)
x = admission$Chance.of.Admit
y = admission$Research
plot(x, y)
#Plot polynomial regression
ggplot(admission, aes(x =Chance.of.Admit , y=GRE.Score)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.95,)+
ggtitle(paste("Polynomial Regression (Power 2) -", "GRE.Score vs Chance of Admit"))
ggplot(admission, aes(x =Chance.of.Admit , y=TOEFL.Score
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)+
ggtitle(paste("Polynomial Regression (Power 2) -", "TOEFL.Score vs Chance of Admit"))
ggplot(admission, aes(x =Chance.of.Admit , y=University.Rating
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)
#title(paste("Polynomial Regression (Power 2)-","Rating vs chance of admit"))
ggplot(admission, aes(x =Chance.of.Admit , y=SOP
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)
ggplot(admission, aes(x =Chance.of.Admit , y=LOR
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)
ggplot(admission, aes(x =Chance.of.Admit , y=CGPA
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)
ggplot(admission, aes(x =Chance.of.Admit , y=Research
)) +
geom_point() +
geom_smooth(method = "lm", formula = y~poly(x, 2), level = 0.9,)
#Regression Model
lm_model = lm(Chance.of.Admit ~GRE.Score+TOEFL.Score+University.Rating+SOP+LOR+CGPA+Research,data=admission)
summary(lm_model)
Call:
lm(formula = Chance.of.Admit ~ GRE.Score + TOEFL.Score + University.Rating +
SOP + LOR + CGPA + Research, data = admission)
Residuals:
Min 1Q Median 3Q Max
-0.26259 -0.02103 0.01005 0.03628 0.15928
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.2594325 0.1247307 -10.097 < 2e-16 ***
GRE.Score 0.0017374 0.0005979 2.906 0.00387 **
TOEFL.Score 0.0029196 0.0010895 2.680 0.00768 **
University.Rating 0.0057167 0.0047704 1.198 0.23150
SOP -0.0033052 0.0055616 -0.594 0.55267
LOR 0.0223531 0.0055415 4.034 6.6e-05 ***
CGPA 0.1189395 0.0122194 9.734 < 2e-16 ***
Research 0.0245251 0.0079598 3.081 0.00221 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.06378 on 392 degrees of freedom
Multiple R-squared: 0.8035, Adjusted R-squared: 0.8
F-statistic: 228.9 on 7 and 392 DF, p-value: < 2.2e-16