Skip to main content
5 of 5
added 61 characters in body

Some code based on Rob's workaround of setting the negatives to zero and reconcile

# Re-reconciliate when zero values present

# Extract groups
groups <- hts.obj %>%
  aggts() %>%
  get_groups()

x=0 #Counter

# Loop until all positive
while(sum(hts.obj[[1]] <0) > 0){

  # Generate all time series
  hts.obj <- aggts(hts.obj)    
  
  # Overwrite negatives by zero
  hts.obj[hts.obj<0] <- 0
  
  # Reconcile
  hts.obj <- hts.obj %>%
    ts() %>%
    combinef(groups = groups, keep ="gts")
  
  # Count up
  x=x+1

  # Break after 10 loops
  if(x>=10)break

  }

rm("x")

# Overwrite remaining negatives by zero
hts.obj[[1]][hts.obj[[1]]<0] <- 0

In this example forecasts are indexed by [[1]] - this might change. Also note that overwriting zeros leads to biased forecasts.