## ---- include = [encoded data, 9982 characters]
library(knitr)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = '#>'
)
knitr::opts_chunk$set(fig.height = 5)
knitr::opts_chunk$set(fig.width = 8)
knitr::opts_chunk$set(fig.align = 'center')

## ---- include = FALSE, fig.[encoded data, 9971 characters]
include_graphics('logo.png')

## ---- out.width='70%', out.height='70%', fig.align='center', echo = [encoded data, 9930 characters]
include_graphics('UBay_UML.png')

## ---- include = [encoded data, 9982 characters]
library(UBayFS)
data(bcw)

## ---- include = [encoded data, 9982 characters]
model = build.UBaymodel(data = bcw$data,
                        target = bcw$labels,
                        M = 100, 
                        tt_split = 0.75,
                        nr_features = 10,
                        method = 'mRMR',
                        prior_model ='dirichlet',
                        weights = 0.01,
                        lambda = 1,
                        constraints = buildConstraints(constraint_types = c('max_size'), 
                                                       constraint_vars = list(3), 
                                                       num_elements = dim(bcw$data)[2], 
                                                       rho = 1),
                        optim_method = 'GA',
                        popsize = 100,
                        maxiter = 100,
                        shiny = FALSE
                        )
summary(model)

## ---- include = TRUE, [encoded data, 9976 characters]
#  
#  library(rpart)
#  decision_tree <- function(X, y, n, name = 'tree'){
#    rf_data = as.data.frame(cbind(y, X))
#    colnames(rf_data) <- make.names(colnames(rf_data))
#    tree = rpart::rpart(y~., data = rf_data)
#  return(list(ranks= which(colnames(X) %in% names(tree$variable.importance)[1:n]),
#              name = name))
#  }
#  
#  model = build.UBaymodel(data = bcw$data,
#                          target = bcw$labels,
#                          M = 100,
#                          tt_split = 0.75,
#                          nr_features = 10,
#                          method = c('mRMR', decision_tree),
#                          prior_model ='dirichlet',
#                          weights = 0.01,
#                          lambda = 1,
#                          constraints = buildConstraints(constraint_types = c('max_size'),
#                                                         constraint_vars = list(3),
#                                                         num_elements = dim(bcw$data)[2],
#                                                         rho = 1),
#                          optim_method = 'GA',
#                          popsize = 100,
#                          maxiter = 100,
#                          shiny = FALSE
#                          )

## ---- include=TRUE, [encoded data, 9978 characters]
#  
#  # recursive feature elimination
#  library(caret)
#  rec_fe <- function(X,y,n, name='rfe'){
#    if(is.factor(y)){
#          control <- rfeControl(functions=rfFuncs, method = 'cv', number = 2)
#        }
#        else{
#          control <- rfeControl(functions=lmFuncs, method = 'cv', number = 2)
#        }
#        results <- caret::rfe(X, y, sizes = n, rfeControl=control)
#        return(list(ranks = which(colnames(X) %in% results$optVariables),
#                    name = name))
#  }
#  
#  # Lasso
#  library(glmnet)
#  lasso <- function(X, y, n=NULL, name='lasso'){
#    family = ifelse(is.factor(y), 'binomial', 'gaussian')
#    cv.lasso <- cv.glmnet(as.matrix(X), y, intercept = FALSE, alpha = 1, family = family, nfolds=3)
#    model <- glmnet(as.matrix(X), y, intercept = FALSE, alpha = 1, family = family,
#                    lambda = cv.lasso$lambda.min)
#    return(list(ranks = which(as.vector(model$beta) != 0),
#                name = name))
#  }
#  
#  # HSIC Lasso
#  library(GSelection)
#  hsic_lasso <- function(X, y, n, name='hsic'){
#    ifelse(is.factor(y), {tl = as.numeric(as.integer(y)-1)}, {tl = y})
#    results = feature.selection(X, tl, n)
#    return(list(ranks = results$hsic_selected_feature_index,
#                name = name))
#  }

## ---- [encoded data, 9992 characters]
weights = rep(c(10,15,20,16,15,10,12,17,21,14), 3)
strength = 1
weights = weights * strength / sum(weights)
print(weights)

model = setWeights(model = model, 
                   weights = weights)

## ---- out.width='70%', out.height='70%', fig.align='center', echo = [encoded data, 9930 characters]
include_graphics('UBay_UML2.png')

## ---- [encoded data, 9992 characters]
constraints = buildConstraints(constraint_types = c('max_size', 
                                                    'must_link', 
                                                    rep('cannot_link', 2)),
                               constraint_vars = list(10, # max-size (maximal 10 features)
                                                      c(1,11,21), # must-link between features 1, 11, and 21
                                                      c(1,10), # cannot-link between features 1, and 10
                                                      c(20,23,24)), # cannot-link between features 20, 23, and 24
                               num_elements = ncol(model$data),
                                rho = c(Inf, # max_size
                                        0.1, # rho for must-link
                                        1, # rho for first cannot-link
                                        1)) # rho for second cannot-link
print(constraints)

model = setConstraints(model = model, constraints = constraints)

## ---- [encoded data, 9992 characters]
model = setOptim(model = model,
                 popsize = 100, 
                 maxiter = 200)


## ---- include=TRUE, fig.width=7, fig.[encoded data, 9961 characters]
model = UBayFS::train(x = model)
summary(model)
plot(model)

## ---- [encoded data, 9992 characters]
# evaluation feature set
evaluateMultiple(state = model$output$feature_set, model = model)

## [encoded data, 9997 characters]
#  runInteractive()

## ---- out.width='100%', echo = [encoded data, 9967 characters]
include_graphics('UBay_Shiny_Screenshot.png')

