robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.11k stars 342 forks source link

is it possible to dispaly metic statistics of time series #703

Closed jasperDD closed 6 years ago

jasperDD commented 6 years ago

I perform time series analysis with separating by groups part of mydat

mydat=structure(list(id = c(1030879980L, 1030879980L, 1030879980L, 
1030879980L, 1030879980L, 1030879980L, 1030879980L, 1030879980L, 
1030879980L, 1030879980L, 1030879980L, 1030880090L, 1030880090L, 
1030880090L, 1030880090L, 1030880090L, 1030880090L, 1030880090L, 
1030880090L, 1030880090L, 1030879980L, 1030879980L, 1030879980L, 
1030879980L, 1030879980L, 1030879980L, 1030879980L, 1030879980L, 
1030879980L, 1030879980L, 1030879980L, 1030880090L, 1030880090L, 
1030880090L, 1030880090L, 1030880090L, 1030880090L, 1030880090L, 
1030880090L, 1030880090L), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Jan.14", "Jan.15"), class = "factor"), 
    value = c(170L, 265L, 571L, 961L, 0L, 418L, 70L, 278L, 68L, 
    164L, 454L, 180L, 349L, 125L, 368L, 286L, 64L, 179L, 187L, 
    46L, 170L, 265L, 571L, 961L, 0L, 418L, 70L, 278L, 68L, 164L, 
    454L, 180L, 349L, 125L, 368L, 286L, 64L, 179L, 187L, 46L)), .Names = c("id", 
"variable", "value"), class = "data.frame", row.names = c(NA, 
-40L))

Split by group

group_list <- split(mydat, list(mydat$id))
group_list <- group_list[sapply(group_list, function(x) nrow(x) != 0)]

library("forecast")

#create forecast
fun_msts <- function(mydat){
  m=msts(mydat$value, seasonal.periods = c(2014,1),start = 12)
}

fun_sp <- function(m){
  tbats <- tbats(m)
  predict(tbats, h=24) #24 months forecast
}

msts_list <- lapply(group_list, fun_msts)
sp_list <- lapply(msts_list, fun_sp)

#if using dummy
fun_tslm <- function(mydat, start = c(2014,1), frequency = 12)
  {
  tsw <- ts(mydat[["value"]], start =c(2014,1), frequency = 12)
  #View(tsw)
  mytslm <- tslm(tsw ~ trend + season)
  mytslm
}

fun_forecast <- function(x, h = 24){
  residarima1 <- auto.arima(x[["residuals"]])
  residualsArimaForecast <- forecast(residarima1, h = h)
  residualsF <- as.numeric(residualsArimaForecast$mean)
  regressionForecast <- forecast(x, h = h)
  regressionF <- as.numeric(regressionForecast$mean)
  forecastR <- regressionF + residualsF
  forecastR
}

tslm_list <- lapply(group_list, fun_tslm)
fore_list <- lapply(tslm_list, fun_forecast)

So , when i perform forecast, i just get predictive values. How can i calculate statistical metrix for each group i am interested in R^2, stationary R^2, Rmse, and MAPE.

As output i expect something like this

id  R^2 stationary R^2  mape    rmse
1   0,9   0,9                  1          1
2   0,9  0,9                   1          1
3   0,9  0,9                  1       1
robjhyndman commented 6 years ago

You obviously already know how to write your own functions. So return whatever statistics you want to record.

jasperDD commented 6 years ago

Hello i want get in output table with metrics like in SPSS how can i do it in R for each id default

here the screen

jasperDD commented 6 years ago

i don't know how to write this function

robjhyndman commented 6 years ago

This is not a help site. Please seek R help on stackoverflow.com