Maxwell Chandler
Maxwell Chandler

Reputation: 642

How to generate Classification Analysis tables in R?

So far I have done the discriminant analysis. I generated the posterior probabilities, structure loadings, and group centroids.

I have 1 grouping variable : history

I have 3 discriminant variables : mhpg, exercise, and control

here is the code so far

td <- read.delim("H:/Desktop/TAB DATA.txt")
td$history<-factor(td$history)
fit<-lda(history~mhpg+exercise+control, data=td)
git<-predict(fit)
xx<-subset(td, select=c(mhpg, control, exercise))
cor(xx,git$x)
aggregate(git$x~history,data=td,FUN=mean)
tst<-lm(cbind(mhpg,control,exercise)~history,data=td)

Basically, the above code is for discriminant analysis.

Now I want generate frequency classification and percent classification tables for classification analysis.

my attempted code (which i sampled from someone else to no avail) is:

td[6] <- git$class
td$V6<-factor(td$V6)
ftab<-table(td$history,dt$V6)
prop.table(ftab,1)

Where column 6 is my grouping variable history.

I get the following error when trying to make td$V6 a categorical variable with factor

Error in `$<-.data.frame`(`*tmp*`, "V6", value = integer(0)) : 
  replacement has 0 rows, data has 50

Can anyone steer me in the right direction? I really don't know why the sample code used a capital V out of nowhere. Below is the data. Column 6 is the grouping variable, history. Column 5 is the discriminant variable, control. column 7 is the discriminant variable, exercise. Column 8 is the discriminant variable, mhpg.

1   3   6   0   2   0   4   2   4   3   0   6   0
1   4   5   0   0   1   2   5   4   6   1   4   1
1   4   4   0   2   1   1   8   6   7   1   2   1
2   4   9   0   2   1   0   6   7   8   1   4   1
2   4   3   1   4   1   2   6   6   6   1   4   1
2   5   7   0   1   1   3   6   7   7   1   1   1
2   5   8   0   1   1   1   6   6   7   1   5   1
2   6   7   0   1   1   0   9   8   8   1   3   1
2   6   4   1   2   1   2   5   7   6   1   5   1
3   4   10  0   1   1   1   8   5   7   1   4   1
3   4   4   0   1   1   1   8   9   8   1   3   1
3   4   7   0   1   0   1   6   3   4   0   8   0
3   5   4   1   4   1   2   5   4   5   0   5   1
3   5   7   0   2   1   1   7   5   7   1   4   1
3   5   6   0   0   1   0   10  9   10  1   3   1
3   5   6   0   2   1   1   9   10  9   1   2   1
3   5   5   1   2   1   2   5   4   4   0   9   1
3   6   2   1   4   1   3   6   4   4   0   7   1
3   6   3   1   2   1   2   7   5   5   0   6   1
3   6   5   1   2   1   2   6   7   6   1   6   1
3   6   7   1   3   1   3   5   4   4   0   8   1
3   6   5   1   2   1   2   5   3   3   0   10  1
3   7   8   0   0   1   1   7   6   7   1   5   1
3   7   5   1   2   1   1   5   5   5   0   6   1
3   7   6   1   2   0   4   3   1   2   0   9   0
3   8   6   1   2   1   1   6   5   5   0   7   1
3   8   9   0   0   1   0   7   5   6   1   3   1
4   5   5   1   2   1   1   5   6   5   0   6   1
4   5   5   1   2   0   2   3   3   4   0   8   0
4   6   8   0   0   1   2   8   7   7   1   4   1
4   6   6   1   3   1   2   5   4   4   0   7   0
4   6   5   1   3   1   2   4   3   2   0   8   0
4   7   2   0   3   0   4   3   6   6   1   4   1
4   7   4   1   3   0   3   4   2   1   0   7   0
4   7   7   1   3   0   4   4   5   5   0   7   0
4   7   6   1   3   0   3   3   6   5   0   4   0
5   7   5   1   1   0   4   1   7   4   0   7   1
5   8   1   1   3   0   3   4   8   7   1   5   0
5   8   3   1   3   0   3   4   5   6   1   5   1
5   9   4   1   4   0   3   2   7   5   0   5   1
5   9   6   1   4   0   3   4   6   6   1   7   0
5   10  4   1   3   0   3   4   2   3   0   6   0
1   1   8   0   1   0   2   5   6   5   0   6   1
1   2   7   0   1   1   1   7   8   9   1   5   0
1   2   7   0   1   1   0   7   5   6   1   5   1
1   3   5   0   1   1   2   7   8   8   1   5   0
2   3   3   1   2   1   2   6   7   6   1   6   0
2   3   6   1   1   1   2   7   6   4   0   7   0
2   4   6   1   3   1   3   6   5   5   0   6   0
2   5   4   1   3   1   3   4   4   3   0   6   0

Upvotes: 0

Views: 589

Answers (1)

jlhoward
jlhoward

Reputation: 59375

Try:

tbl <- table(td$history,git$class)
tbl
#     0  1
#   0 13  2
#   1  1 34

prop.table(tbl)
#        0    1
#   0 0.26 0.04
#   1 0.02 0.68

These are the classification tables.

Regarding why your "borrowed" code does not run, there are too many possibilities.

First, if you import the data set you provided without column names, R will assign names Vn where n is 1,2,3, etc. But if this was the case none of your code would run as you refer to columns history, control, etc. So at least those must be named properly.

Second, in the line:

ftab<-table(td$history,dt$V6)

you refer to dt$V6. AFAICT there is no dt (is this a typo?).

Upvotes: 1

Related Questions