4

When I execute the following my "predictors" dataset is populated correctly:

library(rhdf5)
library(forecast)
library(sltl)
library(tseries)

fid <- H5Fcreate(output_file)
## TODO: compute the order p
p <- 4
# write predictors
h5createDataset(output_file, dataset="predictors", c(p, length(tsstl.remainder) - (p - 1)), storage.mode='double')
predictors <- as.matrix(tsstl.remainder)
for (i in 1:(p - 1)) {    
    predictors <- as.matrix(cbind(predictors, Lag(as.matrix(tsstl.remainder), i)))
}
predictors <- as.matrix(predictors[-1:-(p-1),])
head(predictors)
h5write(predictors, output_file, name="predictors")
H5Fclose(fid)

The generated (correct) output for head(predictors) is:

            [,1]        [,2]        [,3]       [,4]
[1,]   0.3089645   6.7722063   5.1895389  5.2323261
[2,]   8.7607228   0.3089645   6.7722063  5.1895389
[3,]  -0.9411553   8.7607228   0.3089645  6.7722063
[4,] -14.1390243  -0.9411553   8.7607228  0.3089645
[5,] -26.6605296 -14.1390243  -0.9411553  8.7607228
[6,]  -8.1293076 -26.6605296 -14.1390243 -0.9411553

However, when I read it the results are not correct:

tsmatrix <- t(as.matrix(h5read(output_file, "predictors")))
head(tsmatrix)

Incorrectly outputs:

            [,1]      [,2]       [,3]      [,4]
[1,]   0.3089645  8.760723 -0.9411553 -14.13902
[2,] -26.6605296 -8.129308 -9.8687675  31.52086
[3,]  54.2703126 43.902489 31.8164836  43.87957
[4,]  22.1260636 36.733055 54.7064107  56.35158
[5,]  36.3919851 25.193068 48.2244464  57.12196
[6,]  48.0585673 72.402673 68.3265518  80.18960

How come what I write does not correspond to what I get back? I double-checked and hdfview HDF5 viewer also shows this incorrect values for the "predictors" dataset.

What is wrong here?

SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • Please mention any and all packages you're using – Dason Aug 21 '13 at 14:14
  • 1
    Try it without the `t`? – blmoore Aug 21 '13 at 14:15
  • libraries provided, with the t is exactly the same output that I get in hdfview which is very weird. – SkyWalker Aug 21 '13 at 14:19
  • did you want transpose? t is for transpose – user2510479 Aug 21 '13 at 14:22
  • I understand `t` is for transpose, I wanted to see the output how I see it in hdfview, the way to do so is to take the transpose `t(...)` I know it sounds crazy but this is precisely the problem: I write something and get something completely different written into the file. – SkyWalker Aug 21 '13 at 14:24
  • From the rhdf5 docs: `Please note, that arrays appear as transposed matrices when opening it with a C-program (h5dump or HDFView). This is due to the fact the fastest changing dimension on C is the last one, but on R it is the first one (as in Fortran).` – blmoore Aug 21 '13 at 14:36
  • @blmoore, you're right this exactly is the issue. If I write my data transposed then hdfview shows the correct results. Please post this as the answer and I will accept it. – SkyWalker Aug 21 '13 at 14:41

1 Answers1

4

From the rhdf5 docs:

Please note, that arrays appear as transposed matrices when opening it with a C-program (h5dump or HDFView). This is due to the fact the fastest changing dimension on C is the last one, but on R it is the first one (as in Fortran).

blmoore
  • 1,487
  • 16
  • 31