s_data<-read.csv('E:/R files/Soikot_vai/New folder/seeds.csv')
s_new_data<-s_data
s_new_data
3.Write a few sentences to explain the dataset.
# Dataset contain 8 columns and 199 rows where 8th column is a categorical variable
# a) Numerical variable are all the varibales except "Type" in the dataset
# b)Categorical variable is "Type"
# c) Target varibale is Kernel groove
4.Remove all the categorical columns (not the target column) from the dataset. Now we will call all the numerical columns as features and the last column as target or class.
s_data<-s_new_data[,-8]
s_data
5.Use the basic plot function to plot the following and explain each figure in (2-3) sentences.
plot(s_data)
a.Scatter plot of any two features - each class (target) should be represented with different colors.
plot(s_data$Area,s_data$Perimeter, xlab="Area", ylab="Perimeter", main=" Scatter plot",col.main = 'blue',
col.axis = 'red',
col.lab = 'cyan',)
hist(s_data$Perimeter,xlab="Perimeter", ylab="Frequency", main=" Histogram of Perimeter",col.main = 'blue',
col.axis = 'red',
col.lab = "yellow",)
boxplot(s_data$Perimeter,xlab="Perimeter", ylab="Frequency", main=" Boxplot of Perimeter",col.main = 'blue',
col.axis = 'red',
col.lab = 'green')
Scatterplot
library(ggplot2)
ggplot(s_data, aes(x=s_data$Area,y=s_data$Perimeter))+
geom_point()+
geom_smooth(method = "lm")+
theme_bw()+
labs(title = "Area Vs Perimeter",
x = "Area",
y = "Perimeter")
Histogram
ggplot(s_data, aes(x=s_data$Perimeter))+
geom_histogram()+
labs(title = " Perimeter",
x = "Area",
y = "Freuency")
Boxplot
s_new_data$Type<-as.factor(s_new_data$Type)
ggplot(s_new_data, aes(Type,Kernel.Length, fill=Type))+
geom_boxplot()+
labs(title = " Kernel length",
x = "type",
y = "Kernel length")
ggplot(s_new_data, aes(Area,Perimeter, fill=Type))+
geom_point()+
geom_smooth(method = "lm")+
theme_bw()+
labs(title = "Area Vs Perimeter",
x = "Area",
y = "Perimeter")
data<-read.csv('E:/R files/Soikot_vai/New folder/seeds.csv')
data$Type<-factor(data$Type, labels = c('One','Two','Three'))
data
library(plotly)
area<-data %>%
plot_ly(y= ~ data$Area, type = 'violin')%>%
layout(title="Area Violin Plot", yaxis = list(title = 'Area'))
area
Perimeter<-data %>%
plot_ly(y= ~ data$Perimeter, type = 'violin')%>%
layout(title="Perimeter Violin Plot", yaxis = list(title = 'Perimeter'), plot_bgcolor = "#B2F3E1")
Perimeter
Compactness<-data %>%
plot_ly(y= ~ data$Compactness, type = 'violin')%>%
layout(title="Compactness Violin Plot", yaxis = list(title = 'Compactness'), plot_bgcolor = "#D3FBF0")
Compactness
Kernel_lenght<-data %>%
plot_ly(y= ~ data$Kernel.Length, type = 'violin')%>%
layout(title="Compactness Violin Plot", yaxis = list(title = 'Kernel Length'), plot_bgcolor = "#FBF5D3")
Kernel_lenght
Kernel_width<-data %>%
plot_ly(y= ~ data$Kernel.Width, type = "violin") %>%
layout(title=" Kernel Width Violin Plot", yaxix=list(title="Kernel Width"), plot_bgcolor="#AAB7B8")
Kernel_width
Warning: 'layout' objects don't have these attributes: 'yaxix'
Valid attributes include:
'_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
Warning: 'layout' objects don't have these attributes: 'yaxix'
Valid attributes include:
'_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
Asymmetry_Coeff<-data %>%
plot_ly(y=~ data$Asymmetry.Coeff, type = "violin") %>%
layout(title="Assymetric Coefficient Plot", yaxis=list(title="Asymetric Coefficient"), plot_bgcolor="#D6EAF8")
Asymmetry_Coeff
kernel_groove<-data%>%
plot_ly(y= ~ data$Kernel.Groove, type = "violin")%>%
layout(title="Kernel Groove plot", yaxis=list(title="Kernel Groove"), plot_bgcolor="#D6EAF8")
kernel_groove
library(ggplot2)
data$Type<-as.factor(data$Type)
ggplot(data, aes(x=Type, y=Compactness, fill=Type))+
geom_violin()+
labs(title = "Violin Plot of Compactness",
x="Type",
y="Compactness",
caption = "Source: Seeds Dataset")
area<-data%>%
plot_ly(x=~data$Type,y=~data$Area, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Area"))
area
Perimeter<-data%>%
plot_ly(x=~data$Type,y=~data$Perimeter, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Perimeter"))
Perimeter
Compactness<-data%>%
plot_ly(x=~data$Type,y=~data$Compactness, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Compactness"))
Compactness
Kernel_Length<-data%>%
plot_ly(x=~data$Type,y=~data$Kernel.Length, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Kernel Length"))
Kernel_Length
Kernel_width<-data%>%
plot_ly(x=~data$Type,y=~data$Kernel.Width, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Kernel Width"))
Kernel_width
Asymmetry_Coeff<-data%>%
plot_ly(x=~data$Type,y=~data$Asymmetry.Coeff, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Astmmetric Coefficient"))
Asymmetry_Coeff
kernel_groove<-data%>%
plot_ly(x=~data$Type,y=~data$Kernel.Groove, type = "box")%>%
layout(title="Boxplot", xaxis=list(title="Type"),yaxis=list(title="Kernel Groove"))
kernel_groove
correlation_matrix<-cor(data[ ,1:7])
correlation_matrix
Area Perimeter Compactness Kernel.Length Kernel.Width Asymmetry.Coeff
Area 1.0000000 0.9944373 0.6098875 0.9511258 0.9709796 -0.222820450
Perimeter 1.0000000 0.5318407 0.9729442 0.9454957 -0.211034252 0.6098875 0.5318407 1.0000000 0.3740113 0.7621670 -0.329400178
Kernel.Length 0.9511258 0.9729442 0.3740113 1.0000000 0.8626799 -0.169704426
Kernel.Width 0.9709796 0.9454957 0.7621670 0.8626799 1.0000000 -0.253122410
Asymmetry.Coeff -0.2228204 -0.2110343 -0.3294002 -0.1697044 -0.2531224 1.000000000
Kernel.Groove 0.8895434 0.2270165 0.9321241 0.7482279 -0.003349395
Kernel.Groove
Area 0.862727206
Perimeter 0.889543418
Compactness 0.227016463
Kernel.Length 0.932124102 0.748227942
Asymmetry.Coeff -0.003349395
Kernel.Groove 1.000000000
#Explaination
new_data<-data[ ,1:7]
library(PerformanceAnalytics)
chart.Correlation(new_data, histogram = FALSE,method = c("pearson", "kendall",
"spearman"))
library(ggcorrplot)
matrix<-ggcorrplot(correlation_matrix, type = "lower",lab = TRUE)
matrix
pca<-prcomp(new_data, scale =TRUE, center = TRUE )
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
Area -0.4444453 -0.02689510 0.02541939 0.19763627 -0.19751625 -0.42905925
Perimeter -0.4415674 -0.08298812 -0.06019769 0.30034829 -0.16637062 -0.47503082
Compactness -0.2779150 0.52535969 0.63238211 -0.34000075 0.32627308 -0.13947927
Kernel.Length -0.4241261 -0.20007500 -0.21443932 0.25098045 0.76832639 0.28042137
Kernel.Width -0.4329366 0.11572453 0.21512380 0.20373132 -0.46870015 0.70024261
Asymmetry.Coeff 0.1157433 -0.72122562 0.67513087 0.09326364 0.04045161 -0.01691821
Kernel.Groove -0.3866993 -0.37788656 -0.21853787 -0.80120818 -0.12333864 0.03920887
PC7
Area -0.734121454
Perimeter 0.671539178
Kernel.Length -0.046369104
Kernel.Width 0.037006864
Asymmetry.Coeff 0.003839455
Kernel.Groove 0.034960897
summary(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
library(factoextra)
screeplot<-fviz_eig(pca, addlabels = TRUE)
screeplot
contib_plot<-fviz_pca_var(pca, col.var = 'contrib')
contib_plot
library(corrplot)
var<-get_pca_var(pca)
contib_heat<-corrplot(var$cos2)
contib_heat
$corr
Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 Dim.6
Area 0.99388142 0.0008673894 0.000436934 0.002664020 7.339242e-04 9.785523e-04
Perimeter 0.0082584707 0.002450447 0.006152542 5.207134e-04 1.199480e-03
Compactness 0.38861715 0.3309643840 0.270423736 0.007884315 2.002664e-03 1.034115e-04
Kernel.Length 0.90508196 0.0480013454 0.031095281 0.004296196 1.110547e-02 4.179956e-04
Kernel.Width 0.94307576 0.0160590036 0.031294108 0.002830870 4.132717e-03 2.606433e-03
Asymmetry.Coeff 0.06740463 0.6237492711 0.308220545 3.078345e-05 1.521451e-06
Kernel.Groove 0.75239318 0.1712342555 0.043781939 2.861830e-04
Dim.7 4.377643e-04
Perimeter
Compactness 4.340987e-06
Kernel.Length 1.746473e-06
Kernel.Width 1.112421e-06
Asymmetry.Coeff 1.197412e-08
Kernel.Groove 9.928180e-07
$corrPos
$arg
$arg$type
[1] "full"
fviz_pca_ind(pca,
geom.ind = 'point',
col.ind = data$Type,
addEllipses = TRUE)
library(lattice)
library(e1071)
library(caret)
train_index<-createDataPartition(data$Type, p=0.8, list = FALSE)
train_data<-data[train_index, ]
train_data
test_data<-data[-train_index, ]
test_data
svm_model<-svm(Type ~ Area+Perimeter+Compactness+Kernel.Length+Kernel.Width+Asymmetry.Coeff+Kernel.Groove, data = train_data, kernel='linear')
svm_model
Call:
svm(formula = Type ~ Area + Perimeter + Compactness + Kernel.Length + Kernel.Width +
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 1
Number of Support Vectors: 35
prediction<-predict(svm_model,newdata = test_data)
prediction
10 16 21 23 30 41 45 56 62 63 64 66 68 83 89
One One One Three One One One One One One One Three Two Two Two
95 100 103 106 108 110 117 122 144 145 157 160 165 167
Two Two Two Two Two Two Two Two Two Three Three Three Three Three Three
181 184 185 188 189 192 196
Three Three Three Three One Three Three
Levels: One Two Three
library(ggplot2)
confusion_matrix<-confusionMatrix(prediction,test_data$Type)
confusion_matrix
Confusion Matrix and Statistics
Reference
Prediction One Two Three
One 11 0 1
Two 0 13 0
Three 2 0 12
Overall Statistics
Accuracy : 0.9231
95% CI : (0.7913, 0.9838)
No Information Rate : 0.3333
P-Value [Acc > NIR] : 1.879e-14
Kappa : 0.8846
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: One Class: Two Class: Three
Sensitivity 0.8462 1.0000 0.9231
Specificity 0.9615 1.0000 0.9231
Pos Pred Value 0.9167 1.0000 0.8571
Neg Pred Value 0.9259 1.0000 0.9600
Prevalence 0.3333 0.3333 0.3333
Detection Rate 0.2821 0.3333 0.3077
Detection Prevalence 0.3077 0.3590 0.9038 1.0000 0.9231
cnf<-as.data.frame(confusion_matrix$table)
ggplot(cnf,aes(Prediction,Reference,fill=Freq))+
geom_tile()+
geom_text(aes(label=Freq))+
scale_fill_gradient(low = 'white',high = 'orange')
ad_data<-read.csv('E:/R files/Soikot_vai/New folder/US Admission.csv')
ad_data
new_ad_data<-ad_data[ ,-1]
new_ad_data
library(GGally)
ggpairs(new_ad_data)
lm_T_model<-lm(Chance.of.Admit ~ TOEFL.Score, data = new_ad_data)
lm_T_model
Call:
lm(formula = Chance.of.Admit ~ TOEFL.Score, data = new_ad_data)
Coefficients:
TOEFL.Score
-1.2734 0.0186
summary(lm_T_model)
Call:
lm(formula = Chance.of.Admit ~ TOEFL.Score, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.31252 -0.05128 0.01328 0.05453 0.21067
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.2734005 0.0774217 -16.45 <2e-16 ***
TOEFL.Score 0.0185993 0.0007197 25.84 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.08725 on 398 degrees of freedom
Multiple R-squared: 0.6266, Adjusted R-squared: 0.6257
F-statistic: 667.9 on 1and 398 DF, p-value: < 2.2e-16
lm_U_model<-lm(Chance.of.Admit ~ University.Rating, data = new_ad_data)
lm_U_model
Call:
lm(formula = Chance.of.Admit ~ University.Rating, data = new_ad_data)
University.Rating
0.45054 0.08868
summary(lm_U_model)
Call:
lm(formula = Chance.of.Admit ~ University.Rating, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.38527 -0.04560 0.01473 0.06341 0.27209
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.450537 0.014463 31.15 <2e-16 ***
University.Rating 0.088684 0.004393 20.19 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1004 on 398 degrees of freedom
Multiple R-squared: 0.5059, Adjusted R-squared: 0.5046
F-statistic: 407.5 on 1 and 398 DF, p-value: < 2.2e-16
lm_S_model<-lm(Chance.of.Admit ~ SOP, data = new_ad_data)
lm_S_model
Call:
lm(formula = Chance.of.Admit ~ SOP, data = new_ad_data)
Coefficients:
(Intercept) SOP
0.39894 0.09571
summary(lm_S_model)
Call:
lm(formula = Chance.of.Admit ~ SOP, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.49748 -0.05392 0.01823 0.07037 0.22393
Coefficients:
Estimate Std. Error Pr(>|t|)
(Intercept) 0.398942 0.018556 21.50 <2e-16 ***
SOP 0.095708 0.005233 18.29 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1053 398 degrees of freedom
Multiple R-squared: 0.4566, Adjusted R-squared: 0.4552
F-statistic: 334.4 and 398 DF, p-value: < 2.2e-16
lm_L_model<-lm(Chance.of.Admit ~ LOR, data = new_ad_data)
lm_L_model
Call:
lm(formula = Chance.of.Admit ~ LOR, data = new_ad_data)
Coefficients:
(Intercept) LOR
0.3573 0.1063
summary(lm_L_model)
Call:
lm(formula = Chance.of.Admit ~ LOR, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.34940 -0.06256 0.00060 0.07389 0.29325
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.357256 0.021072 16.95 <2e-16 ***
LOR 0.106327 0.005907 18.00 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.106 on 398 degrees of freedom
Multiple R-squared: 0.4488, Adjusted R-squared: 0.4474
F-statistic: 324 on 1 and 398 DF, p-value: < 2.2e-16
lm_G_model<-lm(Chance.of.Admit ~ CGPA, data = new_ad_data)
lm_G_model
Call:
lm(formula = Chance.of.Admit ~ CGPA, data = new_ad_data)
Coefficients:
(Intercept) CGPA
-1.0715 0.2088
summary(lm_G_model)
Call:
lm(formula = Chance.of.Admit ~ CGPA, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.274575 -0.030084 0.009443 0.041954 0.180734
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.07151 0.05034 -21.29 <2e-16 ***
CGPA 0.20885 0.00584 35.76 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.06957 on 398 degrees of freedom
Multiple R-squared: 0.7626, Adjusted R-squared: 0.762
F-statistic: 1279 on 1 and 398 DF, p-value: < 2.2e-16
ggplot(new_ad_data, aes(x=Chance.of.Admit,y=TOEFL.Score))+
geom_point()+
geom_smooth(method = 'lm', formula = y ~ poly(x,2), level=0.95)
ggplot(new_ad_data, aes(x=Chance.of.Admit,y=University.Rating))+
geom_point()+
geom_smooth(method = 'lm', formula = y ~ poly(x,7), level=0.95)
ggplot(new_ad_data, aes(x=Chance.of.Admit,y=SOP))+
geom_point()+
geom_smooth(method = 'lm', formula = y ~ poly(x,20), level=0.95)
ggplot(new_ad_data, aes(x=Chance.of.Admit,y=LOR))+
geom_point()+
geom_smooth(method = 'lm', formula = y ~ poly(x,2), level=0.95)
ggplot(new_ad_data, aes(x=Chance.of.Admit,y=CGPA))+
geom_point()+
geom_smooth(method = 'lm', formula = y ~ poly(x,2), level=0.95)
M_lm_model<-lm(Chance.of.Admit ~ TOEFL.Score + University.Rating + SOP + LOR + CGPA, data = new_ad_data)
M_lm_model
Call:
lm(formula = Chance.of.Admit ~ TOEFL.Score + University.Rating +
SOP + LOR + CGPA, data = new_ad_data)
University.Rating SOP LOR
-1.075107 0.004744 0.007176 -0.003202 0.022618
CGPA
0.139613
summary(M_lm_model)
Call:
lm(formula = Chance.of.Admit ~ TOEFL.Score + University.Rating +
SOP + LOR + CGPA, data = new_ad_data)
Residuals:
Min 1Q Median 3Q Max
-0.26612 -0.02516 0.01342 0.04055 0.15874
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.0751074 0.0784102 < 2e-16 ***
TOEFL.Score 0.0047443 0.0009947 4.770 2.60e-06 ***
University.Rating 0.0048999 1.465
SOP -0.0032024 0.0056974 -0.562 0.574
LOR 0.0226178 0.0057016 3.967 8.65e-05 ***
CGPA 0.1396125 0.0114901 12.151 < 2e-16
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.06564 on 394 degrees of freedom
Multiple R-squared: 0.7908, Adjusted R-squared: 0.7881
F-statistic: 297.8 on 5 and 394 DF, p-value: < 2.2e-16