2

I need to do some forecasting with a not so popular algorithm (GMDH) which essentially is polynomial neural network. I've found the algorithm and the example works fine my problem occurs when I try to apply my own dataset.

so my data is a daily dataset which consist of 7 values (5,5,6,7,8,9,18)and to convert this to time series I use the following code : ts = ts(test, start=decimal_date(as.Date("2020-01-01")), frequency=365.25) with test being the df holding the data.

Now when I pass the ts object into the algorithm I get an error which states incorrect number of dimensions upon investigating essentially comparing my dataset to the example data which is the data(cancer) I see that the ts object I converted differs from the ts object the sample is using as seen below :

I guess my question is how do I convert my initial data to the same format which I'm assuming the algorithm would accept or even perhaps convert or modify my ts object to the same format as the data(cancer). In case you can't see the difference my data seem to be in rows while the cancer data values are all in a column accros..sorry I'm new to r and my terminology isn't great.

The information you requested concerning the data: dput(head(cancer)) structure(c(100.4, 105.1, 109.4, 110.5, 116.9, 118.2), .Tsp = c(1930, 1935, 1), class = "ts") dput(head(ts)) structure(c(5L, 5L, 6L, 7L, 8L, 9L), .Dim = c(6L, 1L), .Dimnames = list( NULL, "V1"), .Tsp = c(2020, 2020.01368925394, 365.25), class = "ts"

I hope this is what you requested again apologies

Olayemi
  • 69
  • 6
  • Have you tried `dim(ts) <- NULL`? Also, for future reference, rather than showing a screenshot of your data, the preferred way is to past the output of `dput(your_data)` (or `dput(head(your_data))`; this is much more informative for those trying to help. Additionally, while this isn't strictly necessary when you're talking about a package dataset like `cancer`, that part really only helps if we know what package the dataset comes from. – duckmayr Jul 25 '20 at 20:09
  • dim(ts) and dim(cancer) both return null – Olayemi Jul 26 '20 at 07:17
  • If that is true, then that **really** underscores my point that you should [edit] your question to include the output of the R commands `dput(ts)` and `dput(cancer)` (that last one is not necessary if you tell us which package the `cancer` data comes from). A `dput()` representation of the data would have automatically given me that information. – duckmayr Jul 26 '20 at 11:25
  • the cancer data comes from the GMDH package. install.packages("GMDH") library("GMDH") data(cancer) – Olayemi Jul 26 '20 at 14:47
  • Thanks; now the only thing left for a [mcve] is to [edit] your question to either (1) include the output of the R command `dput(ts)`, or (2) include the full code that _you used_ to construct `ts`. You get close to the second with `ts = ts(test, start=decimal_date(as.Date("2020-01-01")), frequency=365.25)`, but without knowing the _exact_ structure of `test`, there are things we still do not know about `test` that could affect the answer to your problem. – duckmayr Jul 26 '20 at 15:50
  • I've added the test data as requested, I assumed since I specifically listed the data sequence in the original question that you guys would be able to recreate it – Olayemi Jul 27 '20 at 12:51
  • You still added a screenshot. Why? I described specifically how to add the data in a helpful way. There is a reason I ask for it that way: As you say, you listed the data sequence so we could recreate it. But, when I did, on my machine, doing the first fix I mention in the comments eliminated the problem of the data having rows, while you say it didn't work for you. That means there's some info about the data we do not have. If you will not add a [mcve] following the guidance in [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/8386140), then I cannot help you. – duckmayr Jul 27 '20 at 13:29
  • ok ive used the `dput(head(ts))` to show the information you requested. Apologies about this not used to StackOverflow and was also in a hurry – Olayemi Jul 27 '20 at 14:12

1 Answers1

1

So we can fix the issue of ts being a one by n matrix rather than a vector like cancer by setting dim(ts) <- NULL. First, observe that using your newly posted data, we in fact have the same data structures:

cancer <- structure(c(100.4, 105.1, 109.4, 110.5, 116.9, 118.2),
                    .Tsp = c(1930, 1935, 1), class = "ts")

ts <- structure(c(5L, 5L, 6L, 7L, 8L, 9L), .Dim = c(6L, 1L),
                .Dimnames = list( NULL, "V1"),
                .Tsp = c(2020, 2020.01368925394, 365.25), class = "ts")

ts
# Time Series:
# Start = 2020 
# End = 2020.01368925394 
# Frequency = 365.25 
#      V1
# [1,]  5
# [2,]  5
# [3,]  6
# [4,]  7
# [5,]  8
# [6,]  9
cancer
# Time Series:
# Start = 1930 
# End = 1935 
# Frequency = 1 
# [1] 100.4 105.1 109.4 110.5 116.9 118.2

Now inspect a copy of ts with dim() set to NULL:

ts2 <- ts
dim(ts2) <- NULL
ts2
# Time Series:
# Start = 2020 
# End = 2020.01368925394 
# Frequency = 365.25 
# [1] 5 5 6 7 8 9

This should prevent your incorrect number of dimensions error

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • Thanks for that it solved the dimensions problem. Could you do me a favour and run the following`ts <- structure(c(5L, 5L, 6L, 7L, 8L, 9L), .Dim = c(6L, 1L), .Dimnames = list( NULL, "V1"), .Tsp = c(2020, 2020.01368925394, 365.25), class = "ts")` `install.packages("GMDH")` `library("GMDH")` `out = fcast(ts, method = "GMDH", input = 6, layer = 30, f.number = 3, level = 95, tf = "polynomial", weight = 0.70)` – Olayemi Jul 27 '20 at 16:19
  • Hmm... It looks like you have a _new_ error @Olayemi . My suggestion would be to ask a new question. First show the code you ran with `cancer` that worked. Then post the `dput()` for your new data that you're having trouble with, as well as the code that you ran leading to the new error. This error seems to be less of a simple problem of dimensions, but rather may need some more in-depth examination of the specific problem, for which a new question would be appropriate. – duckmayr Jul 27 '20 at 16:41