0

I want to extract yearly data of M4 competition. For M3 and M1 data I extract "yearly" data using the following codes

library(Mcomp)
yearly_m1 <- subset(M1, "yearly")
yearly_m3 <- subset(M3, "yearly")

When I using following for the M4comp

library(M4comp)
yearly_m4 <- subset(M4, "yearly")

I get an error message. I am wondering how to extract yearly data of M4comp.

user7892705
  • 137
  • 2
  • 9
  • 4
    To be frank, I have no interest in installing a package, looking at the data, and trying yout code, with the sole purpose of sharing this coding moment. If you want help, you really need to put forth a little more effort. For starters, ***what error message***? I suggest you skim through [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), [help/mvce](https://stackoverflow.com/help/mcve), and [help/how-to-ask](https://stackoverflow.com/help/how-to-ask). – r2evans Aug 17 '17 at 03:26
  • `M4` has different classes from `M3` and `M1` amd you need to convert it to a data frame before operating `subset`. Are you looking for `yearly_m4 <- M4[[1]]` ? – MFR Aug 17 '17 at 03:44
  • No, M4[[1]] gives the first time series in the M4. I want to extract all YEARLY data. You are correct M4 has a different class from M3 and M1 – user7892705 Aug 17 '17 at 04:55
  • Do you realize how vague this seems? There are people here honestly wanting to help, it would help if you could provide something more concrete. You have not provided any clear indication of the error condition. Furthermore, since (as the [documentation states](https://github.com/bsouhaib/M4comp/blob/master/R/data.R)) `M4` is a list of 10k time-series elements, you either need to (a) specify which `id` you have interest in; or (b) ask how to extract yearly data from the first element (@MFR hinted at it), repeat that for the remain 9,999 elements, and somehow combine the different time-series. – r2evans Aug 17 '17 at 05:24
  • I need to extract all yearly time series of M4 competition data. – user7892705 Aug 17 '17 at 05:52

1 Answers1

1

Use this to summarise the data

if(!require("devtools")) install.packages("devtools")
library(devtools)

if(!require("M4comp2018")) devtools::install_github("carlanetto/M4comp2018")
library(M4comp2018)

data(M4)
df <- data.frame(matrix(ncol = 5, nrow = 100000))
colnames(df) <- c( "st", "n", "type", "h", "period")
df$st     <- unlist(Map(function(l) { as.character(l$st[[1]][1]) }, M4))
df$n      <- unlist(Map(function(l) { c(l$n[[1]][1]) }, M4))
df$type   <- unlist(Map(function(l) { as.character(l$type[[1]][1]) }, M4))
df$h      <- unlist(Map(function(l) { c(l$h[[1]][1]) }, M4))
df$period <- unlist(Map(function(l) { as.character(l$period[[1]][1]) }, M4))
M4.Summary <- df
rm(df, M4)
ishonest
  • 433
  • 4
  • 8