From: Benjamin Auder Date: Thu, 16 Mar 2017 21:55:52 +0000 (+0100) Subject: R CMD check compliant; TODO: doc, vignette X-Git-Url: https://git.auder.net/img/css/js/current/gitweb.css?a=commitdiff_plain;h=4e25de2cf40b946ac6e8c2abc824637a249941d1;p=talweg.git R CMD check compliant; TODO: doc, vignette --- diff --git a/.gitignore b/.gitignore index 6bf4539..04ce472 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,42 @@ -#NOTE: knitr generated files in ignored folder under reports (?!) -.RData +#roxygen2 generated files +NAMESPACE +*.Rd +!*-package.Rd + +#temporary files +*~ +*.swp + +#R session files .Rhistory +.RData + +#R CMD build/check files +/*.Rcheck/ +/*.tar.gz + +#object & library files +*.[oa] +*.so + +#jupyter checkpoints .ipynb_checkpoints/ -*.swp -*~ + +#large CSV files data/*.csv -*.Rd -!*-package.Rd -*.tar.gz -/*.tar.xz -NAMESPACE -/reports/*.html -/pkg/vignettes/*.html -nohup.out + +#.gitattributes/.gitfat files are generated by initialize.sh +.gitattributes +.gitfat + +#Rmarkdown, knitr + LaTeX generated files +*.tex +*.pdf +!/biblio/*.pdf +*.aux +*.dvi +*.log +*.out +*.toc +*.synctex.gz +figure/ diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 7fb20cc..b932d77 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,28 +1,30 @@ Package: talweg -Title: talweg : Time-series sAmpLes forecasted With ExoGenous variables +Title: Time-Series Samples Forecasted With Exogenous Variables Version: 0.1-0 -Description: Forecast a curve sampled within the day (seconds, minutes, - hours...), using past measured curves, history of some exogenous variables - measurements and the exogenous prediction for tomorrow. Main method is - computeForecast() -Authors: Benjamin Auder [aut,cre], +Description: Forecast a curve sampled within the day (seconds, minutes, hours...), + using past measured curves + paste exogenous informations, + which could be some aggregated measure on the past curves, the weather... + Main starting point: computeForecast(). +Author: Benjamin Auder [aut,cre], Jean-Michel Poggi [ctb], Bruno Portier , [ctb] Maintainer: Benjamin Auder Depends: R (>= 3.0), - R6 +Imports: + R6, + methods Suggests: + devtools, roxygen2, testthat, - rmarkdown, - rainbow, - knitr + rainbow LazyData: yes URL: http://git.auder.net/?p=talweg.git License: MIT + file LICENSE RoxygenNote: 6.0.1 -Collate: +Collate: + 'A_NAMESPACE.R' 'Data.R' 'Forecaster.R' 'F_Average.R' @@ -38,4 +40,3 @@ Collate: 'getData.R' 'plot.R' 'utils.R' -VignetteBuilder: knitr diff --git a/pkg/R/A_NAMESPACE.R b/pkg/R/A_NAMESPACE.R new file mode 100644 index 0000000..4558e59 --- /dev/null +++ b/pkg/R/A_NAMESPACE.R @@ -0,0 +1,7 @@ +#' @importFrom grDevices colors gray.colors +#' @importFrom graphics abline hist par plot +#' @importFrom methods hasArg is +#' @importFrom stats quantile sd +#' @importFrom utils getFromNamespace read.csv tail +#' +NULL diff --git a/pkg/R/plot.R b/pkg/R/plot.R index 4cafd7d..52b077b 100644 --- a/pkg/R/plot.R +++ b/pkg/R/plot.R @@ -14,7 +14,7 @@ plotCurves <- function(data, indices=seq_len(data$getSize())) for (i in seq_along(indices)) { plot(series[,i], type="l", ylim=yrange, - xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10","")) + xlab=ifelse(i==1,"Time (hours)",""), ylab=ifelse(i==1,"PM10","")) if (i < length(indices)) par(new=TRUE) } @@ -41,32 +41,32 @@ plotError <- function(err, cols=seq_along(err)) yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$day ) ), na.rm=TRUE ) for (i in seq_len(L)) { - plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""), - ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i]) + plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Time (hours)",""), + ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i]) if (i < L) par(new=TRUE) } yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE ) for (i in seq_len(L)) { - plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""), - ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i]) + plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Time (days)",""), + ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i]) if (i < L) par(new=TRUE) } yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE ) for (i in seq_len(L)) { - plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""), - ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i]) + plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Time (hours)",""), + ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i]) if (i < L) par(new=TRUE) } yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE ) for (i in seq_len(L)) { - plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""), - ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i]) + plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Time (days)",""), + ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i]) if (i < L) par(new=TRUE) } @@ -88,7 +88,7 @@ plotPredReal <- function(data, pred, index) prediction = pred$getSerie(index) yrange = range(measure, prediction) par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3) - plot(measure, type="l", ylim=yrange, xlab="Temps (en heures)", ylab="PM10") + plot(measure, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10") par(new=TRUE) plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="") } @@ -107,7 +107,7 @@ plotSimils <- function(pred, index) if (is.null(weights)) stop("plotSimils only works on 'Neighbors' forecasts") par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) - hist(pred$getParams(index)$weights, nclass=20, main="", xlab="Poids", ylab="Effectif") + hist(pred$getParams(index)$weights, nclass=20, main="", xlab="Weight", ylab="Count") } #' Functional boxplot @@ -116,7 +116,6 @@ plotSimils <- function(pred, index) #' #' @param data Object return by \code{getData} #' @param indices integer or date indices to process -#' @param plot_bivariate Should the bivariate plot appear? #' #' @export plotFbox <- function(data, indices=seq_len(data$getSize())) @@ -132,7 +131,7 @@ plotFbox <- function(data, indices=seq_len(data$getSize())) series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix) par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) - rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10", + rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10", plotlegend=FALSE, lwd=2) rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE) } @@ -181,7 +180,7 @@ computeFilaments <- function(data, pred, index, limit=60, plot=TRUE) for (i in nn:1) { plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i], - xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centré","")) + xlab=ifelse(i==1,"Time (hours)",""), ylab=ifelse(i==1,"Centered PM10","")) par(new=TRUE) } # Also plot ref curve, in red @@ -203,7 +202,7 @@ computeFilaments <- function(data, pred, index, limit=60, plot=TRUE) #' @param fil Output of \code{computeFilaments} #' #' @export -plotFilamentsBox = function(data, fil, ...) +plotFilamentsBox = function(data, fil) { if (!requireNamespace("rainbow", quietly=TRUE)) stop("Functional boxplot requires the rainbow package") @@ -212,7 +211,7 @@ plotFilamentsBox = function(data, fil, ...) data$getSeries(fil$neighb_indices), data$getSeries(fil$neighb_indices+1) ) series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix) par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) - rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10", + rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10", plotlegend=FALSE, lwd=2) # "Magic" found at http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r @@ -233,7 +232,7 @@ plotFilamentsBox = function(data, fil, ...) #' @param fil Output of \code{computeFilaments} #' #' @export -plotRelVar = function(data, fil, ...) +plotRelVar = function(data, fil) { ref_var = c( apply(data$getSeries(fil$neighb_indices),1,sd), apply(data$getSeries(fil$neighb_indices+1),1,sd) ) @@ -243,7 +242,7 @@ plotRelVar = function(data, fil, ...) yrange = range(ref_var, global_var) par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) plot(ref_var, type="l", col=1, lwd=3, ylim=yrange, - xlab="Temps (heures)", ylab="Écart-type") + xlab="Time (hours)", ylab="Standard deviation") par(new=TRUE) plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="") abline(v=24, lty=2, col=colors()[56]) diff --git a/pkg/R/utils.R b/pkg/R/utils.R index 3486ee9..712a4f8 100644 --- a/pkg/R/utils.R +++ b/pkg/R/utils.R @@ -53,11 +53,11 @@ integerIndexToDate = function(index, data) #' getSimilarDaysIndices #' -#' Find similar days indices in the past +#' Find similar days indices in the past. #' #' @param index Day index (numeric or date) #' @param limit Maximum number of indices to return -#' @param same_seaon Should the indices correspond to day in same season? +#' @param same_season Should the indices correspond to day in same season? #' #' @export getSimilarDaysIndices = function(index, limit, same_season) diff --git a/pkg/man/talweg-package.Rd b/pkg/man/talweg-package.Rd index c0a7515..b49c8bf 100644 --- a/pkg/man/talweg-package.Rd +++ b/pkg/man/talweg-package.Rd @@ -11,20 +11,15 @@ \packageDescription{talweg} } +%TODO: better details \details{ - The package devtools should be useful in development stage, since we rely on testthat for - unit tests, and roxygen2 for documentation. knitr is used to generate the package vignette. - Concerning the other suggested packages: - \itemize{ - \item{TODO...;} - \item{TODO...;} - } - - The three main functions are located in R/main.R: - \itemize{ - \item{TODO...;} - \item{TODO...;} - } + To start working with this package, use ?computeForecast + + The package R6 is used to abstract data and forecast(er)s implementation from their usage. + + The package devtools could be useful in development stage, since we rely on testthat for + unit tests and roxygen2 for documentation. rainbow package is used for some plots: + \code{plotFbox()} and \code{plotFilamentsBox()}. } \author{ @@ -36,7 +31,3 @@ %\references{ % TODO: Literature or other references for background information %} - -%\examples{ -% TODO: simple examples of the most important functions -%} diff --git a/pkg/vignettes/talweg.Rmd b/pkg/vignettes/talweg.Rmd deleted file mode 100644 index b6e94cb..0000000 --- a/pkg/vignettes/talweg.Rmd +++ /dev/null @@ -1,160 +0,0 @@ ---- -title: "talweg" -author: "Benjamin Auder" -date: "`r Sys.Date()`" -output: rmarkdown::html_vignette -vignette: > - %\VignetteIndexEntry{Vignette Title} - %\VignetteEngine{knitr::rmarkdown} - %\VignetteEncoding{UTF-8} ---- - - -## Introduction - -J'ai fait quelques essais dans différentes configurations pour la méthode "Neighbors" (la seule dont on a parlé).
Il semble que le mieux soit - - * simtype="exo" ou "mix" : similarités exogènes avec/sans endogènes (fenêtre optimisée par VC) - * same_season=FALSE : les indices pour la validation croisée ne tiennent pas compte des saisons - * mix_strategy="mult" : on multiplie les poids (au lieu d'en éteindre) - -J'ai systématiquement comparé à une approche naïve : la moyennes des lendemains des jours "similaires" dans tout le passé ; à chaque fois sans prédiction du saut (sauf pour Neighbors : prédiction basée sur les poids calculés). - -Ensuite j'affiche les erreurs, quelques courbes prévues/mesurées, quelques filaments puis les histogrammes de quelques poids. Concernant les graphes de filaments, la moitié gauche du graphe correspond aux jours similaires au jour courant, tandis que la moitié droite affiche les lendemains : ce sont donc les voisinages tels qu'utilisés dans l'algorithme. - -

Pollution par chauffage

- - -loop: indices_ch, ep, np et titres: Pollution par chauffage, Pollution par épandage, Semaine non polluée - - - -p_nn_exo = computeForecast(data, indices_ch, "Neighbors", "Neighbors", simtype="exo", horizon=H) -p_nn_mix = computeForecast(data, indices_ch, "Neighbors", "Neighbors", simtype="mix", horizon=H) -p_az = computeForecast(data, indices_ch, "Average", "Zero", horizon=H) #, memory=183) -p_pz = computeForecast(data, indices_ch, "Persistence", "Zero", horizon=H, same_day=TRUE) - -e_nn_exo = computeError(data, p_nn_exo) -e_nn_mix = computeError(data, p_nn_mix) -e_az = computeError(data, p_az) -e_pz = computeError(data, p_pz) -options(repr.plot.width=9, repr.plot.height=7) -plotError(list(e_nn_mix, e_pz, e_az, e_nn_exo), cols=c(1,2,colors()[258], 4)) - -#Noir: neighbors_mix, bleu: neighbors_exo, vert: moyenne, rouge: persistence - -i_np = which.min(e_nn_exo$abs$indices) -i_p = which.max(e_nn_exo$abs$indices) - -options(repr.plot.width=9, repr.plot.height=4) -par(mfrow=c(1,2)) - -plotPredReal(data, p_nn_exo, i_np); title(paste("PredReal nn exo day",i_np)) -plotPredReal(data, p_nn_exo, i_p); title(paste("PredReal nn exo day",i_p)) - -plotPredReal(data, p_nn_mix, i_np); title(paste("PredReal nn mix day",i_np)) -plotPredReal(data, p_nn_mix, i_p); title(paste("PredReal nn mix day",i_p)) - -plotPredReal(data, p_az, i_np); title(paste("PredReal az day",i_np)) -plotPredReal(data, p_az, i_p); title(paste("PredReal az day",i_p)) - -#Bleu: prévue, noir: réalisée - -par(mfrow=c(1,2)) -f_np_exo = computeFilaments(data, p_nn_exo, i_np, plot=TRUE); title(paste("Filaments nn exo day",i_np)) -f_p_exo = computeFilaments(data, p_nn_exo, i_p, plot=TRUE); title(paste("Filaments nn exo day",i_p)) - -f_np_mix = computeFilaments(data, p_nn_mix, i_np, plot=TRUE); title(paste("Filaments nn mix day",i_np)) -f_p_mix = computeFilaments(data, p_nn_mix, i_p, plot=TRUE); title(paste("Filaments nn mix day",i_p)) - -par(mfrow=c(1,2)) -plotFilamentsBox(data, f_np_exo); title(paste("FilBox nn exo day",i_np)) -plotFilamentsBox(data, f_p_exo); title(paste("FilBox nn exo day",i_p)) - -plotFilamentsBox(data, f_np_mix); title(paste("FilBox nn mix day",i_np)) -plotFilamentsBox(data, f_p_mix); title(paste("FilBox nn mix day",i_p)) - -par(mfrow=c(1,2)) -plotRelVar(data, f_np_exo); title(paste("StdDev nn exo day",i_np)) -plotRelVar(data, f_p_exo); title(paste("StdDev nn exo day",i_p)) - -plotRelVar(data, f_np_mix); title(paste("StdDev nn mix day",i_np)) -plotRelVar(data, f_p_mix); title(paste("StdDev nn mix day",i_p)) - -#Variabilité globale en rouge ; sur les 60 voisins (+ lendemains) en noir - -par(mfrow=c(1,2)) -plotSimils(p_nn_exo, i_np); title(paste("Weights nn exo day",i_np)) -plotSimils(p_nn_exo, i_p); title(paste("Weights nn exo day",i_p)) - -plotSimils(p_nn_mix, i_np); title(paste("Weights nn mix day",i_np)) -plotSimils(p_nn_mix, i_p); title(paste("Weights nn mix day",i_p) - -#- pollué à gauche, + pollué à droite - -#Fenêtres sélectionnées dans ]0,10] / endo à gauche, exo à droite -p_ch_nn_exo$getParams(i_np)$window -p_ch_nn_exo$getParams(i_p)$window - -p_ch_nn_mix$getParams(i_np)$window -p_ch_nn_mix$getParams(i_p)$window - - - - -## Bilan - -Problème difficile : on ne fait guère mieux qu'une naïve moyenne des lendemains des jours similaires dans le passé, ce qui n'est pas loin de prédire une série constante égale à la dernière valeur observée (méthode "zéro"). La persistence donne parfois de bons résultats mais est trop instable (sensibilité à l'argument same_day). - -Comment améliorer la méthode ? - - - - -Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format: - -- Never uses retina figures -- Has a smaller default figure size -- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style - -## Vignette Info - -Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette. - -## Styles - -The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows: - - output: - rmarkdown::html_vignette: - css: mystyles.css - -## Figures - -The figure sizes have been customised so that you can easily put two images side-by-side. - -```{r, fig.show='hold'} -plot(1:10) -plot(10:1) -``` - -You can enable figure captions by `fig_caption: yes` in YAML: - - output: - rmarkdown::html_vignette: - fig_caption: yes - -Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**. - -## More Examples - -You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`. - -```{r, echo=FALSE, results='asis'} -knitr::kable(head(mtcars, 10)) -``` - -Also a quote using `>`: - -> "He who gives up [code] safety for [code] speed deserves neither." -([via](https://twitter.com/hadleywickham/status/504368538874703872)) diff --git a/pkg/vignettes/talweg.ipynb b/pkg/vignettes/talweg.ipynb new file mode 100644 index 0000000..fde3ae3 --- /dev/null +++ b/pkg/vignettes/talweg.ipynb @@ -0,0 +1,32 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#TODO" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "R", + "language": "R", + "name": "ir" + }, + "language_info": { + "codemirror_mode": "r", + "file_extension": ".r", + "mimetype": "text/x-r-source", + "name": "R", + "pygments_lexer": "r", + "version": "3.3.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}