X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FgetData.R;h=9ed021714c45175f40f12fae62300e06e3519abe;hb=7f90df63d1ac659d450a180b50b994a76d5fc7b9;hp=153a660f35a56667342232331fdad2c8992b8f97;hpb=1e20780ee1505fac6c7ed68d340892c497524561;p=talweg.git diff --git a/pkg/R/getData.R b/pkg/R/getData.R index 153a660..9ed0217 100644 --- a/pkg/R/getData.R +++ b/pkg/R/getData.R @@ -2,8 +2,7 @@ #' #' @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}). Current limitation: series (in working_tz) must start at -#' right after midnight (to keep in sync with exogenous vars) +#' for one day (see \code{?Data}). #' #' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time, #' second is value) or a CSV file @@ -14,9 +13,14 @@ #' see \code{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 limit Number of days to extract (default: Inf, for "all") #' #' @return An object of class Data #' +#' @examples +#' ts_data = read.csv(system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")) +#' exo_data = read.csv(system.file("extdata","meteo_extra_noNAs.csv",package="talweg")) +#' data = getData(ts_data, exo_data, limit=120) #' @export getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M", working_tz="GMT", predict_at=0, limit=Inf) @@ -26,10 +30,13 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:% stop("Bad timezone (see ?timezone)") input_tz = input_tz[1] working_tz = working_tz[1] - if (!is.data.frame(ts_data) && !is.character(ts_data)) - stop("Bad time-series input (data frame or CSV file)") + if ( (!is.data.frame(ts_data) && !is.character(ts_data)) || + (!is.data.frame(exo_data) && !is.character(exo_data)) ) + stop("Bad time-series / exogenous input (data frame or CSV file)") if (is.character(ts_data)) ts_data = ts_data[1] + if (is.character(exo_data)) + exo_data = exo_data[1] predict_at = as.integer(predict_at)[1] if (predict_at<0 || predict_at>23) stop("Bad predict_at (0-23)") @@ -38,25 +45,25 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:% date_format = date_format[1] ts_df = - if (is.character(ts_data)) { + if (is.character(ts_data)) read.csv(ts_data) - } else { + 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)) { + if (is.character(exo_data)) read.csv(exo_data) - } else { + 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() #new("Data") + data = Data$new() i = 1 #index of a cell in data while (line <= nb_lines) { @@ -73,18 +80,21 @@ getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:% break } - # NOTE: if predict_at does not cut days at midnight, exogenous vars need to be shifted - exo_hat = as.data.frame( exo_df[ - ifelse(predict_at>0,max(1,i-1),i) , (1+nb_exos+1):(1+2*nb_exos) ] ) - exo = as.data.frame( exo_df[ ifelse(predict_at>0,max(1,i-1),i) , 2:(1+nb_exos) ] ) + 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$append(time, centered_serie, level, exo_hat, exo_Jm1) #too slow; TODO: use R6 class - data[[length(data)+1]] = list("time"=time, "serie"=centered_serie, "level"=level, - "exo_hat"=exo_hat, "exo"=exo) + data$append(time, centered_serie, level, exo, exo_hat) if (i >= limit) break i = i + 1 } - new("Data",data=data) + if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2))) + data$removeFirst() + if (length(data$getCenteredSerie(data$getSize())) + < length(data$getCenteredSerie(data$getSize()-1))) + { + data$removeLast() + } + data }