- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the confusion matrix for linear discriminant analysis in R?
To find the confusion matrix for linear discriminant analysis in R, we can follow the below steps −
- First of all, create a data frame.
- Create new features using linear discriminant analysis.
- Find the confusion matrix for linear discriminant analysis using table and predict function.
Create the data frame
Let's create a data frame as shown below −
Group<-sample(c("I","II","III","IV"),25,replace=TRUE) Score1<-sample(1:10,25,replace=TRUE) Score2<-sample(1:10,25,replace=TRUE) Score3<-sample(1:10,25,replace=TRUE) Score4<-sample(1:10,25,replace=TRUE) df<-data.frame(Group,Score1,Score2,Score3,Score4) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Group Score1 Score2 Score3 Score4 1 IV 7 5 2 5 2 III 5 3 2 4 3 III 8 9 4 7 4 IV 6 1 1 5 5 III 8 4 6 8 6 IV 9 2 1 7 7 I 3 2 3 2 8 IV 5 8 3 3 9 II 7 4 4 1 10 IV 5 4 1 10 11 II 3 1 2 4 12 III 3 2 1 7 13 IV 1 4 7 6 14 III 10 8 9 2 15 II 3 7 8 1 16 I 9 2 3 1 17 III 2 7 3 2 18 IV 7 7 1 7 19 IV 2 6 1 3 20 I 4 10 6 1 21 I 1 6 4 4 22 I 6 3 6 2 23 III 6 6 3 5 24 I 2 3 10 10 25 II 4 4 2 5
Create new features using linear discriminant analysis
Use lda function of MASS package to find the new features for data in data frame df −
Group<-sample(c("I","II","III","IV"),25,replace=TRUE) Score1<-sample(1:10,25,replace=TRUE) Score2<-sample(1:10,25,replace=TRUE) Score3<-sample(1:10,25,replace=TRUE) Score4<-sample(1:10,25,replace=TRUE) df<-data.frame(Group,Score1,Score2,Score3,Score4) library(MASS) LDA_df=lda(Group~.,data=df) LDA_df
Output
Call: lda(Group ~ ., data = df) Prior probabilities of groups: I II III IV 0.24 0.16 0.28 0.32 Group means: Score1 Score2 Score3 Score4 I 4.166667 4.333333 5.333333 3.333333 II 4.250000 4.000000 4.000000 2.750000 III 6.000000 5.571429 4.000000 5.000000 IV 5.250000 4.625000 2.125000 5.750000 Coefficients of linear discriminants: LD1 LD2 LD3 Score1 0.1358158 0.18645755 -0.18790651 Score2 0.2598956 0.15492088 -0.07433529 Score3 -0.3052648 0.25571648 0.14567716 Score4 0.3117567 0.08656138 0.25216169 Proportion of trace: LD1 LD2 LD3 0.8681 0.1161 0.0159
Find the confusion matrix
Create the confusion matrix for linear discriminant analysis performed above by using table and predict function as shown below −
Group<-sample(c("I","II","III","IV"),25,replace=TRUE) Score1<-sample(1:10,25,replace=TRUE) Score2<-sample(1:10,25,replace=TRUE) Score3<-sample(1:10,25,replace=TRUE) Score4<-sample(1:10,25,replace=TRUE) df<-data.frame(Group,Score1,Score2,Score3,Score4) library(MASS) LDA_df=lda(Group~.,data=df) table(predict(LDA_df,type="class")$class,df$Group)
Output
I II III IV I 3 2 1 1 II 2 1 0 0 III 1 0 3 1 IV 0 1 3 6
Advertisements