GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Examples/CensusAdultIncomeExample/E2.md at main · WinVector/Examples

Expand file tree

/

E2.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
79 lines (72 loc) · 2.13 KB
/

E2.md

Copy path

File metadata and controls

79 lines (72 loc) · 2.13 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

Example showing caret varaible preperation followed by caret training.

library('caret')
library('gbm')
library('doMC')

# parallel for caret
ncores <- parallel::detectCores()
registerDoMC(cores=ncores)
# load data
# data from: http://archive.ics.uci.edu/ml/machine-learning-databases/adult/
colnames <-
  c(
    'age',
    'workclass',
    'fnlwgt',
    'education',
    'education.num',
    'marital.status',
    'occupation',
    'relationship',
    'race',
    'sex',
    'capital.gain',
    'capital.loss',
    'hours.per.week',
    'native.country',
    'class'
  )
dTrain <- read.table(
  'adult.data.txt',
  header = FALSE,
  sep = ',',
  strip.white = TRUE,
  stringsAsFactors = FALSE,
  na.strings = c('NA', '?', '')
)
colnames(dTrain) <- colnames
dTest <- read.table(
  'adult.test.txt',
  skip = 1,
  header = FALSE,
  sep = ',',
  strip.white = TRUE,
  stringsAsFactors = FALSE,
  na.strings = c('NA', '?', '')
)
colnames(dTest) <- colnames
# define problem
yName <- 'class'
yTarget <- '>50K'
varNames <- setdiff(colnames,yName)

# Try to use caret to impute missing values
# looks like methods 'knnImpute' and 'bagImpute' don't like categorical/nominal variables (throw exception).
# Also need to make sure outcome is not part of imputation plan as it won't be known at application time.
xform <- preProcess(dTrain[,varNames], method ='medianImpute',na.remove = TRUE)
dTrainTreated <- predict(xform,dTrain[,varNames],na.action=na.omit)
# And no obvous (at least to me after reading the documentation and trying experiments) 
# combination of caret controls seem to remove NA from categorical variables.
print(summary(is.na(dTrainTreated$native.country)))
##    Mode   FALSE    TRUE    NA's 
## logical   31978     583       0
# Could replace NAs in categoricals with a fresh category, but we would also have to watch
# out for the novel level encoding problem (and we are fixing the data by hand again instead 
# of using a convenient package service).  Or we could call `model.matrix` per column and NA-out 
# all indicators of levels that don't code.  We could do it, but it would not be a demonstration of caret.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.