Only 0's may be mixed with negative subscripts

Hello everyone. I am currently working with a program which can be found here. The main part of the code is a for loop which first lines read as

for(ind_today in ind_eval){
  cat("\n", "Starting at", paste(Sys.time()), ":", ind_today, "of", 72,"\n"); flush(stdout())
  
  ind_training <- (ind_today-train_length):(ind_today-1)
  cat("... post-processing", "\n")
  # extract training u and v wind means and variances
  uwind_fc_means_train <- apply(uwind_fc_keep[,,,ind_training], c(2,3,4), mean)

In the first iteration of the loop I have no issues, but in the second, the following error message appears.

Error in dataECMWF_U[, , , ind_training] : 
  Only 0's may be mixed with negative subscripts

I think the issue is with the value of ind_training. In the first iteration of the loop, it's value is [-9 -8 -7 -6 -5 -4 -3 -2 -1 0] and in the second it is [-8 -7 -6 -5 -4 -3 -2 -1 0 1].

Can someone please tell me why does this happens and offer me any advice on how to fix it?

Thanks in advance.
Regards.
Jaime.

Hello,

Could you create a reprex of this example?
A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

My guess is that indeed you are trying to subset data structure in two opposing ways. You can either use 'inclusion' like myList[1:3] will select all indices 1 - 3, or 'exclusion' like myList[-c(1:3)] will select everything but index 1 - 3. You can't however combine the two.

Grtz,
PJ

Here it is

rm(list=ls())

library(MASS)
library(abind)
library(scoringRules)

library(ncdf4)
library(raster)

vector1 <- c(5, 9, 3) 
vector2 <- c(10, 11, 12, 13, 14, 15) 

result <- array(c(vector1, vector2),  dim = c(50, 361, 200, 31)) 

ind_eval <- x <- 1:31
train_length <- 10

uwind_pp_save <- array(NA, dim = c(50,361,201,length(ind_eval)))

for(ind_today in ind_eval){
  cat("\n", "Starting at", paste(Sys.time()), ":", ind_today, "of", 72,"\n"); flush(stdout())
  
  ind_training <- (ind_today-train_length):(ind_today-1)
  
  cat ("ind_training is ", ind_training ,"\n")
  cat("... post-processing", "\n")
  uwind_fc_means_train <- apply(result[,,,ind_training], c(2,3,4), mean)#Entiendo que se está calculando la media en cada punto
}

I have altered the data because I am dealing with some files very heavy to upload. However, the error is the same.

Thanks for the answer.
Jaime.

Hi,

So yes indeed the issue is with incorrect subsetting in this line

ind_training <- (ind_today-train_length):(ind_today-1)

In the second iteration, ind_today will generate both pos and neg numbers. Looking at the code the goal of this line it so make a sliding window across the data and use 10 samples at the time, but the way it's implemented is incorrect.

Given I don't know how exactly you want this sliding window to work, I can't provide a definite answer. train_length says that there should be 10 samples per iteration, and there is a total of 31 dimensions, so how do you like to slide 10 over 31?

PJ

The code is an implementation of this article. The basic idea is to compare a set of predictions made in the past with an observation to "train" the prgram and then use it with the current prediction to remove bias introduced by the method to improve the prediction. I am using 31 because it's the number of days in august and I have a prediction from august 2016. Each

Would it work if I limited myself to the last 20 days of the month?

Regards.
Jaime.

Hi,

It seems you're trying to do some sort of cross validation. I'm still a bit confused: how many samples do you have to work with? Just 31? When you pick 10 samples for example, what are you doing with the other ones?

Your code is a bit weird since uwind_fc_means_train gets overwritten every run of the loop, so what you end up with is only the values for the last round, and all the rest is wasted... unless you just shortened the loop for the reprex of course :slight_smile:

PJ

In the end, what I have done is to start counting in the 11th day, that way I have a training period of 10 days for each of my members, which is reduced to the last 21 from the 31avaiable days. That way it works.

The loop is shortened. I have just included the lines wich trigger the error.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.