demo

Introduction

This is a simple demo to carry out variable selection

library(fspls2)

#devtools::load_all( "~/github/fspls2/R")
##set the types
  options("fspls.types"= jsonlite::fromJSON('{"gaussian":["correlation","rms"],"binomial":["AUC"],"multinomial":["AUC"],"ordinal" : "AUC_all"}'))

  ## set the y_transforms to explore  
  transform_x=getTransform(pow = 1,    ## transformations raise to the powers indicated here.  Default is 1 but can add others
                            n_random=5,perm=FALSE ## number of random transformations, also used in stopping critera
                            )
  ## set the flags
  flags = list(min=0,  #minimum number of variables
               max=20, # maximum number of variables,  
               nfold=5,## number of folds.  10 fold implies a 10 non-overlapping 10% splits
               batchsize=0, ##batchsize can be specified alternatively to nfold, but only one can be nonzero
               angles_only=FALSE, ## whether to use purely angles to find best variables (RECOMMENDED FALSE)
               topn=20,  ## if using model fit to rank variables, how many top angles to take  (larger takes more time)
               beam=1, ## how many best combinations to take through to next round of iteration. minimum value is 1, but higher explores more combinations
               all_v_all=FALSE,  ## for multi-class outcomes, this builds all vs all classifier, instead of a one vs all
               project=TRUE,   ## whether to use the projection approach in model building (TRUE recommended)
            useglmnet=FALSE,  ##whether to useglmnet for model fitting (after variables selected) doesnt work well for randomisation.  Note that useglmnet is TRUE by default for multinomial
                        verbose=TRUE,
            lambda = NULL, ## for using glmnet
              show_warnings=FALSE,
            show_pvalue_plots=FALSE  ## get plots of pvalues over iterations
    )

  #check_flags(flags)  ## runs som simple checks
  examples = c("binomial", "gaussian", "ordinal", "multinomial");
#example="gaussian"
  



  #FUNCTION TO SHOW HOW TO RUN EACH EXAMPLE
  runExample<-function(example,nfold=5){
   flags$nfold = nfold
    fi = system.file("extdata", paste0(example,"_data.rds"), package = "fspls2")
    if (fi == "") stop("Could not find extdata/", example, "_data.rds in fspls2 package")
    dataset = readRDS(fi)
       ## set up the objects
    
     
    dh = dataH$new(dataset$dataset, y = dataset$y,    nme=example, flags=flags)
                
  
    ##Step 1. run variable selection 
   
        datasH = list(dh)
    #      
    #vars_all = analysis$select(datasH,  flags, transform_x, phens = dh$pheno()$all)
  
      analysis =analysisEnv$new(flags=flags, dbDir=NULL)
     
      data_types = dh$data_types();
      dh$update(phens = dh$pheno()$all, flags = flags, transform_x = transform_x, data_types = data_types)
    variables = dh$select( analysis=analysis)
  #  selected_plot= dh$plotData(variables, update=T)
  # print(selected_plot)
    #variables = fspls.select(list(dh),  flags, transform_x) #, phens = dh$pheno()$all)
    #Step 2. make models with selected variables
    all_models =dh$makeAllModels(variables)
    
    #optionally get predictions
    
    #Step 3. evaluate the models
    eval1= dh$evaluateAllModels(all_models)
    ## Now visuaise the resultsy
    ggps1=plotEval(eval1,legend=T, grid1=c("subpheno","pheno"), grid0=c("measure","cv_full"),linetype="data", ##"full_model"
                   shape_color=c("data","transf"),sep_by=c("beam"), showranges=T,text="variable",logy=example=="gaussian",
                   scales="free",title =names(phens)[1], title1="pheno" ) #, grid="pheno~cv_full",showranges = F)
  
    ##optionally get all predictions
    #predictions =     dh$extractPredictions(all_models)
   
    ggps1[[1]]
  }


##iterative function carries out imputation of uncertain y values at the same time as feature selection
##only works for multinomial and binomial
runIterative<-function(example, prop_na=0.2, seed=42){
 
  flags$nfold=1 ## only works with one fold currently
  
     fi = system.file("extdata", paste0(example,"_data.rds"), package = "fspls2")
    dataset = readRDS(fi); 
    if(example=="binomial") dataset$y$y = factor(dataset$y$y, labels=c("A","B"))
    dataset$certainty = rep(1, nrow(dataset$y));
    set.seed(seed)
    na_inds1=sort(sample.int(nrow(dataset$y),nrow(dataset$y)*prop_na))
  dataset$certainty[na_inds1]=0.5;##1/length(levels(dataset$y[[1]]))
  #dataset$y[na_inds,] = NA
  
  flags$mult = 100
  flags$select_each_iteration = TRUE;
  results = fspls.iterative(dataset, flags, transform_x)
  print(results$updates)
  na_remaining= length(which(results$certainty_new<0.95))
  message(paste("na remaining", na_remaining))  
  message(paste("error rate",results$error_rate))
  print(results$vars_all$full)
}

Example 1

ggp1=runExample("binomial", nfold=5);
ggp1

Example 2


  ggp2 = runExample("gaussian", nfold=5)
  ggp2

Example 3

ggp3 =   runExample("ordinal", nfold=5)
ggp3

Example 4

 ggp4= runExample("multinomial", nfold=5)
ggp4

Iteratively replacing uncertain values

## test Iterative works on binomial and multinomial
runIterative("binomial")
## test Iterative works on binomial and multinomial
runIterative("multinomial")