Commit | Line | Data |
---|---|---|
3d69ff21 BA |
1 | #' @title Acquire data in a clean format |
2 | #' | |
3 | #' @description Take in input data frames and/or files containing raw data, and timezones, and | |
4 | #' output a Data object, roughly corresponding to a list where each cell contains all value | |
5 | #' for one day (see \code{?Data}). Current limitation: series (in working_tz) must start at | |
6 | #' right after midnight (to keep in sync with exogenous vars) | |
7 | #' | |
8 | #' @param ts_data Time-series, as a data frame (DB style: 2 columns, first is date/time, | |
9 | #' second is value) or a CSV file | |
10 | #' @param exo_data Exogenous variables, as a data frame or a CSV file; first comlumn is dates, | |
11 | #' next block are measurements for the day, and final block are exogenous forecasts | |
12 | #' @param input_tz Timezone in the input files ("GMT" or e.g. "Europe/Paris") | |
13 | #' @param date_format How date/time are stored (e.g. year/month/day hour:minutes; | |
14 | #' see \code{strptime}) | |
15 | #' @param working_tz Timezone to work with ("GMT" or e.g. "Europe/Paris") | |
09cf9c19 | 16 | #' @param predict_at When does the prediction take place ? Integer, in hours. Default: 0 |
3d69ff21 BA |
17 | #' |
18 | #' @return An object of class Data | |
19 | #' | |
20 | #' @export | |
1e20780e BA |
21 | getData = function(ts_data, exo_data, input_tz="GMT", date_format="%d/%m/%Y %H:%M", |
22 | working_tz="GMT", predict_at=0, limit=Inf) | |
3d69ff21 BA |
23 | { |
24 | # Sanity checks (not full, but sufficient at this stage) | |
25 | if (!is.character(input_tz) || !is.character(working_tz)) | |
26 | stop("Bad timezone (see ?timezone)") | |
27 | input_tz = input_tz[1] | |
28 | working_tz = working_tz[1] | |
6d97bfec BA |
29 | if ( (!is.data.frame(ts_data) && !is.character(ts_data)) || |
30 | (!is.data.frame(exo_data) && !is.character(exo_data)) ) | |
31 | stop("Bad time-series / exogenous input (data [frame] or CSV file)") | |
3d69ff21 BA |
32 | if (is.character(ts_data)) |
33 | ts_data = ts_data[1] | |
6d97bfec BA |
34 | if (is.character(exo_data)) |
35 | exo_data = exo_data[1] | |
09cf9c19 BA |
36 | predict_at = as.integer(predict_at)[1] |
37 | if (predict_at<0 || predict_at>23) | |
38 | stop("Bad predict_at (0-23)") | |
3d69ff21 BA |
39 | if (!is.character(date_format)) |
40 | stop("Bad date_format (character)") | |
41 | date_format = date_format[1] | |
42 | ||
43 | ts_df = | |
44 | if (is.character(ts_data)) { | |
6d97bfec BA |
45 | if (ts_data %in% data(package="talweg")$results[,"Item"]) |
46 | ts_data = | |
47 | ||
48 | ||
49 | ||
50 | ||
51 | ############CONTINUE: http://r-pkgs.had.co.nz/data.html | |
52 | ||
53 | ||
54 | ||
55 | ||
56 | ||
57 | read.csv(ts_data) | |
3d69ff21 BA |
58 | } else { |
59 | ts_data | |
60 | } | |
61 | exo_df = | |
62 | if (is.character(exo_data)) { | |
63 | read.csv(exo_data) | |
64 | } else { | |
65 | exo_data | |
66 | } | |
67 | # Convert to the desired timezone (usually "GMT" or "Europe/Paris") | |
68 | formatted_dates_POSIXlt = strptime(as.character(ts_df[,1]), date_format, tz=input_tz) | |
69 | ts_df[,1] = format(as.POSIXct(formatted_dates_POSIXlt), tz=working_tz, usetz=TRUE) | |
70 | ||
3d69ff21 BA |
71 | line = 1 #index in PM10 file (24 lines for 1 cell) |
72 | nb_lines = nrow(ts_df) | |
73 | nb_exos = ( ncol(exo_df) - 1 ) / 2 | |
74 | data = list() #new("Data") | |
75 | i = 1 #index of a cell in data | |
76 | while (line <= nb_lines) | |
77 | { | |
78 | time = c() | |
79 | serie = c() | |
09cf9c19 | 80 | repeat |
3d69ff21 | 81 | { |
09cf9c19 BA |
82 | { |
83 | time = c(time, ts_df[line,1]) | |
84 | serie = c(serie, ts_df[line,2]) | |
85 | line = line + 1 | |
86 | }; | |
87 | if (line >= nb_lines + 1 || as.POSIXlt(ts_df[line-1,1])$hour == predict_at) | |
88 | break | |
89 | } | |
3d69ff21 | 90 | |
1e20780e BA |
91 | # NOTE: if predict_at does not cut days at midnight, exogenous vars need to be shifted |
92 | exo_hat = as.data.frame( exo_df[ | |
93 | ifelse(predict_at>0,max(1,i-1),i) , (1+nb_exos+1):(1+2*nb_exos) ] ) | |
94 | exo = as.data.frame( exo_df[ ifelse(predict_at>0,max(1,i-1),i) , 2:(1+nb_exos) ] ) | |
3d69ff21 BA |
95 | level = mean(serie, na.rm=TRUE) |
96 | centered_serie = serie - level | |
1e20780e | 97 | #data$append(time, centered_serie, level, exo_hat, exo_Jm1) #too slow; TODO: use R6 class |
3d69ff21 | 98 | data[[length(data)+1]] = list("time"=time, "serie"=centered_serie, "level"=level, |
dea7ff86 | 99 | "exo_hat"=exo_hat, "exo"=exo) |
1e20780e BA |
100 | if (i >= limit) |
101 | break | |
102 | i = i + 1 | |
3d69ff21 BA |
103 | } |
104 | new("Data",data=data) | |
105 | } |