##plot the first few examples: if(i<=4) { image(matrix(s,10,10,byrow=T),main="S example",col=grey(100:0/100)) image(matrix(x,10,10,byrow=T),main = "X example",col=grey(100:0/100)) } dataX[i,] <- x dataS[i,] <- s } data <- rbind(dataX,dataS) merged <- data.frame(letter=as.factor(letter),data) model3 <- nnet(letter~.,data=merged,size=2,rarg=.1,decay=.0001,maxit=500) table(letter,predict(model3,type="class")) library(MASS) library(DAAG) feature1 <- rnorm(200) feature2 <- rnorm(200) outcome <- as.factor((feature1 > .6 & feature2 > .3) | (feature1>.6 & feature2<.3)) outcome <- as.factor((feature1 * (-feature2) + rnorm(200))>0) lmodel <- lda(outcome~feature1+feature2) confusion(outcome,predict(lmodel)$class) lmodel2 <- lda(outcome~feature1*feature2) confusion(outcome,predict(lmodel2)$class) n1 <- nnet(outcome~feature1+feature2,size=0,skip=TRUE) confusion(outcome,factor(predict(n1,type="class"),levels=c(TRUE,FALSE))) n2 <- nnet(outcome~feature1+feature2,size=3,skip=TRUE) confusion(outcome,factor(predict(n2,type="class"),levels=c(FALSE,TRUE))) phone <- read.csv("data_study1.csv") phone$Smartphone <- (as.factor(phone$Smartphone)) phone$Gender <- as.numeric(as.factor(phone$Gender)) set.seed(100) phonemodel <- nnet(Smartphone~.,size=3,data=phone) confusion(phone$Smartphone,factor(predict(phonemodel,newdata=phone,type="class",levels=c("Android","iPhone")))) set.seed(101) phonemodel2 <- nnet(Smartphone~.,data=phone,size=2) confusion(phone$Smartphone,factor(predict(phonemodel2,newdata=phone,type="class"),levels=c("Android","iPhone"))) preds <- matrix(0,100) for(i in 1:100) { phonemodel3 <- nnet(Smartphone~.,data=phone,size=2) preds[i] <- confusion(phone$Smartphone,factor(predict(phonemodel3,newdata=phone,type="class"),levels=c("Android","iPhone")))$overall } hist(preds,col="gold",main="Accuracy of 100 fitted models (2 hidden nodes)",xlim=c(0,1)) preds <- matrix(0,100) for(i in 1:100) { phonemodel3 <- nnet(Smartphone~.,data=phone,size=6) preds[i] <- confusion(phone$Smartphone,factor(predict(phonemodel3,newdata=phone,type="class"),levels=c("Android","iPhone")))$overall } hist(preds,col="gold",main="Accuracy of 100 fitted models (6 hidden nodes)",xlim=c(0,1)) train <- rep(FALSE,nrow(phone)) train[sample(1:nrow(phone),size=300)] <- TRUE test <- !train phonemodel2 <- nnet(Smartphone~.,data=phone,size=6,subset=train) confusion(phone$Smartphone[test], predict(phonemodel2,newdata=phone[test,],type="class")) preds <- rep(0,100) for(i in 1:100) { train <- rep(FALSE,nrow(phone)) train[sample(1:nrow(phone),size=300)] <- TRUE test <- !train phonemodel2 <- nnet(Smartphone~.,data=phone,size=6,subset=train) preds[i] <- confusion(phone$Smartphone[test],factor(predict(phonemodel2,newdata=phone[test,],type="class"),levels=c("Android","iPhone")))$overall } hist(preds,col="gold",main="Accuracy of 100 cross-validated models (6 hidden nodes)",xlim=c(0,1)) preds <- rep(0,100) for(i in 1:100) { train <- rep(FALSE,nrow(phone)) train[sample(1:nrow(phone),size=300)] <- TRUE test <- !train phonemodel2 <- nnet(Smartphone~Gender+Avoidance.Similarity+Phone.as.status.object+Age, data=phone,size=6,subset=train) preds[i] <- confusion(phone$Smartphone[test],factor(predict(phonemodel2,newdata=phone[test,],type="class"),levels=c("Android","iPhone")))$overall } hist(preds,col="gold",main="Accuracy of 100 cross-validated models (6 hidden nodes)",xlim=c(0,1)) library(neuralnet) set.seed(10313) train <- rep(FALSE,nrow(phone)) train[sample(1:nrow(phone),size=300)] <- TRUE test <- !train phonemodel3 <- neuralnet(Smartphone~., hidden=c(2,2), data=phone[train,]) pred <- apply(predict(phonemodel3,newdata=phone[test,]),1,which.max) ptest <- phone[test,] acc <- (sum(ptest[pred==1,]$Smartphone=="Android") + sum(ptest[pred==2,]$Smartphone=="iPhone") )/sum(test) print(acc) plot(phonemodel3,rep="best") library(neuralnet) library(DAAG) ##10K examples of 0 to 9 mnistdat <-read.csv("mnist_test.csv") mnistdat$label <- factor(mnistdat$label) ## this needs to be a factor if it has more than 2 levels mnist47 <- mnistdat[mnistdat$label==4 | mnistdat$label==7| mnistdat$label==9,] train <- mnist47[sample(1:nrow(mnist47),1000),] #train on some examples set.seed(100) mnist <- neuralnet(label~., hidden=c(4), data=train) ##examine the trained data: table(train$label,c(4,7,9)[apply(mnist$response,1,which.max)]) p2 <- predict(mnist,newdata=mnist47) pred <- c(4,7,9)[apply(p2,1,which.max)] table(mnist47$label,pred) mean(mnist47$label==pred) set.seed(101) mnist33 <- neuralnet(label~., hidden=c(4,3), algorithm="backprop",rep=5,learningrate=.001,stepmax=5000, lifesign="full",lifesign.step=1000, data=train) ##examine the trained data: table(train$label,c(4,7,9)[apply(mnist33$response,1,which.max)]) p2 <- predict(mnist33,newdata=mnist47) error47 <- mnist47[mnist47$label==4 & pred==7,] cols <- grey(100:1/100) par(mfrow=c(2,4)) image((matrix(as.numeric(error47[1,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[2,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[3,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[4,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[5,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[6,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[7,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[8,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") library(neuralnet) library(DAAG) ##10K examples of 0 to 9 mnistdat <-read.csv("mnist_test.csv") mnistdat$label <- factor(mnistdat$label) ## this needs to be a factor if it has more than 2 levels mnist47 <- mnistdat[mnistdat$label==4 | mnistdat$label==7| mnistdat$label==9,] train <- mnist47[sample(1:nrow(mnist47),1000),] #train on some examples set.seed(100) mnist <- neuralnet(label~., hidden=c(4), data=train) ##examine the trained data: table(train$label,c(4,7,9)[apply(mnist$response,1,which.max)]) p2 <- predict(mnist,newdata=mnist47) pred <- c(4,7,9)[apply(p2,1,which.max)] table(mnist47$label,pred) mean(mnist47$label==pred) library(neuralnet) library(DAAG) ##10K examples of 0 to 9 mnistdat <-read.csv("mnist_test.csv") mnistdat$label <- factor(mnistdat$label) ## this needs to be a factor if it has more than 2 levels mnist47 <- mnistdat[mnistdat$label==4 | mnistdat$label==7| mnistdat$label==9,] train <- mnist47[sample(1:nrow(mnist47),1000),] #train on some examples set.seed(100) mnist <- neuralnet(label~., hidden=c(4), data=train) ##examine the trained data: table(train$label,c(4,7,9)[apply(mnist$response,1,which.max)]) p2 <- predict(mnist,newdata=mnist47) pred <- c(4,7,9)[apply(p2,1,which.max)] table(mnist47$label,pred) mean(mnist47$label==pred) error47 <- mnist47[mnist47$label==4 & pred==7,] cols <- grey(100:1/100) par(mfrow=c(2,4)) image((matrix(as.numeric(error47[1,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[2,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[3,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[4,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[5,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[6,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[7,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error47[8,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") error49 <- mnist47[mnist47$label==4 & pred==9,] cols <- grey(100:1/100) par(mfrow=c(2,4)) image((matrix(as.numeric(error49[1,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[2,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[3,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[4,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[5,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[6,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[7,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") image((matrix(as.numeric(error49[8,-1]),nrow=28,ncol=28))[,28:1],xaxt="n",yaxt="n",col=cols,main="4s called 7s") means <- tapply(as.numeric(dat$Age),list(dat$Smartphone),mean) library(knitr) library(rmdformats) ## Global options options(max.print="75") opts_chunk$set(echo=TRUE, cache=TRUE, prompt=FALSE, tidy=TRUE, comment=NA, message=FALSE, warning=FALSE) opts_knit$set(width=75) dat <- read.csv("data_study1.csv") dat$Smartphone <- factor(dat$Smartphone) library(klaR) nb <- NaiveBayes(Smartphone~.,data=dat) library(DAAG) summary(nb) nb$apriori nb$tables means <- tapply(as.numeric(dat$Age),list(dat$Smartphone),mean) sds <- tapply(as.numeric(dat$Age),list(dat$Smartphone),sd) means sds library(ggplot2) phone <- rep(c("iphone","android"),each=100) density<-c(dnorm(1:100,mean=means[2],sd=sds[2]), dnorm(1:100,mean=means[1],sd=sds[1])) df <- data.frame(age=c(1:100,1:100), phone,density) ggplot(df,aes(x=age,y=density,colour=phone)) + geom_line() library(ggplot2) phone <- rep(c("iphone","android"),each=100) density<-c(dnorm(1:100,mean=means[2],sd=sds[2]), dnorm(1:100,mean=means[1],sd=sds[1])) df <- data.frame(age=c(1:100,1:100), phone,density) ggplot(df,aes(x=age,y=density,colour=phone)) + geom_line() + theme_bw() + scale_color_manual(values=c("orange4","navy")) library(ggplot2) phone <- rep(c("iphone","android"),each=100) density<-c(dnorm(1:100,mean=means[2],sd=sds[2]), dnorm(1:100,mean=means[1],sd=sds[1])) df <- data.frame(age=c(1:100,1:100), phone,density) ggplot(df,aes(x=age,y=density,colour=phone)) + geom_line() + theme_bw() + scale_color_manual(values=c("orange2","navy")) library(ggplot2) phone <- rep(c("iphone","android"),each=100) density<-c(dnorm(1:100,mean=means[2],sd=sds[2]), dnorm(1:100,mean=means[1],sd=sds[1])) df <- data.frame(age=c(1:100,1:100), phone,density) ggplot(df,aes(x=age,y=density,colour=phone)) + geom_line(size=2) + theme_bw() + scale_color_manual(values=c("orange2","navy")) df <- data.frame(age=c(1:100), lr = (dnorm(1:100,mean=means[2],sd=sds[2])/ dnorm(1:100,mean=means[1],sd=sds[1])) ) ggplot(df,aes(x=age,y=lr)) + geom_line() + geom_hline(yintercept=1)+ theme_bw() + scale_color_manual(values=c("orange2","navy")) pred <- predict(nb) confusion(dat$Smartphone,pred$class) nb2 <- NaiveBayes(Smartphone~.,usekernel=T,data=dat) nb2$tables plot(nb2) pred2 <- predict(nb2) confusion(dat$Smartphone,pred2$class) ?NaiveBayes nb2 <- NaiveBayes(Smartphone~.,usekernel=T,data=dat) nb2$tables plot(nb2, col=c("navy","orange2")) nb2 <- NaiveBayes(Smartphone~.,usekernel=T,data=dat) nb2$tables plot(nb2, col=c("navy","orange2"), lwd=1.5) pred2 <- predict(nb2) confusion(dat$Smartphone,pred2$class) pred2 <- predict(nb2) confusion(dat$Smartphone,pred2$class) library(e1071) data(dengue) summary(dengue) dengue$NoYes <- as.factor(dengue$NoYes) library(e1071) data(dengue) summary(dengue) dengue$NoYes <- as.factor(dengue$NoYes) #This doesn't work #nb.dengue <- NaiveBayes(NoYes~.,data=dengue) nb.dengue <- NaiveBayes(NoYes~.,data=dengue,na.action="na.omit") ##this works #This one works with the klaR NaiveBayes: nb.dengue2 <- NaiveBayes(NoYes~ h10pix+Xmin+Ymin,data=dengue) #this one works--from e1071 package nb.dengue3<- naiveBayes(NoYes~.,data=dengue) #when we remove the na, we need to remove it from the ground truth too: confusion(dengue$NoYes[!is.na(rowMeans(dengue[,-9]))],predict(nb.dengue)$class) confusion(dengue$NoYes,predict(nb.dengue2)$class) confusion(dengue$NoYes,predict(nb.dengue3,newdata=dengue)) #This doesn't work #nb.dengue <- NaiveBayes(NoYes~.,data=dengue) nb.dengue <- NaiveBayes(NoYes~.,data=dengue,na.action="na.omit") ##this works #This one works with the klaR NaiveBayes: nb.dengue2 <- NaiveBayes(NoYes~ h10pix+Xmin+Ymin,data=dengue) #this one works--from e1071 package nb.dengue3<- naiveBayes(NoYes~.,data=dengue) #when we remove the na, we need to remove it from the ground truth too: confusion(dengue$NoYes[!is.na(rowMeans(dengue[,-9]))],predict(nb.dengue)$class) confusion(dengue$NoYes,predict(nb.dengue2)$class) confusion(dengue$NoYes,predict(nb.dengue3,newdata=dengue)) library(klaR) library(DAAG) train <-read.csv("trainmnist.csv") test <- read.csv("testmnist.csv") ##smooth out the training set a bit by adding some noise, so no pixel ##has a sd of 0. for(i in 1:ncol(train)) { train[,i] <- rnorm(500,as.numeric(train[,i]),.1) } par(mfrow=c(2,4)) image(matrix(unlist(train[1,]),nrow=28)) image(matrix(unlist(train[2,]),nrow=28)) image(matrix(unlist(train[3,]),nrow=28)) image(matrix(colMeans(train[1:250,]),nrow=28)) image(matrix(unlist(train[251,]),nrow=28)) image(matrix(unlist(train[252,]),nrow=28)) image(matrix(unlist(train[253,]),nrow=28)) image(matrix(colMeans(train[251:500,]),nrow=28)) train$labels <- as.factor(rep(0:1,each=250)) nb3 <- NaiveBayes(labels~.,usekernel=T,data=train) p3a <- predict(nb3) ##this takes a while confusion(train$labels,p3a$class) ##almost perfect p3b<- predict(nb3,test) ##equally good confusion(rep(0:1,each=250),p3b$class) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),.35) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28)) image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28)) p3c<- predict(nb3,test) ##This one is pretty bad. confusion(rep(0:1,each=250),p3c$class) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),5) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28)) image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28)) p3c<- predict(nb3,test) ##This one is pretty bad. confusion(rep(0:1,each=250),p3c$class) image(matrix(unlist(noisetest[251,]),nrow=28)) noisetest <- test for(i in 1:ncol(test)) { noisetest[,i] <- rnorm(500,as.numeric(test[,i]),3) } par(mfrow=c(2,4)) image(matrix(unlist(noisetest[1,]),nrow=28)) image(matrix(unlist(noisetest[2,]),nrow=28)) image(matrix(unlist(noisetest[3,]),nrow=28)) image(matrix(colMeans(noisetest[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(noisetest[251,]),nrow=28)) image(matrix(unlist(noisetest[252,]),nrow=28)) image(matrix(unlist(noisetest[253,]),nrow=28)) image(matrix(colMeans(noisetest[251:500,]),nrow=28),main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is pretty bad. confusion(rep(0:1,each=250),p3c$class) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),1) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28), main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is still pretty good confusion(rep(0:1,each=250),p3c$class) set.seed(100) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),3) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28), main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is still pretty good confusion(rep(0:1,each=250),p3c$class) library(klaR) library(DAAG) train <-read.csv("trainmnist.csv") test <- read.csv("testmnist.csv") set.seed(101) ##smooth out the training set a bit by adding some noise, so no pixel ##has a sd of 0. for(i in 1:ncol(train)) { train[,i] <- rnorm(500,as.numeric(train[,i]),.1) } par(mfrow=c(2,4)) image(matrix(unlist(train[1,]),nrow=28)) image(matrix(unlist(train[2,]),nrow=28)) image(matrix(unlist(train[3,]),nrow=28)) image(matrix(colMeans(train[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(train[251,]),nrow=28)) image(matrix(unlist(train[252,]),nrow=28)) image(matrix(unlist(train[253,]),nrow=28)) image(matrix(colMeans(train[251:500,]),nrow=28), main="Average of 250 prototypes") set.seed(100) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),1) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28), main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is still pretty good confusion(rep(0:1,each=250),p3c$class) library(klaR) library(DAAG) train <-read.csv("trainmnist.csv") test <- read.csv("testmnist.csv") set.seed(101) ##smooth out the training set a bit by adding some noise, so no pixel ##has a sd of 0. for(i in 1:ncol(train)) { train[,i] <- rnorm(500,as.numeric(train[,i]),.1) } par(mfrow=c(2,4)) image(matrix(unlist(train[1,]),nrow=28)) image(matrix(unlist(train[2,]),nrow=28)) image(matrix(unlist(train[3,]),nrow=28)) image(matrix(colMeans(train[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(train[251,]),nrow=28)) image(matrix(unlist(train[252,]),nrow=28)) image(matrix(unlist(train[253,]),nrow=28)) image(matrix(colMeans(train[251:500,]),nrow=28), main="Average of 250 prototypes") train$labels <- as.factor(rep(0:1,each=250)) nb3 <- NaiveBayes(labels~.,usekernel=T,data=train) p3a <- predict(nb3) ##this takes a while confusion(train$labels,p3a$class) ##almost perfect p3b<- predict(nb3,test) ##equally good confusion(rep(0:1,each=250),p3b$class) set.seed(100) for(i in 1:ncol(test)) { test[,i] <- rnorm(500,as.numeric(test[,i]),.25) } par(mfrow=c(2,4)) image(matrix(unlist(test[1,]),nrow=28)) image(matrix(unlist(test[2,]),nrow=28)) image(matrix(unlist(test[3,]),nrow=28)) image(matrix(colMeans(test[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(test[251,]),nrow=28)) image(matrix(unlist(test[252,]),nrow=28)) image(matrix(unlist(test[253,]),nrow=28)) image(matrix(colMeans(test[251:500,]),nrow=28), main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is still pretty good confusion(rep(0:1,each=250),p3c$class) noisetest <- test for(i in 1:ncol(test)) { noisetest[,i] <- rnorm(500,as.numeric(test[,i]),2) } par(mfrow=c(2,4)) image(matrix(unlist(noisetest[1,]),nrow=28)) image(matrix(unlist(noisetest[2,]),nrow=28)) image(matrix(unlist(noisetest[3,]),nrow=28)) image(matrix(colMeans(noisetest[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(noisetest[251,]),nrow=28)) image(matrix(unlist(noisetest[252,]),nrow=28)) image(matrix(unlist(noisetest[253,]),nrow=28)) image(matrix(colMeans(noisetest[251:500,]),nrow=28),main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is pretty bad. confusion(rep(0:1,each=250),p3c$class) train[1:5,] noisetest <- test for(i in 1:ncol(test)) { train[i,1:784] <- rnorm(500,as.numeric(train[i,1:784]),2) noisetest[,i] <- rnorm(500,as.numeric(test[,i]),2) } noisetest <- test set.seed(100) for(i in 1:ncol(test)) { train[,i] <- rnorm(500,as.numeric(train[,i]),2) noisetest[,i] <- rnorm(500,as.numeric(test[,i]),2) } nb3 <- NaiveBayes(labels~.,usekernel=T,data=train) par(mfrow=c(2,4)) image(matrix(unlist(noisetest[1,]),nrow=28)) image(matrix(unlist(noisetest[2,]),nrow=28)) image(matrix(unlist(noisetest[3,]),nrow=28)) image(matrix(colMeans(noisetest[1:250,]),nrow=28), main="Average of 250 prototypes") image(matrix(unlist(noisetest[251,]),nrow=28)) image(matrix(unlist(noisetest[252,]),nrow=28)) image(matrix(unlist(noisetest[253,]),nrow=28)) image(matrix(colMeans(noisetest[251:500,]),nrow=28),main="Average of 250 prototypes") p3c<- predict(nb3,test) ##This one is pretty bad. confusion(rep(0:1,each=250),p3c$class)