2
$\begingroup$

I have an imbalanced time series dataset in which older cohorts have more observations than newer cohorts. I'm trying to use strata and sampsize in R's randomForest function to downsample the older cohorts in order to build a regression model. However, my attempt is resulting in the error "sampsize should be of length one". Aside from the error, I'm wondering if this approach is appropriate or necessary for random forest regression. I know it's a common way to deal with imbalanced data in random forest classification.

Below is my R code. Most of it is just to construct a hypothetical data frame with imbalanced cohort data. I then try to build random forest models, one that doesn't try to stratify sampling (that works) and one that does try to stratify sampling (results in error)

#Create data frame in which older cohorts have more observations
date <- c(seq.Date(as.Date('2016-01-01'), as.Date('2016-10-01'), "month"),
  seq.Date(as.Date('2016-02-01'), as.Date('2016-10-01'), "month"),
  seq.Date(as.Date('2016-03-01'), as.Date('2016-10-01'), "month"),
  seq.Date(as.Date('2016-04-01'), as.Date('2016-10-01'), "month"),
  seq.Date(as.Date('2016-05-01'), as.Date('2016-10-01'), "month"),
  seq.Date(as.Date('2016-06-01'), as.Date('2016-10-01'), "month"))

cohort <- factor(c(rep(1,10),rep(2,9),rep(3,8),rep(4,7),rep(5,6),rep(6,5)))

set.seed(10)
x1 <- 1:10 + rnorm(10)
x2 <- sin(10) + rnorm(10)
y <- x1 + x2 + rnorm(10)

set.seed(1)
x1noise2 <- rnorm(9, 0, .1)
x1noise3 <- rnorm(8, 0, .1)
x1noise4 <- rnorm(7, 0, .1)
x1noise5 <- rnorm(6, 0, .1)
x1noise6 <- rnorm(5, 0, .1)

x1 <- c(x1, x1[2:10]+x1noise2, x1[3:10]+x1noise3, x1[4:10]+x1noise4, x1[5:10]+x1noise5, x1[6:10]+x1noise6)

set.seed(2)
x2noise2 <- rnorm(9, 0, .1)
x2noise3 <- rnorm(8, 0, .1)
x2noise4 <- rnorm(7, 0, .1)
x2noise5 <- rnorm(6, 0, .1)
x2noise6 <- rnorm(5, 0, .1)

x2 <- c(x2, x2[2:10]+x2noise2, x2[3:10]+x2noise3, x2[4:10]+x2noise4, x2[5:10]+x2noise5, x2[6:10]+x2noise6)

set.seed(3)
ynoise2 <- rnorm(9, 0, .1)
ynoise3 <- rnorm(8, 0, .1)
ynoise4 <- rnorm(7, 0, .1)
ynoise5 <- rnorm(6, 0, .1)
ynoise6 <- rnorm(5, 0, .1)

y <- c(y, y[2:10]+ynoise2, y[3:10]+ynoise3, y[4:10]+ynoise4, y[5:10]+ynoise5, y[6:10]+ynoise6)

df <- data.frame(date, cohort, x1, x2, y)

#Plot of data shown below
plot(df$date, df$y, col=df$cohort)

#Build random forest models
library(randomForest)
set.seed(4)
rf1 <- randomForest(y~x1+x1, data=df) #This works
set.seed(4)
rf2 <- randomForest(y~x1+x1, data=df, strata=df$cohort, sampsize=rep(4,6)) #This results in error saying 'sampsize should be of length one'

Plot of target variable by date colored by cohort

$\endgroup$
1
  • $\begingroup$ so your random Forrest is being applied independently within time points? or to the whole data set? ie what is your model? $\endgroup$ Jun 2, 2019 at 1:05

1 Answer 1

1
$\begingroup$

You get an error "sampsize should be of length one" because the lowest sample size is 1 - there is only one observation in January. Aside from this, however, the problem here is the fact that it is time-series data, which probably violates the independence assumption.

$\endgroup$
2
  • $\begingroup$ Any other reason this error might show up? I two groups of the strata factor variable that each have greater than or equal to the number of observations given in sampsize. randomForest(tree~., data=training, strata = as.factor(Region), sampsize = rep(16531, 2)) $\endgroup$ Mar 9, 2018 at 14:44
  • $\begingroup$ it took me a while to reproduce this given the incomplete info but between this and your question 332566, the problem seems to be that you are applying sampsize to RF in regression mode, else sampsize is not a vector of the length the number of strata. sampsize tries to talk to 'tree'. Reformulate your question to include a reproducible example for a more detailed response. $\endgroup$
    – katya
    Mar 10, 2018 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.