Hi, i am tryng to run Bagging SVM but I have a problem. I do Bagging SVM based on algoritm bagging Trees by Breiman. I get the following error and I think I am wrong in calculating err.mean and err.vote. I also have difficulty in making the plot. > library(e1071) > gendata <- function(N=30,p=5,r=.95,cut=0.5,lop=0.2,hip=0.8) { + mat <- chol(diag(p) + r - r*diag(p)) + X <- matrix(rnorm(N*p),ncol=p)%*%mat + X <- sweep(X,2,apply(X,2,mean)) + X <- sweep(X,2,apply(X,2,sd),"/") + u <- runif(N) + y <- ifelse(X[,1]<cut,ifelse(u<lop,1,0),ifelse(u<hip,1,0)) + return(list(X=X,y=y)) + } > N <- 100 > tmp <- gendata(N) > data <- data.frame(x=tmp$X,y=tmp$y) > index <- 1:nrow(data) > testindex <- sample(index, trunc(length(index)*30/100)) > test.data <- data[testindex,] > trainindex <- sample(index, trunc(length(index)*70/100)) > train.data <- data[-testindex,] > for (b in 1:3) { + samp <- sample(1:N,N,replace=T) + modelrad <- svm(y ~ ., train.data[samp,], type = "C-classification", cost = cc,gamma = gg, kernel = "radial") + } > modelrad <- svm(y ~ ., train.data, type = "C-classification", cost = cc,gamma = gg, kernel = "radial") > err.base <- mean(abs(test.data$y - round(predict(base.tree,test.data)))) > boots <- c(2:9,seq(10,100,by=5),seq(200,500,by=50)) > Blen <- length(boots) > err.mean <- rep(NA,Blen) > err.vote <- rep(NA,Blen) > for (i in 1:Blen) { + B <- boots[i] + pred <- matrix(NA,nrow=B,ncol=N) + for (b in 1:B) { + samp <- sample(1:N,N,replace=T) + modelrad <- svm(y ~ ., train.data[samp,], type = "C-classification", cost = cc,gamma = gg, kernel = "radial") + pred <- predict(modelrad,test.data) + } + err.mean[i] <- mean(abs(test.data$y - round(apply(pred,2,mean)))) + err.vote[i] <- mean(abs(test.data$y - round(apply(ifelse(pred>.5,1,0),2,mean)))) + } Error in apply(pred, 2, mean) : dim(X) must have a positive length
I'd appreciate any advice about the algorithm. Thanks for any help. best Regards, Iut On Sat, May 11, 2013 at 2:04 AM, Iut Tri Utami <triutami....@gmail.com>wrote: > Hi, > > i am tryng to run Bagging SVM but I have a problem. I do Bagging SVM based > on algoritm bagging Trees by Breiman. I get the following error and I think > I am wrong in calculating err.mean and err.vote. I also have difficulty in > making the plot. > > library(e1071) > > gendata <- function(N=30,p=5,r=.95,cut=0.5,lop=0.2,hip=0.8) { > + mat <- chol(diag(p) + r - r*diag(p)) > + X <- matrix(rnorm(N*p),ncol=p)%*%mat > + X <- sweep(X,2,apply(X,2,mean)) > + X <- sweep(X,2,apply(X,2,sd),"/") > + u <- runif(N) > + y <- ifelse(X[,1]<cut,ifelse(u<lop,1,0),ifelse(u<hip,1,0)) > + return(list(X=X,y=y)) > + } > > N <- 100 > > tmp <- gendata(N) > > data <- data.frame(x=tmp$X,y=tmp$y) > > index <- 1:nrow(data) > > testindex <- sample(index, trunc(length(index)*30/100)) > > test.data <- data[testindex,] > > trainindex <- sample(index, trunc(length(index)*70/100)) > > train.data <- data[-testindex,] > > for (b in 1:3) { > + samp <- sample(1:N,N,replace=T) > + modelrad <- svm(y ~ ., train.data[samp,], type = "C-classification", > cost = cc,gamma = gg, kernel = "radial") > + } > > modelrad <- svm(y ~ ., train.data, type = "C-classification", cost = > cc,gamma = gg, kernel = "radial") > > err.base <- mean(abs(test.data$y - round(predict(base.tree,test.data)))) > > boots <- c(2:9,seq(10,100,by=5),seq(200,500,by=50)) > > Blen <- length(boots) > > err.mean <- rep(NA,Blen) > > err.vote <- rep(NA,Blen) > > for (i in 1:Blen) { > + B <- boots[i] > + pred <- matrix(NA,nrow=B,ncol=N) > + for (b in 1:B) { > + samp <- sample(1:N,N,replace=T) > + modelrad <- svm(y ~ ., train.data[samp,], type = "C-classification", > cost = cc,gamma = gg, kernel = "radial") > + pred <- predict(modelrad,test.data) > + } > + err.mean[i] <- mean(abs(test.data$y - round(apply(pred,2,mean)))) > + err.vote[i] <- mean(abs(test.data$y - > round(apply(ifelse(pred>.5,1,0),2,mean)))) > + } > Error in apply(pred, 2, mean) : dim(X) must have a positive length > > I'd appreciate any advice about the algorithm. Thanks for any help. > > best Regards, > > > Iut > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.