diff --git a/DESCRIPTION b/DESCRIPTION index 1ec245193d899bcbb550b68a62f8e47a9ade318f..66a936f2374b4f3266bc21a4fa2e3507e3077e56 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Title: Kalman Filter for Impulse Noised Outliers Version: 1.0.0 Authors@R: c( person("Bertrand", "Cloez", email = "bertrand.cloez@inrae.fr", role = c("aut", "cre")), - person("Isabelle", "Sanchez", email = "isabelle.sanchez@inra.fr", role = c("ctr")), + person("Isabelle", "Sanchez", email = "isabelle.sanchez@inrae.fr", role = c("ctr")), person("Benedicte", "Fontez", email = "benedicte.fontez@supagro.fr", role = c("ctr"))) Author: Bertrand Cloez [aut, cre], Isabelle Sanchez [ctr], @@ -18,6 +18,11 @@ LazyData: TRUE URL: https://forgemia.inra.fr/isabelle.sanchez/kfino BugReports: https://forgemia.inra.fr/isabelle.sanchez/kfino/issues Imports: ggplot2, dplyr -Suggests: rmarkdown, knitr, RColorBrewer +Suggests: + rmarkdown, + knitr, + RColorBrewer, + testthat (>= 3.0.0) VignetteBuilder: knitr RoxygenNote: 7.1.2 +Config/testthat/edition: 3 diff --git a/R/graph_functions.R b/R/graph_functions.R index 12d4eb533469b6886a0eb03af93911214e4cc049..dfe806af8bddd519cb109fb8d087504dc40471b0 100644 --- a/R/graph_functions.R +++ b/R/graph_functions.R @@ -5,7 +5,8 @@ #' qualitative or quantitative display) or prediction #' @param Tvar char, time variable in the data.frame datain #' @param Yvar char, variable which was analysed in the data.frame datain -#' @param Ident char, colname of the individual id to be analyzed +#' @param Ident char, column name of the individual id to be analyzed +#' @param title char, a graph title #' #' @details The produced graphic can be, according to typeG: #' \describe{ @@ -45,64 +46,77 @@ #' # predictions on OK values #' kfino_plot(resuin=resu2,typeG="prediction", #' Tvar="Day",Yvar="Poids",Ident="IDE") -kfino_plot<-function(resuin,typeG, - Tvar,Yvar,Ident){ +kfino_plot<-function(resuin, + typeG, + Tvar, + Yvar, + Ident, + title=NULL){ - # Tests d'existence - if (!is.null(resuin[[1]])){ - tp<-as.data.frame(resuin[[1]]) - myIDE<-unique(tp[,Ident]) + # Existence check + if (is.null(resuin[[1]])) { + stop("NULL object - No graph to provide. Please check your input object.") + } + if (!typeG %in% c("quanti","quali","prediction")) { + stop("This type is not allowed.") + } + if (typeG %in% c("prediction","quanti") & is.null(resuin[[2]])) { + stop("NULL object - No graph to provide. Please check your input object.") + } + + # Some formatting + tp<-as.data.frame(resuin[[1]]) + myIDE<-unique(tp[,Ident]) - if (typeG == "quali"){ - if (!is.null(resuin[[2]])){ - g1<-ggplot(tp,aes_string(x=Tvar))+ - geom_point( aes_string(y=Yvar,color="flag")) + - geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$prediction)) + - geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$lwr), - color="green") + - geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$upr), - color="green") + - scale_color_manual(values = - c("KO"="purple", "OK" = "black", "Bad"="red")) + - ggtitle(paste0("kfino outlier detection - ",myIDE)) - } else { - g1<-ggplot(tp,aes_string(x=Tvar))+ - geom_point( aes_string(y=Yvar,color="flag")) + - scale_color_manual(values = - c("KO"="purple", "OK" = "black", "Bad"="red")) + - ggtitle(paste0("kfino outlier detection - ",myIDE)) - } - return(g1) - } else if (typeG == "quanti"){ - if (!is.null(resuin[[2]])){ - g1<-ggplot(tp,aes_string(x=Tvar))+ - geom_point( aes_string(y=Yvar,color="label_pred")) + + if (is.null(title)){ + tp.title1<-paste0("kfino outlier detection - ",myIDE) + tp.title2<-paste0("kfino prediction - ",myIDE) + } else { + tp.title1<-paste0(title," - ",myIDE) + tp.title2<-paste0(title," - ",myIDE) + } + + # graphics + if (typeG == "quali"){ + if (!is.null(resuin[[2]])){ + g1<-ggplot(tp,aes_string(x=Tvar))+ + geom_point( aes_string(y=Yvar,color="flag")) + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$prediction)) + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$lwr), color="green") + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$upr), color="green") + - ggtitle(paste0("kfino outlier detection - ",myIDE)) - return(g1) - } else { - print("NULL object - No graph to provide") - } - } else if (typeG == "prediction"){ - if (!is.null(resuin[[2]])){ - tp2<-filter(tp,.data$flag == "OK") - - g1<-ggplot(tp2,aes_string(x=Tvar))+ - geom_point( aes_string(y=Yvar)) + - geom_line(data=tp2, aes(y=.data$prediction)) + - geom_line(data=tp2, aes(y=.data$lwr),color="green") + - geom_line(data=tp2, aes(y=.data$upr),color="green") + - ggtitle(paste0("kfino prediction - ",myIDE)) - return(g1) - } else { - print("NULL object - No graph to provide") - } + scale_color_manual(values = + c("KO"="purple", "OK" = "black", "Bad"="red")) + + ggtitle(tp.title1) + } else { + g1<-ggplot(tp,aes_string(x=Tvar))+ + geom_point( aes_string(y=Yvar,color="flag")) + + scale_color_manual(values = + c("KO"="purple", "OK" = "black", "Bad"="red")) + + ggtitle(tp.title1) } - } else { - stop("NULL object - No graph to provide") + return(g1) + } else if (typeG == "quanti"){ + g1<-ggplot(tp,aes_string(x=Tvar))+ + geom_point( aes_string(y=Yvar,color="label_pred")) + + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$prediction)) + + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$lwr), + color="green") + + geom_line(data=tp[!is.na(tp$prediction),], aes(y=.data$upr), + color="green") + + ggtitle(tp.title1) + return(g1) + } else if (typeG == "prediction"){ + tp2<-filter(tp,.data$flag == "OK") + + g1<-ggplot(tp2,aes_string(x=Tvar))+ + geom_point( aes_string(y=Yvar)) + + geom_line(data=tp2, aes(y=.data$prediction)) + + geom_line(data=tp2, aes(y=.data$lwr),color="green") + + geom_line(data=tp2, aes(y=.data$upr),color="green") + + ggtitle(tp.title2) + return(g1) } -} \ No newline at end of file +} + diff --git a/man/kfino.Rd b/man/kfino.Rd index ab2fb3466f2a024e5c22331aa0f2a12ba84bc51a..3baabd3b88f63784041461e6965fad54ad69644c 100644 --- a/man/kfino.Rd +++ b/man/kfino.Rd @@ -24,7 +24,7 @@ Useful links: Other contributors: \itemize{ - \item Isabelle Sanchez \email{isabelle.sanchez@inra.fr} [contractor] + \item Isabelle Sanchez \email{isabelle.sanchez@inrae.fr} [contractor] \item Benedicte Fontez \email{benedicte.fontez@supagro.fr} [contractor] } diff --git a/man/kfino_plot.Rd b/man/kfino_plot.Rd index 50f479ac57581274f0660c54c698616c9a0363e8..f264dc311f0f4dde44f6bdb55e405919208b4536 100644 --- a/man/kfino_plot.Rd +++ b/man/kfino_plot.Rd @@ -4,7 +4,7 @@ \alias{kfino_plot} \title{graphical function} \usage{ -kfino_plot(resuin, typeG, Tvar, Yvar, Ident) +kfino_plot(resuin, typeG, Tvar, Yvar, Ident, title = NULL) } \arguments{ \item{resuin}{a list resulting of the kfino algorithm} @@ -16,7 +16,9 @@ qualitative or quantitative display) or prediction} \item{Yvar}{char, variable which was analysed in the data.frame datain} -\item{Ident}{char, colname of the individual id to be analyzed} +\item{Ident}{char, column name of the individual id to be analyzed} + +\item{title}{char, a graph title} } \value{ a ggplot2 graphic diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000000000000000000000000000000000000..b38dd0795cf9c6c4384639accc4c8455412190f9 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(kfino) + +test_check("kfino") diff --git a/tests/testthat/test-outputAlgo.R b/tests/testthat/test-outputAlgo.R new file mode 100644 index 0000000000000000000000000000000000000000..3a9a5a0735ce35790926cd02b96c80da11458534 --- /dev/null +++ b/tests/testthat/test-outputAlgo.R @@ -0,0 +1,14 @@ +test_that("Output type", { + data(spring1) + resu1<-kfino_fit(datain=spring1, + Tvar="dateNum",Yvar="Poids", + expertMin=30,expertMax=75, + doOptim=FALSE,aa=0.001,sigma2_mm=0.05, + K=2,sigma2_pp=5) + + expect_equal(str(resu1),"list") + expect_equal(str(resu1[[1]]),"data.frame") + expect_equal(str(resu1[[2]]),"data.frame") + expect_equal(str(resu1[[3]]),"list") + #expect_true(is_SBM(netA)) +}) \ No newline at end of file