library(class) library(MASS) library(nnet) #Funciones auxiliares predplot <- function(object, main="", len = 100, ...) { plot(Cushings[,1], Cushings[,2], type="n", xlab = "Tetrahydrocortisone", ylab = "Pregnanetriol", main = main) for(il in 1:4) { set <- Cushings$Type==levels(Cushings$Type)[il] text(Cushings[set, 1], Cushings[set, 2], labels=as.character(Cushings$Type[set])) } xp <- seq(0, 60, length=len) yp <- seq(0, 20, length=len) cushT <- expand.grid(Tetrahydrocortisone = xp, Pregnanetriol = yp) Z <- predict(object, cushT, ...); zp <- as.numeric(Z$class) zp <- Z$post[,3] - pmax(Z$post[,2], Z$post[,1]) contour(xp, yp, matrix(zp, len), add = T, levels=0,drawlabels=FALSE) zp <- Z$post[,1] - pmax(Z$post[,2], Z$post[,3]) contour(xp, yp, matrix(zp, len), add = T, levels=0,drawlabels=FALSE) invisible() } cushplot <- function(xp, yp, Z) { plot(Cushings[, 1], Cushings[, 2], ylim=c(0,20),type = "n", xlab = "Tetrahydrocortisone", ylab = "Pregnanetriol") for(il in 1:4) { set <- Cushings$Type==levels(Cushings$Type)[il] text(Cushings[set, 1], Cushings[set, 2], labels = as.character(Cushings$Type[set])) } zp <- Z[, 3] - pmax(Z[, 2], Z[, 1]) contour(xp,yp, matrix(zp, np), add = T, levels = 0,drawlabels=FALSE) zp <- Z[, 1] - pmax(Z[, 2], Z[, 3]) contour(xp, yp, matrix(zp, np), add = T, levels = 0, drawlabels=FALSE) invisible() } xp <- seq(0, 60, length = 50) yp <- seq(0, 20, length = 50) np <- length(xp) cushT <- expand.grid(Tetrahydrocortisone = xp, Pregnanetriol = yp) #Grafica de knn-1 tp <- Cushings$Type[1:21, drop = T] Z <- knn(Cushings[1:21,-3], cushT, tp) win.graph() cushplot(xp, yp, class.ind(Z)) color=c("red","blue","green")[as.factor(Z)] points(cushT[,1],cushT[,2],col=color) title("Grafica de las fronteras de knn-1") #Grafica de knn-3 Z <- knn(Cushings[1:21, -3], cushT, tp, k = 3) win.graph() cushplot(xp, yp, class.ind(Z)) color=c("red","blue","green")[as.factor(Z)] points(cushT[,1],cushT[,2],col=color) title("Grafica de las fronteras de knn-3") #Grafica de LDA tp <- Cushings$Type[1:21, drop = T] cush.lda <- lda(as.matrix(Cushings[1:21,-3]), tp) win.graph() predplot(cush.lda, "Grafica de las fronteras de LDA",len=50)