1ddalpha.test <- function(learn, test, ...){ 2 ops <- options(warn = -1) 3 on.exit(options(ops)) 4 5 if(is.vector(test)) 6 test = t(as.matrix(test)) 7 8 tryCatch({ 9 time <- system.time( 10 ddalpha <- ddalpha.train(learn, ...) 11 ) 12 C = ddalpha$dimension+1 13 cc = ddalpha.classify(objects = test[,-C], ddalpha = ddalpha) 14 if (is.numeric(test[,C])){ 15 if(is.factor(cc[[1]]) || is.character(cc[[1]])){ 16 cc <- unlist(lapply(cc, as.character)) 17 cc[cc == "Ignored"] <- NA 18 } 19 equal = (cc == test[,C]) 20 } else { 21 cc <- unlist(lapply(cc, as.numeric)) 22 equal = (cc == as.numeric(test[,C])) 23 } 24 25 if(!(T %in% equal) && !(F %in% equal)) 26 { return(NA)} 27 error = sum(!equal,na.rm = T)/(sum(!equal,na.rm = T)+sum(equal,na.rm = T)) 28 return(list(error = error, correct = sum(equal,na.rm = T), incorrect = sum(!equal,na.rm = T), 29 total = length(cc)-sum(is.na(equal)), ignored = sum(is.na(equal)), n = length(cc), 30 time = time[1])) 31 } 32 # tryCatch({} 33 , error = function(e) { 34 print ("ERROR T") 35 print (e) 36 }, finally = { 37 }) 38 return (NA) 39} 40 41 42ddalpha.getErrorRateCV <- function(data, numchunks = 10, ...){ 43 n = nrow(data) 44 numchunks = min(n, numchunks) 45 chunksize = ceiling(n/numchunks) 46 47 sample = seq(from = 1, by = numchunks, length.out = chunksize) 48 49 errors = 0 50 total = 0 51 times = c() 52 53 for (i in 1:numchunks){ 54 sample = sample[sample<=n] 55 learn = data[-sample,,drop = F] 56 test = data[sample,,drop = F] 57 58 el = ddalpha.test(learn, test, ...) 59 if(is.list(el)){ 60 errors = errors + el$incorrect 61 total = total + el$total 62 times = c(times,el$time) 63 } 64 65 sample = sample+1 66 } 67 68 return (list(errors = errors/total, time = mean(times), time_sd = sd(times))) 69} 70 71 72ddalpha.getErrorRatePart <- function(data, size = 0.3, times = 10, ...){ 73 74 if (!is.numeric(size) || size <=0 || size >= nrow(data)) stop("Wrong size of excluded sequences") 75 76 if(size < 1) 77 size = max(1, size*nrow(data)) # at least 1 point 78 79 size = as.integer(size) 80 81 indexes = 1:nrow(data) 82 83 errors = c() 84 total = 0 85 time = c() 86 87 for (i in 1:times){ 88 samp = sample(indexes, size) 89 learn = data[-samp,,drop = F] 90 test = data[samp,,drop = F] 91 92 el = ddalpha.test(learn, test, ...) 93 if(is.list(el)){ 94 errors = c(errors,el$incorrect/el$total) 95 time = c(time,el$time) 96 } 97 } 98 99 return (list(errors = mean(errors), errors_sd = sd(errors), errors_vec = errors, time = mean(time), time_sd = sd(time))) 100}