library(kfino)
library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> Les objets suivants sont masqués depuis 'package:stats':
#> 
#>     filter, lag
#> Les objets suivants sont masqués depuis 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

This vignette describes how to use the kfino algorithm on time courses in order to detect impulse noised outliers and predict the parameter of interest.

RAJOUTER DU TEXTE

1 Description of the spring1 dataset

blablabla

data(spring1)

# Dimension of this dataset
dim(spring1)
#> [1] 203   5

head(spring1)
#> # A tibble: 6 × 5
#> # Groups:   IDE [1]
#>   Poids Date                IDE             Day                 dateNum
#>   <dbl> <dttm>              <chr>           <dttm>                <dbl>
#> 1  28.6 2017-05-24 00:00:00 250016286863027 2017-05-24 16:34:00   0.469
#> 2  45   2017-05-24 00:00:00 250016286863027 2017-05-24 19:24:00   0.587
#> 3  25   2017-05-25 00:00:00 250016286863027 2017-05-25 05:25:00   1.00 
#> 4  43   2017-05-25 00:00:00 250016286863027 2017-05-25 05:45:00   1.02 
#> 5  23.4 2017-05-25 00:00:00 250016286863027 2017-05-25 05:58:00   1.03 
#> 6   0   2017-05-25 00:00:00 250016286863027 2017-05-25 09:30:00   1.17

2 Kfino algorithm on the spring1 dataset

2.1 Initial parameters not optimized

# --- Without Optimisation on initial parameters
param2<-list(m0=41,
             mm=45,
             pp=0.5,
             aa=0.001,
             expertMin=30,
             expertMax=75,
             sigma2_m0=1,
             sigma2_mm=0.05,
             sigma2_pp=5,
             K=2,
             seqp=seq(0.5,0.7,0.1))

resu2<-kfino_fit(datain=spring1,
              Tvar="dateNum",Yvar="Poids",
              param=param2,
              doOptim=FALSE)     
#> [1] "-------:"
#> [1] "No optimization of initial parameters:"
#> [1] "Used parameters: "
#> [1] 41.0  0.5 45.0

# flags are qualitative
kfino_plot(resuin=resu2,typeG="quali",
            Tvar="Day",Yvar="Poids",Ident="IDE")

            
# flags are quantitative
kfino_plot(resuin=resu2,typeG="quanti",
            Tvar="Day",Yvar="Poids",Ident="IDE")

2.2 Initial parameters optimized

# --- With Optimisation on initial parameters
param1<-list(m0=NULL,
             mm=NULL,
             pp=NULL,
             aa=0.001,
             expertMin=30,
             expertMax=75,
             sigma2_m0=1,
             sigma2_mm=0.05,
             sigma2_pp=5,
             K=2,
             seqp=seq(0.5,0.7,0.1))

resu1<-kfino_fit(datain=spring1,
              Tvar="dateNum",Yvar="Poids",
              doOptim=TRUE,param=param1)  
#> [1] "-------:"
#> [1] "Optimization of initial parameters with ML method - result:"
#> [1] "no sub-sampling performed:"
#> range m0:  40.1 43 
#> initial m0opt:  41.3 
#> initial mmopt:  46.3 
#> [1] "Optimized parameters: "
#> Optimized m0:  42.1 
#> Optimized mm:  61.1 
#> Optimized pp:  0.7 
#> [1] "-------:"

# flags are qualitative
kfino_plot(resuin=resu1,typeG="quali",
            Tvar="Day",Yvar="Poids",Ident="IDE")

            
# flags are quantitative
kfino_plot(resuin=resu1,typeG="quanti",
            Tvar="Day",Yvar="Poids",Ident="IDE")

2.2.1 Prediction of the weight on the cleaned dataset

kfino_plot(resuin=resu1,typeG="prediction",
            Tvar="Day",Yvar="Poids",Ident="IDE")

3 Description of the merinos1 dataset

ewelambs, expected weights between 10 and 45 kg.

data(merinos1)

# Dimension of this dataset
dim(merinos1)
#> [1] 397   5

head(merinos1)
#> # A tibble: 6 × 5
#> # Groups:   IDE [1]
#>   Poids Date                IDE             Day                 dateNum
#>   <dbl> <dttm>              <chr>           <dttm>                <dbl>
#> 1   0   2021-01-27 00:00:00 250017033503004 2021-01-27 08:31:00   0.353
#> 2   0   2021-01-27 00:00:00 250017033503004 2021-01-27 08:31:00   0.353
#> 3   0   2021-01-27 00:00:00 250017033503004 2021-01-27 08:31:00   0.353
#> 4   0   2021-01-27 00:00:00 250017033503004 2021-01-27 08:31:00   0.353
#> 5   0   2021-01-27 00:00:00 250017033503004 2021-01-27 08:31:00   0.353
#> 6  40.2 2021-01-27 00:00:00 250017033503004 2021-01-27 12:37:00   0.524

4 Kfino algorithm on the merinos1 dataset

4.1 Initial parameters optimized

# --- With Optimisation on initial parameters
param3<-list(m0=NULL,
             mm=NULL,
             pp=NULL,
             aa=0.001,
             expertMin=10,
             expertMax=45,
             sigma2_m0=1,
             sigma2_mm=0.05,
             sigma2_pp=5,
             K=2,
             seqp=seq(0.5,0.7,0.1))

resu3<-kfino_fit(datain=merinos2,
              Tvar="dateNum",Yvar="Poids",
              doOptim=TRUE,param=param3)      
#> [1] "-------:"
#> [1] "Optimization of initial parameters with ML method - result:"
#> [1] "no sub-sampling performed:"
#> range m0:  29 41.8 
#> initial m0opt:  36.8 
#> initial mmopt:  37.6 
#> [1] "Optimized parameters: "
#> Optimized m0:  29 
#> Optimized mm:  48 
#> Optimized pp:  0.7 
#> [1] "-------:"

# flags are qualitative
kfino_plot(resuin=resu3,typeG="quali",
            Tvar="Day",Yvar="Poids",Ident="IDE")

            
# flags are quantitative
kfino_plot(resuin=resu3,typeG="quanti",
            Tvar="Day",Yvar="Poids",Ident="IDE")

4.1.1 Prediction of the weight on the cleaned dataset

kfino_plot(resuin=resu3,typeG="prediction",
            Tvar="Day",Yvar="Poids",Ident="IDE")

5 Session informations

#> R version 4.1.3 (2022-03-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.6 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#> 
#> locale:
#>  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.3.6 dplyr_1.0.9   kfino_1.0.0  
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.1.2  xfun_0.31         bslib_0.3.1       remotes_2.4.1    
#>  [5] purrr_0.3.4       colorspace_2.0-3  vctrs_0.4.1       generics_0.1.2   
#>  [9] testthat_3.1.4    usethis_2.1.3     htmltools_0.5.2   yaml_2.3.5       
#> [13] utf8_1.2.2        rlang_1.0.2       pkgbuild_1.2.0    pillar_1.7.0     
#> [17] jquerylib_0.1.4   glue_1.6.2        withr_2.5.0       DBI_1.1.2        
#> [21] sessioninfo_1.2.1 lifecycle_1.0.1   stringr_1.4.0     munsell_0.5.0    
#> [25] gtable_0.3.0      devtools_2.4.2    evaluate_0.15     memoise_2.0.0    
#> [29] labeling_0.4.2    knitr_1.39        callr_3.7.0       fastmap_1.1.0    
#> [33] ps_1.7.0          fansi_1.0.3       highr_0.9         scales_1.2.0     
#> [37] cachem_1.0.6      desc_1.4.1        pkgload_1.2.4     jsonlite_1.8.0   
#> [41] farver_2.1.0      fs_1.5.2          brio_1.1.3        digest_0.6.29    
#> [45] stringi_1.7.6     processx_3.5.3    rprojroot_2.0.3   grid_4.1.3       
#> [49] cli_3.3.0         tools_4.1.3       magrittr_2.0.3    sass_0.4.1       
#> [53] tibble_3.1.7      crayon_1.5.1      pkgconfig_2.0.3   ellipsis_0.3.2   
#> [57] prettyunits_1.1.1 assertthat_0.2.1  rmarkdown_2.14    rstudioapi_0.13  
#> [61] R6_2.5.1          compiler_4.1.3