OneR and ZeroR in R

 ZeroR function in R: ZeroR <- function(X, targetId) { # ZeroR Algorithm: Finds the most commonly occuring class #   # Args: # X: data frame or Matrix # targetId: response/outcome/target/class feature column number # Returns: # A vector containing the commonly occuring class value and its count if ( is.character(X[, targetId]) | is.factor(X[, targetId]) ) { u.x <- unique(X[, targetId]) u.x.temp <- c for (i in u.x) { u.x.temp <- c(u.x.temp, sum(X[, targetId] == i)) }    names(u.x.temp) <- u.x     return( c(max(u.x.temp), names(u.x.temp)[which.max(u.x.temp)]) ) }  return(NULL) } OneR function in R:

Arguments : X: data frame of nominal features, targetId: column index of the response variable

Returns : index of the selected attribute

Also, it prints the error for each attribute. OneR <- function(X, targetId) { error.perc <- c cols <- ncol(X) - 1 for (f in 1:cols) { grps <- unique(X[, f]) err.count <- c for (grp in grps) { grp.data <- X[X[, f] == grp, c(f, targetId)] res <- ZeroR(grp.data, 2) err.grp <- sum(as.integer(grp.data[, 2] != res[2])) err.count <- c(err.count, err.grp) }    error.perc <- c(error.perc, sum(err.count)/nrow(X)) }  cat(error.perc) cat("\n") low.err.idx <- which.min(error.perc) return(low.err.idx) } Finding rules from OneR:

Arguments : X: data frame of nominal features, feature: a categorical feature found using OneR, targetId: column index of the response variable

Returns : a list containing the rules for the feature given as an argument FindRules <- function(X, feature, targetId) { rules <- c rules.ls <- list grps <- unique(X[, feature]) for (grp in grps) { grp.data <- X[X[, feature] == grp, c(feature, targetId)] res <- ZeroR(grp.data, 2) rules <- c(rules, paste(grp,"->",res[2], sep="")) rules.lsgrp <- res[2] } return(list(rules=rules, rules.list=rules.ls)) }