Question

I have a generated data, but I do not know how to plot a decision boundary for these data in R.

The data is generated using the following code:

n=2000
p=2
sigma <- 1 
meanpos <- 2 
meanneg <- 3 
npos <- round(n/20) 
nneg <- round((n-2*npos)) 

xpos1 <- matrix(rnorm(npos*p,mean=4.5,sd=sigma),npos,p)
xpos1[,1]=xpos1[,1]-1
xpos1[,2]=xpos1[,2]+1


xpos2 <- matrix(rnorm(npos*p,mean=6,sd=sigma),npos,p)
xpos2[,1]=xpos2[,1]+6
xpos2[,2]=xpos2[,2]-8

xneg2 <- matrix(rnorm(nneg*p,mean=3,sd=sigma),nneg,p)
xneg2[,1]=xneg2[,1]+3
xneg2[,2]=xneg2[,2]-3

x <- rbind(xpos1,xpos2,xneg2)

y <- matrix(c(rep(-1,2*npos),rep(1,nneg)))

plot(x,xlab="x1",ylim=c(-4,11),ylab="x2",col=ifelse(y<0,"red3","black"))

Could someone help me on this?

EDIT: I found some R code might help, but the decision boundry is plot using Knn, I want to use other learning algorithms such as glmnet, how can I do it with this kind of data? Thanks

GS <- 75 # put data in a Gs x Gs grid
XLIM <- range(x[,1])
tmpx <- seq(XLIM[1], XLIM[2], len=GS)    
YLIM <- range(x[,2])
tmpy <- seq(YLIM[1], YLIM[2], len=GS)
newx <- expand.grid(tmpx, tmpy)   
yhat <- knn(x, newx, y, k=1)

plot(x, xlab="X1", ylab="X2", xlim=XLIM, ylim=YLIM, type="n")

contour(tmpx, tmpy, matrix(as.numeric(yhat),GS,GS), levels=c(1,2), add=TRUE, drawlabels=FALSE)
Was it helpful?

Solution

To use any other model you just need to fit it and make different predictions for yhat

glmnet:

library(glmnet)
mod <- glmnet(x, y, family = "binomial")

yhat <- predict(mod, as.matrix(newx), type = "class", s = 0.001)

plot(x, xlab="X1", ylab="X2", xlim=XLIM, ylim=YLIM)

contour(tmpx, tmpy, matrix(as.numeric(yhat),GS,GS), levels=c(1,2), add=TRUE, drawlabels=FALSE)

enter image description here

randomForest:

library(randomForest)

newx <- as.matrix(newx)
colnames(newx) <- NULL
mod <- randomForest(x, factor(y))
yhat <- predict(mod, as.matrix(newx))

plot(x, xlab="X1", ylab="X2", xlim=XLIM, ylim=YLIM)

contour(tmpx, tmpy, matrix(as.numeric(yhat),GS,GS), levels=c(1,2), add=TRUE, drawlabels=FALSE)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top