Question

this is probably PICNIC, but I'm getting a rather odd set of behaviour when trying to build a function than I do when performing the actions in the console.

I'm trying to use cut2 to produce a set of intervals and assign them to a variable for later reuse. The use outside the function produces the appropiate vector, but inside the function the assignment is coercing differently. I tried wrapping as.vector around it but that produced a char[210].

Can anyone tell me what I'm doing wrong?

Thanks, Steph PS the global assignment is so the intervals can be modified, and reused later

Setup

library("Hmisc")
library("caret")
# functions in use ----------------------------------------------------------------
# functions for splitting data according to Max Kuhn's preferences in caret vignette, code condensed to save space
splitDataset<-function(dataset=rawdata, nPrimaryKeyCol=1, nOutcomeCol=1) {
  end<-as.numeric(ncol(dataset))
  stopifnot(is.numeric(nPrimaryKeyCol),nPrimaryKeyCol<=end,is.numeric(nOutcomeCol),nOutcomeCol<=end,(nPrimaryKeyCol+nOutcomeCol)<=end)
  predstart<-nPrimaryKeyCol+1
  predend<-ncol(dataset)-nOutcomeCol
  assign(x="keys",value=dataset[, 0:nPrimaryKeyCol],envir = parent.frame())
  assign(x="outcomes",value=dataset[,(predend+1):end],envir = parent.frame())
  assign(x="predictors",value=dataset[,predstart:predend], envir = parent.frame())
}
    partitionDataset<-function(proportion=0.7){
  require("caret")
  assign(x="inTrain",  value=createDataPartition(outcomes,p=proportion, list=FALSE),  envir = parent.frame())
  assign(x="trainKeys", value=keys[inTrain], envir = parent.frame())
  assign(x="trainPredictors",value=predictors[inTrain,],envir = parent.frame())
  assign(x="trainOutcomes", value=outcomes[inTrain],envir = parent.frame())
  assign(x="testKeys",value=keys[-inTrain],envir = parent.frame())
  assign(x="testPredictors",  value=predictors[-inTrain,],  envir = parent.frame())
  assign(x="testOutcomes",value=outcomes[-inTrain], envir = parent.frame())
  assign(x="trainPredictors.Bad", value=subset(trainPredictors,trainOutcomes=="bad"), envir = parent.frame())
}

# this is the problem function
equalFreqBins.derive<-function(characteristic,deriveDataset,g=20){

  stopifnot(is.numeric(deriveDataset[,characteristic]),is.numeric(g))
  dnam<-paste0("interval.",characteristic)
# this is the assignment
  intervals<-cut2(deriveDataset[,characteristic],g,onlycuts=TRUE)
  assign(
    x=dnam,
    value=intervals,
    envir=as.environment(".GlobalEnv")
    )
# return to make it easier to look at variable
  return(str(intervals))
  }

# data loadup----------------------------------------------------------------
data(GermanCredit)
GermanCredit$outcome<-GermanCredit$Class
GermanCredit$Class<-NULL
levels(GermanCredit$outcome)<-c("bad","good")
basedata<-GermanCredit
splitDataset(basedata,nPrimaryKeyCol=0,nOutcomeCol=1)
partitionDataset(proportion=0.7)

issue reproduction

# this is outputting a factor of 210
equalFreqBins.derive(characteristic="Age",deriveDataset=trainPredictors.Bad,g=20)
# this is outputting a num [1:20]
intervals<-cut2(trainPredictors.Bad[,"Age"],g=20,onlycuts=TRUE)
str(intervals)
Was it helpful?

Solution

The synopsis for cut2 is

cut2(x, cuts, m, g, levels.mean, digits, minmax=TRUE, oneval=TRUE, onlycuts=FALSE)

so when you call

intervals <- cut2(deriveDataset[,characteristic], g, onlycuts = TRUE)

your g variable, being the second unnamed argument, is taken to be the cuts argument. The solution: call your function with named arguments:

intervals <- cut2(deriveDataset[,characteristic], g = g, onlycuts = TRUE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top