Docsity

Discriminant Analysis using R: An Example with the wheat.R Data Set, Exams of Statistics

Toggle sidebar
bg1
Discriminant analysis using R (wheat.R)
> wheat<-read.table(file = "C:\\chris\\UNL\\STAT873\\Chapter 7\\
wheat.dat", header=FALSE, col.names = c("lot", "class", "type",
"skden1", "skden2", "skden3", "skhard", "sksize", "skwt",
"skmst"))
> kernel<-1:nrow(wheat)
> skden<-(wheat$skden1 + wheat$skden2 + wheat$skden3)/3
> hrw<-ifelse(wheat$class=="hrw", 1, 0)
> type1<-ifelse(wheat$type==1, "Healthy", ifelse(wheat$type==2,
"Sprout", "Scab"))
>
> wheat2<-data.frame(kernel, type1, skden, hrw, skhard =
wheat$skhard, sksize = wheat$sksize, skwt = wheat$skwt, skmst =
wheat$skmst)
>
>###############################################################
> #Discriminant Analysis
>
> library(MASS)
>
> #Using proportional priors and linear discriminant analysis
> save<-lda(formula = type1 ~ skden + hrw + skhard + sksize +
skwt + skmst, data = wheat2, CV = TRUE)
> summary(save) #Not useful
Length Class Mode
class 276 factor numeric
posterior 828 -none- numeric
terms 3 terms call
call 4 -none- call
xlevels 0 -none- list
> names(save)
[1] "class" "posterior" "terms" "call" "xlevels"
>
> #first 10 observations - just to show some results
> results<-data.frame(wheat2$type1, save$class,
2003 Christopher R. Bilder
R. Sup. 1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Discriminant Analysis using R: An Example with the wheat.R Data Set and more Exams Statistics in PDF only on Docsity!

Discriminant analysis using R (wheat.R)

> wheat<-read.table(file = "C:\chris\UNL\STAT873\Chapter 7\ wheat.dat", header=FALSE, col.names = c("lot", "class", "type", "skden1", "skden2", "skden3", "skhard", "sksize", "skwt", "skmst")) > kernel<-1:nrow(wheat) > skden<-(wheat$skden1 + wheat$skden2 + wheat$skden3)/ > hrw<-ifelse(wheat$class=="hrw", 1, 0) > type1<-ifelse(wheat$type==1, "Healthy", ifelse(wheat$type==2, "Sprout", "Scab")) > > wheat2<-data.frame(kernel, type1, skden, hrw, skhard = wheat$skhard, sksize = wheat$sksize, skwt = wheat$skwt, skmst = wheat$skmst) > >############################################################### > #Discriminant Analysis > > library(MASS) > > #Using proportional priors and linear discriminant analysis > save<-lda(formula = type1 ~ skden + hrw + skhard + sksize + skwt + skmst, data = wheat2, CV = TRUE) > summary(save) #Not useful Length Class Mode class 276 factor numeric posterior 828 -none- numeric terms 3 terms call call 4 -none- call xlevels 0 -none- list > names(save) [1] "class" "posterior" "terms" "call" "xlevels" > > #first 10 observations - just to show some results > results<-data.frame(wheat2$type1, save$class,

save$posterior) > results[1:10,] wheat2.type1 save.class Healthy Scab Sprout 1 Healthy Healthy 0.6339101 0.093753069 0. 2 Healthy Healthy 0.6097464 0.023829848 0. 3 Healthy Healthy 0.4955375 0.058804220 0. 4 Healthy Healthy 0.7123584 0.017548054 0. 5 Healthy Healthy 0.4472940 0.162765572 0. 6 Healthy Healthy 0.6312161 0.021990744 0. 7 Healthy Healthy 0.6099260 0.007606216 0. 8 Healthy Sprout 0.4567775 0.055907013 0. 9 Healthy Healthy 0.5481755 0.018513734 0. 10 Healthy Healthy 0.4718556 0.072124795 0. > #Summarize crossvalidation > class.table<-table(wheat2$type1, save$class) > class.table Healthy Scab Sprout Healthy 67 7 22 Scab 11 62 11 Sprout 25 19 52 > #One could make the summary of the classifications nicer by > # writing a function > summarize.class<-function(original, classify) { class.table<-table(original, classify) numb<-rowSums(class.table) prop<-round(class.table/numb,4) list(class.table = class.table, prop = prop) #return a list object type } > summarize.class(original = wheat2$type1, classify = save$class) $class.table classify original Healthy Scab Sprout Healthy 67 7 22 Scab 11 62 11 Sprout 25 19 52

0.8 1.0 1.2 1.4 1.6 1.8 2. 0 1 2 3 4 5 6 group Healthy 0.8 1.0 1.2 1.4 1.6 1.8 2. 0 1 2 3 4 5 6 group Scab 0.8 1.0 1.2 1.4 1.6 1.8 2. 0 1 2 3 4 5 6 group Sprout > > #Using equal priors > save<-lda(formula = type1 ~ skden + hrw + skhard + sksize + skwt + skmst, data = wheat2, prior = c(1/3, 1/3, 1/3), CV = TRUE) > #Could have also done the priors with: > # rep(1,nlevels(wheat2$type1))/nlevels(wheat2$type1) > > #first 10 observations > results<-data.frame(original = wheat2$type1, class = save$class, save$posterior) > results[1:10,] original class Healthy Scab Sprout 1 Healthy Healthy 0.6255321 0.105730287 0. 2 Healthy Healthy 0.6076777 0.027141714 0. 3 Healthy Healthy 0.4914093 0.066644965 0.

4 Healthy Healthy 0.7105771 0.020004769 0. 5 Healthy Healthy 0.4371298 0.181790757 0. 6 Healthy Healthy 0.6292393 0.025053572 0. 7 Healthy Healthy 0.6092639 0.008683383 0. 8 Healthy Sprout 0.4531583 0.063387472 0. 9 Healthy Healthy 0.5467295 0.021102740 0. 10 Healthy Healthy 0.4670434 0.081587695 0. > summarize.class(wheat2$type1, save$class) $class.table classify original Healthy Scab Sprout Healthy 66 9 21 Scab 10 65 9 Sprout 25 20 51 $prop classify original Healthy Scab Sprout Healthy 0.6875 0.0938 0. Scab 0.1190 0.7738 0. Sprout 0.2604 0.2083 0. > > > #Quadratic discriminant analysis > save<-qda(formula = type1 ~ skden + hrw + skhard + sksize + skwt + skmst, data = wheat2, CV = TRUE) > summarize.class(wheat2$type1, save$class) $class.table classify original Healthy Scab Sprout Healthy 66 10 20 Scab 7 64 13 Sprout 27 12 57 $prop classify original Healthy Scab Sprout Healthy 0.6875 0.1042 0. Scab 0.0833 0.7619 0. Sprout 0.2812 0.1250 0.

K-nearest neighbor discriminant analysis

> #These functions are in the "class" package > library(class) > > #remove the first column which contains the kernel number > # and the classifications > wheat3<-data.frame(wheat2$skden, wheat2$hrw, wheat2$skhard, wheat2$sksize, wheat2$skwt, wheat2$skmst) > > #The formula method for specifying the variables does not > # work with this function. The use.all part tells R to use > # all experimental units which have the same distance. For > # example, if the kth and (k+1)th closest experimental > # units to an experimental unit have the same distance to > # it, they both are used to determine the classification of > # the experimental unit. Ties in posterior probabilities > # are broken at random (I do not like this). > save<-knn.cv(train = wheat3, cl = wheat2$type1, k = 5, prob = TRUE, use.all = TRUE) > names(save) #Not helpful - why? NULL > summary(save) Healthy Scab Sprout 92 82 102 > > #In order to see the highest posterior probability for an > # experimental unit, use the command below. I think > # Venables and Ripley (authors of the function) are using > # some older form of syntax. > attributes(save) $levels [1] "Healthy" "Scab" "Sprout" $class [1] "factor" $prob [1] 0.4 0.6 0.8 0.6 0.6 0.6 0.6 0.6 0.8 0.6 0.8 0.4 0.8 0.8 0.4 0.6 0.4 0. [19] 0.4 0.8 0.6 0.6 0.6 0.4 0.6 0.6 0.8 1.0 0.8 0.6 0.8 0.8 0.6 0.8 1.0 0.

[37] 0.4 0.4 0.8 0.6 0.6 0.6 0.8 0.6 0.4 0.6 0.8 0.6 0.6 0.6 0.8 0.6 0.4 0. [55] 1.0 0.6 0.4 0.6 0.6 0.6 0.6 0.8 0.6 1.0 0.6 0.6 0.6 0.6 0.8 0.4 1.0 1. [73] 0.4 0.6 0.4 0.8 0.6 1.0 0.6 0.6 0.4 0.6 0.4 0.6 0.8 0.6 0.6 0.6 0.6 0. [91] 0.6 0.8 0.4 0.6 0.6 1.0 0.8 0.6 0.8 0.8 0.6 0.6 0.4 0.6 0.6 0.8 0.6 0. [109] 0.8 0.8 0.6 0.4 0.4 0.6 0.6 0.4 0.8 0.6 0.6 0.4 0.6 0.4 0.6 0.8 0.4 0. [127] 0.6 1.0 0.4 0.6 0.8 0.6 0.6 0.6 0.8 0.8 0.6 0.4 0.4 0.8 1.0 0.8 0.8 0. [145] 0.6 0.8 0.6 0.6 0.8 0.6 0.6 0.6 0.6 0.6 0.4 0.8 0.6 0.6 0.6 0.6 0.6 0. [163] 0.6 0.4 0.6 0.6 0.6 0.6 0.6 0.8 1.0 1.0 0.6 1.0 0.6 0.8 0.6 0.8 0.6 0. [181] 0.6 0.6 0.4 0.8 0.4 0.6 0.4 0.8 0.6 0.6 0.6 0.4 0.6 0.8 0.4 0.6 0.8 0. [199] 0.8 0.6 0.6 0.6 0.8 0.6 0.8 0.8 0.8 0.4 0.8 0.8 0.4 0.8 0.6 0.4 0.4 0. [217] 0.6 0.6 0.6 0.4 0.8 0.6 0.6 0.6 0.4 0.6 0.6 0.8 0.6 0.6 0.8 0.6 0.6 0. [235] 0.6 0.8 0.6 0.6 0.6 0.4 0.6 0.4 0.6 0.6 0.4 0.6 0.6 0.4 1.0 0.8 0.6 0. [253] 0.8 0.6 0.8 0.6 0.4 0.6 0.6 0.4 0.6 0.6 0.4 0.8 0.8 0.8 1.0 0.8 0.8 0. [271] 1.0 0.8 0.4 0.8 1.0 0. > #This function can be used with a test or validation data > # set > # knn(train = , test = , cl = , k = 1, l = 0, prob = FALSE, use.all = TRUE) > > #Note that this will return perfect classifications! 1 > # nearest neighbor is used on the same data set > # WITHOUT crossvalidation. > save<-knn(train = wheat3, test = wheat3, cl = wheat2$type1, k = 1, prob = TRUE, use.all = TRUE) > summarize.class(wheat2$type1, save) $class.table classify original Healthy Scab Sprout Healthy 96 0 0 Scab 0 84 0 Sprout 0 0 96 $prop classify original Healthy Scab Sprout Healthy 1 0 0 Scab 0 1 0 Sprout 0 0 1