-#' @title Acquire data in a clean format
+#' getData
#'
-#' @description Take in input data frames and/or files containing raw data, and timezones, and
-#' output a Data object, roughly corresponding to a list where each cell contains all value
-#' for one day (see \code{?Data}).
+#' Acquire data as a Data object; see ?Data.
+#'
+#' Since series are given in columns (database format), this function builds series one
+#' by one and incrementally grows a Data object which is finally returned.
#'
#' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time,
-#' second is value) or a CSV file
-#' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is dates,
-#' next block are measurements for the day, and final block are exogenous forecasts
+#' second is value) or a CSV file.
+#' @param exo_data Exogenous variables, as a data frame or a CSV file; first column is
+#' dates, next block are measurements for the day, and final block are exogenous
+#' forecasts (for the same day).
#' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris")
#' @param date_format How date/time are stored (e.g. year/month/day hour:minutes;
-#' see \code{strptime})
+#' see ?strptime)
#' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris")
-#' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0
+#' @param predict_at When does the prediction take place? Integer, in hours. Default: 0
#' @param limit Number of days to extract (default: Inf, for "all")
#'
-#' @return A list where data[[i]] contains
-#' \itemize{
-#' \item time: vector of times
-#' \item centered_serie: centered serie
-#' \item level: corresponding level
-#' \item exo: exogenous variables
-#' \item exo_hat: predicted exogenous variables
-#' }
+#' @return An object of class Data
#'
#' @examples
#' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg"))
read.csv(ts_data)
else
ts_data
+ # Convert to the desired timezone (usually "GMT" or "Europe/Paris")
+ formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz)
+ ts_df[,1] = format(
+ as.POSIXct(formatted_dates_POSIXlt, tz=input_tz), tz=working_tz, usetz=TRUE)
+
exo_df =
if (is.character(exo_data))
read.csv(exo_data)
else
exo_data
- # Convert to the desired timezone (usually "GMT" or "Europe/Paris")
- formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz)
- ts_df[,1] = format(as.POSIXct(formatted_dates_POSIXlt), tz=working_tz, usetz=TRUE)
+ # Times in exogenous variables file are ignored: no conversions required
line = 1 #index in PM10 file (24 lines for 1 cell)
nb_lines = nrow(ts_df)
nb_exos = ( ncol(exo_df) - 1 ) / 2
- data = list()
+ data = Data$new()
i = 1 #index of a cell in data
while (line <= nb_lines)
{
serie = c(serie, ts_df[line,2])
line = line + 1
};
- if (line >= nb_lines + 1 || as.POSIXlt(ts_df[line-1,1])$hour == predict_at)
+ if (line >= nb_lines + 1
+ || as.POSIXlt(ts_df[line-1,1],tz=working_tz)$hour == predict_at)
+ {
break
+ }
}
exo = as.data.frame( exo_df[i,2:(1+nb_exos)] )
- exo_hat = as.data.frame( exo_df[i,(1+nb_exos+1):(1+2*nb_exos)] )
- level = mean(serie, na.rm=TRUE)
- centered_serie = serie - level
- data[[i]] = list("time"=time, "centered_serie"=centered_serie, "level"=level,
- "exo"=exo, "exo_hat"=exo_hat)
+ exo_hat =
+ if (i < nrow(exo_df))
+ as.data.frame( exo_df[i+1,(1+nb_exos+1):(1+2*nb_exos)] )
+ else
+ NA #exogenous prediction for next day are useless on last day
+ data$append(time, serie, exo, exo_hat)
if (i >= limit)
break
i = i + 1
}
- start = 1
- end = length(data)
- if (length(data[[1]]$centered_serie) < length(data[[2]]$centered_serie))
- start = 2
- if (length(data[[length(data)]]$centered_serie) <
- length(data[[length(data)-1]]$centered_serie))
+ if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2)))
+ data$removeFirst()
+ if (length(data$getCenteredSerie(data$getSize()))
+ < length(data$getCenteredSerie(data$getSize()-1)))
{
- end = end-1
+ data$removeLast()
}
- if (start>1 || end<length(data))
- data = data[start:end]
data
}