# [R] Decision boundaries for lda function?

Thomas Larsen tl at leibniz.uni-kiel.de
Tue Aug 25 18:22:56 CEST 2009

```I got hold of 'Modern Applied Statistics with S' by Venables and Ripley (p.
335-336) and I was able to answer my own question:

###

aa.grp <- factor(c(rep('f',13),rep('b',10),rep('p',10)))

aa.lda<-lda(as.matrix(AA[3:9]),AA\$group)
aa.ld<-predict(aa.lda,dimen=2)\$x
eqscplot(aa.ld,type="n",xlab="LD1", ylab="LD2",tol=0.25, las=1)
text(aa.ld,labels = as.character(aa.grp))

aa.lda2 <- lda(aa.ld, aa.grp)
x <- seq(-7, 5.5, 0.25)
y <- seq(-4.5, 6.5, 0.25)
Xcon <- matrix(c(rep(x,length(y)),
rep(y, rep(length(x), length(y)))),,2)

aa.pr1 <- predict(aa.lda2, Xcon)\$post[, c("f","b")] %*% c(1,1)
contour(x, y, matrix(aa.pr1, length(x), length(y)),
aa.pr2 <- predict(aa.lda2, Xcon)\$post[, c("p","b")] %*% c(1,1)
contour(x, y, matrix(aa.pr2, length(x), length(y)),
aa.pr3 <- predict(aa.lda2, Xcon)\$post[, c("f","p")] %*% c(1,1)
contour(x, y, matrix(aa.pr3, length(x), length(y)),
###

Thomas Larsen wrote:
>
> Hi,
>
> I am using the lda function from the MASS library. I would to find the
> decision boundaries of each class and subsequently plot them. I wonder if
> anybody can offer any help on this topic?
>
> Below I applied the lda function on a small dataset of mine.
>
> Any help will be much appreciated.
>
>> library(MASS)
>
>
>> aa.lda<-lda(as.matrix[3:9],AA\$group)
>> aa.ld<-predict(aa.lda,dimen=2)\$x
>> eqscplot(aa.ld,type="n",xlab="LD1", ylab="LD2",las=1)
>> text(aa.ld,c(rep('f',13),rep('b',10),rep('p',10)))
>
>> aa.mean<-lda(aa.ld,AA\$group)\$means
>> points(aa.mean,pch=3)
>
> Best,
> Thomas Larsen
>
> Leibniz-Laboratory for Stable Isotope Research
> Max-Eyth-Str. 11-13, 24118 Kiel, Germany
> Work: +49-431-880-3896
>

--
View this message in context: http://www.nabble.com/Decision-boundaries-for-lda-function--tp24823213p25137633.html
Sent from the R help mailing list archive at Nabble.com.

```