├── .github ├── .gitignore └── workflows │ ├── pkgdown.yaml │ └── R-CMD-check.yaml ├── src ├── .gitignore ├── sadie.cpp ├── mapcomp.cpp └── RcppExports.cpp ├── LICENSE ├── data ├── aphids.rda ├── arthropods.rda ├── citrus_ctv.rda ├── hop_viruses.rda ├── tomato_tswv.rda ├── codling_moths.rda ├── tobacco_viruses.rda ├── dogwood_anthracnose.rda ├── offspring_survival.rda ├── pyrethrum_ray_blight.rda ├── simulated_epidemics.rda └── onion_bacterial_blight.rda ├── .gitignore ├── man ├── figures │ └── logo.png ├── is.intensity.Rd ├── link.Rd ├── tobacco_viruses.Rd ├── coef.smle.Rd ├── codling_moths.Rd ├── pyrethrum_ray_blight.Rd ├── aphids.Rd ├── mapped_var.Rd ├── smle_wrappers.Rd ├── dogwood_anthracnose.Rd ├── arthropods.Rd ├── logLik.smle.Rd ├── offspring_survival.Rd ├── onion_bacterial_blight.Rd ├── epiphy.Rd ├── mapping.Rd ├── simulated_epidemics.Rd ├── indAndSub.Rd ├── as.data.frame.intensity.Rd ├── threshold.Rd ├── hop_viruses.Rd ├── vcov.smle.Rd ├── citrus_ctv.Rd ├── calpha.test.Rd ├── chisq.test.Rd ├── z.test.Rd ├── clump.Rd ├── split.intensity.Rd ├── a2a.Rd ├── power_law.Rd ├── spatial_hier.Rd ├── tomato_tswv.Rd ├── BetaBinomial.Rd ├── mapcomp.Rd ├── sadie.Rd ├── smle.Rd ├── agg_index.Rd ├── fit_two_distr.Rd └── intensity.Rd ├── pkgdown └── favicon │ ├── favicon.ico │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ ├── apple-touch-icon.png │ ├── apple-touch-icon-120x120.png │ ├── apple-touch-icon-152x152.png │ ├── apple-touch-icon-60x60.png │ └── apple-touch-icon-76x76.png ├── .travis.yml ├── .Rbuildignore ├── epiphy.Rproj ├── data-raw ├── pyrethrum_ray_blight.csv ├── codling_moths.csv ├── offspring_survival.csv ├── tobacco_viruses.csv ├── aphids.csv ├── dogwood_anthracnose.csv ├── arthropods.csv ├── export-data.R └── citrus_ctv_IVIA6and7.csv ├── R ├── RcppExports.R ├── epiphy.R ├── betabinom.R ├── mapcomp.R ├── spatial-hier.R └── power-law.R ├── NEWS.md ├── cran-comments.md ├── README.md ├── _pkgdown.yml ├── DESCRIPTION ├── NAMESPACE └── vignettes ├── biblio.bib ├── defs-and-eqns.Rmd └── phytopathology_v2.csl /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dll 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2023 2 | COPYRIGHT HOLDER: Christophe Gigot 3 | -------------------------------------------------------------------------------- /data/aphids.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/aphids.rda -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | inst/doc 6 | docs 7 | -------------------------------------------------------------------------------- /data/arthropods.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/arthropods.rda -------------------------------------------------------------------------------- /data/citrus_ctv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/citrus_ctv.rda -------------------------------------------------------------------------------- /data/hop_viruses.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/hop_viruses.rda -------------------------------------------------------------------------------- /data/tomato_tswv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/tomato_tswv.rda -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/man/figures/logo.png -------------------------------------------------------------------------------- /data/codling_moths.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/codling_moths.rda -------------------------------------------------------------------------------- /data/tobacco_viruses.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/tobacco_viruses.rda -------------------------------------------------------------------------------- /data/dogwood_anthracnose.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/dogwood_anthracnose.rda -------------------------------------------------------------------------------- /data/offspring_survival.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/offspring_survival.rda -------------------------------------------------------------------------------- /data/pyrethrum_ray_blight.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/pyrethrum_ray_blight.rda -------------------------------------------------------------------------------- /data/simulated_epidemics.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/simulated_epidemics.rda -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /data/onion_bacterial_blight.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/data/onion_bacterial_blight.rda -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chgigot/epiphy/HEAD/pkgdown/favicon/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r 2 | 3 | language: R 4 | sudo: false 5 | cache: packages 6 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^data-raw$ 4 | ^_pkgdown\.yml$ 5 | ^docs$ 6 | ^\.travis\.yml$ 7 | ^cran-comments\.md$ 8 | ^pkgdown$ 9 | ^\.github$ 10 | ^CRAN-SUBMISSION$ 11 | -------------------------------------------------------------------------------- /epiphy.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | 23 | SpellingDictionary: en_US 24 | -------------------------------------------------------------------------------- /data-raw/pyrethrum_ray_blight.csv: -------------------------------------------------------------------------------- 1 | i,n 2 | 4,6 3 | 6,6 4 | 6,6 5 | 6,6 6 | 6,6 7 | 6,6 8 | 6,6 9 | 6,6 10 | 4,6 11 | 6,6 12 | 5,6 13 | 6,6 14 | 6,6 15 | 6,6 16 | 6,6 17 | 6,6 18 | 6,6 19 | 3,6 20 | 3,6 21 | 2,6 22 | 4,6 23 | 3,6 24 | 6,6 25 | 3,6 26 | 3,6 27 | 6,6 28 | 6,6 29 | 6,6 30 | 6,6 31 | 6,6 32 | 6,6 33 | 6,6 34 | 6,6 35 | 5,6 36 | 6,6 37 | 6,6 38 | 6,6 39 | 6,6 40 | 6,6 41 | 6,6 42 | 6,6 43 | 6,6 44 | 4,6 45 | 6,6 46 | 5,6 47 | 6,6 48 | 6,6 49 | 6,6 50 | 6,6 51 | 5,6 52 | 6,6 53 | 6,6 54 | 6,6 55 | 6,6 56 | 6,6 57 | 6,6 58 | 6,6 59 | 3,6 60 | 6,6 61 | 6,6 62 | 6,6 63 | 6,6 64 | -------------------------------------------------------------------------------- /data-raw/codling_moths.csv: -------------------------------------------------------------------------------- 1 | xm,ym,i 2 | 0.038,0.937,4 3 | 0.164,0.963,11 4 | 0.108,0.899,4 5 | 0.248,0.873,3 6 | 0.108,0.847,2 7 | 0.205,0.787,0 8 | 0.163,0.73,0 9 | 0.347,0.762,0 10 | 0.43,0.672,0 11 | 0.291,0.697,1 12 | 0.249,0.637,1 13 | 0.374,0.606,0 14 | 0.333,0.533,3 15 | 0.515,0.579,1 16 | 0.472,0.508,1 17 | 0.415,0.437,14 18 | 0.613,0.482,1 19 | 0.557,0.41,12 20 | 0.501,0.345,4 21 | 0.698,0.39,0 22 | 0.655,0.313,0 23 | 0.585,0.247,7 24 | 0.781,0.293,1 25 | 0.739,0.221,9 26 | 0.655,0.162,10 27 | 0.88,0.195,14 28 | 0.809,0.129,18 29 | 0.753,0.052,16 30 | 0.964,0.105,14 31 | 0.908,0.04,16 32 | -------------------------------------------------------------------------------- /data-raw/offspring_survival.csv: -------------------------------------------------------------------------------- 1 | group,i,n 2 | control,13,13 3 | control,12,12 4 | control,9,9 5 | control,9,9 6 | control,8,8 7 | control,8,8 8 | control,12,13 9 | control,11,12 10 | control,9,10 11 | control,9,10 12 | control,8,9 13 | control,11,13 14 | control,4,5 15 | control,5,7 16 | control,7,10 17 | control,7,10 18 | treated,12,12 19 | treated,11,11 20 | treated,10,10 21 | treated,9,9 22 | treated,10,11 23 | treated,9,10 24 | treated,9,10 25 | treated,8,9 26 | treated,8,9 27 | treated,4,5 28 | treated,7,9 29 | treated,4,7 30 | treated,5,10 31 | treated,3,6 32 | treated,3,10 33 | treated,0,7 34 | -------------------------------------------------------------------------------- /R/RcppExports.R: -------------------------------------------------------------------------------- 1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | p_hscaled <- function(nodes, sites, bandwidth, edgeCorrection = FALSE) { 5 | .Call('_epiphy_p_hscaled', PACKAGE = 'epiphy', nodes, sites, bandwidth, edgeCorrection) 6 | } 7 | 8 | costTotiCPP <- function(i1, flow, cost, averaged = TRUE, absolute = FALSE) { 9 | .Call('_epiphy_costTotiCPP', PACKAGE = 'epiphy', i1, flow, cost, averaged, absolute) 10 | } 11 | 12 | costTotCPP <- function(flow, cost) { 13 | .Call('_epiphy_costTotCPP', PACKAGE = 'epiphy', flow, cost) 14 | } 15 | 16 | as_matrix_transport <- function(x, dim_mat) { 17 | .Call('_epiphy_as_matrix_transport', PACKAGE = 'epiphy', x, dim_mat) 18 | } 19 | 20 | -------------------------------------------------------------------------------- /man/is.intensity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{is.intensity} 4 | \alias{is.intensity} 5 | \alias{is.count} 6 | \alias{is.incidence} 7 | \alias{is.severity} 8 | \title{Test if an object is of class \code{intensity} or one of its subclasses.} 9 | \usage{ 10 | is.intensity(x) 11 | 12 | is.count(x) 13 | 14 | is.incidence(x) 15 | 16 | is.severity(x) 17 | } 18 | \arguments{ 19 | \item{x}{An object.} 20 | } 21 | \value{ 22 | TRUE if its argument's value is the corresponding \code{intensity} 23 | object and FALSE otherwise. 24 | } 25 | \description{ 26 | Test if an object is of class \code{intensity} or one of its subclasses 27 | (i.e. \code{count}, \code{incidence} or \code{severity}). 28 | } 29 | -------------------------------------------------------------------------------- /data-raw/tobacco_viruses.csv: -------------------------------------------------------------------------------- 1 | i,n 2 | 1,40 3 | 1,40 4 | 1,40 5 | 1,40 6 | 2,40 7 | 2,40 8 | 2,40 9 | 2,40 10 | 2,40 11 | 2,40 12 | 2,40 13 | 2,40 14 | 2,40 15 | 3,40 16 | 3,40 17 | 3,40 18 | 3,40 19 | 3,40 20 | 3,40 21 | 3,40 22 | 3,40 23 | 4,40 24 | 4,40 25 | 4,40 26 | 4,40 27 | 4,40 28 | 4,40 29 | 4,40 30 | 4,40 31 | 4,40 32 | 4,40 33 | 4,40 34 | 5,40 35 | 5,40 36 | 5,40 37 | 6,40 38 | 6,40 39 | 6,40 40 | 6,40 41 | 6,40 42 | 6,40 43 | 6,40 44 | 6,40 45 | 6,40 46 | 7,40 47 | 7,40 48 | 7,40 49 | 7,40 50 | 7,40 51 | 7,40 52 | 7,40 53 | 7,40 54 | 8,40 55 | 8,40 56 | 8,40 57 | 8,40 58 | 8,40 59 | 8,40 60 | 9,40 61 | 10,40 62 | 10,40 63 | 10,40 64 | 10,40 65 | 10,40 66 | 10,40 67 | 11,40 68 | 11,40 69 | 11,40 70 | 11,40 71 | 12,40 72 | 12,40 73 | 13,40 74 | 14,40 75 | 15,40 76 | 25,40 77 | -------------------------------------------------------------------------------- /man/link.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{link} 4 | \alias{link} 5 | \alias{logit} 6 | \alias{probit} 7 | \alias{cloglog} 8 | \title{Some link functions.} 9 | \usage{ 10 | logit(x, rev = FALSE) 11 | 12 | probit(x, rev = FALSE) 13 | 14 | cloglog(x, rev = FALSE) 15 | } 16 | \arguments{ 17 | \item{x}{A numeric vector.} 18 | 19 | \item{rev}{The inverse of the function?} 20 | } 21 | \value{ 22 | A numeric vector. 23 | } 24 | \description{ 25 | Logit, probit and cloglog functions are available. 26 | The logit and the logistic (with rev = TRUE), i.e. the inverse-logit functions. 27 | Probit is a wrapper around \code{qnorm} (for \eqn{probit}) and \code{pnorm} (for \eqn{probit^{-1}}) 28 | Complementary log-log transformation. 29 | } 30 | -------------------------------------------------------------------------------- /man/tobacco_viruses.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{tobacco_viruses} 5 | \alias{tobacco_viruses} 6 | \title{Incidence of tobacco plants infected with viruses.} 7 | \format{ 8 | A data frame with 75 rows and 2 variables: 9 | \tabular{rll}{ 10 | [, 1] \tab i \tab Number of diseased plants (from 0 to 40). \cr 11 | [, 2] \tab n \tab Sampling unit size. Here, n = 40 plants per sampling 12 | unit. \cr 13 | } 14 | } 15 | \source{ 16 | Madden LV, Pirone TP, Raccah B. 1987. Analysis of spatial patterns of 17 | virus-diseased tobacco plants. Phytopathology 77, 1409–1417. 18 | } 19 | \usage{ 20 | tobacco_viruses 21 | } 22 | \description{ 23 | Experimental plot consisted of 75 sampling units with 40 tobacco plants in 24 | each one. 25 | } 26 | \keyword{datasets} 27 | -------------------------------------------------------------------------------- /man/coef.smle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle-factory.R 3 | \name{coef.smle} 4 | \alias{coef.smle} 5 | \title{Extract Model Coefficients} 6 | \usage{ 7 | \method{coef}{smle}(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{an object for which the extraction of model coefficients is 11 | meaningful.} 12 | 13 | \item{...}{other arguments.} 14 | } 15 | \value{ 16 | Coefficients extracted from the model object \code{object}. 17 | 18 | For standard model fitting classes this will be a named numeric vector. 19 | For \code{"maov"} objects (produced by \code{\link[stats]{aov}}) it will be a matrix. 20 | } 21 | \description{ 22 | \code{coef} is a generic function which extracts model coefficients 23 | from objects returned by modeling functions. \code{coefficients} is 24 | an \emph{alias} for it. 25 | } 26 | \keyword{internal} 27 | -------------------------------------------------------------------------------- /man/codling_moths.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{codling_moths} 5 | \alias{codling_moths} 6 | \title{Count of codling moth larvae.} 7 | \format{ 8 | A data frame with 30 rows and 3 variables: 9 | \tabular{lll}{ 10 | [, 1:2] \tab x,y \tab Metric spatial coordonates. \cr 11 | [, 3] \tab i \tab Counts of codling moth larvae. \cr 12 | } 13 | } 14 | \source{ 15 | Lavigne C, Ricci B, Franck P, Senoussi R. 2010. Spatial analyses of 16 | ecological count data: A density map comparison approach. Basic and 17 | Applied Ecology 11: 734-42. 18 | \doi{10.1016/j.baae.2010.08.011} 19 | } 20 | \usage{ 21 | codling_moths 22 | } 23 | \description{ 24 | Codling moth diapausing larvae were collected in an apple orchard in 25 | south-eastern France. Larvae were caught on strip traps wrapped around tree 26 | trunks in July 2008 and collected the following October. 30 traps were used. 27 | } 28 | \keyword{datasets} 29 | -------------------------------------------------------------------------------- /man/pyrethrum_ray_blight.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{pyrethrum_ray_blight} 5 | \alias{pyrethrum_ray_blight} 6 | \title{Incidence of ray blight disease of pyrethrum.} 7 | \format{ 8 | A data frame with 62 rows and 2 variables: 9 | \tabular{rll}{ 10 | [, 1] \tab i \tab Number of diseased plants (from 0 to 6). \cr 11 | [, 2] \tab n \tab Sampling unit size. Here, n = 6 plants per sampling 12 | unit. \cr 13 | } 14 | } 15 | \source{ 16 | Pethybridge SJ, Esker P, Hay F, Wilson C, Nutter FW. 2005. 17 | Spatiotemporal description of epidemics caused by Phoma ligulicola in 18 | Tasmanian pyrethrum fields. \emph{Phytopathology} 95, 648–658. 19 | \doi{10.1094/PHYTO-95-0648} 20 | } 21 | \usage{ 22 | pyrethrum_ray_blight 23 | } 24 | \description{ 25 | An assessment of the incidence of ray blight disease of pyrethrum in 62 26 | sampling units, containing 6 plants each. 27 | } 28 | \keyword{datasets} 29 | -------------------------------------------------------------------------------- /man/aphids.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{aphids} 5 | \alias{aphids} 6 | \title{Counts of aphids.} 7 | \format{ 8 | A data frame with 63 rows and 3 variables: 9 | \tabular{lll}{ 10 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 11 | [, 3:4] \tab xm,ym \tab Metric spatial coordonates. \cr 12 | [, 5] \tab i \tab Counts of aphids. \cr 13 | } 14 | } 15 | \source{ 16 | Perry JN, Winder L, Holland JM, Alston RD. 1999. Red-blue plots for 17 | detecting clusters in count data. Ecology Letters 2, 106-13. 18 | \doi{10.1046/j.1461-0248.1999.22057.x} 19 | } 20 | \usage{ 21 | aphids 22 | } 23 | \description{ 24 | Counts of 554 aphids of the species Sitobion avenae, sampled on 28 June 1996 25 | in a 250 x 180-m field of winter wheat near Wimborne, Dorset, UK. The 63 26 | sampling units, made of the inspection of five tillers each, were located on 27 | a 9 x 7 rectangular grid at intervals of 30 m. 28 | } 29 | \keyword{datasets} 30 | -------------------------------------------------------------------------------- /man/mapped_var.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{mapped_var} 4 | \alias{mapped_var} 5 | \alias{mapped_var<-} 6 | \title{Existing variable mappings.} 7 | \usage{ 8 | mapped_var(x) 9 | 10 | mapped_var(x, keep = TRUE) <- value 11 | } 12 | \arguments{ 13 | \item{x}{An \code{intensity} object.} 14 | 15 | \item{keep}{Logical. Do we keep any previous mapped variables that are not 16 | redifined in the \code{mapping} object?} 17 | 18 | \item{value}{A \code{mapping} object.} 19 | } 20 | \value{ 21 | \code{mapped_var} returns the list of current mapped names of the object 22 | \code{x}. 23 | } 24 | \description{ 25 | Get or set existing variable mappings. 26 | } 27 | \examples{ 28 | my_data <- count(aphids) 29 | my_data 30 | mapped_var(my_data) 31 | mapped_var(my_data) <- mapping(x = X, y = Y) 32 | mapped_var(my_data) 33 | mapped_var(my_data) <- mapping(x = x, r = r, keep = FALSE) 34 | mapped_var(my_data) 35 | 36 | } 37 | \seealso{ 38 | \code{\link{mapping}} 39 | } 40 | -------------------------------------------------------------------------------- /man/smle_wrappers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle-factory.R 3 | \name{smle_wrappers} 4 | \alias{smle_wrappers} 5 | \alias{smle_pois} 6 | \alias{smle_nbinom} 7 | \alias{smle_binom} 8 | \alias{smle_betabinom} 9 | \title{Wrappers using maximum likelihood estimation for some distributions} 10 | \usage{ 11 | smle_pois(data) 12 | 13 | smle_nbinom(data) 14 | 15 | smle_binom(data) 16 | 17 | smle_betabinom(data) 18 | } 19 | \arguments{ 20 | \item{data}{The data set to work with. It can be a vector (if there is only 21 | one variable), a data frame (if there is one or more variables) or an 22 | \code{\link{intensity}} object.} 23 | } 24 | \value{ 25 | See \code{\link{smle}} 26 | } 27 | \description{ 28 | These functions are the core of the fitting processes performed in 29 | \code{\link{fit_two_distr}}. 30 | } 31 | \examples{ 32 | set.seed(12345) 33 | data <- rpois(100, lambda = 5) 34 | res <- smle_pois(data) 35 | res 36 | summary(res) 37 | 38 | data <- count(aphids) 39 | res <- smle_pois(data) 40 | res 41 | summary(res) 42 | 43 | } 44 | \keyword{internal} 45 | -------------------------------------------------------------------------------- /data-raw/aphids.csv: -------------------------------------------------------------------------------- 1 | x,y,xm,ym,i 2 | 1,1,0,0,0 3 | 2,1,30,0,0 4 | 3,1,60,0,3 5 | 4,1,90,0,7 6 | 5,1,120,0,9 7 | 6,1,150,0,1 8 | 7,1,180,0,14 9 | 1,2,0,30,5 10 | 2,2,30,30,0 11 | 3,2,60,30,16 12 | 4,2,90,30,0 13 | 5,2,120,30,2 14 | 6,2,150,30,0 15 | 7,2,180,30,6 16 | 1,3,0,60,2 17 | 2,3,30,60,1 18 | 3,3,60,60,0 19 | 4,3,90,60,8 20 | 5,3,120,60,0 21 | 6,3,150,60,52 22 | 7,3,180,60,16 23 | 1,4,0,90,9 24 | 2,4,30,90,1 25 | 3,4,60,90,3 26 | 4,4,90,90,6 27 | 5,4,120,90,0 28 | 6,4,150,90,1 29 | 7,4,180,90,9 30 | 1,5,0,120,3 31 | 2,5,30,120,0 32 | 3,5,60,120,4 33 | 4,5,90,120,0 34 | 5,5,120,120,8 35 | 6,5,150,120,27 36 | 7,5,180,120,14 37 | 1,6,0,150,20 38 | 2,6,30,150,2 39 | 3,6,60,150,0 40 | 4,6,90,150,0 41 | 5,6,120,150,0 42 | 6,6,150,150,0 43 | 7,6,180,150,102 44 | 1,7,0,180,6 45 | 2,7,30,180,3 46 | 3,7,60,180,5 47 | 4,7,90,180,0 48 | 5,7,120,180,16 49 | 6,7,150,180,21 50 | 7,7,180,180,6 51 | 1,8,0,210,6 52 | 2,8,30,210,8 53 | 3,8,60,210,9 54 | 4,8,90,210,3 55 | 5,8,120,210,0 56 | 6,8,150,210,71 57 | 7,8,180,210,1 58 | 1,9,0,240,4 59 | 2,9,30,240,1 60 | 3,9,60,240,18 61 | 4,9,90,240,2 62 | 5,9,120,240,0 63 | 6,9,150,240,23 64 | 7,9,180,240,0 65 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # epiphy (development version) 2 | 3 | # epiphy 0.5.0 4 | 5 | - Works now with R >= 4. 6 | - 'Return value' section for all exported functions. 7 | - URL of website updated. 8 | 9 | # epiphy 0.3.4 10 | 11 | - Minor bug fixes. 12 | - Improve documentation. 13 | - Add Travis CI and a website. 14 | 15 | # epiphy 0.3.3 16 | 17 | - Add a grayscale feature in plot for intensity objects. 18 | - Improve documentation and examples (sadie, intensity objects). 19 | 20 | # epiphy 0.3.2 21 | 22 | ## Major new features 23 | 24 | - Fixed bugs in the SADIE algorithm. 25 | - Mapcomp procedure proposed by Lavigne et al. (2010). 26 | 27 | # epiphy 0.3.0 28 | 29 | ## Major new features 30 | 31 | - Computation of aggregation indices (Fisher's, Lloyd's, etc.). 32 | - Distribution fitting, with two distributions, for count and incidence data. It is thus an R alternative to the BBD software from Madden & Hughes (1994) for incidence data. 33 | - Spatial hierarchy analysis. 34 | - Power law analysis (Taylor's and binary forms). 35 | - A first working version of the SADIE procedure. 36 | - Twelve historical data sets. 37 | - Many other features! 38 | 39 | -------------------------------------------------------------------------------- /man/dogwood_anthracnose.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{dogwood_anthracnose} 5 | \alias{dogwood_anthracnose} 6 | \title{Incidence of dogwood anthracnose.} 7 | \format{ 8 | A data frame with 329 rows and 3 variables: 9 | \tabular{rll}{ 10 | [, 1] \tab t \tab Year of disease assessments (1990 or 1991).. \cr 11 | [, 2] \tab i \tab Number of diseased plants (from 0 to 10). \cr 12 | [, 3] \tab n \tab Sampling unit size. Here, n = 10 plants per sampling 13 | unit (or plot). \cr 14 | } 15 | } 16 | \source{ 17 | Zarnoch SJ, Anderson RL, Sheffield RM. 1995. Using the \eqn{\beta}-binomial 18 | distribution to characterize forest health. Canadian journal of forest 19 | research 25, 462–469. 20 | } 21 | \usage{ 22 | dogwood_anthracnose 23 | } 24 | \description{ 25 | Incidence data from the Dogwood Anthracnose Impact Assessment Program for 26 | 1990 and 1991, in the Southeast of the USA, reported by Zarnoch et al (1995). 27 | Only plots with exactly n = 10 dogwood trees are present in the data set (168 28 | and 161 plots in 1990 and 1991, respectively). 29 | } 30 | \keyword{datasets} 31 | -------------------------------------------------------------------------------- /man/arthropods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{arthropods} 5 | \alias{arthropods} 6 | \title{Counts of arthropods.} 7 | \format{ 8 | A data frame with 378 rows and 4 variables: 9 | \tabular{lll}{ 10 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 11 | [, 3:4] \tab xm,ym \tab Metric spatial coordonates. \cr 12 | [, 5] \tab t \tab Sampling date. 1: 7 Jun, 2: 14 Jun, 3: 21 Jun, 4: 13 | 28 Jun, 5: 5 Jul, 6: 12 Jul 1996. \cr 14 | [, 6] \tab i \tab Counts of arthropods. \cr 15 | } 16 | } 17 | \source{ 18 | Holland JM, Winder L, Perry JN. 1999. Arthropod prey of farmland 19 | birds: Their spatial distribution within a sprayed field with and without 20 | buffer zones. Aspects of Applied Biology 54: 53-60. 21 | } 22 | \usage{ 23 | arthropods 24 | } 25 | \description{ 26 | A sampling unit was made of a pitfall to collect arthropods in a field of 27 | organic winter wheat, near Wimborne, Dorset, UK in 1996. The sampling units 28 | were located on a 9 x 7 rectangular grid at intervals of 30 m. There were 29 | six sampling dates. 30 | } 31 | \keyword{datasets} 32 | -------------------------------------------------------------------------------- /man/logLik.smle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle-factory.R 3 | \name{logLik.smle} 4 | \alias{logLik.smle} 5 | \title{Extract log-likelihood} 6 | \usage{ 7 | \method{logLik}{smle}(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{any object from which a log-likelihood value, or a 11 | contribution to a log-likelihood value, can be extracted.} 12 | 13 | \item{...}{some methods for this generic function require additional 14 | arguments.} 15 | } 16 | \value{ 17 | Returns an object of class \code{logLik}. This is a number with at 18 | least one attribute, \code{"df"} (\bold{d}egrees of \bold{f}reedom), 19 | giving the number of (estimated) parameters in the model. 20 | 21 | There is a simple \code{print} method for \code{"logLik"} objects. 22 | 23 | There may be other attributes depending on the method used: see the 24 | appropriate documentation. One that is used by several methods is 25 | \code{"nobs"}, the number of observations used in estimation (after 26 | the restrictions if \code{REML = TRUE}). 27 | } 28 | \description{ 29 | This function returns the maximal log-likelihood estimated with 30 | \code{\link{smle}}, if \code{f} returned log-likelihood value. 31 | } 32 | \keyword{internal} 33 | -------------------------------------------------------------------------------- /man/offspring_survival.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{offspring_survival} 5 | \alias{offspring_survival} 6 | \title{Offspring survival of rats experiencing different diets.} 7 | \format{ 8 | A data frame with 32 rows and 3 variables: 9 | \tabular{rll}{ 10 | [, 1] \tab group \tab Either control or treated group. \cr 11 | [, 2] \tab i \tab Pups weaned. \cr 12 | [, 3] \tab n \tab Pups alive at 4 days. \cr 13 | } 14 | } 15 | \source{ 16 | Weil CS. 1970. Selection of the valid number of sampling units and a 17 | consideration of their combination in toxicological studies involving 18 | reproduction, teratogenesis or carcinogenesis. Food and Cosmetics 19 | Toxicology 8: 177-182. 20 | } 21 | \usage{ 22 | offspring_survival 23 | } 24 | \description{ 25 | Results of an experiment where two groups of 16 female rats were fed 26 | different diets during pregnancy and lactation periods. One group's diet 27 | contained a chemical under review, and the other one was a control. For each 28 | litter, the number of pups alive at 4 days, and the number of pups weaned 29 | (i.e. that survived the 21-day lactation period) were recorded. 30 | } 31 | \keyword{datasets} 32 | -------------------------------------------------------------------------------- /man/onion_bacterial_blight.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{onion_bacterial_blight} 5 | \alias{onion_bacterial_blight} 6 | \title{Incidence of bacterial blight of onion.} 7 | \format{ 8 | A data frame with 1134 rows and 5 variables: 9 | \tabular{rll}{ 10 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 11 | [, 3] \tab t \tab Date of disease assessments. \cr 12 | [, 4] \tab i \tab Disease incidence. 0: Healthy, 1: Diseased. \cr 13 | [, 5] \tab n \tab Sampling unit size. n = 1 means that the sampling 14 | unit size is the plant. \cr 15 | } 16 | } 17 | \source{ 18 | Roumagnac P, Pruvost O, Chiroleu F, Hughes G. 2004. Spatial and 19 | temporal analyses of bacterial blight of onion caused by Xanthomonas 20 | axonopodis pv. allii. Phytopathology 94, 138–146. 21 | \doi{10.1094/PHYTO.2004.94.2.138} 22 | } 23 | \usage{ 24 | onion_bacterial_blight 25 | } 26 | \description{ 27 | Assessments of bacterial blight of onion at two dates. The experimental plot 28 | was sown with naturally X. axonopodis pv. allii-contaminated onion (A. cepa 29 | L. cv. Chateau-vieux) seed lot, with a contamination rate of about 0.04\%. 30 | } 31 | \keyword{datasets} 32 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Resubmission 2 | 3 | This is a resubmission. In this version I have: 4 | 5 | * added a \value section for all exported functions where such section was missing. 6 | * clump.Rd 7 | * split.intensity.Rd 8 | * threshold.Rd 9 | 10 | The package was archived on CRAN because of the use of 'default.stringsAsFactors'. See: 11 | https://cran-archive.r-project.org/web/checks/2022/2022-03-10_check_results_epiphy.html 12 | Uses of 'default.stringsAsFactors' have been removed to make the package compatible with R>=4. 13 | 14 | ## Test environments 15 | 16 | * local Linux Mint 21.2, R 4.3.2 17 | * GitHub Actions: 18 | * macos-latest (release), R 4.3.2 19 | * windows-latest (release), R 4.3.2 20 | * ubuntu-latest (devel), R-devel 21 | * ubunut-latest (release), R 4.3.2 22 | * ubunut-latest (oldrel-1), R 4.2.3 23 | * win-builder: 24 | * release, R 4.3.2 25 | * devel, R-devel 26 | * macOS builder, R 4.3.0 27 | 28 | ## R CMD check results 29 | 30 | 0 errors ✔ | 0 warnings ✔ | 1 note ✖ 31 | 32 | * There was only 1 NOTE (this is a new release): 33 | * Maintainer: 'Christophe Gigot ' 34 | * New submission 35 | * Package was archived on CRAN 36 | 37 | ## Reverse dependencies 38 | 39 | This are no reverse dependencies. 40 | 41 | -------------------------------------------------------------------------------- /man/epiphy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/epiphy.R 3 | \docType{package} 4 | \name{epiphy} 5 | \alias{epiphy} 6 | \title{\CRANpkg{epiphy}: An R package to analyze plant disease epidemics.} 7 | \description{ 8 | \CRANpkg{epiphy} makes it easy to analyze plant disease epidemics. It provides 9 | a common framework for plant disease intensity data recorded over time and/or 10 | space. Implemented statistical methods are currently mainly focused on 11 | spatial pattern analysis (e.g., aggregation indices, Taylor and binary power 12 | laws, distribution fitting, SADIE and mapcomp methods). Several data sets 13 | that were mainly published in plant disease epidemiology literature are also 14 | included in this package 15 | } 16 | \seealso{ 17 | Useful references: 18 | 19 | Gosme M. 2008. Comment analyser la structure spatiale et modéliser le 20 | développement spatio-temporel des épiphyties? Canadian Journal of Plant 21 | Pathology, 30:4-23. 22 | 23 | Madden LV, Hughes G, van den Bosch F. 2007. Spatial aspects of epidemics - 24 | III: Patterns of plant disease. In: The study of plant disease epidemics, 25 | 235–278. American Phytopathological Society, St Paul, MN. 26 | } 27 | \author{ 28 | \strong{Maintainer:} Christophe Gigot \email{ch.gigot@gmail.com} 29 | } 30 | \keyword{internal} 31 | -------------------------------------------------------------------------------- /man/mapping.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{mapping} 4 | \alias{mapping} 5 | \alias{mapping_} 6 | \alias{remap} 7 | \title{Construct data mappings.} 8 | \usage{ 9 | mapping(...) 10 | 11 | mapping_(x) 12 | 13 | remap(data, mapping, keep_only_std = TRUE) 14 | } 15 | \arguments{ 16 | \item{...}{One or more unquoted expressions separated by commas.} 17 | 18 | \item{x}{Vector of one or more character strings.} 19 | 20 | \item{data}{An \code{intensity} object.} 21 | 22 | \item{mapping}{A \code{mapping} object.} 23 | 24 | \item{keep_only_std}{Keep only standard variables.} 25 | } 26 | \value{ 27 | A list of mapped names. 28 | } 29 | \description{ 30 | Data mappings describe how variables in the data are mapped to standard names 31 | used throughout \code{epiphy}. 32 | } 33 | \details{ 34 | Standard names are \code{x}, \code{y} and \code{z} for the three spatial 35 | dimensions, and \code{t} for the time. \code{r} corresponds to the records 36 | of (disease) intensity, and \code{n}, the number of individuals in a sampling 37 | unit (if applicable). 38 | 39 | \code{mapping()} works with expressions, and \code{mapping_()}, with a vector 40 | of characters. 41 | } 42 | \examples{ 43 | mapping(x = col1, y = col2) 44 | mapping_(c("x = col1", "y = col2")) 45 | 46 | } 47 | \seealso{ 48 | \code{\link{mapped_var}} 49 | } 50 | -------------------------------------------------------------------------------- /man/simulated_epidemics.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{simulated_epidemics} 5 | \alias{simulated_epidemics} 6 | \title{Examples of simulated epidemic data.} 7 | \format{ 8 | A data frame with 864 rows and 6 variables: 9 | \tabular{rll}{ 10 | [, 1] \tab pattern \tab Either clumped (i.e. aggregated), random or 11 | regular. \cr 12 | [, 2] \tab mu \tab Median spore dispersal parameter. \cr 13 | [, 3:4] \tab x,y \tab Grid spatial coordinates. \cr 14 | [, 5] \tab i \tab Number of diseased plants (from 0 to 100). \cr 15 | [, 6] \tab n \tab Sampling unit size. Here, n = 100 plants per 16 | sampling unit. \cr 17 | } 18 | } 19 | \source{ 20 | Xu XM, Madden LV. 2004. Use of SADIE statistics to study spatial 21 | dynamics of plant disease epidemics. Plant Pathology 53, 38–49. 22 | \doi{10.1111/j.1365-3059.2004.00949.x} 23 | } 24 | \usage{ 25 | simulated_epidemics 26 | } 27 | \description{ 28 | Epidemics were generated using the stochastic simulator from Xu and Madden 29 | (2004). The data consist of the numbers of diseased plants per sampling 30 | unit (out of a total of n = 100 plants in each sampling unit). N = 144 31 | sampling units, and different values for the parameters \code{pattern} and 32 | \code{mu} were used for the simulations. 33 | } 34 | \keyword{datasets} 35 | -------------------------------------------------------------------------------- /man/indAndSub.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{indAndSub} 4 | \alias{indAndSub} 5 | \alias{ind2sub} 6 | \alias{sub2ind} 7 | \title{Retrieve vector or array indices} 8 | \usage{ 9 | ind2sub(ind, .dim, .dimnames = NULL, useNames = FALSE) 10 | 11 | sub2ind(sub, .dim) 12 | } 13 | \arguments{ 14 | \item{ind}{Vector indices.} 15 | 16 | \item{.dim}{\code{\link[base]{dim}(.)} integer vector} 17 | 18 | \item{.dimnames}{optional list of character \code{\link[base]{dimnames}(.)}. 19 | If \code{useNames} is true, to be used for constructing dimnames for 20 | \code{arrayInd()} (and hence, \code{which(*, arr.ind=TRUE)}). 21 | If \code{\link[base]{names}(.dimnames)} is not empty, these are used as 22 | column names. \code{.dimnames[[1]]} is used as row names.} 23 | 24 | \item{useNames}{logical indicating if the value of \code{arrayInd()} 25 | should have (non-null) dimnames at all.} 26 | 27 | \item{sub}{Array/matrix indices.} 28 | } 29 | \value{ 30 | See \code{\link[base]{arrayInd}}. 31 | } 32 | \description{ 33 | \code{ind2sub} is just an alias for \code{\link[base]{arrayInd}}. 34 | \code{sub2ind} is the reverse of \code{ind2sub}. 35 | } 36 | \examples{ 37 | set.seed(12345) 38 | mat <- matrix(round(runif(6, min = 0, max = 10)), nrow = 2, ncol = 3) 39 | ind2sub(4, dim(mat)) 40 | sub2ind(c(2, 2), dim(mat)) 41 | subs <- as.matrix(expand.grid(1:2,2:3)) 42 | sub2ind(subs, dim(mat)) 43 | 44 | } 45 | \keyword{internal} 46 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | release: 9 | types: [published] 10 | workflow_dispatch: 11 | 12 | name: pkgdown 13 | 14 | jobs: 15 | pkgdown: 16 | runs-on: ubuntu-latest 17 | # Only restrict concurrency for non-PR jobs 18 | concurrency: 19 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 20 | env: 21 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 22 | permissions: 23 | contents: write 24 | steps: 25 | - uses: actions/checkout@v3 26 | 27 | - uses: r-lib/actions/setup-pandoc@v2 28 | 29 | - uses: r-lib/actions/setup-r@v2 30 | with: 31 | use-public-rspm: true 32 | 33 | - uses: r-lib/actions/setup-r-dependencies@v2 34 | with: 35 | extra-packages: any::pkgdown, local::. 36 | needs: website 37 | 38 | - name: Build site 39 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 40 | shell: Rscript {0} 41 | 42 | - name: Deploy to GitHub pages 🚀 43 | if: github.event_name != 'pull_request' 44 | uses: JamesIves/github-pages-deploy-action@v4.4.1 45 | with: 46 | clean: false 47 | branch: gh-pages 48 | folder: docs 49 | -------------------------------------------------------------------------------- /man/as.data.frame.intensity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{as.data.frame.intensity} 4 | \alias{as.data.frame.intensity} 5 | \title{Coerce to a data frame.} 6 | \usage{ 7 | \method{as.data.frame}{intensity}( 8 | x, 9 | row.names = NULL, 10 | optional = FALSE, 11 | ..., 12 | stringsAsFactors = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{x}{An \code{intensity} object.} 17 | 18 | \item{row.names}{\code{NULL} or a character vector giving the row 19 | names for the data frame. Missing values are not allowed.} 20 | 21 | \item{optional}{logical. If \code{TRUE}, setting row names and 22 | converting column names (to syntactic names: see 23 | \code{\link[base]{make.names}}) is optional. Note that all of \R's 24 | \pkg{base} package \code{as.data.frame()} methods use 25 | \code{optional} only for column names treatment, basically with the 26 | meaning of \code{\link[base]{data.frame}(*, check.names = !optional)}. 27 | See also the \code{make.names} argument of the \code{matrix} method.} 28 | 29 | \item{...}{additional arguments to be passed to or from methods.} 30 | 31 | \item{stringsAsFactors}{logical: should the character vector be converted 32 | to a factor?} 33 | } 34 | \value{ 35 | A data frame. 36 | } 37 | \description{ 38 | Functions to coerce an \code{intensity} object to a data frame. 39 | } 40 | \examples{ 41 | my_data <- incidence(tomato_tswv$field_1929) 42 | head(as.data.frame(my_data)) 43 | 44 | } 45 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - uses: r-lib/actions/setup-pandoc@v2 35 | 36 | - uses: r-lib/actions/setup-r@v2 37 | with: 38 | r-version: ${{ matrix.config.r }} 39 | http-user-agent: ${{ matrix.config.http-user-agent }} 40 | use-public-rspm: true 41 | 42 | - uses: r-lib/actions/setup-r-dependencies@v2 43 | with: 44 | extra-packages: any::rcmdcheck 45 | needs: check 46 | 47 | - uses: r-lib/actions/check-r-package@v2 48 | with: 49 | upload-snapshots: true 50 | -------------------------------------------------------------------------------- /man/threshold.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{threshold} 4 | \alias{threshold} 5 | \title{To go to higher level in the hierarchy.} 6 | \usage{ 7 | threshold(data, value, ...) 8 | } 9 | \arguments{ 10 | \item{data}{A numeric vector or an \code{\link{intensity}} object.} 11 | 12 | \item{value}{All the intensity values lower or equal to this value are set 13 | to 0. The other values are set to 1.} 14 | 15 | \item{...}{Additional arguments to be passed to other methods.} 16 | } 17 | \value{ 18 | A numeric vector or an \code{\link{intensity}} object. 19 | } 20 | \description{ 21 | This function transforms the current numeric vector or \code{intensity} data 22 | set into a "simplified black and white image" of this same data set: every 23 | value of disease intensity below and above a given threshold is given the 24 | value 0 and 1, respectively. 25 | } 26 | \details{ 27 | By default, everything above 0 is given 1, and 0 stays at 0. \code{threshold} 28 | is thus useful to report a whole sampling unit as "healthy" (0), if no 29 | diseased individual at all was found within the sampling unit, or "diseased" 30 | (1) if at least one diseased individual was found. 31 | } 32 | \examples{ 33 | my_incidence <- incidence(tomato_tswv$field_1929) 34 | plot(my_incidence, type = "all") 35 | my_incidence_clumped_1 <- clump(my_incidence, unit_size = c(x = 3, y = 3)) 36 | plot(my_incidence_clumped_1, type = "all") 37 | my_incidence_thr <- threshold(my_incidence_clumped_1, value = 4) 38 | plot(my_incidence_thr, type = "all") 39 | 40 | } 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # epiphy epiphy website 2 | 3 | [![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.r-pkg.org/pkg/epiphy) 4 | [![R-CMD-check](https://github.com/chgigot/epiphy/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/chgigot/epiphy/actions/workflows/R-CMD-check.yaml) 5 | [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/epiphy)](https://CRAN.R-project.org/package=epiphy) 6 | [![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/epiphy)](https://CRAN.R-project.org/package=epiphy) 7 | [![Downloads-per-month](https://cranlogs.r-pkg.org/badges/epiphy)](https://CRAN.R-project.org/package=epiphy) 8 | 9 | ## Overview 10 | 11 | **epiphy** makes it easy to analyze plant disease epidemics. It provides a common framework for plant disease intensity data recorded over time and/or space. Implemented statistical methods are currently mainly focused on spatial pattern analysis (e.g., aggregation indices, Taylor and binary power laws, distribution fitting, SADIE and mapcomp methods). Several data sets that were mainly published in plant disease epidemiology literature are also included in this package. The online documentation is available at https://chgigot.github.io/epiphy/. 12 | 13 | ## Installation 14 | 15 | Install the latest released version from **CRAN**: 16 | 17 | ```r 18 | install.packages("epiphy") 19 | ``` 20 | 21 | Install the latest development version from **GitHub**: 22 | 23 | ```r 24 | # install.packages("devtools") 25 | devtools::install_github("chgigot/epiphy") 26 | ``` 27 | -------------------------------------------------------------------------------- /man/hop_viruses.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{hop_viruses} 5 | \alias{hop_viruses} 6 | \title{Incidence of three viruses in an Australian hop garden.} 7 | \format{ 8 | There are three data frames, one for each virus (\code{HpLV}, \code{HpMV} and 9 | \code{ApMV}). Each data frame consists of 2550 rows and 7 variables: 10 | \tabular{lll}{ 11 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 12 | [, 3:4] \tab xm,ym \tab Metric spatial coordinates. \cr 13 | [, 5] \tab t \tab Year of disease assessments. \cr 14 | [, 6] \tab i \tab Incidence. 0: Healthy, 1: Diseased. \cr 15 | [, 7] \tab n \tab Sampling unit size. n = 1 means that the sampling 16 | unit size is the plant. \cr 17 | } 18 | } 19 | \source{ 20 | Pethybridge SJ, Madden LV. 2003. Analysis of spatiotemporal dynamics 21 | of virus spread in an Australian hop garden by stochastic modeling. Plant 22 | Disease 87:56-62. 23 | \doi{10.1094/PDIS.2003.87.1.56} 24 | } 25 | \usage{ 26 | hop_viruses 27 | } 28 | \description{ 29 | Three viruses, i.e. Hop latent virus (HpLV), Hop mosaic virus (HpMV), and 30 | Apple mosaic virus (ApMV), were monitored in an Australian hop garden for two 31 | consecutive years (1996 and 1997). The hop garden was established in 1989 32 | with the variety Victoria in a commercial hop farm at Bushy Park, Tasmania, 33 | Australia. It consisted of 25 rows containing 51 plants each, so that there 34 | were 1275 hop plants in total. There were 2.1 m between rows, and 1.8 m 35 | between plants within rows. 36 | } 37 | \keyword{datasets} 38 | -------------------------------------------------------------------------------- /man/vcov.smle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle-factory.R 3 | \name{vcov.smle} 4 | \alias{vcov.smle} 5 | \title{Calculate Variance-Covariance Matrix for a Fitted Model Object} 6 | \usage{ 7 | \method{vcov}{smle}(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{ 11 | a fitted model object, typically. Sometimes also a 12 | \code{\link{summary}()} object of such a fitted model. 13 | } 14 | 15 | \item{...}{ 16 | additional arguments for method functions. For the 17 | \code{\link[stats]{glm}} method this can be used to pass a 18 | \code{dispersion} parameter.} 19 | } 20 | \value{ 21 | A matrix of the estimated covariances between the parameter estimates 22 | in the linear or non-linear predictor of the model. This should have 23 | row and column names corresponding to the parameter names given by the 24 | \code{\link[stats]{coef}} method. 25 | 26 | When some coefficients of the (linear) model are undetermined and 27 | hence \code{NA} because of linearly dependent terms (or an 28 | \dQuote{over specified} model), also called 29 | \dQuote{aliased}, see \code{\link[stats]{alias}}, then since \R version 3.5.0, 30 | \code{vcov()} (iff \code{complete = TRUE}, i.e., by default for 31 | \code{lm} etc, but not for \code{aov}) contains corresponding rows and 32 | columns of \code{NA}s, wherever \code{\link[stats]{coef}()} has always 33 | contained such \code{NA}s. 34 | } 35 | \description{ 36 | Returns the variance-covariance matrix of the main parameters of 37 | a fitted model object. The \dQuote{main} parameters of model 38 | correspond to those returned by \code{\link[stats]{coef}}, and typically do 39 | not contain a nuisance scale parameter (\code{\link[stats]{sigma}}). 40 | } 41 | \keyword{internal} 42 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://chgigot.github.io/epiphy/ 2 | template: 3 | bootstrap: 5 4 | 5 | reference: 6 | - title: Data sets 7 | contents: 8 | - aphids 9 | - arthropods 10 | - citrus_ctv 11 | - codling_moths 12 | - dogwood_anthracnose 13 | - hop_viruses 14 | - offspring_survival 15 | - onion_bacterial_blight 16 | - pyrethrum_ray_blight 17 | - simulated_epidemics 18 | - tobacco_viruses 19 | - tomato_tswv 20 | - title: Disease intensity objects 21 | desc: > 22 | Create and manipulate disease intensity objects (i.e., count, incidence 23 | and severity objects). 24 | contents: 25 | - as.data.frame.intensity 26 | - clump 27 | - count 28 | - incidence 29 | - is.intensity 30 | - mapped_var 31 | - mapping 32 | - severity 33 | - split.intensity 34 | - title: Statistical methods 35 | contents: 36 | - agg_index 37 | - calpha.test 38 | - chisq.test 39 | - fit_two_distr 40 | - mapcomp 41 | - power_law 42 | - sadie 43 | - spatial_hier 44 | - z.test 45 | - title: Miscellaneous 46 | desc: > 47 | Mainly helper functions. 48 | contents: 49 | - a2a 50 | - dbetabinom 51 | - pbetabinom 52 | - qbetabinom 53 | - rbetabinom 54 | - logit 55 | - probit 56 | - cloglog 57 | - threshold 58 | 59 | navbar: 60 | left: 61 | - text: Primer 62 | href: articles/epiphy.html 63 | - text: Functions and data 64 | href: reference/index.html 65 | - text: Articles 66 | menu: 67 | - text: Definitions and relationships between parameters 68 | href: articles/defs-and-eqns.html 69 | - text: News 70 | href: news/index.html 71 | 72 | -------------------------------------------------------------------------------- /R/epiphy.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | #' \CRANpkg{epiphy}: An R package to analyze plant disease epidemics. 3 | #' 4 | #' \CRANpkg{epiphy} makes it easy to analyze plant disease epidemics. It provides 5 | #' a common framework for plant disease intensity data recorded over time and/or 6 | #' space. Implemented statistical methods are currently mainly focused on 7 | #' spatial pattern analysis (e.g., aggregation indices, Taylor and binary power 8 | #' laws, distribution fitting, SADIE and mapcomp methods). Several data sets 9 | #' that were mainly published in plant disease epidemiology literature are also 10 | #' included in this package 11 | #' 12 | #' @author 13 | #' \strong{Maintainer:} Christophe Gigot \email{ch.gigot@@gmail.com} 14 | #' 15 | #' @seealso 16 | #' 17 | #' Useful references: 18 | #' 19 | #' Gosme M. 2008. Comment analyser la structure spatiale et modéliser le 20 | #' développement spatio-temporel des épiphyties? Canadian Journal of Plant 21 | #' Pathology, 30:4-23. 22 | #' 23 | #' Madden LV, Hughes G, van den Bosch F. 2007. Spatial aspects of epidemics - 24 | #' III: Patterns of plant disease. In: The study of plant disease epidemics, 25 | #' 235–278. American Phytopathological Society, St Paul, MN. 26 | #' 27 | #' @keywords internal 28 | #' 29 | #' @docType package 30 | #' @name epiphy 31 | #' @useDynLib epiphy 32 | #' 33 | #' @import stats 34 | #' @import ggplot2 35 | #' @importFrom Rcpp sourceCpp 36 | #' @importFrom grDevices terrain.colors 37 | #' @importFrom methods is 38 | #' @importFrom utils head tail 39 | #------------------------------------------------------------------------------# 40 | NULL 41 | 42 | # TODO: Needed for checking process... 43 | utils::globalVariables(c("Number per sampling unit", "Frequency", "key", 44 | "x", "y", "z", "i", "phs")) 45 | -------------------------------------------------------------------------------- /man/citrus_ctv.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{citrus_ctv} 5 | \alias{citrus_ctv} 6 | \title{Incidence of citrus tristeza virus (CTV) disease in three fields.} 7 | \format{ 8 | There are three data frames: 9 | \itemize{ 10 | \item \code{IVI3and4}: A data frame with 864 rows and 5 variables. 11 | \item \code{IVI6and7}: A data frame with 648 rows and 5 variables. 12 | \item \code{El_Realengo}: A data frame with 2000 rows and 5 variables. 13 | } 14 | 15 | The structure is the same for all the data frames: 16 | 17 | \tabular{rll}{ 18 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 19 | [, 3] \tab t \tab Year of disease assessments. \cr 20 | [, 4] \tab i \tab Disease incidence. 0: Healthy, 1: Diseased. \cr 21 | [, 5] \tab n \tab Sampling unit size. n = 1 means that the sampling 22 | unit size is the plant. \cr 23 | } 24 | } 25 | \source{ 26 | Gottwald TR, Cambra M, Moreno P, Camarasa E, Piquer J. 1996. Spatial 27 | and temporal analyses of citrus tristeza virus in eastern Spain. 28 | Phytopathology 86, 45–55. 29 | 30 | Gibson GJ. 1997. Investigating mechanisms of spatiotemporal epidemic 31 | spread using stochastic models. Phytopathology 87, 139-46. 32 | \doi{10.1094/PHYTO.1997.87.2.139} 33 | } 34 | \usage{ 35 | citrus_ctv 36 | } 37 | \description{ 38 | CTV incidence data for three orchards in eastern Spain reported for 39 | consecutive years. 40 | } 41 | \details{ 42 | Both \code{IVI3and4} and \code{IVI6and7} orchards consisted of 216 trees each 43 | of Washington navel orange on Troyer citrange planted in 1978 on a 2 x 6-m 44 | spacing. \code{El_Realengo} orchard consisted of 400 Marsh seedless 45 | grapefruit on Troyer citrange planted in 1973 on a 5.5 x 5.5-m spacing. 46 | } 47 | \keyword{datasets} 48 | -------------------------------------------------------------------------------- /man/calpha.test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/indices.R 3 | \name{calpha.test} 4 | \alias{calpha.test} 5 | \alias{calpha.test.fisher} 6 | \title{C(alpha) test.} 7 | \usage{ 8 | calpha.test(x, ...) 9 | 10 | \method{calpha.test}{fisher}(x, ...) 11 | } 12 | \arguments{ 13 | \item{x}{The output of the \code{\link{agg_index}} function with 14 | \code{method = "fisher"} as parameter.} 15 | 16 | \item{...}{Not yet implemented.} 17 | } 18 | \value{ 19 | Same kind of object as the one returns by the stats 20 | \code{\link[stats]{chisq.test}} function for example. 21 | } 22 | \description{ 23 | The C(alpha) test is a test of the binomial distribution against the 24 | alternative of the beta-binomial distribution. 25 | } 26 | \details{ 27 | It is based on calculation of a test statistic, z, that has an asymptotic 28 | standard normal distribution under the null hypothesis. It is one-sided (in 29 | the way that the alternative is aggregation, not just "non-randomness"), thus 30 | with a confidence level of 95%, the null hypothesis is rejected when z > 31 | 1.64. When all the sampling units contain the same total number of 32 | individuals, n, the test statistic is calculated from: 33 | 34 | z = (n(N - 1)I - Nn)/(2Nn(n - 1))^(1/2) 35 | 36 | where N is the number of sampling units, and I, Fisher's index of aggregation 37 | for incidence data. 38 | } 39 | \examples{ 40 | # For incidence data: 41 | my_incidence <- incidence(tobacco_viruses) 42 | my_fisher <- agg_index(my_incidence, method = "fisher") 43 | calpha.test(my_fisher) 44 | 45 | } 46 | \references{ 47 | Neyman J. 1959. Optimal asymptotic tests of composite statistical hypotheses. 48 | In: Probability and Statistics, 213-234. Wiley, New York. 49 | 50 | Tarone RE. 1979. Testing the goodness of fit of the binomial distribution. 51 | Biometrika, 66(3): 585-590. 52 | } 53 | \seealso{ 54 | \code{\link{chisq.test}}, \code{\link{z.test}} 55 | } 56 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: epiphy 2 | Type: Package 3 | Title: Analysis of Plant Disease Epidemics 4 | Version: 0.5.0.9000 5 | Authors@R: c( 6 | person("Christophe", "Gigot", email = "ch.gigot@gmail.com", 7 | role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0002-1748-2902")), 8 | person("Adam H.", "Sparks", role = "ctb"), 9 | person("Katrin", "Leinweber", role = "ctb") 10 | ) 11 | Description: A toolbox to make it easy to analyze plant disease epidemics. It 12 | provides a common framework for plant disease intensity data recorded over 13 | time and/or space. Implemented statistical methods are currently mainly 14 | focused on spatial pattern analysis (e.g., aggregation indices, Taylor and 15 | binary power laws, distribution fitting, SADIE and 'mapcomp' methods). See 16 | Laurence V. Madden, Gareth Hughes, Franck van den Bosch (2007) 17 | for further information on these methods. 18 | Several data sets that were mainly published in plant disease epidemiology 19 | literature are also included in this package. 20 | License: MIT + file LICENSE 21 | URL: https://github.com/chgigot/epiphy, https://chgigot.github.io/epiphy/ 22 | BugReports: https://github.com/chgigot/epiphy/issues 23 | Encoding: UTF-8 24 | LazyData: true 25 | RoxygenNote: 7.2.3 26 | LinkingTo: Rcpp 27 | Depends: 28 | R (>= 4.0) 29 | Imports: 30 | stats, 31 | methods, 32 | utils, 33 | grDevices, 34 | ggplot2, 35 | transport, 36 | msm, 37 | pbapply, 38 | Rcpp 39 | Suggests: 40 | magrittr, 41 | dplyr, 42 | tidyr, 43 | spdep, 44 | emdist, 45 | vegan, 46 | MASS, 47 | emdbook, 48 | knitr, 49 | rmarkdown 50 | Collate: 51 | 'RcppExports.R' 52 | 'betabinom.R' 53 | 'data.R' 54 | 'utils.R' 55 | 'mle-factory.R' 56 | 'epiphy.R' 57 | 'intensity-classes.R' 58 | 'distr-fitting.R' 59 | 'indices.R' 60 | 'mapcomp.R' 61 | 'power-law.R' 62 | 'sadie.R' 63 | 'spatial-hier.R' 64 | VignetteBuilder: knitr 65 | -------------------------------------------------------------------------------- /man/chisq.test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/indices.R 3 | \name{chisq.test} 4 | \alias{chisq.test} 5 | \alias{chisq.test.default} 6 | \alias{chisq.test.fisher} 7 | \title{Chi-squared test.} 8 | \usage{ 9 | chisq.test(x, ...) 10 | 11 | \method{chisq.test}{default}(x, ...) 12 | 13 | \method{chisq.test}{fisher}(x, ...) 14 | } 15 | \arguments{ 16 | \item{x}{Either the output of the \code{\link{agg_index}} function with 17 | \code{method = "fisher"} as parameter, or another R object. In the latter 18 | case, stats::\code{\link[stats]{chisq.test}} is called.} 19 | 20 | \item{...}{Further arguments to be passed to 21 | stats::\code{\link[stats]{chisq.test}}.} 22 | } 23 | \value{ 24 | Same kind of object as the one returns by the stats 25 | \code{\link[stats]{chisq.test}} function. 26 | } 27 | \description{ 28 | Performs chi-squared tests for Fisher's aggregation indices (computed with 29 | either count or incidence data). If another kind of data is provided, the R 30 | standard \code{chisq.test} function is called. 31 | } 32 | \details{ 33 | Under the null hypothesis for Fisher's aggregation index (index = 1, i.e. a 34 | random pattern is observed), (N - 1)*index follows a chi-squared distribution 35 | with N - 1 degrees of freedom. N is the number of sampling units. 36 | } 37 | \examples{ 38 | # For incidence data: 39 | my_incidence <- incidence(tobacco_viruses) 40 | my_fisher <- agg_index(my_incidence, method = "fisher") 41 | chisq.test(my_fisher) 42 | 43 | } 44 | \references{ 45 | For count and incidence data: 46 | 47 | Madden LV, Hughes G. 1995. Plant disease incidence: Distributions, 48 | heterogeneity, and temporal analysis. Annual Review of Phytopathology 33(1): 49 | 529–564. 50 | \doi{10.1146/annurev.py.33.090195.002525} 51 | 52 | Patil GP, Stiteler WM. 1973. Concepts of aggregation and their 53 | quantification: a critical review with some new results and applications. 54 | Researches on Population Ecology, 15(1): 238-254. 55 | } 56 | \seealso{ 57 | \code{\link{calpha.test}}, \code{\link{z.test}} 58 | } 59 | -------------------------------------------------------------------------------- /man/z.test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/indices.R 3 | \name{z.test} 4 | \alias{z.test} 5 | \alias{z.test.default} 6 | \alias{z.test.fisher} 7 | \title{Z-test.} 8 | \usage{ 9 | z.test(x, ...) 10 | 11 | \method{z.test}{default}(x, ...) 12 | 13 | \method{z.test}{fisher}( 14 | x, 15 | alternative = c("two.sided", "less", "greater"), 16 | conf.level = 0.95, 17 | ... 18 | ) 19 | } 20 | \arguments{ 21 | \item{x}{The output of the \code{\link{agg_index}} function with 22 | \code{method = "fisher"} as parameter.} 23 | 24 | \item{...}{Not yet implemented.} 25 | 26 | \item{alternative}{A character string specifying the alternative hypothesis. 27 | It must be one of "two.sided" (default), "less" or "greater".} 28 | 29 | \item{conf.level}{The confidence level of the interval.} 30 | } 31 | \value{ 32 | Same kind of object as the one returns by the stats 33 | \code{\link[stats]{chisq.test}} function for example. 34 | } 35 | \description{ 36 | Performs z-tests for Fisher's aggregation indices (computed with either count 37 | or incidence data). 38 | } 39 | \details{ 40 | For two-sided tests with a confidence level of 95%, if -1.96 <= z <= 1.95, 41 | the spatial pattern would be random. If z < -1.96 or z > 1.96, it would be 42 | uniform or aggregated, respectively. 43 | } 44 | \examples{ 45 | # For incidence data: 46 | my_incidence <- incidence(tobacco_viruses) 47 | my_fisher <- agg_index(my_incidence, method = "fisher") 48 | z.test(my_fisher) 49 | 50 | } 51 | \references{ 52 | For count and incidence data: 53 | 54 | Moradi-Vajargah M, Golizadeh A, Rafiee-Dastjerdi H, Zalucki MP, Hassanpour M, 55 | Naseri B. 2011. Population density and spatial distribution pattern of Hypera 56 | postica (Coleoptera: Curculionidae) in Ardabil, Iran. Notulae Botanicae Horti 57 | Agrobotanici Cluj-Napoca, 39(2): 42-48. 58 | 59 | Sun P, Madden LV. 1997. Using a normal approximation to test for the binomial 60 | distribution. Biometrical journal, 39(5): 533-544. 61 | } 62 | \seealso{ 63 | \code{\link{calpha.test}}, \code{\link{chisq.test}} 64 | } 65 | -------------------------------------------------------------------------------- /man/clump.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{clump} 4 | \alias{clump} 5 | \alias{clump.intensity} 6 | \title{Regroup observational data into even clumps of individuals.} 7 | \usage{ 8 | clump(object, ...) 9 | 10 | \method{clump}{intensity}(object, unit_size, fun = sum, inclusive_unspecified = FALSE, ...) 11 | } 12 | \arguments{ 13 | \item{object}{An \code{intensity} object.} 14 | 15 | \item{...}{Additional arguments to be passed to \code{fun}.} 16 | 17 | \item{unit_size}{Size of a group unit. It must be a named vector, with names 18 | corresponding to non-observational variables (i.e. space and time 19 | variables). If the size of a variable in the data set is not a multiple 20 | of the provided value in \code{unit_size}, some sampling units (the last 21 | ones) will be dropped so that clumps of individuals remain even 22 | throughout the data set.} 23 | 24 | \item{fun}{Function used to group observational data together.} 25 | 26 | \item{inclusive_unspecified}{Not yet implemented. Do unspecified mapped 27 | variables (different from i and n) need to be included into the bigger 28 | possible sampling unit (TRUE) or splited into as many sampling units as 29 | possible (FALSE, default)?} 30 | } 31 | \value{ 32 | An \code{\link{intensity}} object. 33 | } 34 | \description{ 35 | This function provides a easy way to regroup recorded data into groups of 36 | same number of individuals. 37 | } 38 | \examples{ 39 | my_incidence <- incidence(tomato_tswv$field_1929) 40 | plot(my_incidence, type = "all") 41 | 42 | # Different spatial size units: 43 | my_incidence_clumped_1 <- clump(my_incidence, unit_size = c(x = 3, y = 3)) 44 | plot(my_incidence_clumped_1, type = "all") 45 | 46 | my_incidence_clumped_2 <- clump(my_incidence, unit_size = c(x = 4, y = 5)) 47 | plot(my_incidence_clumped_2, type = "all") 48 | 49 | # To get mean disease incidence for each plant over the 3 scoring dates: 50 | my_incidence_clumped_3 <- clump(my_incidence, unit_size = c(t = 3), fun = mean) 51 | plot(my_incidence_clumped_3) 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /man/split.intensity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{split.intensity} 4 | \alias{split.intensity} 5 | \title{Divide into groups and reassemble.} 6 | \usage{ 7 | \method{split}{intensity}(x, f, drop = FALSE, ..., by, unit_size) 8 | } 9 | \arguments{ 10 | \item{x}{vector or data frame containing values to be divided into groups.} 11 | 12 | \item{f}{a \sQuote{factor} in the sense that \code{\link[base]{as.factor}(f)} 13 | defines the grouping, or a list of such factors in which case their 14 | interaction is used for the grouping. If \code{x} is a data frame, 15 | \code{f} can also be a formula of the form \code{ ~ g} to split by 16 | the variable \code{g}, or more generally of the form \code{ ~ g1 + 17 | \dots + gk} to split by the interaction of the variables 18 | \code{g1}, \dots, \code{gk}, where these variables are evaluated in 19 | the data frame \code{x} using the usual non-standard evaluation 20 | rules.} 21 | 22 | \item{drop}{logical indicating if levels that do not occur should be dropped 23 | (if \code{f} is a \code{factor} or a list).} 24 | 25 | \item{...}{further potential arguments passed to methods.} 26 | 27 | \item{by}{The name(s) of the variable(s) which define(s) the grouping.} 28 | 29 | \item{unit_size}{Size of a group unit. It must be a named vector, with names 30 | corresponding to non-observational variables (i.e. space and time 31 | variables). If the size of a variable in the data set is not a multiple 32 | of the provided value in \code{unit_size}, some sampling units (the last 33 | ones) will be dropped so that clumps of individuals remain even 34 | throughout the data set.} 35 | } 36 | \value{ 37 | A list of \code{\link{intensity}} objects. 38 | } 39 | \description{ 40 | Divide into groups and reassemble. 41 | } 42 | \examples{ 43 | my_incidence <- incidence(tomato_tswv$field_1929) 44 | plot(my_incidence, type = "all") 45 | my_incidence_spl1 <- split(my_incidence, by = "t") 46 | my_incidence_spl2 <- split(my_incidence, unit_size = c(x = 8, y = 20, t = 1)) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/sadie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // [[Rcpp::export]] 5 | double costTotiCPP(long i1, const Rcpp::NumericMatrix & flow, 6 | const Rcpp::NumericMatrix & cost, 7 | bool averaged = true, // TODO: used? 8 | bool absolute = false) { // TODO: used? 9 | i1 -= 1; // Because indices start at 0 in C++, and 1 in R. 10 | double res(0), inflow(0.0), outflow(0.0), sign(1.0), denom(1.0); 11 | // A donor unit (outflow > 0): 12 | if ((outflow = Rcpp::sum(flow(i1, Rcpp::_))) > 0) { 13 | denom = (averaged ? outflow : 1.0); 14 | res += Rcpp::sum(flow(i1, Rcpp::_) * cost(i1, Rcpp::_)) / denom; 15 | } 16 | // A receiver unit (inflow > 0): 17 | if ((inflow = Rcpp::sum(flow(Rcpp::_, i1))) > 0) { 18 | sign = (absolute ? 1.0 : -1.0); 19 | denom = (averaged ? inflow : 1.0); 20 | res += sign * Rcpp::sum(flow(Rcpp::_, i1) * cost(Rcpp::_, i1)) / denom; 21 | } 22 | return res; 23 | } 24 | 25 | // [[Rcpp::export]] 26 | Rcpp::NumericVector costTotCPP(const Rcpp::NumericMatrix & flow, 27 | const Rcpp::NumericMatrix & cost) { 28 | Rcpp::NumericVector res(flow.nrow()); 29 | for (int i1 = 0; i1 < flow.nrow(); ++i1) { 30 | res.at(i1) = costTotiCPP(i1 + 1, flow, cost); 31 | // +1 because it is here a R-like index. 32 | } 33 | return res; 34 | } 35 | 36 | // [[Rcpp::export]] 37 | Rcpp::NumericMatrix as_matrix_transport(Rcpp::List & x, double dim_mat) { 38 | Rcpp::NumericMatrix res(dim_mat); // A square matrix. 39 | // By default, a matrix constructed from dimensions will always be 40 | // initialized with all entries being zero. So no need to use res.fill(0.0); 41 | Rcpp::IntegerVector from(x[0]), to(x[1]); 42 | Rcpp::NumericVector mass(x[2]); 43 | for (int i1 = 0; i1 < from.length(); ++i1) { 44 | res.at((from.at(i1) - 1), (to.at(i1) - 1)) = mass.at(i1); 45 | } 46 | for (int i1 = 0; i1 < dim_mat; ++i1) { 47 | res(i1, i1) = 0; // No flows on the diagonal, i.e. not flows between a 48 | // unit and itself. 49 | } 50 | return res; 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /man/a2a.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/power-law.R 3 | \name{a2a} 4 | \alias{a2a} 5 | \alias{a2a.numeric} 6 | \alias{a2a.list} 7 | \title{Easily switch between different power law formulations.} 8 | \usage{ 9 | a2a(x, ...) 10 | 11 | \method{a2a}{numeric}( 12 | x, 13 | slope, 14 | n, 15 | from = c("Ai", "ai", "AI", "aI"), 16 | to = c("Ai", "ai", "AI", "aI"), 17 | ... 18 | ) 19 | 20 | \method{a2a}{list}(x, to = c("Ai", "ai", "AI", "aI"), ...) 21 | } 22 | \arguments{ 23 | \item{x}{Intercept parameter to be converted or a named list with the 24 | parameter to be converted ("Ai", "ai", "AI" or "aI"), the slope 25 | ("slope"), and the number of individual per sampling unit ("n").} 26 | 27 | \item{...}{Additional arguments to be passed to other methods.} 28 | 29 | \item{slope}{Slope parameter.} 30 | 31 | \item{n}{Number of individuals per sampling unit.} 32 | 33 | \item{from}{Kind of the input intercept parameter ("Ai", "ai", "AI" or "aI").} 34 | 35 | \item{to}{Desired kind for the ouput intercept parameter ("Ai", "ai", "AI" or 36 | "aI").} 37 | } 38 | \value{ 39 | A numeric vector. 40 | } 41 | \description{ 42 | \code{a2a} was designed to avoid headaches that are likely to occur when 43 | working with different formulations of the binomial power law analysis. 44 | } 45 | \details{ 46 | The binomial power law can be expressed as: \eqn{s_y^2 = (intercept)(s_{bin}^2)^b}. 47 | But different forms of (intercept) are possible depending on the formulation of the 48 | binomial power law. 49 | \tabular{ccccc}{ 50 | \tab Ai \tab ai \tab AI \tab aI \cr 51 | Ai \tab 1 \tab n^b \tab n^(2(b-1)) \tab n^(b-2) \cr 52 | ai \tab n^(-b) \tab 1 \tab n^(b-2) \tab n^(-2) \cr 53 | AI \tab n^(2(1-b)) \tab n^(2-b) \tab 1 \tab n^(-b) \cr 54 | aI \tab n^(2-b) \tab n^2 \tab n^b \tab 1 \cr 55 | } 56 | } 57 | \examples{ 58 | # Values from the power_law() example: 59 | Ai <- 38.6245 60 | slope <- 1.9356 61 | n <- 9 62 | 63 | # Usual function call syntax: 64 | a2a(Ai, slope, n, from = "Ai", to = "ai") 65 | 66 | # Other syntaxes: 67 | inputs <- list(Ai = Ai, slope = slope, n = n) 68 | a2a(inputs, "ai") 69 | require(magrittr) 70 | inputs \%>\% a2a("ai") 71 | 72 | } 73 | -------------------------------------------------------------------------------- /man/power_law.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/power-law.R 3 | \name{power_law} 4 | \alias{power_law} 5 | \title{Taylor's and binary power laws.} 6 | \usage{ 7 | power_law(data, log_base = exp(1), ...) 8 | } 9 | \arguments{ 10 | \item{data}{A list of \code{intensity} objects (\code{count} or 11 | \code{incidence} objects).} 12 | 13 | \item{log_base}{Logarithm base to be used.} 14 | 15 | \item{...}{Additional arguments to be passed to other methods.} 16 | } 17 | \value{ 18 | A \code{power_law} object. 19 | } 20 | \description{ 21 | Assesses the overall degree of heterogeneity in a collection of data sets at 22 | the sampling-unit scale. 23 | } 24 | \details{ 25 | The power law describes the relationship between the observed variance of 26 | individuals within a data set (\code{s^2}) and the corresponding variance 27 | under the assumption of no aggregation (\code{s\'^2}). It can be expressed 28 | under its logarithmic form as: \code{log(s^2) = log(a) + b log(Y)}, with: 29 | \itemize{ 30 | \item \code{Y = p} in the case of count data (Taylor's power law). 31 | \item \code{Y = p(1 - p)} in the case of incidence data (binary power law). 32 | } 33 | \code{p} corresponds to the mean proportion of recorded individuals in case 34 | of incidence data, and the absolute value in case of count data. 35 | } 36 | \examples{ 37 | require(magrittr) 38 | my_data <- do.call(c, lapply(citrus_ctv, function(citrus_field) { 39 | incidence(citrus_field) \%>\% 40 | clump(unit_size = c(x = 3, y = 3)) \%>\% 41 | split(by = "t") 42 | })) 43 | # my_data is a list of incidence object, each one corresponding to a given 44 | # time at a given location. 45 | my_power_law <- power_law(my_data) 46 | my_power_law 47 | summary(my_power_law) 48 | plot(my_power_law) # Same as: plot(my_power_law, scale = "log") 49 | plot(my_power_law, scale = "lin") 50 | 51 | } 52 | \references{ 53 | Taylor LR. 1961. Aggregation, variance and the mean. Nature 189: 732–35. 54 | 55 | Hughes G, Madden LV. 1992. Aggregation and incidence of disease. Plant 56 | Pathology 41 (6): 657–660. 57 | \doi{10.1111/j.1365-3059.1992.tb02549.x} 58 | 59 | Madden LV, Hughes G, van den Bosch F. 2007. Spatial aspects of epidemics - 60 | III: Patterns of plant disease. In: The study of plant disease epidemics, 61 | 235–278. American Phytopathological Society, St Paul, MN. 62 | } 63 | -------------------------------------------------------------------------------- /man/spatial_hier.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/spatial-hier.R 3 | \name{spatial_hier} 4 | \alias{spatial_hier} 5 | \title{Spatial hierarchy analysis.} 6 | \usage{ 7 | spatial_hier(low, high) 8 | } 9 | \arguments{ 10 | \item{low}{An list of \code{intensity} objects.} 11 | 12 | \item{high}{An list of \code{intensity} objects.} 13 | } 14 | \value{ 15 | A \code{spatial_hier} object. 16 | } 17 | \description{ 18 | The manner in which the data are collected provides information about 19 | aggregation of disease at different levels in a spatial hierarchy (Hughes et 20 | al. 1997). For example, a sampling unit (upper level) can be reported as 21 | "healthy", if no diseased leaves (lower level) were found within the sampling 22 | unit. 23 | } 24 | \details{ 25 | In a pairwise comparison between levels, the probability that an individual 26 | at the lower hierarchical level is diseased is denoted plow, and phigh refers 27 | to the probability of disease at the higher level. The relationship between 28 | these two probabilities can be written as 29 | 30 | phigh = 1 - (1 - plow)^nu 31 | 32 | where n is a parameter ranging from 0 to the corresponding number of 33 | individuals at the hierarchical level referenced by plow. If the value of n 34 | is equal to the number of individuals at the lower hierarchical level 35 | contained in a unit of the higher level (n low ), this suggests that there is 36 | no aggregation of disease incidence at the lower level. Conversely, a value 37 | of n less than n low is indicative of aggregation at that level. The value of 38 | n can be interpreted as an effective sample size (Hughes and Gottwald 1999; 39 | Madden and Hughes 1999) in the statistical sense that its value indicates the 40 | number of independent pieces of information at the lower level. Here, the 41 | effective sample size concerns the equating of the zero-term of the binomial 42 | distribution with the zero-term of an overdispersed distribution, as 43 | described in Madden and Hughes (1999). Using the complementary log-log 44 | transformation, CLL(x) = ln(-ln(1-x)), one can rewrite the Equation 5 as 45 | follows (Madden et al. 2007): 46 | 47 | CLL(phigh) = ln(nu) + CLL(plow) 48 | 49 | from which the value of ln(n) can be obtained as the intercept of a linear 50 | regression when the slope is constrained to 1. 51 | } 52 | \examples{ 53 | my_data_low <- incidence(tomato_tswv$field_1929) 54 | my_data_low <- clump(my_data_low, c(x = 3, y = 3)) 55 | my_data_high <- my_data_low 56 | my_data_high$data$n <- 1 57 | my_data_high$data$i <- ifelse(my_data_high$data$i > 0, 1, 0) 58 | my_data_low <- split(my_data_low, by = "t") 59 | my_data_high <- split(my_data_high, by = "t") 60 | res <- spatial_hier(my_data_low, my_data_high) 61 | 62 | res 63 | summary(res) 64 | plot(res) 65 | 66 | } 67 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method("[",mapping) 4 | S3method(a2a,list) 5 | S3method(a2a,numeric) 6 | S3method(as.character,count) 7 | S3method(as.character,incidence) 8 | S3method(as.character,mapping) 9 | S3method(as.character,severity) 10 | S3method(as.data.frame,intensity) 11 | S3method(c,mapping) 12 | S3method(calpha.test,fisher) 13 | S3method(chisq.test,default) 14 | S3method(chisq.test,fisher) 15 | S3method(clump,intensity) 16 | S3method(coef,smle) 17 | S3method(coef,summary.smle) 18 | S3method(dim,intensity) 19 | S3method(droplevels,list) 20 | S3method(fit_two_distr,count) 21 | S3method(fit_two_distr,default) 22 | S3method(fit_two_distr,incidence) 23 | S3method(logLik,smle) 24 | S3method(mapcomp,count) 25 | S3method(mapcomp,data.frame) 26 | S3method(mapcomp,incidence) 27 | S3method(mapcomp,matrix) 28 | S3method(plot,fit_two_distr) 29 | S3method(plot,intensity) 30 | S3method(plot,mapcomp) 31 | S3method(plot,power_law) 32 | S3method(plot,sadie) 33 | S3method(plot,spatial_hier) 34 | S3method(print,a2a) 35 | S3method(print,agg_index) 36 | S3method(print,fit_two_distr) 37 | S3method(print,intensity) 38 | S3method(print,mapcomp) 39 | S3method(print,mapping) 40 | S3method(print,power_law) 41 | S3method(print,sadie) 42 | S3method(print,smle) 43 | S3method(print,spatial_hier) 44 | S3method(print,summary.fit_two_distr) 45 | S3method(print,summary.power_law) 46 | S3method(print,summary.sadie) 47 | S3method(print,summary.smle) 48 | S3method(print,summary.spatial_hier) 49 | S3method(sadie,count) 50 | S3method(sadie,data.frame) 51 | S3method(sadie,incidence) 52 | S3method(sadie,matrix) 53 | S3method(smle,default) 54 | S3method(smle,intensity) 55 | S3method(split,intensity) 56 | S3method(str,mapping) 57 | S3method(summary,fit_two_distr) 58 | S3method(summary,intensity) 59 | S3method(summary,power_law) 60 | S3method(summary,sadie) 61 | S3method(summary,smle) 62 | S3method(summary,spatial_hier) 63 | S3method(threshold,intensity) 64 | S3method(threshold,numeric) 65 | S3method(vcov,smle) 66 | S3method(z.test,default) 67 | S3method(z.test,fisher) 68 | export("mapped_var<-") 69 | export(a2a) 70 | export(agg_index) 71 | export(calpha.test) 72 | export(chisq.test) 73 | export(cloglog) 74 | export(clump) 75 | export(count) 76 | export(count_data) 77 | export(dbetabinom) 78 | export(fit_two_distr) 79 | export(incidence) 80 | export(incidence_data) 81 | export(ind2sub) 82 | export(is.count) 83 | export(is.incidence) 84 | export(is.intensity) 85 | export(is.severity) 86 | export(logit) 87 | export(mapcomp) 88 | export(mapped_var) 89 | export(mapping) 90 | export(mapping_) 91 | export(pbetabinom) 92 | export(power_law) 93 | export(probit) 94 | export(qbetabinom) 95 | export(rbetabinom) 96 | export(remap) 97 | export(sadie) 98 | export(severity) 99 | export(severity_data) 100 | export(smle) 101 | export(smle_betabinom) 102 | export(smle_binom) 103 | export(smle_nbinom) 104 | export(smle_pois) 105 | export(spatial_hier) 106 | export(sub2ind) 107 | export(threshold) 108 | export(z.test) 109 | import(ggplot2) 110 | import(stats) 111 | importFrom(Rcpp,sourceCpp) 112 | importFrom(grDevices,terrain.colors) 113 | importFrom(methods,is) 114 | importFrom(utils,head) 115 | importFrom(utils,tail) 116 | useDynLib(epiphy) 117 | -------------------------------------------------------------------------------- /man/tomato_tswv.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{tomato_tswv} 5 | \alias{tomato_tswv} 6 | \title{Incidence of tomato spotted wilt virus (TSWV) disease in field trials.} 7 | \format{ 8 | There are two data frames: 9 | 10 | \code{field_1928}: A data frame with 11088 rows and 8 variables: 11 | \tabular{rll}{ 12 | [, 1] \tab plot \tab Plot id. \cr 13 | [, 2] \tab variety \tab Variety name. \cr 14 | [, 3] \tab irrigation \tab Irrigation system. \cr 15 | [, 4:5] \tab x,y \tab Grid spatial coordinates. \cr 16 | [, 6] \tab t \tab Date of disease assessments. 1: 6 Nov, 2: 14 17 | Nov, 3: 21 Nov, 4: 28-29 Nov, 5: 5 Dec, 6: 18 | 12 Dec 1928. \cr 19 | [, 7] \tab i \tab Disease incidence. 0: Healthy, 1: Diseased. \cr 20 | [, 8] \tab n \tab Sampling unit size. n = 1 means that the 21 | sampling unit size is the plant. \cr 22 | } 23 | 24 | \code{field_1929}: A data frame with 4320 rows and 5 variables: 25 | \tabular{rll}{ 26 | [, 1:2] \tab x,y \tab Grid spatial coordinates. \cr 27 | [, 3] \tab t \tab Date of disease assessments. 1: 18 Dec, 2: 31 Dec 28 | 1929, 3: 22 Jan 1930. \cr 29 | [, 4] \tab i \tab Disease incidence. 0: Healthy, 1: Diseased. \cr 30 | [, 5] \tab n \tab Sampling unit size. n = 1 means that the sampling 31 | unit size is the plant. \cr 32 | } 33 | } 34 | \source{ 35 | Cochran WG. 1936. The statistical analysis of field counts of 36 | diseased plants. Supplement to the Journal of the Royal Statistical 37 | Society 3, 49–67. \doi{10.2307/2983677} 38 | 39 | Bald JG. 1937. Investigations on "spotted wilt" of tomatoes. III. 40 | Infection in field plots. Bulletin 106. Melbourne, Australia: Council for 41 | Scientific and Industrial Research. 42 | } 43 | \usage{ 44 | tomato_tswv 45 | } 46 | \description{ 47 | Intensively mapped TSWV incidence data reported by Cochran (1936) and Bald 48 | (1937). The disease assessments were performed in field trials at the Waite 49 | Institute (Australia) in 1928 and 1929. TSWV is a virus disease spread by 50 | thrips. 51 | } 52 | \details{ 53 | The data set \code{field_1928}, reported by Bald (1937), was a set of four 54 | plots. Each plot consisted of 14 rows containing 33 plants each, so that 55 | there were 462 plants in each plot. The tomato variety Early Dwarf Red was 56 | used in two plots, and the variety Burwood Prize in the other two. The 57 | tomatoes were planted out on 15th October 1928. The two plots dedicated to a 58 | given variety experienced different irrigation practices, using either 59 | overhead sprays or trenches. Otherwise, all were treated alike. Weekly 60 | records of TSWV incidence were performed from 6th November to 12th December. 61 | 62 | The data set \code{field_1929}, reported by Cochran (1936), was a field of 24 63 | rows containing 60 plants each, so that there were 1440 plants. The tomatoes 64 | were planted out in 26th November 1929. TSWV incidence records made on 18th 65 | December 1929, 31st December 1929 and 22nd January 1930 are reported in this 66 | data set. 67 | } 68 | \keyword{datasets} 69 | -------------------------------------------------------------------------------- /src/mapcomp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Square supported kernel: 5 | double kern(const Rcpp::NumericVector & site, const double constant = 20.3/4.0) { // # 20.3 # not good (aire sous la courb !!!!) 6 | double exponent(0.0); 7 | if ((-1 <= site(0)) && (site(0) <= 1) && // x coord. 8 | (-1 <= site(1)) && (site(1) <= 1)) { // y coord. 9 | //exponent = (1/(1 - std::pow(site(0), 2)) + 10 | // 1/(1 - std::pow(site(1), 2))); 11 | exponent = (1/(1 - (site(0) * site(0))) + 12 | 1/(1 - (site(1) * site(1)))); 13 | return constant * std::exp(-exponent); 14 | } else { 15 | return 0.0; 16 | } 17 | } 18 | 19 | // h-scaled and renormalized kernel: 20 | double kern_hscaled(const Rcpp::NumericVector & node, 21 | const Rcpp::NumericVector & site, 22 | const double bandwidth, const double denom) { 23 | Rcpp::NumericVector rdist(2); 24 | for (int i1 = 0; i1 < 2; ++i1) { 25 | rdist(i1) = (node(i1) - site(i1)) / bandwidth; 26 | } 27 | if (denom == 0) { 28 | // Without edge correction: 29 | return kern(rdist) / (bandwidth * bandwidth); 30 | } else { 31 | // With edge correction: 32 | return kern(rdist) / denom; 33 | } 34 | } 35 | 36 | // [[Rcpp::export]] 37 | Rcpp::NumericVector p_hscaled(const Rcpp::DataFrame & nodes, 38 | const Rcpp::DataFrame & sites, 39 | double bandwidth, bool edgeCorrection = false) { 40 | 41 | int nRowNodes(nodes.nrow()), nRowSites(sites.nrow()); 42 | Rcpp::NumericVector xNodes = nodes(0), yNodes = nodes(1); 43 | Rcpp::NumericVector xSites = sites(0), ySites = sites(1), iSites = sites(2); 44 | double subDenom(0.0), numer(0.0); 45 | Rcpp::NumericVector res(nRowNodes); 46 | // We only use the two first element of a node/site, i.e. the 47 | // coordinates x and y: 48 | Rcpp::NumericVector tmpNode(2), tmpSite(2), rdist(2); 49 | for (int i1 = 0; i1 < nRowNodes; ++i1) { 50 | numer = 0.0; 51 | for (int i2 = 0; i2 < nRowSites; ++i2) { 52 | tmpNode(0) = xNodes(i1); 53 | tmpNode(1) = yNodes(i1); 54 | tmpSite(0) = xSites(i2); 55 | tmpSite(1) = ySites(i2); 56 | //for (int i3 = 0; i3 < 2; ++i3) { 57 | // tmpNode(i3) = nodes.at(i1, i3); 58 | // tmpSite(i3) = sites.at(i2, i3); 59 | //} 60 | subDenom = 0.0; 61 | if (edgeCorrection) { 62 | for (int i4 = 0; i4 < nRowNodes; ++i4) { 63 | rdist(0) = (xNodes(i4) - tmpSite(0)) / bandwidth; 64 | rdist(1) = (yNodes(i4) - tmpSite(1)) / bandwidth; 65 | //for (int i3 = 0; i3 < 2; ++i3) { 66 | // rdist(i3) = (nodes.at(i4, i3) - tmpSite(i3)) / bandwidth; 67 | //} 68 | subDenom += kern(rdist); 69 | } 70 | } 71 | numer += kern_hscaled(tmpNode, tmpSite, bandwidth, subDenom) * iSites(i2);// sites.at(i2, 2); 72 | } 73 | double denom = Rcpp::sum(iSites);//Rcpp::sum(sites(Rcpp::_, 2)); 74 | res(i1) = numer / denom; 75 | } 76 | return res; 77 | } 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /man/BetaBinomial.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/betabinom.R 3 | \name{BetaBinomial} 4 | \alias{BetaBinomial} 5 | \alias{dbetabinom} 6 | \alias{pbetabinom} 7 | \alias{qbetabinom} 8 | \alias{rbetabinom} 9 | \title{The beta-binomial distribution.} 10 | \usage{ 11 | dbetabinom(x, size, prob, theta, shape1, shape2, log = FALSE) 12 | 13 | pbetabinom( 14 | q, 15 | size, 16 | prob, 17 | theta, 18 | shape1, 19 | shape2, 20 | lower.tail = TRUE, 21 | log.p = FALSE 22 | ) 23 | 24 | qbetabinom( 25 | p, 26 | size, 27 | prob, 28 | theta, 29 | shape1, 30 | shape2, 31 | lower.tail = TRUE, 32 | log.p = FALSE 33 | ) 34 | 35 | rbetabinom(n, size, prob, theta, shape1, shape2) 36 | } 37 | \arguments{ 38 | \item{x, q}{Vector of quantiles.} 39 | 40 | \item{size}{Number of trials.} 41 | 42 | \item{prob}{Probability of success on each trial.} 43 | 44 | \item{theta}{Aggregation parameter (theta = 1 / (shape1 + shape2)).} 45 | 46 | \item{shape1, shape2}{Shape parameters.} 47 | 48 | \item{log, log.p}{Logical; if TRUE, probabilities p are given as log(p).} 49 | 50 | \item{lower.tail}{Logical; if TRUE (default), probabilities are 51 | \eqn{P[X \le x]} otherwise, \eqn{P[X > x]}.} 52 | 53 | \item{p}{Vector of probabilities.} 54 | 55 | \item{n}{Number of observations.} 56 | } 57 | \value{ 58 | \code{dbetabinom} gives the density, \code{pbetabinom} gives the distribution 59 | function, \code{qbetabinom} gives the quantile function and \code{rbetabinom} 60 | generates random deviates. 61 | } 62 | \description{ 63 | Density, distribution function, quantile function and random generation for 64 | the beta-binomial distribution with parameters \code{size}, \code{prob}, 65 | \code{theta}, \code{shape1}, \code{shape2}. This distribution corresponds to 66 | an overdispersed binomial distribution. 67 | } 68 | \details{ 69 | Be aware that in this implementation \code{theta} = 1 / (\code{shape1} + 70 | \code{shape2}). \code{prob} and \code{theta}, or \code{shape1} and 71 | \code{shape2} must be specified. if \code{theta} = 0, use *binom family 72 | instead. 73 | } 74 | \examples{ 75 | # Compute P(25 < X < 50) for X following the Beta-Binomial distribution 76 | # with parameters size = 100, prob = 0.5 and theta = 0.35: 77 | sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = 0.35)) 78 | 79 | # When theta tends to 0, dbetabinom outputs tends to dbinom outputs: 80 | sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = 1e-7)) 81 | sum(dbetabinom(25:50, size = 100, shape1 = 1e7, shape2 = 1e7)) 82 | sum(dbinom(25:50, size = 100, prob = 0.5)) 83 | 84 | # Example of binomial and beta-binomial frequency distributions: 85 | n <- 15 86 | q <- 0:n 87 | p1 <- dbinom(q, size = n, prob = 0.33) 88 | p2 <- dbetabinom(q, size = n, prob = 0.33, theta = 0.22) 89 | res <- rbind(p1, p2) 90 | dimnames(res) <- list(c("Binomial", "Beta-binomial"), q) 91 | barplot(res, beside = TRUE, legend.text = TRUE, ylab = "Frequency") 92 | 93 | # Effect of the aggregation parameter theta on probability density: 94 | thetas <- seq(0.001, 2.5, by = 0.001) 95 | density1 <- rep(sum(dbinom(25:50, size = 100, prob = 0.5)), length(thetas)) 96 | density2 <- sapply(thetas, function(theta) { 97 | sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = theta)) 98 | }) 99 | plot(thetas, density2, type = "l", 100 | xlab = expression("Aggregation parameter ("*theta*")"), 101 | ylab = "Probability density between 25 and 50 (size = 100)") 102 | lines(thetas, density1, lty = 2) 103 | 104 | } 105 | \seealso{ 106 | \code{\link[emdbook]{dbetabinom}} in the package \strong{emdbook} 107 | where the definition of theta is different. 108 | } 109 | -------------------------------------------------------------------------------- /src/RcppExports.cpp: -------------------------------------------------------------------------------- 1 | // Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #include 5 | 6 | using namespace Rcpp; 7 | 8 | #ifdef RCPP_USE_GLOBAL_ROSTREAM 9 | Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); 10 | Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); 11 | #endif 12 | 13 | // p_hscaled 14 | Rcpp::NumericVector p_hscaled(const Rcpp::DataFrame& nodes, const Rcpp::DataFrame& sites, double bandwidth, bool edgeCorrection); 15 | RcppExport SEXP _epiphy_p_hscaled(SEXP nodesSEXP, SEXP sitesSEXP, SEXP bandwidthSEXP, SEXP edgeCorrectionSEXP) { 16 | BEGIN_RCPP 17 | Rcpp::RObject rcpp_result_gen; 18 | Rcpp::RNGScope rcpp_rngScope_gen; 19 | Rcpp::traits::input_parameter< const Rcpp::DataFrame& >::type nodes(nodesSEXP); 20 | Rcpp::traits::input_parameter< const Rcpp::DataFrame& >::type sites(sitesSEXP); 21 | Rcpp::traits::input_parameter< double >::type bandwidth(bandwidthSEXP); 22 | Rcpp::traits::input_parameter< bool >::type edgeCorrection(edgeCorrectionSEXP); 23 | rcpp_result_gen = Rcpp::wrap(p_hscaled(nodes, sites, bandwidth, edgeCorrection)); 24 | return rcpp_result_gen; 25 | END_RCPP 26 | } 27 | // costTotiCPP 28 | double costTotiCPP(long i1, const Rcpp::NumericMatrix& flow, const Rcpp::NumericMatrix& cost, bool averaged, bool absolute); 29 | RcppExport SEXP _epiphy_costTotiCPP(SEXP i1SEXP, SEXP flowSEXP, SEXP costSEXP, SEXP averagedSEXP, SEXP absoluteSEXP) { 30 | BEGIN_RCPP 31 | Rcpp::RObject rcpp_result_gen; 32 | Rcpp::RNGScope rcpp_rngScope_gen; 33 | Rcpp::traits::input_parameter< long >::type i1(i1SEXP); 34 | Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type flow(flowSEXP); 35 | Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type cost(costSEXP); 36 | Rcpp::traits::input_parameter< bool >::type averaged(averagedSEXP); 37 | Rcpp::traits::input_parameter< bool >::type absolute(absoluteSEXP); 38 | rcpp_result_gen = Rcpp::wrap(costTotiCPP(i1, flow, cost, averaged, absolute)); 39 | return rcpp_result_gen; 40 | END_RCPP 41 | } 42 | // costTotCPP 43 | Rcpp::NumericVector costTotCPP(const Rcpp::NumericMatrix& flow, const Rcpp::NumericMatrix& cost); 44 | RcppExport SEXP _epiphy_costTotCPP(SEXP flowSEXP, SEXP costSEXP) { 45 | BEGIN_RCPP 46 | Rcpp::RObject rcpp_result_gen; 47 | Rcpp::RNGScope rcpp_rngScope_gen; 48 | Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type flow(flowSEXP); 49 | Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type cost(costSEXP); 50 | rcpp_result_gen = Rcpp::wrap(costTotCPP(flow, cost)); 51 | return rcpp_result_gen; 52 | END_RCPP 53 | } 54 | // as_matrix_transport 55 | Rcpp::NumericMatrix as_matrix_transport(Rcpp::List& x, double dim_mat); 56 | RcppExport SEXP _epiphy_as_matrix_transport(SEXP xSEXP, SEXP dim_matSEXP) { 57 | BEGIN_RCPP 58 | Rcpp::RObject rcpp_result_gen; 59 | Rcpp::RNGScope rcpp_rngScope_gen; 60 | Rcpp::traits::input_parameter< Rcpp::List& >::type x(xSEXP); 61 | Rcpp::traits::input_parameter< double >::type dim_mat(dim_matSEXP); 62 | rcpp_result_gen = Rcpp::wrap(as_matrix_transport(x, dim_mat)); 63 | return rcpp_result_gen; 64 | END_RCPP 65 | } 66 | 67 | static const R_CallMethodDef CallEntries[] = { 68 | {"_epiphy_p_hscaled", (DL_FUNC) &_epiphy_p_hscaled, 4}, 69 | {"_epiphy_costTotiCPP", (DL_FUNC) &_epiphy_costTotiCPP, 5}, 70 | {"_epiphy_costTotCPP", (DL_FUNC) &_epiphy_costTotCPP, 2}, 71 | {"_epiphy_as_matrix_transport", (DL_FUNC) &_epiphy_as_matrix_transport, 2}, 72 | {NULL, NULL, 0} 73 | }; 74 | 75 | RcppExport void R_init_epiphy(DllInfo *dll) { 76 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 77 | R_useDynamicSymbols(dll, FALSE); 78 | } 79 | -------------------------------------------------------------------------------- /man/mapcomp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mapcomp.R 3 | \name{mapcomp} 4 | \alias{mapcomp} 5 | \alias{mapcomp.data.frame} 6 | \alias{mapcomp.matrix} 7 | \alias{mapcomp.count} 8 | \alias{mapcomp.incidence} 9 | \title{Map Comparison procedure.} 10 | \usage{ 11 | mapcomp(data, ...) 12 | 13 | \method{mapcomp}{data.frame}( 14 | data, 15 | delta, 16 | bandwidth, 17 | nperm = 100, 18 | edge_correction = FALSE, 19 | threads = 1, 20 | verbose = TRUE, 21 | ... 22 | ) 23 | 24 | \method{mapcomp}{matrix}( 25 | data, 26 | delta, 27 | bandwidth, 28 | nperm = 100, 29 | edge_correction = FALSE, 30 | threads = 1, 31 | verbose = TRUE, 32 | ... 33 | ) 34 | 35 | \method{mapcomp}{count}( 36 | data, 37 | delta, 38 | bandwidth, 39 | nperm = 100, 40 | edge_correction = FALSE, 41 | threads = 1, 42 | verbose = TRUE, 43 | ... 44 | ) 45 | 46 | \method{mapcomp}{incidence}( 47 | data, 48 | delta, 49 | bandwidth, 50 | nperm = 100, 51 | edge_correction = FALSE, 52 | threads = 1, 53 | verbose = TRUE, 54 | ... 55 | ) 56 | } 57 | \arguments{ 58 | \item{data}{A data frame or a matrix with only three columns: the two first 59 | ones must be the x and y coordinates of the sampling units, and the last 60 | one, the corresponding disease intensity observations. It can also be a 61 | \code{\link{count}} or an \code{\link{incidence}} object.} 62 | 63 | \item{...}{Additional arguments to be passed to other methods.} 64 | 65 | \item{delta}{Mesh size of the grid over the geographical domain of the 66 | sampling units used to compute the integral Hellinger distance between 67 | the probability density function of observations and the probability 68 | density function of sampling effort.} 69 | 70 | \item{bandwidth}{Bandwidth parameter for smoothing. It allows to test the 71 | spatial extent of heterogeneity if any.} 72 | 73 | \item{nperm}{Number of random permutations to assess probabilities.} 74 | 75 | \item{edge_correction}{Apply edge correction to account for the fact that 76 | bordering points intrinsically suffer from a lack of neighboring 77 | observation sites. FALSE by default.} 78 | 79 | \item{threads}{Number of threads to perform the computations.} 80 | 81 | \item{verbose}{Explain what is being done (TRUE by default).} 82 | } 83 | \value{ 84 | An object of class \code{mapcomp}, which is a list containing the following 85 | components: 86 | \tabular{ll}{ 87 | \code{data} \tab The input data. \cr 88 | \code{coord} \tab The coordinates and normalized intensity for each point of the full grid. \cr 89 | \code{object} \tab The class of \code{data}. \cr 90 | \code{bandwidth} \tab The \code{bandwidth} parameter. \cr 91 | \code{stat, pval} \tab The statistic and corresponding p-value (see references for more details). \cr 92 | } 93 | } 94 | \description{ 95 | \code{mapcomp} performs a spatial pattern analysis based on the calculation 96 | of a formal distance (the Hellinger distance) between the density map of 97 | count or incidence data, and the density map of sampling effort. Statistical 98 | tests of spatial homogeneity are based on permutations across sampling sites 99 | and on valuable properties of the Hellinger distance. 100 | } 101 | \examples{ 102 | set.seed(123) 103 | my_res <- mapcomp(codling_moths, delta = 1, bandwidth = 11, 104 | edge_correction = FALSE, nperm = 20) 105 | my_res 106 | plot(my_res) 107 | 108 | set.seed(123) 109 | my_count <- count(codling_moths, mapping(x = xm, y = ym)) 110 | my_res <- mapcomp(my_count, delta = 1, bandwidth = 11, 111 | edge_correction = FALSE, nperm = 20) 112 | my_res 113 | plot(my_res, bins = 10) 114 | 115 | } 116 | \references{ 117 | Lavigne C, Ricci B, Franck P, Senoussi R. 2010. Spatial analyses of 118 | ecological count data: A density map comparison approach. Basic and Applied 119 | Ecology. 11:734–742. 120 | } 121 | -------------------------------------------------------------------------------- /man/sadie.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sadie.R 3 | \name{sadie} 4 | \alias{sadie} 5 | \alias{sadie.data.frame} 6 | \alias{sadie.matrix} 7 | \alias{sadie.count} 8 | \alias{sadie.incidence} 9 | \title{Spatial Analysis by Distance IndicEs (SADIE).} 10 | \usage{ 11 | sadie(data, ...) 12 | 13 | \method{sadie}{data.frame}( 14 | data, 15 | index = c("Perry", "Li-Madden-Xu", "all"), 16 | nperm = 100, 17 | seed = NULL, 18 | threads = 1, 19 | ..., 20 | method = "shortsimplex", 21 | verbose = TRUE 22 | ) 23 | 24 | \method{sadie}{matrix}( 25 | data, 26 | index = c("Perry", "Li-Madden-Xu", "all"), 27 | nperm = 100, 28 | seed = NULL, 29 | threads = 1, 30 | ..., 31 | method = "shortsimplex", 32 | verbose = TRUE 33 | ) 34 | 35 | \method{sadie}{count}( 36 | data, 37 | index = c("Perry", "Li-Madden-Xu", "all"), 38 | nperm = 100, 39 | seed = NULL, 40 | threads = 1, 41 | ..., 42 | method = "shortsimplex", 43 | verbose = TRUE 44 | ) 45 | 46 | \method{sadie}{incidence}( 47 | data, 48 | index = c("Perry", "Li-Madden-Xu", "all"), 49 | nperm = 100, 50 | seed = NULL, 51 | threads = 1, 52 | ..., 53 | method = "shortsimplex", 54 | verbose = TRUE 55 | ) 56 | } 57 | \arguments{ 58 | \item{data}{A data frame or a matrix with only three columns: the two first 59 | ones must be the x and y coordinates of the sampling units, and the last 60 | one, the corresponding disease intensity observations. It can also be a 61 | \code{\link{count}} or an \code{\link{incidence}} object.} 62 | 63 | \item{...}{Additional arguments to be passed to other methods.} 64 | 65 | \item{index}{The index to be calculated: "Perry", "Li-Madden-Xu" or "all". 66 | By default, only Perry's index is computed for each sampling unit.} 67 | 68 | \item{nperm}{Number of random permutations to assess probabilities.} 69 | 70 | \item{seed}{Fixed seed to be used for randomizations (only useful for 71 | checking purposes). Not fixed by default (= NULL).} 72 | 73 | \item{threads}{Number of threads to perform the computations.} 74 | 75 | \item{method}{Method for the transportation algorithm.} 76 | 77 | \item{verbose}{Explain what is being done (TRUE by default).} 78 | } 79 | \value{ 80 | A \code{sadie} object. 81 | } 82 | \description{ 83 | \code{sadie} performs the SADIE procedure. It computes different indices and 84 | probabilities based on the distance to regularity for the observed spatial 85 | pattern and a specified number of random permutations of this pattern. Both 86 | kind of clustering indices described by Perry et al. (1999) and Li et al. 87 | (2012) can be computed. 88 | } 89 | \details{ 90 | By convention in the SADIE procedure, clustering indices for a donor unit 91 | (outflow) and a receiver unit (inflow) are positive and negative in sign, 92 | respectively. 93 | } 94 | \examples{ 95 | set.seed(123) 96 | # Create an intensity object: 97 | my_count <- count(aphids, mapping(x = xm, y = ym)) 98 | # Only compute Perry's indices: 99 | my_res <- sadie(my_count) 100 | my_res 101 | summary(my_res) 102 | plot(my_res) 103 | plot(my_res, isoclines = TRUE) 104 | 105 | set.seed(123) 106 | # Compute both Perry's and Li-Madden-Xu's indices (using multithreading): 107 | my_res <- sadie(my_count, index = "all", threads = 2, nperm = 20) 108 | my_res 109 | summary(my_res) 110 | plot(my_res) # Identical to: plot(my_res, index = "Perry") 111 | plot(my_res, index = "Li-Madden-Xu") 112 | 113 | set.seed(123) 114 | # Using usual data frames instead of intensity objects: 115 | my_df <- aphids[, c("xm", "ym", "i")] 116 | sadie(my_df) 117 | 118 | } 119 | \references{ 120 | Perry JN. 1995. Spatial analysis by distance indices. Journal of Animal 121 | Ecology 64, 303–314. \doi{10.2307/5892} 122 | 123 | Perry JN, Winder L, Holland JM, Alston RD. 1999. Red–blue plots for detecting 124 | clusters in count data. Ecology Letters 2, 106–113. 125 | \doi{10.1046/j.1461-0248.1999.22057.x} 126 | 127 | Li B, Madden LV, Xu X. 2012. Spatial analysis by distance indices: an 128 | alternative local clustering index for studying spatial patterns. Methods in 129 | Ecology and Evolution 3, 368–377. 130 | \doi{10.1111/j.2041-210X.2011.00165.x} 131 | } 132 | -------------------------------------------------------------------------------- /man/smle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle-factory.R 3 | \name{smle} 4 | \alias{smle} 5 | \alias{smle.default} 6 | \alias{smle.intensity} 7 | \title{Simple maximum likelihood estimation} 8 | \usage{ 9 | smle(data, ...) 10 | 11 | \method{smle}{default}(data, f, param_init, max = TRUE, ...) 12 | 13 | \method{smle}{intensity}(data, f, param_init, max = TRUE, ...) 14 | } 15 | \arguments{ 16 | \item{data}{The data set to work with. It can be a vector (if there is only 17 | one variable), a data frame (if there is one or more variables) or an 18 | \code{\link{intensity}} object.} 19 | 20 | \item{...}{Additional arguments to be passed to \code{\link[stats]{optim}}.} 21 | 22 | \item{f}{A function to be maximized, typically a log-likelihood function. 23 | This function must have only two arguments: \code{data} and \code{param}, 24 | which must correspond to the \code{data} argument of \code{smle} and a 25 | named vector of the parameter(s) to be estimated.} 26 | 27 | \item{param_init}{Either a named vector with proposed initial values of the 28 | parameter(s), or a function that returns such a vector. This parameter 29 | is not needed if the parameter \code{param} of \code{f} is already 30 | provided with such a named vector.} 31 | 32 | \item{max}{Does \code{f} need to be maximized? Set to \code{FALSE} to require 33 | a minimization of \code{f}.} 34 | } 35 | \value{ 36 | An object of class \code{smle}, which is a list containing the following 37 | components: 38 | \tabular{ll}{ 39 | \code{call} \tab The call. \cr 40 | \code{coef} \tab The estimated coefficients. \cr 41 | \code{coef_se} \tab The standard errors of the estimated coefficients. \cr 42 | \code{vcov} \tab The variance-covariance matrix of the estimated coefficients. \cr 43 | \code{data} \tab The \code{data} parameter. \cr 44 | \code{f} \tab The \code{f} parameter. \cr 45 | \code{nobs} \tab The number of observations. \cr 46 | \code{full_input, full_output} \tab The full input and output of the \code{optim} function. \cr 47 | } 48 | } 49 | \description{ 50 | By default, this function performs a maximum likelihood estimation for one or 51 | several parameters, but it can be used for any other optimization problems. 52 | The interface is intented to be rather simple while allowing more advanced 53 | parametrizations. 54 | } 55 | \details{ 56 | The \code{\link[stats]{optim}} tool does the hard work under the hood. Extra 57 | arguments (e.g. method of optimization to be used) can be passed to 58 | \code{\link[stats]{optim}} through the \code{...} argument. Note that 59 | contrary to the default \code{\link[stats]{optim}} arguments, \code{smle} 60 | tries to solve a maximization problem using the method "L-BFGS-B" by default 61 | (see \code{\link[stats]{optim}} documentation for more information). 62 | } 63 | \examples{ 64 | set.seed(12345) 65 | data <- rlogis(100, location = 5, scale = 2) 66 | ll_logis <- function(data, param = c(location = 0, scale = 1)) { 67 | sum(dlogis(x = data, location = param[["location"]], 68 | scale = param[["scale"]], log = TRUE)) 69 | } 70 | res <- smle(data, ll_logis) 71 | res 72 | summary(res) 73 | 74 | # Using the magrittr syntax: 75 | require(magrittr) 76 | data \%>\% smle(ll_logis) 77 | 78 | # Comparision with the output of fitdistr (MASS package), which works for a 79 | # limited number of predefined distributions: 80 | require(MASS) 81 | fitdistr(data, "logistic") 82 | 83 | # Example with an intensity object: 84 | require(magrittr) 85 | require(dplyr) 86 | data <- tomato_tswv$field_1929 \%>\% 87 | filter(t == 1) \%>\% 88 | incidence() \%>\% 89 | clump(unit_size = c(x = 3, y = 3)) 90 | ll_betabinom <- function(data, param) { 91 | sum(dbetabinom(x = data[["i"]], size = data[["n"]], 92 | prob = param[["prob"]], theta = param[["theta"]], 93 | log = TRUE)) 94 | } 95 | epsilon <- 1e-7 96 | res <- smle(data, ll_betabinom, param_init = c(prob = 0.5, theta = 0.5), 97 | lower = c(prob = 0 + epsilon, 98 | theta = 0 + epsilon), 99 | upper = c(prob = 1 - epsilon, 100 | theta = Inf)) 101 | res 102 | summary(res) 103 | 104 | param_init <- data.frame(lower = c(0 + epsilon, 0 + epsilon), 105 | start = c(0.5, 0.5), 106 | upper = c(1 - epsilon, Inf)) 107 | rownames(param_init) <- c("prob", "theta") 108 | res <- smle(data, ll_betabinom, param_init) 109 | res 110 | summary(res) 111 | 112 | } 113 | \keyword{internal} 114 | -------------------------------------------------------------------------------- /man/agg_index.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/indices.R 3 | \name{agg_index} 4 | \alias{agg_index} 5 | \title{Several aggregation indices.} 6 | \usage{ 7 | agg_index( 8 | x, 9 | method = c("fisher", "lloyd", "morisita"), 10 | flavor = c("count", "incidence"), 11 | n = NULL, 12 | ... 13 | ) 14 | } 15 | \arguments{ 16 | \item{x}{A numeric vector or a \code{count}/\code{incidence} object.} 17 | 18 | \item{method}{The name of the method to be used. "fisher" method is used by 19 | default. See details below.} 20 | 21 | \item{flavor}{Which flavor of this index must be calculated ("count" or 22 | "incidence")?} 23 | 24 | \item{n}{Number of individuals per sampling unit. If \code{n} is provided, 25 | the "incidence" flavor is calculated whatever the value of \code{flavor}. 26 | Note that current implementation only deals with equal size sampling 27 | units.} 28 | 29 | \item{...}{Additional arguments to be passed to other methods.} 30 | } 31 | \value{ 32 | An object of class \code{agg_index}, which is a list containing the following 33 | components: 34 | \tabular{ll}{ 35 | \code{index} \tab The value of the index. \cr 36 | \code{name} \tab The name of the index. \cr 37 | \code{flavor} \tab The flavor of the calculated index ("count" or "incidence"). \cr 38 | \code{N} \tab The number of sampling units. \cr 39 | \code{n} \tab The number of individuals in each sampling unit (if relevant). \cr 40 | } 41 | } 42 | \description{ 43 | This function can compute different aggregation indices. See "Details" 44 | section for more information about the available indices. 45 | } 46 | \details{ 47 | There are currently three implemented methods to compute indices of 48 | aggregation. 49 | 50 | \code{fisher}: Fisher's index of aggregation. In case of a count, this index 51 | corresponds to the ratio of the observed variance to the observed mean, and 52 | this is why this index is also known as the variance to mean ratio. For a 53 | binary variable, a similar index can be calculated using instead the ratio of 54 | the observed variance to the theoretical variance if data follow a binomial 55 | law (i.e. a reference distribution for a random pattern of individuals within 56 | sampling units). 57 | 58 | \code{lloyd}: Lloyd's index of patchiness. The value of this index increases 59 | with the degree of aggregation. Note that Lloyd's mean crowding can also be 60 | returned if \code{type = "mean-crowding"} is provided as parameter. 61 | 62 | \code{morisita}: Morisita's coefficient of dispersion. This index can be 63 | computed for either count or incidence data, but its interpretation can be 64 | uncertain. 65 | 66 | Values of Fisher's and Lloyd's indices can be interpreted as follows: 67 | \itemize{ 68 | \item index < 1: uniform pattern; 69 | \item index = 1: random pattern; 70 | \item index > 1: aggregated pattern. 71 | } 72 | 73 | The following table gives information about the applicability of the various 74 | methods. 75 | 76 | \tabular{llll}{ 77 | \tab count \tab incidence \tab severity \cr 78 | fisher \tab + \tab + \tab - \cr 79 | lloyd \tab + \tab - \tab - \cr 80 | morisita \tab + \tab + \tab - \cr 81 | } 82 | where + means implemented, and -, not implemented (or not possible). At the 83 | moment, there is no index of aggregation for severity data. 84 | } 85 | \examples{ 86 | # Count flavor of Fisher's index: 87 | my_fisher_count <- agg_index(aphids$i) 88 | my_fisher_count 89 | 90 | # And incidence flavor of Fisher's index: 91 | my_fisher_incidence <- agg_index(tobacco_viruses$i, n = tobacco_viruses$n) 92 | my_fisher_incidence 93 | 94 | # Either standard R or epiphy idioms can be used: 95 | identical(my_fisher_count, agg_index(count(aphids))) 96 | identical(my_fisher_incidence, agg_index(incidence(tobacco_viruses))) 97 | 98 | # Lloyd's index (only for count data): 99 | agg_index(aphids$i, method = "lloyd") 100 | # Lloyd's mean crowding: 101 | agg_index(aphids$i, method = "lloyd", type = "mean-crowding") 102 | 103 | # Count flavor of Morisita's index: 104 | agg_index(aphids$i, method = "morisita") 105 | # Incidence flavor of Morisita's index: 106 | agg_index(tobacco_viruses$i, n = tobacco_viruses$n, method = "morisita") 107 | 108 | } 109 | \references{ 110 | Fisher RA. 1925. Statistical methods for research workers. Oliver and Boyd, 111 | Edinburgh. 112 | 113 | Lloyd M. 1967. Mean crowding. The Journal of Animal Ecology 36, 1–30. 114 | 115 | Morisita M. 1962. I\eqn{\delta}-Index, a measure of dispersion of 116 | individuals. Researches on Population Ecology 4, 1–7. 117 | \doi{10.1007/BF02533903} 118 | 119 | Madden LV, Hughes G. 1995. Plant disease incidence: Distributions, 120 | heterogeneity, and temporal analysis. Annual Review of Phytopathology 33(1): 121 | 529–564. 122 | \doi{10.1146/annurev.py.33.090195.002525} 123 | } 124 | \seealso{ 125 | \code{\link[vegan]{vegdist}} in \strong{vegan} package. 126 | } 127 | -------------------------------------------------------------------------------- /man/fit_two_distr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/distr-fitting.R 3 | \name{fit_two_distr} 4 | \alias{fit_two_distr} 5 | \alias{fit_two_distr.default} 6 | \alias{fit_two_distr.count} 7 | \alias{fit_two_distr.incidence} 8 | \title{Maximum likelihood fitting of two distributions and goodness-of-fit 9 | comparison.} 10 | \usage{ 11 | fit_two_distr(data, ...) 12 | 13 | \method{fit_two_distr}{default}(data, random, aggregated, ...) 14 | 15 | \method{fit_two_distr}{count}( 16 | data, 17 | random = smle_pois, 18 | aggregated = smle_nbinom, 19 | n_est = c(random = 1, aggregated = 2), 20 | ... 21 | ) 22 | 23 | \method{fit_two_distr}{incidence}( 24 | data, 25 | random = smle_binom, 26 | aggregated = smle_betabinom, 27 | n_est = c(random = 1, aggregated = 2), 28 | ... 29 | ) 30 | } 31 | \arguments{ 32 | \item{data}{An \code{intensity} object.} 33 | 34 | \item{...}{Additional arguments to be passed to other methods.} 35 | 36 | \item{random}{Distribution to describe random patterns.} 37 | 38 | \item{aggregated}{Distribution to describe aggregated patterns.} 39 | 40 | \item{n_est}{Number of estimated parameters for both distributions.} 41 | } 42 | \value{ 43 | An object of class \code{fit_two_distr}, which is a list containing at least 44 | the following components: 45 | \tabular{ll}{ 46 | \code{call} \tab The function \code{\link[base]{call}}. \cr 47 | \code{name} \tab The names of both distributions. \cr 48 | \code{model} \tab The outputs of fitting process for both distributions. \cr 49 | \code{llr} \tab The result of the log-likelihood ratio test. \cr 50 | } 51 | Other components can be present such as: 52 | \tabular{ll}{ 53 | \code{param} \tab A numeric matrix of estimated parameters (that can be 54 | printed using \code{\link[stats]{printCoefmat}}). \cr 55 | \code{freq} \tab A data frame or a matrix with the observed and expected 56 | frequencies for both distributions for the different 57 | categories. \cr 58 | \code{gof} \tab Goodness-of-fit tests for both distributions (which are 59 | typically chi-squared goodness-of-fit tests). \cr 60 | } 61 | } 62 | \description{ 63 | Different distributions may be used depending on the kind of provided data. 64 | By default, the Poisson and negative binomial distributions are fitted to 65 | count data, whereas the binomial and beta-binomial distributions are used 66 | with incidence data. Either Randomness assumption (Poisson or binomial 67 | distributions) or aggregation assumption (negative binomial or beta-binomial) 68 | are made, and then, a goodness-of-fit comparison of both distributions is 69 | made using a log-likelihood ratio test. 70 | } 71 | \details{ 72 | Under the hood, \code{distr_fit} relies on the \code{\link{smle}} utility 73 | which is a wrapped around the \code{\link[stats]{optim}} procedure. 74 | 75 | Note that there may appear warnings about chi-squared goodness-of-fit tests 76 | if any expected count is less than 5 (Cochran's rule of thumb). 77 | } 78 | \examples{ 79 | # Simple workflow for incidence data: 80 | my_data <- count(arthropods) 81 | my_data <- split(my_data, by = "t")[[3]] 82 | my_res <- fit_two_distr(my_data) 83 | summary(my_res) 84 | plot(my_res) 85 | 86 | # Simple workflow for incidence data: 87 | my_data <- incidence(tobacco_viruses) 88 | my_res <- fit_two_distr(my_data) 89 | summary(my_res) 90 | plot(my_res) 91 | 92 | # Note that there are other methods to fit some common distributions. 93 | # For example for the Poisson distribution, one can use glm: 94 | my_arthropods <- arthropods[arthropods$t == 3, ] 95 | my_model <- glm(my_arthropods$i ~ 1, family = poisson) 96 | lambda <- exp(coef(my_model)[[1]]) # unique(my_model$fitted.values) works also. 97 | lambda 98 | # ... or the fitdistr function in MASS package: 99 | require(MASS) 100 | fitdistr(my_arthropods$i, "poisson") 101 | 102 | # For the binomial distribution, glm still works: 103 | my_model <- with(tobacco_viruses, glm(i/n ~ 1, family = binomial, weights = n)) 104 | prob <- logit(coef(my_model)[[1]], rev = TRUE) 105 | prob 106 | # ... but the binomial distribution is not yet recognized by MASS::fitdistr. 107 | 108 | # Examples featured in Madden et al. (2007). 109 | # p. 242-243 110 | my_data <- incidence(dogwood_anthracnose) 111 | my_data <- split(my_data, by = "t") 112 | my_fit_two_distr <- lapply(my_data, fit_two_distr) 113 | lapply(my_fit_two_distr, function(x) x$param$aggregated[c("prob", "theta"), ]) 114 | lapply(my_fit_two_distr, plot) 115 | 116 | my_agg_index <- lapply(my_data, agg_index) 117 | lapply(my_agg_index, function(x) x$index) 118 | lapply(my_agg_index, chisq.test) 119 | 120 | } 121 | \references{ 122 | Madden LV, Hughes G. 1995. Plant disease incidence: Distributions, 123 | heterogeneity, and temporal analysis. Annual Review of Phytopathology 33(1): 124 | 529–564. 125 | \doi{10.1146/annurev.py.33.090195.002525} 126 | } 127 | -------------------------------------------------------------------------------- /data-raw/dogwood_anthracnose.csv: -------------------------------------------------------------------------------- 1 | t,i,n 2 | 1990,0,10 3 | 1990,0,10 4 | 1990,0,10 5 | 1990,0,10 6 | 1990,0,10 7 | 1990,0,10 8 | 1990,0,10 9 | 1990,0,10 10 | 1990,0,10 11 | 1990,0,10 12 | 1990,0,10 13 | 1990,0,10 14 | 1990,0,10 15 | 1990,0,10 16 | 1990,0,10 17 | 1990,0,10 18 | 1990,0,10 19 | 1990,0,10 20 | 1990,0,10 21 | 1990,0,10 22 | 1990,0,10 23 | 1990,0,10 24 | 1990,0,10 25 | 1990,0,10 26 | 1990,0,10 27 | 1990,0,10 28 | 1990,0,10 29 | 1990,0,10 30 | 1990,0,10 31 | 1990,0,10 32 | 1990,0,10 33 | 1990,0,10 34 | 1990,0,10 35 | 1990,0,10 36 | 1990,0,10 37 | 1990,0,10 38 | 1990,0,10 39 | 1990,0,10 40 | 1990,0,10 41 | 1990,0,10 42 | 1990,0,10 43 | 1990,0,10 44 | 1990,0,10 45 | 1990,0,10 46 | 1990,0,10 47 | 1990,0,10 48 | 1990,0,10 49 | 1990,0,10 50 | 1990,0,10 51 | 1990,0,10 52 | 1990,0,10 53 | 1990,0,10 54 | 1990,0,10 55 | 1990,0,10 56 | 1990,0,10 57 | 1990,0,10 58 | 1990,0,10 59 | 1990,0,10 60 | 1990,0,10 61 | 1990,0,10 62 | 1990,0,10 63 | 1990,0,10 64 | 1990,0,10 65 | 1990,0,10 66 | 1990,0,10 67 | 1990,0,10 68 | 1990,0,10 69 | 1990,0,10 70 | 1990,0,10 71 | 1990,0,10 72 | 1990,0,10 73 | 1990,0,10 74 | 1990,0,10 75 | 1990,0,10 76 | 1990,0,10 77 | 1990,0,10 78 | 1990,0,10 79 | 1990,0,10 80 | 1990,0,10 81 | 1990,0,10 82 | 1990,0,10 83 | 1990,0,10 84 | 1990,0,10 85 | 1990,0,10 86 | 1990,0,10 87 | 1990,0,10 88 | 1990,0,10 89 | 1990,0,10 90 | 1990,0,10 91 | 1990,0,10 92 | 1990,0,10 93 | 1990,0,10 94 | 1990,1,10 95 | 1990,1,10 96 | 1990,1,10 97 | 1990,1,10 98 | 1990,1,10 99 | 1990,1,10 100 | 1990,1,10 101 | 1990,1,10 102 | 1990,1,10 103 | 1990,1,10 104 | 1990,1,10 105 | 1990,1,10 106 | 1990,1,10 107 | 1990,1,10 108 | 1990,1,10 109 | 1990,2,10 110 | 1990,2,10 111 | 1990,2,10 112 | 1990,2,10 113 | 1990,2,10 114 | 1990,2,10 115 | 1990,2,10 116 | 1990,2,10 117 | 1990,2,10 118 | 1990,2,10 119 | 1990,2,10 120 | 1990,2,10 121 | 1990,2,10 122 | 1990,2,10 123 | 1990,2,10 124 | 1990,2,10 125 | 1990,3,10 126 | 1990,3,10 127 | 1990,3,10 128 | 1990,3,10 129 | 1990,3,10 130 | 1990,3,10 131 | 1990,3,10 132 | 1990,3,10 133 | 1990,3,10 134 | 1990,3,10 135 | 1990,3,10 136 | 1990,3,10 137 | 1990,3,10 138 | 1990,4,10 139 | 1990,4,10 140 | 1990,4,10 141 | 1990,4,10 142 | 1990,4,10 143 | 1990,4,10 144 | 1990,5,10 145 | 1990,5,10 146 | 1990,5,10 147 | 1990,5,10 148 | 1990,5,10 149 | 1990,5,10 150 | 1990,5,10 151 | 1990,5,10 152 | 1990,5,10 153 | 1990,6,10 154 | 1990,6,10 155 | 1990,6,10 156 | 1990,6,10 157 | 1990,6,10 158 | 1990,6,10 159 | 1990,6,10 160 | 1990,6,10 161 | 1990,6,10 162 | 1990,7,10 163 | 1990,7,10 164 | 1990,7,10 165 | 1990,7,10 166 | 1990,7,10 167 | 1990,8,10 168 | 1990,8,10 169 | 1990,9,10 170 | 1991,0,10 171 | 1991,0,10 172 | 1991,0,10 173 | 1991,0,10 174 | 1991,0,10 175 | 1991,0,10 176 | 1991,0,10 177 | 1991,0,10 178 | 1991,0,10 179 | 1991,0,10 180 | 1991,0,10 181 | 1991,0,10 182 | 1991,0,10 183 | 1991,0,10 184 | 1991,0,10 185 | 1991,0,10 186 | 1991,0,10 187 | 1991,0,10 188 | 1991,0,10 189 | 1991,0,10 190 | 1991,0,10 191 | 1991,0,10 192 | 1991,0,10 193 | 1991,0,10 194 | 1991,0,10 195 | 1991,0,10 196 | 1991,0,10 197 | 1991,0,10 198 | 1991,0,10 199 | 1991,0,10 200 | 1991,0,10 201 | 1991,0,10 202 | 1991,0,10 203 | 1991,0,10 204 | 1991,0,10 205 | 1991,0,10 206 | 1991,0,10 207 | 1991,0,10 208 | 1991,0,10 209 | 1991,0,10 210 | 1991,0,10 211 | 1991,0,10 212 | 1991,0,10 213 | 1991,0,10 214 | 1991,0,10 215 | 1991,0,10 216 | 1991,0,10 217 | 1991,0,10 218 | 1991,0,10 219 | 1991,0,10 220 | 1991,0,10 221 | 1991,0,10 222 | 1991,0,10 223 | 1991,0,10 224 | 1991,0,10 225 | 1991,0,10 226 | 1991,0,10 227 | 1991,0,10 228 | 1991,0,10 229 | 1991,0,10 230 | 1991,0,10 231 | 1991,0,10 232 | 1991,0,10 233 | 1991,1,10 234 | 1991,1,10 235 | 1991,1,10 236 | 1991,1,10 237 | 1991,1,10 238 | 1991,1,10 239 | 1991,1,10 240 | 1991,1,10 241 | 1991,1,10 242 | 1991,1,10 243 | 1991,1,10 244 | 1991,1,10 245 | 1991,1,10 246 | 1991,1,10 247 | 1991,1,10 248 | 1991,1,10 249 | 1991,1,10 250 | 1991,1,10 251 | 1991,1,10 252 | 1991,1,10 253 | 1991,2,10 254 | 1991,2,10 255 | 1991,2,10 256 | 1991,2,10 257 | 1991,2,10 258 | 1991,2,10 259 | 1991,2,10 260 | 1991,2,10 261 | 1991,3,10 262 | 1991,3,10 263 | 1991,3,10 264 | 1991,3,10 265 | 1991,3,10 266 | 1991,3,10 267 | 1991,3,10 268 | 1991,3,10 269 | 1991,3,10 270 | 1991,4,10 271 | 1991,4,10 272 | 1991,4,10 273 | 1991,4,10 274 | 1991,5,10 275 | 1991,5,10 276 | 1991,5,10 277 | 1991,5,10 278 | 1991,5,10 279 | 1991,5,10 280 | 1991,5,10 281 | 1991,5,10 282 | 1991,5,10 283 | 1991,5,10 284 | 1991,5,10 285 | 1991,5,10 286 | 1991,6,10 287 | 1991,6,10 288 | 1991,6,10 289 | 1991,6,10 290 | 1991,6,10 291 | 1991,6,10 292 | 1991,7,10 293 | 1991,7,10 294 | 1991,7,10 295 | 1991,7,10 296 | 1991,7,10 297 | 1991,7,10 298 | 1991,7,10 299 | 1991,7,10 300 | 1991,7,10 301 | 1991,7,10 302 | 1991,7,10 303 | 1991,7,10 304 | 1991,7,10 305 | 1991,8,10 306 | 1991,8,10 307 | 1991,8,10 308 | 1991,8,10 309 | 1991,8,10 310 | 1991,8,10 311 | 1991,8,10 312 | 1991,9,10 313 | 1991,9,10 314 | 1991,9,10 315 | 1991,9,10 316 | 1991,9,10 317 | 1991,9,10 318 | 1991,9,10 319 | 1991,9,10 320 | 1991,9,10 321 | 1991,9,10 322 | 1991,9,10 323 | 1991,9,10 324 | 1991,10,10 325 | 1991,10,10 326 | 1991,10,10 327 | 1991,10,10 328 | 1991,10,10 329 | 1991,10,10 330 | 1991,10,10 331 | -------------------------------------------------------------------------------- /vignettes/biblio.bib: -------------------------------------------------------------------------------- 1 | @Manual{R_core_team_2015, 2 | title = {R: A Language and Environment for Statistical Computing}, 3 | author = {{R Core Team}}, 4 | organization = {R Foundation for Statistical Computing}, 5 | address = {Vienna, Austria}, 6 | year = {2015}, 7 | url = {https://www.R-project.org/}, 8 | } 9 | 10 | @article{McRoberts_etal_2003, 11 | title = {The theoretical basis and practical application of relationships between different disease intensity measurements in plants}, 12 | volume = {142}, 13 | issn = {1744-7348}, 14 | doi = {10.1111/j.1744-7348.2003.tb00242.x}, 15 | pages = {191--211}, 16 | number = {2}, 17 | journaltitle = {Annals of Applied Biology}, 18 | author = {McRoberts, N. and Hughes, G. and Madden, L. V.}, 19 | date = {2003}, 20 | } 21 | 22 | @article{Cochran_1936, 23 | title = {The statistical analysis of field counts of diseased plants}, 24 | volume = {3}, 25 | doi = {10.2307/2983677}, 26 | pages = {49--67}, 27 | number = {1}, 28 | journaltitle = {Supplement to the Journal of the Royal Statistical Society}, 29 | author = {Cochran, W. G.}, 30 | date = {1936}, 31 | } 32 | 33 | @book{Madden_etal_2007, 34 | title = {The study of plant disease epidemics}, 35 | publisher = {American Phytopathological Society St Paul, {MN}}, 36 | author = {Madden, L. V. and Hughes, G. and van den Bosch, F.}, 37 | date = {2007} 38 | } 39 | 40 | @article{Mak_1988, 41 | title = {Analysing intraclass correlation for dichotomous variables}, 42 | volume = {37}, 43 | pages = {344-352}, 44 | number = {3}, 45 | journaltitle = {Journal of the Royal Statistical Society. Series C (Applied Statistics)}, 46 | author = {Mak, T. K.}, 47 | date = {1988} 48 | } 49 | 50 | @article{Madden_Hughes_1994, 51 | title = {BBD --- Computer software for fitting the beta-binomial distribution to disease incidence data}, 52 | volume = {78}, 53 | pages = {536--540}, 54 | number = {5}, 55 | journaltitle = {Plant Disease}, 56 | author = {Madden, L. V. and Hughes, G.}, 57 | date = {1994}, 58 | } 59 | 60 | @article{Bald_1937, 61 | title = {Investigations on 'spotted wilt' of tomatoes. {III}. Infection in field plots}, 62 | volume = {Bulletin 106}, 63 | journaltitle = {Council for Scientific and Industrial Research (Melbourne, Australia)}, 64 | author = {Bald, J. G.}, 65 | date = {1937} 66 | } 67 | 68 | @article{Perry_etal_1999, 69 | title = {Red–blue plots for detecting clusters in count data}, 70 | volume = {2}, 71 | doi = {10.1046/j.1461-0248.1999.22057.x}, 72 | pages = {106--113}, 73 | number = {2}, 74 | journaltitle = {Ecology Letters}, 75 | author = {Perry, J. N. and Winder, L. and Holland, J. M. and Alston, R. D.}, 76 | date = {1999} 77 | } 78 | 79 | @article{Holland_etal_1999, 80 | title = {Arthropod prey of farmland birds: their spatial distribution within a sprayed field with and without buffer zones}, 81 | volume = {54}, 82 | pages = {53--60}, 83 | journaltitle = {Aspects of Applied Biology}, 84 | author = {Holland, J. M. and Winder, L. and Perry, J. N.}, 85 | date = {1999} 86 | } 87 | 88 | @article{Hughes_Madden_1993, 89 | title = {Using the beta-binomial distribution to describe aggegated patterns of disease incidence}, 90 | volume = {83}, 91 | pages = {759--763}, 92 | number = {7}, 93 | journaltitle = {Phytopathology}, 94 | author = {Hughes, G. and Madden, L. V.}, 95 | date = {1993} 96 | } 97 | 98 | @article{Madden_Hughes_1995, 99 | title = {Plant disease incidence: distributions, heterogeneity, and temporal analysis}, 100 | volume = {33}, 101 | doi = {10.1146/annurev.py.33.090195.002525}, 102 | pages = {529--564}, 103 | number = {1}, 104 | journaltitle = {Annual Review of Phytopathology}, 105 | author = {Madden, L. V. and Hughes, G.}, 106 | date = {1995} 107 | } 108 | 109 | @article{Taylor_1961, 110 | title = {Aggregation, variance and the mean}, 111 | volume = {189}, 112 | pages = {732--735}, 113 | journaltitle = {Nature}, 114 | author = {Taylor, L. R.}, 115 | date = {1961} 116 | } 117 | 118 | @article{Hughes_Madden_1992, 119 | title = {Aggregation and incidence of disease}, 120 | volume = {41}, 121 | doi = {10.1111/j.1365-3059.1992.tb02549.x}, 122 | pages = {657--660}, 123 | number = {6}, 124 | journaltitle = {Plant Pathology}, 125 | author = {Hughes, G. and Madden, L. V.}, 126 | date = {1992} 127 | } 128 | 129 | @article{Hughes_etal_1997, 130 | title = {Relationships between disease incidence at two levels in a spatial hierarchy}, 131 | volume = {87}, 132 | doi = {10.1094/PHYTO.1997.87.5.542}, 133 | pages = {542--550}, 134 | number = {5}, 135 | journaltitle = {Phytopathology}, 136 | author = {Hughes, G. and {McRoberts}, N. and Madden, L. V. and Gottwald, T. R.}, 137 | date = {1997-05} 138 | } 139 | 140 | @article{Perry_1995, 141 | title = {Spatial analysis by distance indices}, 142 | volume = {64}, 143 | doi = {10.2307/5892}, 144 | pages = {303--314}, 145 | journaltitle = {Journal of Animal Ecology}, 146 | author = {Perry, J. N.}, 147 | date = {1995} 148 | } 149 | 150 | @article{Lavigne_etal_2010, 151 | title = {Spatial analyses of ecological count data: A density map comparison approach}, 152 | volume = {11}, 153 | doi = {10.1016/j.baae.2010.08.011}, 154 | pages = {734--742}, 155 | journaltitle = {Basic and Applied Ecology}, 156 | author = {Lavigne, C. and Ricci, B. and Franck, P. and Senoussi, R.}, 157 | date = {2010} 158 | } 159 | 160 | -------------------------------------------------------------------------------- /vignettes/defs-and-eqns.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Definitions and relationships between parameters" 3 | author: "Christophe Gigot" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Definitions and relationships between parameters} 8 | %\VignetteEncoding{UTF-8} 9 | %\VignetteEngine{knitr::rmarkdown} 10 | editor_options: 11 | chunk_output_type: console 12 | bibliography: biblio.bib 13 | --- 14 | 15 | 16 | ## Variable definitions 17 | 18 | Throughout the package **epiphy**, special attention is given to stay consistent consistent with variable and parameter names. Some of the most significant names are the followings: 19 | 20 | - $x$, $y$ and $z$ correspond to the three spatial dimensions. Note that no currently implemented method deals with the third dimension $z$; 21 | - $t$ stands for the temporal dimension, if any; 22 | - $i$ corresponds to the used disease intensity metric. Intensity data can be either count, incidence or severity data. Note that there is no implemented methods for severity data at the moment. 23 | - $n$ denotes the number of individuals in a sampling unit only when disease incidence data is used. Note that currently most of the implemented methods only work with equal size sampling units (i.e. there is only one value for $n$ in a given data set). 24 | - $N$ means the total number of sampling units in a data set. 25 | - $p$ is the average disease intensity. $p = \sum_k i_k / N$ for count data ($p \in [0, +\infty[$) and severity data ($p \in [0, 1]$). $p = \sum_k (i_k/n_k) / N$ for incidence data, i.e. $p$ is an average proportion ($p \in [0, 1]$). 26 | 27 | 28 | ## Parameters of the beta-binomial distribution 29 | 30 | ### Definitions 31 | 32 | The aggregation parameter, $\theta$, can be computed from the two shape parameters of the beta-binomial distribution, $\alpha$ and $\beta$. Note that in **epiphy**, $\theta = 1 / (\alpha + \beta)$ as in @Madden_etal_2007, but this definition is not necessarily consistent with what can be found somewhere else in the literature. For example, $\theta = \alpha + \beta$ in the package **emdbook**. 33 | 34 | The intra-cluster correlation coefficient, $\rho$ [@Mak_1988], characterizes the spatial aggregation as the tendency for elements in a sampling unit to have the same disease status more frequently than expected on the basis of spatial randomness [@Madden_etal_2007]. In **epiphy**, $\rho = \theta / (\theta + 1)$. 35 | 36 | ### Base relationships 37 | 38 | Functions of the two shape parameters of the beta-binomial distribution ($\alpha$ and $\beta$): 39 | 40 | $$p = \frac{\alpha}{\alpha + \beta}; \theta = \frac{1}{\alpha + \beta}; \rho = \frac{1}{\alpha + \beta + 1}$$ 41 | 42 | Functions of the aggregation parameter ($\theta$) or the intra-cluster correlation coefficient ($\rho$): 43 | 44 | $$\theta = \frac{\rho}{1 - \rho}; \rho = \frac{\theta}{\theta + 1}$$ 45 | 46 | Functions of the average disease intensity ($p$) and the aggregation parameter ($\theta$): 47 | 48 | $$\alpha = \frac{p}{\theta}; \beta = \frac{1 - p}{\theta}$$ 49 | 50 | Functions of the average disease intensity ($p$) and the intra-cluster correlation coefficient ($\rho$): 51 | 52 | $$\alpha = \frac{p(1-\rho)}{\rho}; \beta = \frac{(1 - p)(1-\rho)}{\rho}$$ 53 | 54 | 55 | ## Parameters of the binary power law 56 | 57 | There are different parametrizations of the binary form of the power law. The user should therefore be cautious when making computations and comparisons with published results. Below are reminders about definitions and relationships between these different parametrizations. 58 | 59 | ### Two possible formulas for parametrization 60 | 61 | $$ 62 | s_{obs}^2 = V_n = A_n [np(1-p)]^b = a_n [p(1-p)]^b 63 | $$ 64 | 65 | $$ 66 | s_{obs}^2 = V_p = A_p [p(1-p)/n]^b = a_p [p(1-p)]^b 67 | $$ 68 | 69 | where $s_{obs}^2$ stands for the observed variance. The relationships between the different binary power law parameters ($A_p$, $a_p$, $A_n$, $a_n$ and $b$) are specified in the following relationship tables. Note that $V_n = n^2 V_p$. 70 | 71 | ### Relationship tables 72 | 73 | **Full version.** 74 | 75 | | | $A_p$ | $a_p$ | $A_n$ | $a_n$ | 76 | |:-----:|:----------------------:|:-------------------:|:----------------------:|:-------------------:| 77 | | $A_p$ | $1$ | $A_p = a_p n^b$ | $A_p = A_n n^{2(b-1)}$ | $A_p = a_n n^{b-2}$ | 78 | | $a_p$ | $a_p = A_p n^{-b}$ | $1$ | $a_p = A_n n^{b-2}$ | $a_p = a_n n^{-2}$ | 79 | | $A_n$ | $A_n = A_p n^{2(1-b)}$ | $A_n = a_p n^{2-b}$ | $1$ | $A_n = a_n n^{-b}$ | 80 | | $a_n$ | $a_n = A_p n^{2-b}$ | $a_n = a_p n^2$ | $a_p = A_n n^b$ | $1$ | 81 | 82 | **Reader-friendly version.** To read it, the formula $\text{row} = \text{col} \times \text{cell}$ must be used. 83 | 84 | | | $A_p$ | $a_p$ | $A_n$ | $a_n$ | 85 | |:-----:|:------------:|:---------:|:------------:|:---------:| 86 | | $A_p$ | $1$ | $n^b$ | $n^{2(b-1)}$ | $n^{b-2}$ | 87 | | $a_p$ | $n^{-b}$ | $1$ | $n^{b-2}$ | $n^{-2}$ | 88 | | $A_n$ | $n^{2(1-b)}$ | $n^{2-b}$ | $1$ | $n^{-b}$ | 89 | | $a_n$ | $n^{2-b}$ | $n^2$ | $n^b$ | $1$ | 90 | 91 | Note that the function `a2a` is kindly provided in **epiphy** to make these tricky conversions as easy as possible. 92 | 93 | ## Relationship between beta-binomial and binary power law parameters 94 | 95 | $$ 96 | \theta = \frac{a_p - f(p)/n}{f(p) - a_p} \text{, with } f(p) = [p(1-p)]^{1-b} 97 | $$ 98 | 99 | ## References 100 | 101 | -------------------------------------------------------------------------------- /R/betabinom.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | #' The beta-binomial distribution. 3 | #' 4 | #' Density, distribution function, quantile function and random generation for 5 | #' the beta-binomial distribution with parameters \code{size}, \code{prob}, 6 | #' \code{theta}, \code{shape1}, \code{shape2}. This distribution corresponds to 7 | #' an overdispersed binomial distribution. 8 | #' 9 | #' Be aware that in this implementation \code{theta} = 1 / (\code{shape1} + 10 | #' \code{shape2}). \code{prob} and \code{theta}, or \code{shape1} and 11 | #' \code{shape2} must be specified. if \code{theta} = 0, use *binom family 12 | #' instead. 13 | #' 14 | #' @param x,q Vector of quantiles. 15 | #' @param p Vector of probabilities. 16 | #' @param n Number of observations. 17 | #' @param size Number of trials. 18 | #' @param prob Probability of success on each trial. 19 | #' @param theta Aggregation parameter (theta = 1 / (shape1 + shape2)). 20 | #' @param shape1,shape2 Shape parameters. 21 | #' @param log,log.p Logical; if TRUE, probabilities p are given as log(p). 22 | #' @param lower.tail Logical; if TRUE (default), probabilities are 23 | #' \eqn{P[X \le x]} otherwise, \eqn{P[X > x]}. 24 | #' 25 | #' @returns 26 | #' \code{dbetabinom} gives the density, \code{pbetabinom} gives the distribution 27 | #' function, \code{qbetabinom} gives the quantile function and \code{rbetabinom} 28 | #' generates random deviates. 29 | #' 30 | #' @examples 31 | #' # Compute P(25 < X < 50) for X following the Beta-Binomial distribution 32 | #' # with parameters size = 100, prob = 0.5 and theta = 0.35: 33 | #' sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = 0.35)) 34 | #' 35 | #' # When theta tends to 0, dbetabinom outputs tends to dbinom outputs: 36 | #' sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = 1e-7)) 37 | #' sum(dbetabinom(25:50, size = 100, shape1 = 1e7, shape2 = 1e7)) 38 | #' sum(dbinom(25:50, size = 100, prob = 0.5)) 39 | #' 40 | #' # Example of binomial and beta-binomial frequency distributions: 41 | #' n <- 15 42 | #' q <- 0:n 43 | #' p1 <- dbinom(q, size = n, prob = 0.33) 44 | #' p2 <- dbetabinom(q, size = n, prob = 0.33, theta = 0.22) 45 | #' res <- rbind(p1, p2) 46 | #' dimnames(res) <- list(c("Binomial", "Beta-binomial"), q) 47 | #' barplot(res, beside = TRUE, legend.text = TRUE, ylab = "Frequency") 48 | #' 49 | #' # Effect of the aggregation parameter theta on probability density: 50 | #' thetas <- seq(0.001, 2.5, by = 0.001) 51 | #' density1 <- rep(sum(dbinom(25:50, size = 100, prob = 0.5)), length(thetas)) 52 | #' density2 <- sapply(thetas, function(theta) { 53 | #' sum(dbetabinom(25:50, size = 100, prob = 0.5, theta = theta)) 54 | #' }) 55 | #' plot(thetas, density2, type = "l", 56 | #' xlab = expression("Aggregation parameter ("*theta*")"), 57 | #' ylab = "Probability density between 25 and 50 (size = 100)") 58 | #' lines(thetas, density1, lty = 2) 59 | #' 60 | #' @seealso \code{\link[emdbook]{dbetabinom}} in the package \strong{emdbook} 61 | #' where the definition of theta is different. 62 | #' 63 | #' @name BetaBinomial 64 | #' @export 65 | #------------------------------------------------------------------------------# 66 | dbetabinom <- function(x, size, prob, theta, shape1, shape2, log = FALSE) { 67 | list2env(check_betabinom(prob, theta, shape1, shape2), envir = environment()) 68 | lpmf <- rep(-Inf, length(x)) 69 | if (!all(int <- is.wholenumber(x))) { 70 | call <- match.call() 71 | warning(paste0("In ", deparse(call), " : non-integer x = ", 72 | eval(call$x)[!int], "\n"), call. = FALSE) 73 | } 74 | lpmf[int] <- lchoose(size, x[int]) + 75 | lbeta(x[int] + shape1, size - x[int] + shape2) - 76 | lbeta(shape1, shape2) 77 | if (log) lpmf else exp(lpmf) 78 | } 79 | 80 | #------------------------------------------------------------------------------# 81 | #' @rdname BetaBinomial 82 | #' @export 83 | #------------------------------------------------------------------------------# 84 | pbetabinom <- function(q, size, prob, theta, shape1, shape2, lower.tail = TRUE, 85 | log.p = FALSE) { 86 | # TODO: Double-check. 87 | list2env(check_betabinom(prob, theta, shape1, shape2), envir = environment()) 88 | q <- trunc(q) 89 | p <- sapply(q, function(x) 90 | sum(vapply(0:x, dbetabinom, FUN.VALUE = numeric(1L), size = size, 91 | shape1 = shape1, shape2 = shape2))) 92 | if (!lower.tail) p <- 1 - p 93 | if (log.p) log(p) else p 94 | } 95 | 96 | #------------------------------------------------------------------------------# 97 | #' @rdname BetaBinomial 98 | #' @export 99 | #------------------------------------------------------------------------------# 100 | qbetabinom <- function(p, size, prob, theta, shape1, shape2, lower.tail = TRUE, 101 | log.p = FALSE) { 102 | call <- match.call() 103 | list2env(check_betabinom(prob, theta, shape1, shape2), envir = environment()) 104 | if (log.p) p <- exp(p) 105 | q <- sapply(p, function(x) { 106 | if (0 > x | x > 1) return(NaN) 107 | y <- 0 108 | while (x > pbetabinom(y, size = size, shape1 = shape1, shape2 = shape2)) 109 | y <- y + 1 110 | y 111 | }) 112 | if (lower.tail) q else size - q 113 | } 114 | 115 | #------------------------------------------------------------------------------# 116 | #' @rdname BetaBinomial 117 | #' @export 118 | #------------------------------------------------------------------------------# 119 | rbetabinom <- function(n, size, prob, theta, shape1, shape2) { 120 | list2env(check_betabinom(prob, theta, shape1, shape2), envir = environment()) 121 | rbinom(n, size = size, prob = rbeta(n, shape1 = shape1, shape2 = shape2)) 122 | } 123 | 124 | #------------------------------------------------------------------------------# 125 | #' @keywords internal 126 | #------------------------------------------------------------------------------# 127 | check_betabinom <- function(prob, theta, shape1, shape2) {#, env) { 128 | pair1 <- pair2 <- FALSE 129 | if (!missing(prob) && !missing(theta)) pair1 <- TRUE 130 | if (!missing(shape1) && !missing(shape2)) pair2 <- TRUE 131 | if (!xor(pair1, pair2)) { 132 | stop("(prob, theta) or (shape1, shape2) must be specified.") 133 | } 134 | if (pair1) { 135 | shape1 <- prob / theta 136 | shape2 <- (1 - prob) / theta 137 | } 138 | list(shape1 = shape1, shape2 = shape2) 139 | } 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /vignettes/phytopathology_v2.csl: -------------------------------------------------------------------------------- 1 | 2 | 172 | -------------------------------------------------------------------------------- /man/intensity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intensity-classes.R 3 | \name{intensity} 4 | \alias{intensity} 5 | \alias{count} 6 | \alias{count_data} 7 | \alias{incidence} 8 | \alias{incidence_data} 9 | \alias{severity} 10 | \alias{severity_data} 11 | \title{Construct count, incidence and severity objects.} 12 | \usage{ 13 | count(data, mapping, keep_only_std = TRUE) 14 | 15 | incidence(data, mapping, keep_only_std = TRUE) 16 | 17 | severity(data, mapping, keep_only_std = TRUE) 18 | } 19 | \arguments{ 20 | \item{data}{A data frame. Each line corresponds to a record (or case, or 21 | entry).} 22 | 23 | \item{mapping}{A \code{mapping} object, created with \code{mapping()} or 24 | \code{mapping_()} functions. ... A vector with all the corresponding variables. The different 25 | elements can be named (names of the elements) of the data frame in the 26 | incidence object), or unamed. In the latter case, elements must be 27 | correctly ordered, i.e. x, y, z, t, r and then n. If variables in NULL, 28 | then only the 6 first ... will be take into account in the following (1, 2, 29 | ...), i.e. the id of the value. All the 'parameters' need to be specified.} 30 | 31 | \item{keep_only_std}{Are only standard names kept when proceeding to mapping? 32 | Setting \code{keep_only_std} to TRUE may be useful for subsequent data splitting 33 | using extra labels.} 34 | } 35 | \value{ 36 | An \code{intensity} object. 37 | 38 | When printed, difference information are available: 39 | 40 | \itemize{ 41 | \item The number of sampling units. 42 | \item The time. 43 | \item Is it georeferenced (TRUE/FALSE) 44 | \item Are there any NA data (TRUE/FALSE) 45 | \item Is it a complet array (TRUE/FALSE)? A complete array means that all the recorded values allow to 46 | display an array (even if some data are not available), but this was explicitelly specified. To 47 | complete a dataset, just use \code{complete(data)}. You can also remove NA, which is necessary to use 48 | some analysis technics, using \code{replaceNA(data)} or \code{replace.na(data)}. Note that using both 49 | commands will results in modifying the original data sets which will be specified. 50 | } 51 | } 52 | \description{ 53 | \code{count()}, \code{incidence()} and \code{severity()} create eponym 54 | objects. All of these classes inherit from the base class \code{intensity}. 55 | The choice of the class depends on the nature of the data set. 56 | } 57 | \details{ 58 | \code{incidence} reads disease incidence data from a data frame and return an 59 | incidence object. All of these classes inherit from \code{intensity} class. 60 | \itemize{ 61 | \item count: Each sampling unit contains from 0 to theoreticaly an infinity of data. 62 | Number are positive integers. 63 | \item incidence: Each sampling unit contains an number of diseased plants, 64 | ranging from 0 to \code{n} which is the total amount of plants per sampling 65 | unit. 66 | \item severity: Each sampling unit contain a percentage of disease, a positive 67 | real number ranging from 0.0 to 1.0. 68 | } 69 | 70 | Class intensity and inherited classes 71 | 72 | All the classes recording disease intensity measurements inherit from this 73 | class. The class \code{intensity} is virtual which means that no object of a 74 | class \code{intensity} can be constructed. This class only describes common 75 | features of all the different disease intensity measurements implemented in 76 | this package (\code{\link{count}}, \code{\link{incidence}} and 77 | \code{\link{severity}}). You should call one of these inherited classes 78 | instead, depending on the nature of your data. 79 | 80 | By convention, the first columns of the different data frames of each slots 81 | have names, but the spatial, temporal or even disease information do not need 82 | to fit to these conventions or may be less straightforward and need more 83 | columns to record correctly all the information. In such unusual situations, 84 | the automatic options of the analysis tools would need to be overridden to be 85 | able to work in the desired way. 86 | 87 | The differences between the different inherited classes regard only the 88 | \code{obs} slot. In the case of \code{\link{count}}, the data expected for 89 | each record are positive integers (N+). For \code{\link{incidence}}, the data 90 | sets are supposed to be two information set per records, the number of 91 | diseased unit per sampling unit (r) and the total number of units per 92 | sampling unit (n). Note that in its current implementation, n is supposed to 93 | be the same for a whole data set. Unequal sampling units are not implemented 94 | yet. Finally, for \code{\link{severity}}, r is positive real ranging from 0 95 | to 1 and depecting a percentage. 96 | 97 | space A data frame containing only spatial information. Each row 98 | corresponds to a sampling unit. By convention, the first 3 columns are 99 | names \code{x}, \code{y}, \code{z}. 100 | 101 | time A data frame containing temporal information. By convention, the 102 | first column is named \code{t}. 103 | 104 | obs A data frame containing disease observations themselves. The name 105 | of the columns may differ between the sub-class chosed to record the data. 106 | 107 | Note that it is possible to create a "severity" object but no statistical 108 | tools are currently implemented to deal with such an object. 109 | 110 | An \code{intensity} object contains at very least the "pure" intensity 111 | records (column \code{r}) which is a so-called observational variable. 112 | Another observational variable, the number of individuals in a sampling unit 113 | (\code{n}), is present in the case of a \code{incidence} object. Very often 114 | in addition to observational variables, there are spatial (columns \code{x}, 115 | \code{y} and/or \code{z}) and/or temporal (column \code{t}) variables. 116 | 117 | Note that the \code{severity} class and the \code{z} variable (the 3rd 118 | spatial dimension) are implemented but no statistical methods use them at 119 | this point. 120 | } 121 | \examples{ 122 | ## Create intensity objects 123 | # Implicite call: The variable mapping does not need to be specified if the 124 | # column names of the input data frame follow the default names. 125 | colnames(tomato_tswv$field_1929) # Returns c("x", "y", "t", "i", "n") 126 | my_incidence_1 <- incidence(tomato_tswv$field_1929) 127 | my_incidence_1 128 | my_incidence_2 <- incidence(tomato_tswv$field_1929, 129 | mapping(x = x, y = y, t = t, i = i, n = n)) 130 | identical(my_incidence_1, my_incidence_2) 131 | 132 | # Explicite call: Otherwise, the variable mapping need to be specified, at 133 | # least for column names that do not correspond to default names. 134 | colnames(aphids) # Returns c("xm", "ym", "i") 135 | my_count_1 <- count(aphids, mapping(x = xm, y = ym, i = i)) 136 | my_count_1 137 | # We can drop the "i = i" in the mapping. 138 | my_count_2 <- count(aphids, mapping(x = xm, y = ym)) 139 | identical(my_count_1, my_count_2) 140 | 141 | # It is possible to change the variable mapping after the creation of an 142 | # intensity object: 143 | another_incidence <- incidence(hop_viruses$HpLV) 144 | another_incidence 145 | remap(another_incidence, mapping(x = xm, y = ym)) 146 | 147 | ## Plotting data 148 | plot(my_incidence_1) # Same as: plot(my_incidence_1, type = "spatial") 149 | plot(my_incidence_1, type = "temporal") 150 | 151 | plot(my_count_1, tile = FALSE, size = 5) 152 | plot(my_count_1, type = "temporal") # Not possible: there is only 1 date. 153 | 154 | # Using grayscale: 155 | plot(my_count_1, grayscale = TRUE) 156 | plot(my_count_1, grayscale = TRUE, tile = FALSE, size = 5) 157 | 158 | } 159 | -------------------------------------------------------------------------------- /data-raw/arthropods.csv: -------------------------------------------------------------------------------- 1 | x,y,xm,ym,t,i 2 | 1,1,0,0,1,29 3 | 2,1,30,0,1,32 4 | 3,1,60,0,1,2 5 | 4,1,90,0,1,24 6 | 5,1,120,0,1,20 7 | 6,1,150,0,1,10 8 | 7,1,180,0,1,26 9 | 1,2,0,30,1,21 10 | 2,2,30,30,1,25 11 | 3,2,60,30,1,12 12 | 4,2,90,30,1,30 13 | 5,2,120,30,1,16 14 | 6,2,150,30,1,11 15 | 7,2,180,30,1,34 16 | 1,3,0,60,1,16 17 | 2,3,30,60,1,18 18 | 3,3,60,60,1,33 19 | 4,3,90,60,1,16 20 | 5,3,120,60,1,11 21 | 6,3,150,60,1,17 22 | 7,3,180,60,1,39 23 | 1,4,0,90,1,33 24 | 2,4,30,90,1,28 25 | 3,4,60,90,1,16 26 | 4,4,90,90,1,10 27 | 5,4,120,90,1,5 28 | 6,4,150,90,1,13 29 | 7,4,180,90,1,27 30 | 1,5,0,120,1,11 31 | 2,5,30,120,1,11 32 | 3,5,60,120,1,10 33 | 4,5,90,120,1,11 34 | 5,5,120,120,1,9 35 | 6,5,150,120,1,7 36 | 7,5,180,120,1,31 37 | 1,6,0,150,1,24 38 | 2,6,30,150,1,24 39 | 3,6,60,150,1,5 40 | 4,6,90,150,1,11 41 | 5,6,120,150,1,8 42 | 6,6,150,150,1,23 43 | 7,6,180,150,1,26 44 | 1,7,0,180,1,14 45 | 2,7,30,180,1,16 46 | 3,7,60,180,1,14 47 | 4,7,90,180,1,9 48 | 5,7,120,180,1,19 49 | 6,7,150,180,1,19 50 | 7,7,180,180,1,44 51 | 1,8,0,210,1,20 52 | 2,8,30,210,1,19 53 | 3,8,60,210,1,12 54 | 4,8,90,210,1,24 55 | 5,8,120,210,1,23 56 | 6,8,150,210,1,35 57 | 7,8,180,210,1,27 58 | 1,9,0,240,1,7 59 | 2,9,30,240,1,15 60 | 3,9,60,240,1,11 61 | 4,9,90,240,1,15 62 | 5,9,120,240,1,20 63 | 6,9,150,240,1,21 64 | 7,9,180,240,1,47 65 | 1,1,0,0,2,6 66 | 2,1,30,0,2,24 67 | 3,1,60,0,2,16 68 | 4,1,90,0,2,20 69 | 5,1,120,0,2,9 70 | 6,1,150,0,2,24 71 | 7,1,180,0,2,22 72 | 1,2,0,30,2,22 73 | 2,2,30,30,2,10 74 | 3,2,60,30,2,4 75 | 4,2,90,30,2,36 76 | 5,2,120,30,2,9 77 | 6,2,150,30,2,18 78 | 7,2,180,30,2,8 79 | 1,3,0,60,2,11 80 | 2,3,30,60,2,2 81 | 3,3,60,60,2,9 82 | 4,3,90,60,2,11 83 | 5,3,120,60,2,8 84 | 6,3,150,60,2,15 85 | 7,3,180,60,2,26 86 | 1,4,0,90,2,16 87 | 2,4,30,90,2,14 88 | 3,4,60,90,2,16 89 | 4,4,90,90,2,5 90 | 5,4,120,90,2,5 91 | 6,4,150,90,2,12 92 | 7,4,180,90,2,18 93 | 1,5,0,120,2,16 94 | 2,5,30,120,2,13 95 | 3,5,60,120,2,9 96 | 4,5,90,120,2,7 97 | 5,5,120,120,2,0 98 | 6,5,150,120,2,13 99 | 7,5,180,120,2,18 100 | 1,6,0,150,2,9 101 | 2,6,30,150,2,11 102 | 3,6,60,150,2,6 103 | 4,6,90,150,2,7 104 | 5,6,120,150,2,9 105 | 6,6,150,150,2,28 106 | 7,6,180,150,2,13 107 | 1,7,0,180,2,11 108 | 2,7,30,180,2,8 109 | 3,7,60,180,2,7 110 | 4,7,90,180,2,9 111 | 5,7,120,180,2,16 112 | 6,7,150,180,2,17 113 | 7,7,180,180,2,22 114 | 1,8,0,210,2,5 115 | 2,8,30,210,2,10 116 | 3,8,60,210,2,8 117 | 4,8,90,210,2,8 118 | 5,8,120,210,2,14 119 | 6,8,150,210,2,22 120 | 7,8,180,210,2,8 121 | 1,9,0,240,2,0 122 | 2,9,30,240,2,6 123 | 3,9,60,240,2,10 124 | 4,9,90,240,2,10 125 | 5,9,120,240,2,10 126 | 6,9,150,240,2,22 127 | 7,9,180,240,2,34 128 | 1,1,0,0,3,3 129 | 2,1,30,0,3,26 130 | 3,1,60,0,3,10 131 | 4,1,90,0,3,8 132 | 5,1,120,0,3,16 133 | 6,1,150,0,3,12 134 | 7,1,180,0,3,11 135 | 1,2,0,30,3,12 136 | 2,2,30,30,3,10 137 | 3,2,60,30,3,7 138 | 4,2,90,30,3,14 139 | 5,2,120,30,3,27 140 | 6,2,150,30,3,22 141 | 7,2,180,30,3,8 142 | 1,3,0,60,3,4 143 | 2,3,30,60,3,10 144 | 3,3,60,60,3,10 145 | 4,3,90,60,3,6 146 | 5,3,120,60,3,12 147 | 6,3,150,60,3,9 148 | 7,3,180,60,3,13 149 | 1,4,0,90,3,11 150 | 2,4,30,90,3,13 151 | 3,4,60,90,3,13 152 | 4,4,90,90,3,5 153 | 5,4,120,90,3,8 154 | 6,4,150,90,3,18 155 | 7,4,180,90,3,11 156 | 1,5,0,120,3,4 157 | 2,5,30,120,3,11 158 | 3,5,60,120,3,6 159 | 4,5,90,120,3,3 160 | 5,5,120,120,3,16 161 | 6,5,150,120,3,21 162 | 7,5,180,120,3,15 163 | 1,6,0,150,3,8 164 | 2,6,30,150,3,4 165 | 3,6,60,150,3,4 166 | 4,6,90,150,3,1 167 | 5,6,120,150,3,9 168 | 6,6,150,150,3,19 169 | 7,6,180,150,3,13 170 | 1,7,0,180,3,12 171 | 2,7,30,180,3,5 172 | 3,7,60,180,3,17 173 | 4,7,90,180,3,2 174 | 5,7,120,180,3,18 175 | 6,7,150,180,3,44 176 | 7,7,180,180,3,6 177 | 1,8,0,210,3,11 178 | 2,8,30,210,3,6 179 | 3,8,60,210,3,17 180 | 4,8,90,210,3,7 181 | 5,8,120,210,3,10 182 | 6,8,150,210,3,35 183 | 7,8,180,210,3,4 184 | 1,9,0,240,3,7 185 | 2,9,30,240,3,7 186 | 3,9,60,240,3,10 187 | 4,9,90,240,3,4 188 | 5,9,120,240,3,7 189 | 6,9,150,240,3,28 190 | 7,9,180,240,3,16 191 | 1,1,0,0,4,31 192 | 2,1,30,0,4,17 193 | 3,1,60,0,4,24 194 | 4,1,90,0,4,24 195 | 5,1,120,0,4,26 196 | 6,1,150,0,4,45 197 | 7,1,180,0,4,2 198 | 1,2,0,30,4,19 199 | 2,2,30,30,4,21 200 | 3,2,60,30,4,19 201 | 4,2,90,30,4,29 202 | 5,2,120,30,4,28 203 | 6,2,150,30,4,25 204 | 7,2,180,30,4,9 205 | 1,3,0,60,4,18 206 | 2,3,30,60,4,10 207 | 3,3,60,60,4,1 208 | 4,3,90,60,4,8 209 | 5,3,120,60,4,14 210 | 6,3,150,60,4,33 211 | 7,3,180,60,4,20 212 | 1,4,0,90,4,31 213 | 2,4,30,90,4,8 214 | 3,4,60,90,4,24 215 | 4,4,90,90,4,5 216 | 5,4,120,90,4,9 217 | 6,4,150,90,4,20 218 | 7,4,180,90,4,21 219 | 1,5,0,120,4,22 220 | 2,5,30,120,4,7 221 | 3,5,60,120,4,37 222 | 4,5,90,120,4,4 223 | 5,5,120,120,4,12 224 | 6,5,150,120,4,23 225 | 7,5,180,120,4,17 226 | 1,6,0,150,4,30 227 | 2,6,30,150,4,6 228 | 3,6,60,150,4,19 229 | 4,6,90,150,4,5 230 | 5,6,120,150,4,5 231 | 6,6,150,150,4,16 232 | 7,6,180,150,4,14 233 | 1,7,0,180,4,18 234 | 2,7,30,180,4,29 235 | 3,7,60,180,4,26 236 | 4,7,90,180,4,4 237 | 5,7,120,180,4,28 238 | 6,7,150,180,4,18 239 | 7,7,180,180,4,37 240 | 1,8,0,210,4,24 241 | 2,8,30,210,4,39 242 | 3,8,60,210,4,13 243 | 4,8,90,210,4,8 244 | 5,8,120,210,4,8 245 | 6,8,150,210,4,23 246 | 7,8,180,210,4,8 247 | 1,9,0,240,4,20 248 | 2,9,30,240,4,5 249 | 3,9,60,240,4,18 250 | 4,9,90,240,4,1 251 | 5,9,120,240,4,19 252 | 6,9,150,240,4,20 253 | 7,9,180,240,4,28 254 | 1,1,0,0,5,16 255 | 2,1,30,0,5,17 256 | 3,1,60,0,5,17 257 | 4,1,90,0,5,36 258 | 5,1,120,0,5,36 259 | 6,1,150,0,5,11 260 | 7,1,180,0,5,11 261 | 1,2,0,30,5,14 262 | 2,2,30,30,5,29 263 | 3,2,60,30,5,0 264 | 4,2,90,30,5,35 265 | 5,2,120,30,5,31 266 | 6,2,150,30,5,7 267 | 7,2,180,30,5,6 268 | 1,3,0,60,5,16 269 | 2,3,30,60,5,12 270 | 3,3,60,60,5,38 271 | 4,3,90,60,5,7 272 | 5,3,120,60,5,17 273 | 6,3,150,60,5,12 274 | 7,3,180,60,5,5 275 | 1,4,0,90,5,5 276 | 2,4,30,90,5,10 277 | 3,4,60,90,5,11 278 | 4,4,90,90,5,10 279 | 5,4,120,90,5,25 280 | 6,4,150,90,5,10 281 | 7,4,180,90,5,10 282 | 1,5,0,120,5,16 283 | 2,5,30,120,5,6 284 | 3,5,60,120,5,10 285 | 4,5,90,120,5,6 286 | 5,5,120,120,5,0 287 | 6,5,150,120,5,0 288 | 7,5,180,120,5,3 289 | 1,6,0,150,5,10 290 | 2,6,30,150,5,38 291 | 3,6,60,150,5,6 292 | 4,6,90,150,5,15 293 | 5,6,120,150,5,11 294 | 6,6,150,150,5,3 295 | 7,6,180,150,5,14 296 | 1,7,0,180,5,6 297 | 2,7,30,180,5,19 298 | 3,7,60,180,5,17 299 | 4,7,90,180,5,6 300 | 5,7,120,180,5,0 301 | 6,7,150,180,5,9 302 | 7,7,180,180,5,9 303 | 1,8,0,210,5,4 304 | 2,8,30,210,5,32 305 | 3,8,60,210,5,0 306 | 4,8,90,210,5,17 307 | 5,8,120,210,5,12 308 | 6,8,150,210,5,3 309 | 7,8,180,210,5,3 310 | 1,9,0,240,5,8 311 | 2,9,30,240,5,30 312 | 3,9,60,240,5,12 313 | 4,9,90,240,5,7 314 | 5,9,120,240,5,6 315 | 6,9,150,240,5,4 316 | 7,9,180,240,5,15 317 | 1,1,0,0,6,37 318 | 2,1,30,0,6,45 319 | 3,1,60,0,6,33 320 | 4,1,90,0,6,14 321 | 5,1,120,0,6,41 322 | 6,1,150,0,6,96 323 | 7,1,180,0,6,50 324 | 1,2,0,30,6,69 325 | 2,2,30,30,6,19 326 | 3,2,60,30,6,18 327 | 4,2,90,30,6,46 328 | 5,2,120,30,6,47 329 | 6,2,150,30,6,35 330 | 7,2,180,30,6,33 331 | 1,3,0,60,6,62 332 | 2,3,30,60,6,36 333 | 3,3,60,60,6,35 334 | 4,3,90,60,6,31 335 | 5,3,120,60,6,26 336 | 6,3,150,60,6,37 337 | 7,3,180,60,6,41 338 | 1,4,0,90,6,54 339 | 2,4,30,90,6,26 340 | 3,4,60,90,6,9 341 | 4,4,90,90,6,9 342 | 5,4,120,90,6,24 343 | 6,4,150,90,6,24 344 | 7,4,180,90,6,57 345 | 1,5,0,120,6,43 346 | 2,5,30,120,6,29 347 | 3,5,60,120,6,40 348 | 4,5,90,120,6,36 349 | 5,5,120,120,6,18 350 | 6,5,150,120,6,43 351 | 7,5,180,120,6,46 352 | 1,6,0,150,6,39 353 | 2,6,30,150,6,25 354 | 3,6,60,150,6,23 355 | 4,6,90,150,6,29 356 | 5,6,120,150,6,23 357 | 6,6,150,150,6,45 358 | 7,6,180,150,6,54 359 | 1,7,0,180,6,45 360 | 2,7,30,180,6,20 361 | 3,7,60,180,6,23 362 | 4,7,90,180,6,29 363 | 5,7,120,180,6,23 364 | 6,7,150,180,6,59 365 | 7,7,180,180,6,67 366 | 1,8,0,210,6,26 367 | 2,8,30,210,6,20 368 | 3,8,60,210,6,24 369 | 4,8,90,210,6,65 370 | 5,8,120,210,6,34 371 | 6,8,150,210,6,30 372 | 7,8,180,210,6,31 373 | 1,9,0,240,6,59 374 | 2,9,30,240,6,28 375 | 3,9,60,240,6,22 376 | 4,9,90,240,6,30 377 | 5,9,120,240,6,24 378 | 6,9,150,240,6,27 379 | 7,9,180,240,6,43 380 | -------------------------------------------------------------------------------- /data-raw/export-data.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | # Useful packages 3 | #------------------------------------------------------------------------------# 4 | library(magrittr) 5 | library(tidyverse) 6 | library(devtools) 7 | #options(stringsAsFactors = FALSE) # To use "standard" data frame, do noy use that. 8 | 9 | #------------------------------------------------------------------------------# 10 | # Useful functions 11 | #------------------------------------------------------------------------------# 12 | wide2long <- function(data) { 13 | data$x <- as.integer(row.names(data)) 14 | res <- data %>% gather(y, value, -x) 15 | # substring(...) because R gives 'V#' as default column names 16 | res$y <- as.integer(substring(res$y, 2)) 17 | res 18 | } 19 | 20 | listOfValuesSup0 <- function(data, vals) 21 | lapply(vals, function(val) filter(data, value == val)) 22 | 23 | generateGrid <- function(data, times) { 24 | res <- expand.grid(x = unique(data$x), 25 | y = unique(data$y), 26 | t = times, 27 | KEEP.OUT.ATTRS = FALSE) 28 | res <- res %>% arrange(x, y, t) 29 | res$i <- 0 30 | res$n <- 0 31 | res 32 | } 33 | 34 | fillDis <- function(data, listVals) { 35 | lapply(seq_len(length(listVals)), function(id) { 36 | # ida: row-ids in data where (x, y) are also present in listVals[[id]] 37 | ida <- which( 38 | apply(select(data, x, y), 1, paste0, collapse = "_") %in% 39 | apply(select(listVals[[id]], x, y), 1, paste0, collapse = "_")) 40 | # idb: row-ids in data where t >= id 41 | idb <- which(data$t >= id) 42 | data[intersect(ida, idb), ]$i <<- 1 # Here, 1 means diseased (0: healthy) 43 | }) 44 | data 45 | } 46 | 47 | #------------------------------------------------------------------------------# 48 | # Dataset: tomato_tswv (ex. dataCochran1936) 49 | #------------------------------------------------------------------------------# 50 | # Plants recorded as diseased at time: 51 | # - 1: 18 December 1929 52 | # - 2: 31 December 1929 53 | tomato_tswv_1929_1plot <- read.csv("data-raw/tomato_tswv_1929_1plot.csv", header = TRUE) 54 | #write.csv(tomato_tswv_1928_4plots, file = "data-raw/tomato-tswv-1928-4plots.csv", quote = FALSE, row.names = FALSE) 55 | 56 | #------------------------------------------------------------------------------# 57 | ### Export tomato_tswv_4plots (ex. dataBald197) 58 | # 0 = Healthy plants 59 | # Below: diseased plants 60 | # 1 = 6 November 1928 61 | # 2 = 14 November 1928 62 | # 3 = 21 November 1928 63 | # 4 = 28-29 November 1928 64 | # 5 = 5 December 1928 65 | # 6 = 12 December 1928 66 | tomato_tswv_1928_4plots <- read.csv("data-raw/tomato_tswv_1928_4plots.csv", header = TRUE) 67 | 68 | tomato_tswv <- list("field_1928" = tomato_tswv_1928_4plots, 69 | "field_1929" = tomato_tswv_1929_1plot) 70 | use_data(tomato_tswv) 71 | 72 | 73 | 74 | #------------------------------------------------------------------------------# 75 | # Dataset: citrus_ctv (ex. dataGottwald1996) 76 | #------------------------------------------------------------------------------# 77 | citrus_ctv <- list("IVI3and4" = read.csv("data-raw/citrus_ctv_IVIA3and4.csv", header = TRUE), 78 | "IVI6and7" = read.csv("data-raw/citrus_ctv_IVIA6and7.csv", header = TRUE), 79 | "El_Realengo" = read.csv("data-raw/citrus_ctv_El-Realengo.csv", header = TRUE)) 80 | use_data(citrus_ctv) 81 | 82 | #------------------------------------------------------------------------------# 83 | ### Export tobacco_viruses (ex. dataMadden1987) 84 | tobacco_viruses <- read.csv("data-raw/tobacco_viruses.csv", header = TRUE) 85 | use_data(tobacco_viruses) 86 | 87 | #------------------------------------------------------------------------------# 88 | ### Export pyrethrum_ray_blight (ex. dataPethybridge2005) 89 | pyrethrum_ray_blight <- read.csv("data-raw/pyrethrum_ray_blight.csv", header = TRUE) 90 | use_data(pyrethrum_ray_blight) 91 | 92 | #------------------------------------------------------------------------------# 93 | ### Export onion_bacterial_blight (ex. dataRoumagnac2004) 94 | # 1 = new diseased at first date 95 | # 2 = new diseased at second data 96 | onion_bacterial_blight <- read.csv("data-raw/onion_bacterial_blight.csv", header = TRUE) 97 | use_data(onion_bacterial_blight) 98 | 99 | #------------------------------------------------------------------------------# 100 | ### Export dataSkellam1948 101 | #dataSkellam1948 <- read.csv("data-raw/Skellam1948.csv", header = TRUE) 102 | #use_data(dataSkellam1948) 103 | 104 | #------------------------------------------------------------------------------# 105 | ### Export offspring_survival (ex. dataWilliams1975) 106 | # Interesting : n not the same size everywhere 107 | offspring_survival <- read.csv("data-raw/offspring_survival.csv", header = TRUE) 108 | use_data(offspring_survival) 109 | 110 | #------------------------------------------------------------------------------# 111 | ### Export simulated_epidemics (ex. dataXuMadden2004) 112 | # pattern = clumped, regular or random 113 | # mu = median spore dispersal parameter 114 | simulated_epidemics <- read.csv("data-raw/simulated_epidemics.csv", header = TRUE) 115 | use_data(simulated_epidemics) 116 | 117 | #------------------------------------------------------------------------------# 118 | ### Export dogwood_anthracnose (ex. dataZarnoch1995) 119 | dogwood_anthracnose <- read.csv("data-raw/dogwood_anthracnose.csv", header = TRUE) 120 | use_data(dogwood_anthracnose) 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | #------------------------------------------------------------------------------# 131 | # Export aphid counts (Perry et al., 1999) 132 | #------------------------------------------------------------------------------# 133 | aphids <- read.csv("data-raw/aphids.csv", header = TRUE) 134 | 135 | with(aphids, plot(xm, ym, pch = NA)) 136 | with(aphids, text(xm, ym, labels = i)) 137 | 138 | use_data(aphids) 139 | 140 | #------------------------------------------------------------------------------# 141 | # Export arthropods counts (Holland et al., 1999) 142 | #------------------------------------------------------------------------------# 143 | arthropods <- read.csv("data-raw/arthropods.csv", header = TRUE) 144 | arthropods_splitted <- split(arthropods, arthropods$t) 145 | 146 | opar <- par() 147 | par(mar = c(2,2,2,2)) # We do not see x and y labels anymore. 148 | layout(matrix(1:6, nrow = 3, ncol = 2, byrow = TRUE)) 149 | invisible(lapply(arthropods_splitted, function(set) { 150 | with(set, plot(xm, ym, pch = NA)) 151 | with(set, text(xm, ym, labels = i)) 152 | })) 153 | par(opar) 154 | layout(1) 155 | 156 | use_data(arthropods) 157 | 158 | #------------------------------------------------------------------------------# 159 | # Export codling moth counts (Lavigne et al., 2010) 160 | #------------------------------------------------------------------------------# 161 | codling_moths <- read.csv("data-raw/codling_moths.csv", header = TRUE) 162 | 163 | coef <- 80 # Coef to convert arbitrary units into meters (m = coef * u.a.) 164 | codling_moths[, 1:2] %<>% multiply_by(coef) 165 | 166 | with(codling_moths, plot(xm, ym, pch = NA)) 167 | with(codling_moths, text(xm, ym, labels = i)) 168 | 169 | use_data(codling_moths) 170 | 171 | 172 | #------------------------------------------------------------------------------# 173 | 174 | # reorg <- function(my_data, year) { 175 | # 176 | # my_data$x <- round((my_data$x * (50.4 / 50)) / 2.1) + 1 177 | # my_data$y <- round((my_data$y * (90 / 90)) / 1.8) + 1 178 | # my_data$i <- 1 179 | # 180 | # tmp <- expand.grid(x = min(my_data$x):max(my_data$x), 181 | # y = min(my_data$y):max(my_data$y)) 182 | # 183 | # my_data <- left_join(tmp, my_data) 184 | # my_data[is.na(my_data$i), ]$i <- 0 185 | # my_data$n <- 1 186 | # my_data$xm <- (my_data$x - 1) * 2.1 187 | # my_data$ym <- (my_data$y - 1) * 1.8 188 | # my_data$t <- year 189 | # 190 | # my_data <- my_data %>% 191 | # select(x, y, xm, ym, t, r, n) %>% 192 | # arrange(x, y, t) 193 | # 194 | # my_data 195 | # } 196 | # 197 | # year_1996 <- c("data-raw/Pethybridge_apple_A_1996.csv", 198 | # "data-raw/Pethybridge_apple_B_1996.csv", 199 | # "data-raw/Pethybridge_apple_C_1996.csv") 200 | # year_1997 <- c("data-raw/Pethybridge_apple_A_1997_subsequent.csv", 201 | # "data-raw/Pethybridge_apple_B_1997_subsequent.csv", 202 | # "data-raw/Pethybridge_apple_C_1997_subsequent.csv") 203 | # viruses <- c("HpLV", "HpMV", "ApMV") 204 | # 205 | # for (i1 in 1:3) { 206 | # my_data_1 <- read.csv2(year_1996[i1], header = TRUE) 207 | # my_data_2 <- read.csv2(year_1997[i1], header = TRUE) 208 | # my_data_2 <- bind_rows(my_data_1, my_data_2) 209 | # my_data_3 <- bind_rows(reorg(my_data_1, 1996), 210 | # reorg(my_data_2, 1997)) 211 | # 212 | # #with(my_data_3 %>% filter(i == 1, t == 1997), 213 | # # plot(xm, ym, pch = 19, col = rgb(0,0,0,1), yaxt = "n")) 214 | # #axis(2, at = seq(0, 90, by = 10), las = 2) 215 | # 216 | # write.csv(my_data_3, file = paste0("data-raw/hop_", i1, "_", viruses[i1], ".csv"), 217 | # quote = FALSE, row.names = FALSE) 218 | # } 219 | 220 | hop_viruses <- list("HpLV" = read.csv("data-raw/hop_1_HpLV.csv", header = TRUE), 221 | "HpMV" = read.csv("data-raw/hop_2_HpMV.csv", header = TRUE), 222 | "ApMV" = read.csv("data-raw/hop_3_ApMV.csv", header = TRUE)) 223 | use_data(hop_viruses) 224 | 225 | #------------------------------------------------------------------------------# 226 | 227 | -------------------------------------------------------------------------------- /R/mapcomp.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | #' Map Comparison procedure. 3 | #' 4 | #' \code{mapcomp} performs a spatial pattern analysis based on the calculation 5 | #' of a formal distance (the Hellinger distance) between the density map of 6 | #' count or incidence data, and the density map of sampling effort. Statistical 7 | #' tests of spatial homogeneity are based on permutations across sampling sites 8 | #' and on valuable properties of the Hellinger distance. 9 | #' 10 | #' @param data A data frame or a matrix with only three columns: the two first 11 | #' ones must be the x and y coordinates of the sampling units, and the last 12 | #' one, the corresponding disease intensity observations. It can also be a 13 | #' \code{\link{count}} or an \code{\link{incidence}} object. 14 | #' 15 | #' @param bandwidth Bandwidth parameter for smoothing. It allows to test the 16 | #' spatial extent of heterogeneity if any. 17 | #' @param delta Mesh size of the grid over the geographical domain of the 18 | #' sampling units used to compute the integral Hellinger distance between 19 | #' the probability density function of observations and the probability 20 | #' density function of sampling effort. 21 | #' @param edge_correction Apply edge correction to account for the fact that 22 | #' bordering points intrinsically suffer from a lack of neighboring 23 | #' observation sites. FALSE by default. 24 | #' @param nperm Number of random permutations to assess probabilities. 25 | #' @param threads Number of threads to perform the computations. 26 | #' @param verbose Explain what is being done (TRUE by default). 27 | #' @param ... Additional arguments to be passed to other methods. 28 | #' 29 | #' @returns 30 | #' An object of class \code{mapcomp}, which is a list containing the following 31 | #' components: 32 | #' \tabular{ll}{ 33 | #' \code{data} \tab The input data. \cr 34 | #' \code{coord} \tab The coordinates and normalized intensity for each point of the full grid. \cr 35 | #' \code{object} \tab The class of \code{data}. \cr 36 | #' \code{bandwidth} \tab The \code{bandwidth} parameter. \cr 37 | #' \code{stat, pval} \tab The statistic and corresponding p-value (see references for more details). \cr 38 | #' } 39 | #' 40 | #' @references 41 | #' 42 | #' Lavigne C, Ricci B, Franck P, Senoussi R. 2010. Spatial analyses of 43 | #' ecological count data: A density map comparison approach. Basic and Applied 44 | #' Ecology. 11:734–742. 45 | #' 46 | #' @examples 47 | #' set.seed(123) 48 | #' my_res <- mapcomp(codling_moths, delta = 1, bandwidth = 11, 49 | #' edge_correction = FALSE, nperm = 20) 50 | #' my_res 51 | #' plot(my_res) 52 | #' 53 | #' set.seed(123) 54 | #' my_count <- count(codling_moths, mapping(x = xm, y = ym)) 55 | #' my_res <- mapcomp(my_count, delta = 1, bandwidth = 11, 56 | #' edge_correction = FALSE, nperm = 20) 57 | #' my_res 58 | #' plot(my_res, bins = 10) 59 | #' 60 | #' @name mapcomp 61 | #' @export 62 | #------------------------------------------------------------------------------# 63 | mapcomp <- function(data, ...) UseMethod("mapcomp") 64 | 65 | #------------------------------------------------------------------------------# 66 | #' @rdname mapcomp 67 | #' @method mapcomp data.frame 68 | #' @export 69 | #------------------------------------------------------------------------------# 70 | mapcomp.data.frame <- function(data, delta, bandwidth, nperm = 100, 71 | edge_correction = FALSE, threads = 1, 72 | verbose = TRUE, ...) { 73 | 74 | if (!verbose) { 75 | op <- pbapply::pboptions(type = "none") 76 | } 77 | 78 | dots <- list(...) 79 | if (is.null(call <- dots[["call"]])) { 80 | call <- match.call() 81 | } 82 | # data structure: 83 | # - 1st and 2nd columns: x and y coordinates, respectively. 84 | # - 3rd column: observed disease intensity data. 85 | stopifnot(ncol(data) == 3) 86 | colnames(data) <- c("x", "y", "i") 87 | # ^ Needed for ggplot2 figures and to simplify the code below. 88 | 89 | # delta = mesh size of G (delta = delta_min here) 90 | # Define the mesh G 91 | grid_inter <- mesh_intersect(data, delta_min = delta) 92 | flat_data <- data 93 | flat_data[, "i"] <- 1 94 | 95 | sub_mapcomp <- function(data, flat_data, grid_inter, delta, bandwidth, 96 | edge_correction) { 97 | phs <- p_hscaled(grid_inter, data, bandwidth, edge_correction) 98 | qhs <- p_hscaled(grid_inter, flat_data, bandwidth, edge_correction) 99 | stat <- delta / sqrt(2) * sqrt(sum((sqrt(phs) - sqrt(qhs))^2)) 100 | list(phs = phs, qhs = qhs, stat = stat) 101 | } 102 | 103 | res <- sub_mapcomp(data, flat_data, grid_inter, delta, bandwidth, 104 | edge_correction) 105 | 106 | randomizations <- pbapply::pbsapply(seq_len(nperm), function(i) { 107 | new_data <- data 108 | new_data[, 3] <- sample(new_data[, 3]) 109 | res <- sub_mapcomp(new_data, flat_data, grid_inter, 110 | delta, bandwidth, edge_correction) 111 | res[["stat"]] 112 | }, cl = threads) 113 | 114 | coord <- data.frame(grid_inter, phs = res[["phs"]]) 115 | res <- list(object = class(data), 116 | bandwidth = bandwidth, 117 | data = data, 118 | coord = coord, 119 | stat = res[["stat"]], 120 | pval = (sum(randomizations > res[["stat"]]) + 1) / (nperm + 1)) # To double check 121 | attr(res, "class") <- "mapcomp" 122 | attr(res, "call") <- call 123 | 124 | if (!verbose) { 125 | pbapply::pboptions(op) 126 | } 127 | 128 | res 129 | } 130 | 131 | #------------------------------------------------------------------------------# 132 | #' @rdname mapcomp 133 | #' @export 134 | #------------------------------------------------------------------------------# 135 | mapcomp.matrix <- function(data, delta, bandwidth, nperm = 100, 136 | edge_correction = FALSE, threads = 1, 137 | verbose = TRUE, ...) { 138 | mapcomp.data.frame(as.data.frame(data), delta, bandwidth, nperm, 139 | edge_correction, threads, verbose, ..., 140 | call = match.call()) 141 | } 142 | 143 | #------------------------------------------------------------------------------# 144 | #' @rdname mapcomp 145 | #' @export 146 | #------------------------------------------------------------------------------# 147 | mapcomp.count <- function(data, delta, bandwidth, nperm = 100, 148 | edge_correction = FALSE, threads = 1, verbose = TRUE, 149 | ...) { 150 | mapped_data <- map_data(data) 151 | mapped_data <- mapped_data[, c("x", "y", "i")] # no t 152 | mapcomp.data.frame(mapped_data, delta, bandwidth, nperm, edge_correction, 153 | threads, verbose, ..., call = match.call()) 154 | } 155 | 156 | #------------------------------------------------------------------------------# 157 | #' @rdname mapcomp 158 | #' @export 159 | #------------------------------------------------------------------------------# 160 | mapcomp.incidence <- function(data, delta, bandwidth, nperm = 100, 161 | edge_correction = FALSE, threads = 1, 162 | verbose = TRUE, ...) { 163 | mapped_data <- map_data(data) 164 | mapped_data <- mapped_data[, c("x", "y", "i")] # no t, no n 165 | mapcomp.data.frame(mapped_data, delta, bandwidth, nperm, edge_correction, 166 | threads, verbose, ..., call = match.call()) 167 | } 168 | 169 | 170 | #==============================================================================# 171 | # Print, summary and plot 172 | #==============================================================================# 173 | 174 | #------------------------------------------------------------------------------# 175 | #' @export 176 | #------------------------------------------------------------------------------# 177 | print.mapcomp <- function(x, ...) { 178 | cat("Map Comparison analysis (mapcomp)\n") 179 | cat("\nCall:\n") 180 | print(attr(x, "call")) 181 | cat("\nStat: ", format(x[["stat"]], digits = 1, nsmall = 4), 182 | " (P = ", format.pval(x[["pval"]]), ")\n\n", sep = "") 183 | } 184 | 185 | #------------------------------------------------------------------------------# 186 | #' @export 187 | #------------------------------------------------------------------------------# 188 | plot.mapcomp <- function(x, bins = 5,...) { 189 | gg <- ggplot() 190 | gg <- gg + geom_raster(inherit.aes = FALSE, data = x$coord, 191 | aes(x, y, fill = phs)) 192 | gg <- gg + geom_contour(inherit.aes = FALSE, data = x$coord, 193 | aes(x, y, z = phs), 194 | bins = bins, size = 0.6, color = "black") 195 | gg <- gg + geom_point(inherit.aes = FALSE, data = x$data, 196 | aes(x, y, size = i)) 197 | gg <- gg + scale_size_continuous("Observed\nintensity") 198 | gg <- gg + scale_fill_gradient(paste0("Theoretical\nnormalized\n", 199 | "intensity for a\n", 200 | "bandwidth\nh = ", x[["bandwidth"]]), 201 | low = "white", high = "red") 202 | gg <- gg + theme_bw() 203 | print(gg) 204 | invisible(NULL) 205 | } 206 | 207 | 208 | #==============================================================================# 209 | # Utilities 210 | #==============================================================================# 211 | 212 | mesh_intersect <- function(sites, delta_min, delta_max = 2 * delta_min, ..., 213 | threads = 1) { 214 | xrange <- range(sites[, "x"]) + c(-delta_min, delta_min) 215 | yrange <- range(sites[, "y"]) + c(-delta_min, delta_min) 216 | expand.grid(x = seq(xrange[1], xrange[2], by = delta_min), 217 | y = seq(yrange[1], yrange[2], by = delta_min), 218 | KEEP.OUT.ATTRS = FALSE) 219 | } 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /R/spatial-hier.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | #' @include utils.R 3 | #' @include intensity-classes.R 4 | #------------------------------------------------------------------------------# 5 | NULL 6 | 7 | #------------------------------------------------------------------------------# 8 | #' Spatial hierarchy analysis. 9 | #' 10 | #' The manner in which the data are collected provides information about 11 | #' aggregation of disease at different levels in a spatial hierarchy (Hughes et 12 | #' al. 1997). For example, a sampling unit (upper level) can be reported as 13 | #' "healthy", if no diseased leaves (lower level) were found within the sampling 14 | #' unit. 15 | #' 16 | #' In a pairwise comparison between levels, the probability that an individual 17 | #' at the lower hierarchical level is diseased is denoted plow, and phigh refers 18 | #' to the probability of disease at the higher level. The relationship between 19 | #' these two probabilities can be written as 20 | #' 21 | #' phigh = 1 - (1 - plow)^nu 22 | #' 23 | #' where n is a parameter ranging from 0 to the corresponding number of 24 | #' individuals at the hierarchical level referenced by plow. If the value of n 25 | #' is equal to the number of individuals at the lower hierarchical level 26 | #' contained in a unit of the higher level (n low ), this suggests that there is 27 | #' no aggregation of disease incidence at the lower level. Conversely, a value 28 | #' of n less than n low is indicative of aggregation at that level. The value of 29 | #' n can be interpreted as an effective sample size (Hughes and Gottwald 1999; 30 | #' Madden and Hughes 1999) in the statistical sense that its value indicates the 31 | #' number of independent pieces of information at the lower level. Here, the 32 | #' effective sample size concerns the equating of the zero-term of the binomial 33 | #' distribution with the zero-term of an overdispersed distribution, as 34 | #' described in Madden and Hughes (1999). Using the complementary log-log 35 | #' transformation, CLL(x) = ln(-ln(1-x)), one can rewrite the Equation 5 as 36 | #' follows (Madden et al. 2007): 37 | #' 38 | #' CLL(phigh) = ln(nu) + CLL(plow) 39 | #' 40 | #' from which the value of ln(n) can be obtained as the intercept of a linear 41 | #' regression when the slope is constrained to 1. 42 | #' 43 | #' @param low An list of \code{intensity} objects. 44 | #' @param high An list of \code{intensity} objects. 45 | #' 46 | #' @returns A \code{spatial_hier} object. 47 | #' 48 | #' @examples 49 | # # my_data_low <- incidence(tomato_tswv$field_1928) 50 | # # TODO: 2 bugs to correct here (before) 51 | # # my_data_low <- split(my_data_low, by = "t")[[1]] 52 | # # my_data_high <- clump(my_data_low, unit_size = c(x = 3, y = 3)) 53 | #' my_data_low <- incidence(tomato_tswv$field_1929) 54 | #' my_data_low <- clump(my_data_low, c(x = 3, y = 3)) 55 | #' my_data_high <- my_data_low 56 | #' my_data_high$data$n <- 1 57 | #' my_data_high$data$i <- ifelse(my_data_high$data$i > 0, 1, 0) 58 | #' my_data_low <- split(my_data_low, by = "t") 59 | #' my_data_high <- split(my_data_high, by = "t") 60 | #' res <- spatial_hier(my_data_low, my_data_high) 61 | #' 62 | #' res 63 | #' summary(res) 64 | #' plot(res) 65 | #' 66 | #' @export 67 | #------------------------------------------------------------------------------# 68 | spatial_hier <- function(low, high) { 69 | 70 | call <- match.call() 71 | # Checks and variable allocation: 72 | if (missing(low) || missing(high)) { 73 | stop("Both 'low' and 'high' must be provided.") 74 | } 75 | object_class_low <- unique(vapply(low, function(x) class(x)[[1L]], 76 | character(1L))) 77 | object_class_high <- unique(vapply(high, function(x) class(x)[[1L]], 78 | character(1L))) 79 | stopifnot(length(object_class_low) == 1 && length(object_class_high) == 1) 80 | stopifnot(object_class_low == object_class_high) 81 | if (length(low) != length(high)) { 82 | stop("'low' and 'high' lengths differ.") 83 | } 84 | if (object_class_low[1L] != "incidence") { 85 | stop("Only spatial hierarchy analysis for 'incidence' data is implemented.") 86 | } 87 | data_class <- object_class_low 88 | 89 | # Get formatted data for low and high: 90 | data_low <- get_fmt_obs(low, type = "incidence") 91 | data_high <- get_fmt_obs(high, type = "incidence") 92 | 93 | # Compute mean n: 94 | n_low <- mean(vapply(data_low, function(obj) mean(obj$n), numeric(1L))) 95 | n_high <- mean(vapply(data_high, function(obj) mean(obj$n), numeric(1L))) 96 | if (n_high != 1) { 97 | stop(paste0("Number of individuals per sampling units for 'high' must ", 98 | "be equal to 1.")) 99 | } 100 | 101 | # Compute mean p for each data set: 102 | p_low <- vapply(data_low, function(obj) mean(obj$p), numeric(1L)) 103 | p_high <- vapply(data_high, function(obj) mean(obj$p), numeric(1L)) 104 | 105 | # Perform the analysis 106 | coord_obs <- data.frame(x = p_low, y = p_high) 107 | data <- cloglog(coord_obs) # TODO: Offer the possibility to use other log base 108 | data <- data[with(data, is.finite(x) & is.finite(y)), ] 109 | if ((nr <- nrow(data)) < 1) { 110 | stop("Too few finite cloglog data to perform a regression.") 111 | } else if (nr < 5) { 112 | warning(paste0("Only ", nr, " cloglog data points were used to ", 113 | "perform the regression.")) 114 | } 115 | model <- lm(y ~ offset(x), data = data) 116 | # ^ is the same as lm((y - x) ~ 1, ...), i.e. we just look for an intercept. 117 | param <- coef(summary(model)) 118 | rownames(param) <- "log_nu" 119 | baseLog <- exp(1) 120 | new_param <- list(nu = estimate_param(model, bquote(.(baseLog)^x1))) 121 | param <- rbind_param(param, new_param) 122 | param <- param[sort(rownames(param)),, drop = FALSE] # Not need, but same logic as in other functions. 123 | 124 | coord_the <- data.frame(x = p_low, y = 1 - (1 - p_low)^param["nu", "Estimate"]) 125 | 126 | # Return a spatial_hier object 127 | structure(list(call = call, # TODO: Add more information about the transformation? 128 | data_class = data_class, 129 | model = model, 130 | param = param, 131 | n = n_low, # TODO: Where to put n??? 132 | coord_obs = coord_obs, 133 | coord_the = coord_the), 134 | class = "spatial_hier") 135 | } 136 | 137 | #------------------------------------------------------------------------------# 138 | #' @export 139 | #------------------------------------------------------------------------------# 140 | print.spatial_hier <- function(x, ...) { # TODO 141 | cat("Spatial hierarchy analysis for '", x$data_class[1L], "' data.\n\n", 142 | "Parameter estimate:\n", sep = "") 143 | print(x$param[, 1:2, drop = FALSE]) 144 | invisible(x) 145 | } 146 | 147 | #------------------------------------------------------------------------------# 148 | #' @export 149 | #------------------------------------------------------------------------------# 150 | summary.spatial_hier <- function(object, ...) { 151 | structure(list(call = object$call, 152 | model = object$model, 153 | coefficients = object$param 154 | ), class = "summary.spatial_hier") 155 | } 156 | 157 | #------------------------------------------------------------------------------# 158 | #' @method print summary.spatial_hier 159 | #' @export 160 | #------------------------------------------------------------------------------# 161 | print.summary.spatial_hier <- function(x, ...) { 162 | res <- summary.lm(x$model, ...) 163 | res$call <- x$call 164 | res$coefficients <- x$coefficients 165 | print(res) 166 | invisible(res) 167 | } 168 | 169 | #------------------------------------------------------------------------------# 170 | #' @export 171 | #------------------------------------------------------------------------------# 172 | plot.spatial_hier <- function(x, ..., scale = c("linear", "cloglog"), 173 | observed = TRUE, model = TRUE, random = TRUE) { 174 | scale <- match.arg(scale) 175 | gg <- ggplot() 176 | 177 | switch (scale, 178 | "linear" = { 179 | gg <- gg + labs(x = expression(p[low]), y = expression(p[high])) 180 | gg <- gg + scale_x_continuous(limits = c(0, 1)) 181 | gg <- gg + scale_y_continuous(limits = c(0, 1)) 182 | if (observed) { 183 | gg <- gg + geom_point(data = x[["coord_obs"]], aes(x, y), ...) 184 | } 185 | if (model) { 186 | p_low <- seq(0, 1, by = 0.01) 187 | p_high <- 1 - (1 - p_low)^(x$param["nu", "Estimate"]) 188 | gg <- gg + geom_line(data = data.frame(x = p_low, y = p_high), 189 | aes(x, y), ...) 190 | } 191 | if (random) { 192 | p_low <- seq(0, 1, by = 0.01) 193 | p_high <- 1 - (1 - p_low)^(x[["n"]]) 194 | gg <- gg + geom_line(data = data.frame(x = p_low, y = p_high), 195 | aes(x, y), linetype = "dashed", ...) 196 | } 197 | }, 198 | "cloglog" = { 199 | gg <- gg + labs(x = expression("cloglog(" * p[low] * ")"), 200 | y = expression("cloglog(" * p[high] * ")")) 201 | if (observed) { 202 | gg <- gg + geom_point(data = cloglog(x[["coord_obs"]]), 203 | aes(x, y), ...) 204 | } 205 | if (model) { 206 | p_low <- seq(0, 1, by = 0.01) 207 | p_high <- 1 - (1 - p_low)^(x$param["nu", "Estimate"]) 208 | gg <- gg + geom_line(data = cloglog(data.frame(x = p_low, y = p_high)), 209 | aes(x, y), ...) 210 | } 211 | if (random) { 212 | p_low <- seq(0, 1, by = 0.001) 213 | p_high <- 1 - (1 - p_low)^(x[["n"]]) 214 | gg <- gg + geom_line(data = cloglog(data.frame(x = p_low, y = p_high)), 215 | aes(x, y), linetype = "dashed", ...) 216 | } 217 | } 218 | ) 219 | gg <- gg + theme_bw() 220 | print(gg) 221 | invisible(NULL) 222 | } 223 | 224 | -------------------------------------------------------------------------------- /data-raw/citrus_ctv_IVIA6and7.csv: -------------------------------------------------------------------------------- 1 | x,y,t,i,n 2 | 1,1,1990,1,1 3 | 2,1,1990,1,1 4 | 3,1,1990,0,1 5 | 4,1,1990,0,1 6 | 5,1,1990,0,1 7 | 6,1,1990,0,1 8 | 7,1,1990,1,1 9 | 8,1,1990,1,1 10 | 9,1,1990,0,1 11 | 10,1,1990,1,1 12 | 11,1,1990,0,1 13 | 12,1,1990,1,1 14 | 13,1,1990,0,1 15 | 14,1,1990,0,1 16 | 15,1,1990,0,1 17 | 16,1,1990,0,1 18 | 17,1,1990,0,1 19 | 18,1,1990,0,1 20 | 1,2,1990,1,1 21 | 2,2,1990,0,1 22 | 3,2,1990,0,1 23 | 4,2,1990,0,1 24 | 5,2,1990,0,1 25 | 6,2,1990,1,1 26 | 7,2,1990,0,1 27 | 8,2,1990,0,1 28 | 9,2,1990,0,1 29 | 10,2,1990,1,1 30 | 11,2,1990,1,1 31 | 12,2,1990,0,1 32 | 13,2,1990,0,1 33 | 14,2,1990,1,1 34 | 15,2,1990,1,1 35 | 16,2,1990,0,1 36 | 17,2,1990,0,1 37 | 18,2,1990,0,1 38 | 1,3,1990,0,1 39 | 2,3,1990,1,1 40 | 3,3,1990,1,1 41 | 4,3,1990,1,1 42 | 5,3,1990,0,1 43 | 6,3,1990,0,1 44 | 7,3,1990,0,1 45 | 8,3,1990,0,1 46 | 9,3,1990,0,1 47 | 10,3,1990,0,1 48 | 11,3,1990,0,1 49 | 12,3,1990,0,1 50 | 13,3,1990,0,1 51 | 14,3,1990,0,1 52 | 15,3,1990,0,1 53 | 16,3,1990,0,1 54 | 17,3,1990,0,1 55 | 18,3,1990,0,1 56 | 1,4,1990,1,1 57 | 2,4,1990,0,1 58 | 3,4,1990,0,1 59 | 4,4,1990,0,1 60 | 5,4,1990,0,1 61 | 6,4,1990,1,1 62 | 7,4,1990,0,1 63 | 8,4,1990,0,1 64 | 9,4,1990,0,1 65 | 10,4,1990,0,1 66 | 11,4,1990,0,1 67 | 12,4,1990,0,1 68 | 13,4,1990,0,1 69 | 14,4,1990,0,1 70 | 15,4,1990,0,1 71 | 16,4,1990,0,1 72 | 17,4,1990,0,1 73 | 18,4,1990,0,1 74 | 1,5,1990,1,1 75 | 2,5,1990,0,1 76 | 3,5,1990,1,1 77 | 4,5,1990,0,1 78 | 5,5,1990,1,1 79 | 6,5,1990,0,1 80 | 7,5,1990,0,1 81 | 8,5,1990,0,1 82 | 9,5,1990,1,1 83 | 10,5,1990,0,1 84 | 11,5,1990,1,1 85 | 12,5,1990,0,1 86 | 13,5,1990,0,1 87 | 14,5,1990,0,1 88 | 15,5,1990,0,1 89 | 16,5,1990,0,1 90 | 17,5,1990,0,1 91 | 18,5,1990,0,1 92 | 1,6,1990,0,1 93 | 2,6,1990,0,1 94 | 3,6,1990,0,1 95 | 4,6,1990,1,1 96 | 5,6,1990,1,1 97 | 6,6,1990,1,1 98 | 7,6,1990,1,1 99 | 8,6,1990,0,1 100 | 9,6,1990,0,1 101 | 10,6,1990,1,1 102 | 11,6,1990,0,1 103 | 12,6,1990,1,1 104 | 13,6,1990,0,1 105 | 14,6,1990,0,1 106 | 15,6,1990,0,1 107 | 16,6,1990,0,1 108 | 17,6,1990,0,1 109 | 18,6,1990,0,1 110 | 1,7,1990,0,1 111 | 2,7,1990,0,1 112 | 3,7,1990,0,1 113 | 4,7,1990,0,1 114 | 5,7,1990,0,1 115 | 6,7,1990,0,1 116 | 7,7,1990,1,1 117 | 8,7,1990,0,1 118 | 9,7,1990,0,1 119 | 10,7,1990,0,1 120 | 11,7,1990,1,1 121 | 12,7,1990,0,1 122 | 13,7,1990,0,1 123 | 14,7,1990,1,1 124 | 15,7,1990,0,1 125 | 16,7,1990,0,1 126 | 17,7,1990,0,1 127 | 18,7,1990,0,1 128 | 1,8,1990,1,1 129 | 2,8,1990,0,1 130 | 3,8,1990,0,1 131 | 4,8,1990,0,1 132 | 5,8,1990,0,1 133 | 6,8,1990,0,1 134 | 7,8,1990,0,1 135 | 8,8,1990,0,1 136 | 9,8,1990,1,1 137 | 10,8,1990,0,1 138 | 11,8,1990,0,1 139 | 12,8,1990,0,1 140 | 13,8,1990,0,1 141 | 14,8,1990,0,1 142 | 15,8,1990,0,1 143 | 16,8,1990,0,1 144 | 17,8,1990,0,1 145 | 18,8,1990,0,1 146 | 1,9,1990,1,1 147 | 2,9,1990,1,1 148 | 3,9,1990,1,1 149 | 4,9,1990,0,1 150 | 5,9,1990,0,1 151 | 6,9,1990,1,1 152 | 7,9,1990,0,1 153 | 8,9,1990,0,1 154 | 9,9,1990,0,1 155 | 10,9,1990,0,1 156 | 11,9,1990,0,1 157 | 12,9,1990,0,1 158 | 13,9,1990,0,1 159 | 14,9,1990,0,1 160 | 15,9,1990,1,1 161 | 16,9,1990,1,1 162 | 17,9,1990,1,1 163 | 18,9,1990,1,1 164 | 1,10,1990,1,1 165 | 2,10,1990,0,1 166 | 3,10,1990,0,1 167 | 4,10,1990,0,1 168 | 5,10,1990,0,1 169 | 6,10,1990,1,1 170 | 7,10,1990,1,1 171 | 8,10,1990,1,1 172 | 9,10,1990,0,1 173 | 10,10,1990,0,1 174 | 11,10,1990,0,1 175 | 12,10,1990,1,1 176 | 13,10,1990,1,1 177 | 14,10,1990,0,1 178 | 15,10,1990,1,1 179 | 16,10,1990,0,1 180 | 17,10,1990,0,1 181 | 18,10,1990,1,1 182 | 1,11,1990,1,1 183 | 2,11,1990,1,1 184 | 3,11,1990,0,1 185 | 4,11,1990,0,1 186 | 5,11,1990,0,1 187 | 6,11,1990,0,1 188 | 7,11,1990,0,1 189 | 8,11,1990,0,1 190 | 9,11,1990,0,1 191 | 10,11,1990,0,1 192 | 11,11,1990,0,1 193 | 12,11,1990,0,1 194 | 13,11,1990,0,1 195 | 14,11,1990,0,1 196 | 15,11,1990,0,1 197 | 16,11,1990,0,1 198 | 17,11,1990,1,1 199 | 18,11,1990,0,1 200 | 1,12,1990,1,1 201 | 2,12,1990,1,1 202 | 3,12,1990,0,1 203 | 4,12,1990,1,1 204 | 5,12,1990,1,1 205 | 6,12,1990,0,1 206 | 7,12,1990,0,1 207 | 8,12,1990,0,1 208 | 9,12,1990,0,1 209 | 10,12,1990,1,1 210 | 11,12,1990,0,1 211 | 12,12,1990,0,1 212 | 13,12,1990,0,1 213 | 14,12,1990,1,1 214 | 15,12,1990,1,1 215 | 16,12,1990,0,1 216 | 17,12,1990,0,1 217 | 18,12,1990,1,1 218 | 1,1,1991,1,1 219 | 2,1,1991,1,1 220 | 3,1,1991,0,1 221 | 4,1,1991,0,1 222 | 5,1,1991,0,1 223 | 6,1,1991,0,1 224 | 7,1,1991,1,1 225 | 8,1,1991,1,1 226 | 9,1,1991,0,1 227 | 10,1,1991,1,1 228 | 11,1,1991,0,1 229 | 12,1,1991,1,1 230 | 13,1,1991,0,1 231 | 14,1,1991,0,1 232 | 15,1,1991,0,1 233 | 16,1,1991,0,1 234 | 17,1,1991,0,1 235 | 18,1,1991,0,1 236 | 1,2,1991,1,1 237 | 2,2,1991,0,1 238 | 3,2,1991,0,1 239 | 4,2,1991,0,1 240 | 5,2,1991,0,1 241 | 6,2,1991,1,1 242 | 7,2,1991,0,1 243 | 8,2,1991,0,1 244 | 9,2,1991,0,1 245 | 10,2,1991,1,1 246 | 11,2,1991,1,1 247 | 12,2,1991,0,1 248 | 13,2,1991,0,1 249 | 14,2,1991,1,1 250 | 15,2,1991,1,1 251 | 16,2,1991,0,1 252 | 17,2,1991,0,1 253 | 18,2,1991,0,1 254 | 1,3,1991,0,1 255 | 2,3,1991,1,1 256 | 3,3,1991,1,1 257 | 4,3,1991,1,1 258 | 5,3,1991,0,1 259 | 6,3,1991,0,1 260 | 7,3,1991,0,1 261 | 8,3,1991,0,1 262 | 9,3,1991,0,1 263 | 10,3,1991,0,1 264 | 11,3,1991,0,1 265 | 12,3,1991,0,1 266 | 13,3,1991,0,1 267 | 14,3,1991,0,1 268 | 15,3,1991,0,1 269 | 16,3,1991,0,1 270 | 17,3,1991,0,1 271 | 18,3,1991,0,1 272 | 1,4,1991,1,1 273 | 2,4,1991,0,1 274 | 3,4,1991,0,1 275 | 4,4,1991,0,1 276 | 5,4,1991,0,1 277 | 6,4,1991,1,1 278 | 7,4,1991,0,1 279 | 8,4,1991,0,1 280 | 9,4,1991,1,1 281 | 10,4,1991,0,1 282 | 11,4,1991,0,1 283 | 12,4,1991,0,1 284 | 13,4,1991,0,1 285 | 14,4,1991,0,1 286 | 15,4,1991,0,1 287 | 16,4,1991,0,1 288 | 17,4,1991,0,1 289 | 18,4,1991,0,1 290 | 1,5,1991,1,1 291 | 2,5,1991,1,1 292 | 3,5,1991,1,1 293 | 4,5,1991,1,1 294 | 5,5,1991,1,1 295 | 6,5,1991,0,1 296 | 7,5,1991,1,1 297 | 8,5,1991,1,1 298 | 9,5,1991,1,1 299 | 10,5,1991,0,1 300 | 11,5,1991,1,1 301 | 12,5,1991,0,1 302 | 13,5,1991,0,1 303 | 14,5,1991,0,1 304 | 15,5,1991,0,1 305 | 16,5,1991,0,1 306 | 17,5,1991,0,1 307 | 18,5,1991,0,1 308 | 1,6,1991,0,1 309 | 2,6,1991,0,1 310 | 3,6,1991,1,1 311 | 4,6,1991,1,1 312 | 5,6,1991,1,1 313 | 6,6,1991,1,1 314 | 7,6,1991,1,1 315 | 8,6,1991,0,1 316 | 9,6,1991,0,1 317 | 10,6,1991,1,1 318 | 11,6,1991,0,1 319 | 12,6,1991,1,1 320 | 13,6,1991,0,1 321 | 14,6,1991,0,1 322 | 15,6,1991,0,1 323 | 16,6,1991,0,1 324 | 17,6,1991,0,1 325 | 18,6,1991,0,1 326 | 1,7,1991,0,1 327 | 2,7,1991,0,1 328 | 3,7,1991,0,1 329 | 4,7,1991,0,1 330 | 5,7,1991,1,1 331 | 6,7,1991,0,1 332 | 7,7,1991,1,1 333 | 8,7,1991,0,1 334 | 9,7,1991,0,1 335 | 10,7,1991,0,1 336 | 11,7,1991,1,1 337 | 12,7,1991,0,1 338 | 13,7,1991,0,1 339 | 14,7,1991,1,1 340 | 15,7,1991,0,1 341 | 16,7,1991,0,1 342 | 17,7,1991,1,1 343 | 18,7,1991,0,1 344 | 1,8,1991,1,1 345 | 2,8,1991,0,1 346 | 3,8,1991,0,1 347 | 4,8,1991,0,1 348 | 5,8,1991,0,1 349 | 6,8,1991,0,1 350 | 7,8,1991,0,1 351 | 8,8,1991,0,1 352 | 9,8,1991,1,1 353 | 10,8,1991,1,1 354 | 11,8,1991,0,1 355 | 12,8,1991,0,1 356 | 13,8,1991,0,1 357 | 14,8,1991,1,1 358 | 15,8,1991,1,1 359 | 16,8,1991,0,1 360 | 17,8,1991,0,1 361 | 18,8,1991,0,1 362 | 1,9,1991,1,1 363 | 2,9,1991,1,1 364 | 3,9,1991,1,1 365 | 4,9,1991,0,1 366 | 5,9,1991,0,1 367 | 6,9,1991,1,1 368 | 7,9,1991,0,1 369 | 8,9,1991,0,1 370 | 9,9,1991,0,1 371 | 10,9,1991,1,1 372 | 11,9,1991,0,1 373 | 12,9,1991,0,1 374 | 13,9,1991,1,1 375 | 14,9,1991,0,1 376 | 15,9,1991,1,1 377 | 16,9,1991,1,1 378 | 17,9,1991,1,1 379 | 18,9,1991,1,1 380 | 1,10,1991,1,1 381 | 2,10,1991,1,1 382 | 3,10,1991,1,1 383 | 4,10,1991,0,1 384 | 5,10,1991,0,1 385 | 6,10,1991,1,1 386 | 7,10,1991,1,1 387 | 8,10,1991,1,1 388 | 9,10,1991,1,1 389 | 10,10,1991,0,1 390 | 11,10,1991,0,1 391 | 12,10,1991,1,1 392 | 13,10,1991,1,1 393 | 14,10,1991,0,1 394 | 15,10,1991,1,1 395 | 16,10,1991,0,1 396 | 17,10,1991,0,1 397 | 18,10,1991,1,1 398 | 1,11,1991,1,1 399 | 2,11,1991,1,1 400 | 3,11,1991,0,1 401 | 4,11,1991,0,1 402 | 5,11,1991,0,1 403 | 6,11,1991,0,1 404 | 7,11,1991,0,1 405 | 8,11,1991,0,1 406 | 9,11,1991,0,1 407 | 10,11,1991,0,1 408 | 11,11,1991,0,1 409 | 12,11,1991,0,1 410 | 13,11,1991,0,1 411 | 14,11,1991,0,1 412 | 15,11,1991,0,1 413 | 16,11,1991,0,1 414 | 17,11,1991,1,1 415 | 18,11,1991,0,1 416 | 1,12,1991,1,1 417 | 2,12,1991,1,1 418 | 3,12,1991,0,1 419 | 4,12,1991,1,1 420 | 5,12,1991,1,1 421 | 6,12,1991,0,1 422 | 7,12,1991,0,1 423 | 8,12,1991,0,1 424 | 9,12,1991,0,1 425 | 10,12,1991,1,1 426 | 11,12,1991,0,1 427 | 12,12,1991,0,1 428 | 13,12,1991,1,1 429 | 14,12,1991,1,1 430 | 15,12,1991,1,1 431 | 16,12,1991,0,1 432 | 17,12,1991,0,1 433 | 18,12,1991,1,1 434 | 1,1,1992,1,1 435 | 2,1,1992,1,1 436 | 3,1,1992,0,1 437 | 4,1,1992,0,1 438 | 5,1,1992,1,1 439 | 6,1,1992,1,1 440 | 7,1,1992,1,1 441 | 8,1,1992,1,1 442 | 9,1,1992,1,1 443 | 10,1,1992,1,1 444 | 11,1,1992,0,1 445 | 12,1,1992,1,1 446 | 13,1,1992,0,1 447 | 14,1,1992,0,1 448 | 15,1,1992,0,1 449 | 16,1,1992,0,1 450 | 17,1,1992,0,1 451 | 18,1,1992,0,1 452 | 1,2,1992,1,1 453 | 2,2,1992,0,1 454 | 3,2,1992,0,1 455 | 4,2,1992,1,1 456 | 5,2,1992,0,1 457 | 6,2,1992,1,1 458 | 7,2,1992,1,1 459 | 8,2,1992,1,1 460 | 9,2,1992,0,1 461 | 10,2,1992,1,1 462 | 11,2,1992,1,1 463 | 12,2,1992,0,1 464 | 13,2,1992,0,1 465 | 14,2,1992,1,1 466 | 15,2,1992,1,1 467 | 16,2,1992,0,1 468 | 17,2,1992,0,1 469 | 18,2,1992,1,1 470 | 1,3,1992,1,1 471 | 2,3,1992,1,1 472 | 3,3,1992,1,1 473 | 4,3,1992,1,1 474 | 5,3,1992,0,1 475 | 6,3,1992,1,1 476 | 7,3,1992,1,1 477 | 8,3,1992,0,1 478 | 9,3,1992,1,1 479 | 10,3,1992,0,1 480 | 11,3,1992,0,1 481 | 12,3,1992,0,1 482 | 13,3,1992,0,1 483 | 14,3,1992,1,1 484 | 15,3,1992,0,1 485 | 16,3,1992,0,1 486 | 17,3,1992,0,1 487 | 18,3,1992,0,1 488 | 1,4,1992,1,1 489 | 2,4,1992,0,1 490 | 3,4,1992,0,1 491 | 4,4,1992,0,1 492 | 5,4,1992,1,1 493 | 6,4,1992,1,1 494 | 7,4,1992,0,1 495 | 8,4,1992,0,1 496 | 9,4,1992,1,1 497 | 10,4,1992,1,1 498 | 11,4,1992,1,1 499 | 12,4,1992,1,1 500 | 13,4,1992,0,1 501 | 14,4,1992,0,1 502 | 15,4,1992,0,1 503 | 16,4,1992,0,1 504 | 17,4,1992,1,1 505 | 18,4,1992,0,1 506 | 1,5,1992,1,1 507 | 2,5,1992,1,1 508 | 3,5,1992,1,1 509 | 4,5,1992,1,1 510 | 5,5,1992,1,1 511 | 6,5,1992,0,1 512 | 7,5,1992,1,1 513 | 8,5,1992,1,1 514 | 9,5,1992,1,1 515 | 10,5,1992,0,1 516 | 11,5,1992,1,1 517 | 12,5,1992,0,1 518 | 13,5,1992,0,1 519 | 14,5,1992,0,1 520 | 15,5,1992,0,1 521 | 16,5,1992,0,1 522 | 17,5,1992,0,1 523 | 18,5,1992,1,1 524 | 1,6,1992,1,1 525 | 2,6,1992,0,1 526 | 3,6,1992,1,1 527 | 4,6,1992,1,1 528 | 5,6,1992,1,1 529 | 6,6,1992,1,1 530 | 7,6,1992,1,1 531 | 8,6,1992,1,1 532 | 9,6,1992,1,1 533 | 10,6,1992,1,1 534 | 11,6,1992,0,1 535 | 12,6,1992,1,1 536 | 13,6,1992,0,1 537 | 14,6,1992,0,1 538 | 15,6,1992,0,1 539 | 16,6,1992,0,1 540 | 17,6,1992,1,1 541 | 18,6,1992,0,1 542 | 1,7,1992,0,1 543 | 2,7,1992,0,1 544 | 3,7,1992,0,1 545 | 4,7,1992,0,1 546 | 5,7,1992,1,1 547 | 6,7,1992,0,1 548 | 7,7,1992,1,1 549 | 8,7,1992,0,1 550 | 9,7,1992,1,1 551 | 10,7,1992,1,1 552 | 11,7,1992,1,1 553 | 12,7,1992,1,1 554 | 13,7,1992,1,1 555 | 14,7,1992,1,1 556 | 15,7,1992,1,1 557 | 16,7,1992,0,1 558 | 17,7,1992,1,1 559 | 18,7,1992,1,1 560 | 1,8,1992,1,1 561 | 2,8,1992,0,1 562 | 3,8,1992,0,1 563 | 4,8,1992,0,1 564 | 5,8,1992,0,1 565 | 6,8,1992,0,1 566 | 7,8,1992,0,1 567 | 8,8,1992,0,1 568 | 9,8,1992,1,1 569 | 10,8,1992,1,1 570 | 11,8,1992,0,1 571 | 12,8,1992,1,1 572 | 13,8,1992,0,1 573 | 14,8,1992,1,1 574 | 15,8,1992,1,1 575 | 16,8,1992,1,1 576 | 17,8,1992,1,1 577 | 18,8,1992,1,1 578 | 1,9,1992,1,1 579 | 2,9,1992,1,1 580 | 3,9,1992,1,1 581 | 4,9,1992,1,1 582 | 5,9,1992,0,1 583 | 6,9,1992,1,1 584 | 7,9,1992,0,1 585 | 8,9,1992,0,1 586 | 9,9,1992,0,1 587 | 10,9,1992,1,1 588 | 11,9,1992,1,1 589 | 12,9,1992,0,1 590 | 13,9,1992,1,1 591 | 14,9,1992,1,1 592 | 15,9,1992,1,1 593 | 16,9,1992,1,1 594 | 17,9,1992,1,1 595 | 18,9,1992,1,1 596 | 1,10,1992,1,1 597 | 2,10,1992,1,1 598 | 3,10,1992,1,1 599 | 4,10,1992,0,1 600 | 5,10,1992,0,1 601 | 6,10,1992,1,1 602 | 7,10,1992,1,1 603 | 8,10,1992,1,1 604 | 9,10,1992,1,1 605 | 10,10,1992,1,1 606 | 11,10,1992,1,1 607 | 12,10,1992,1,1 608 | 13,10,1992,1,1 609 | 14,10,1992,1,1 610 | 15,10,1992,1,1 611 | 16,10,1992,1,1 612 | 17,10,1992,1,1 613 | 18,10,1992,1,1 614 | 1,11,1992,1,1 615 | 2,11,1992,1,1 616 | 3,11,1992,0,1 617 | 4,11,1992,0,1 618 | 5,11,1992,0,1 619 | 6,11,1992,1,1 620 | 7,11,1992,0,1 621 | 8,11,1992,0,1 622 | 9,11,1992,1,1 623 | 10,11,1992,1,1 624 | 11,11,1992,1,1 625 | 12,11,1992,0,1 626 | 13,11,1992,0,1 627 | 14,11,1992,1,1 628 | 15,11,1992,0,1 629 | 16,11,1992,1,1 630 | 17,11,1992,1,1 631 | 18,11,1992,1,1 632 | 1,12,1992,1,1 633 | 2,12,1992,1,1 634 | 3,12,1992,1,1 635 | 4,12,1992,1,1 636 | 5,12,1992,1,1 637 | 6,12,1992,0,1 638 | 7,12,1992,0,1 639 | 8,12,1992,1,1 640 | 9,12,1992,0,1 641 | 10,12,1992,1,1 642 | 11,12,1992,1,1 643 | 12,12,1992,0,1 644 | 13,12,1992,1,1 645 | 14,12,1992,1,1 646 | 15,12,1992,1,1 647 | 16,12,1992,0,1 648 | 17,12,1992,0,1 649 | 18,12,1992,1,1 650 | -------------------------------------------------------------------------------- /R/power-law.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------# 2 | #' @include utils.R 3 | #' @include intensity-classes.R 4 | #------------------------------------------------------------------------------# 5 | NULL 6 | 7 | #------------------------------------------------------------------------------# 8 | #' Taylor's and binary power laws. 9 | #' 10 | #' Assesses the overall degree of heterogeneity in a collection of data sets at 11 | #' the sampling-unit scale. 12 | #' 13 | #' The power law describes the relationship between the observed variance of 14 | #' individuals within a data set (\code{s^2}) and the corresponding variance 15 | #' under the assumption of no aggregation (\code{s\'^2}). It can be expressed 16 | #' under its logarithmic form as: \code{log(s^2) = log(a) + b log(Y)}, with: 17 | #' \itemize{ 18 | #' \item \code{Y = p} in the case of count data (Taylor's power law). 19 | #' \item \code{Y = p(1 - p)} in the case of incidence data (binary power law). 20 | #' } 21 | #' \code{p} corresponds to the mean proportion of recorded individuals in case 22 | #' of incidence data, and the absolute value in case of count data. 23 | #' 24 | #' @param data A list of \code{intensity} objects (\code{count} or 25 | #' \code{incidence} objects). 26 | #' @param log_base Logarithm base to be used. 27 | #' @param ... Additional arguments to be passed to other methods. 28 | #' 29 | #' @return A \code{power_law} object. 30 | #' 31 | #' @examples 32 | #' require(magrittr) 33 | #' my_data <- do.call(c, lapply(citrus_ctv, function(citrus_field) { 34 | #' incidence(citrus_field) %>% 35 | #' clump(unit_size = c(x = 3, y = 3)) %>% 36 | #' split(by = "t") 37 | #' })) 38 | #' # my_data is a list of incidence object, each one corresponding to a given 39 | #' # time at a given location. 40 | #' my_power_law <- power_law(my_data) 41 | #' my_power_law 42 | #' summary(my_power_law) 43 | #' plot(my_power_law) # Same as: plot(my_power_law, scale = "log") 44 | #' plot(my_power_law, scale = "lin") 45 | #' 46 | #' @references 47 | #' 48 | #' Taylor LR. 1961. Aggregation, variance and the mean. Nature 189: 732–35. 49 | #' 50 | #' Hughes G, Madden LV. 1992. Aggregation and incidence of disease. Plant 51 | #' Pathology 41 (6): 657–660. 52 | #' \doi{10.1111/j.1365-3059.1992.tb02549.x} 53 | #' 54 | #' Madden LV, Hughes G, van den Bosch F. 2007. Spatial aspects of epidemics - 55 | #' III: Patterns of plant disease. In: The study of plant disease epidemics, 56 | #' 235–278. American Phytopathological Society, St Paul, MN. 57 | #' 58 | #' @export 59 | #------------------------------------------------------------------------------# 60 | power_law <- function(data, log_base = exp(1), ...) { 61 | 62 | call <- match.call() 63 | # Checks: 64 | stopifnot(is.list(data)) 65 | if (length(data) < 2) { 66 | stop("Less than 2 points is not enough to perform linear regressions.") 67 | } 68 | object_class <- unique(vapply(data, function(x) class(x)[[1L]], 69 | character(1L))) 70 | stopifnot(length(object_class) == 1) 71 | stopifnot(object_class %in% c("count", "incidence")) 72 | 73 | # Perform power law analysis: 74 | switch(object_class, 75 | "count" = { 76 | name <- "Taylor's Power Law" 77 | data <- get_fmt_obs(data, type = object_class) 78 | x <- vapply(data, mean, numeric(1L)) 79 | y <- vapply(data, var, numeric(1L)) 80 | }, 81 | "incidence" = { 82 | name <- "Binary Power Law" 83 | data <- get_fmt_obs(data, type = object_class) 84 | # For incidence data as proportions: 85 | # v_t = p(1 - p)/n (Madden & Hughes, 1995) 86 | x <- vapply(data, function(obj) { 87 | with(obj, (mean(p) * (1 - mean(p))) / mean(n)) 88 | }, numeric(1L)) 89 | y <- vapply(data, function(obj) var(obj$p), numeric(1L)) 90 | } 91 | ) 92 | coord_obs <- data.frame(x = x, y = y) 93 | if (log_base == exp(1)) { 94 | # To get a nice display with print and summary 95 | model_formula <- as.formula(log(y) ~ log(x)) 96 | } else { 97 | model_formula <- as.formula(bquote( 98 | log(y, base = .(log_base)) ~ log(x, base = .(log_base)) 99 | )) 100 | } 101 | model <- lm(model_formula, ...) 102 | y_the <- predict(model, type = "response") 103 | coord_the <- data.frame(x = x, y = log_base^(y_the)) 104 | 105 | # Retrieve summary matrice of coefficients, and eventually add some extra 106 | # estimates: 107 | param <- coef(summary(model)) 108 | rownames(param) <- paste0(rownames(param), ": ", c("log_base(Ar)", "b")) 109 | switch (object_class, 110 | "count" = { 111 | # Nothing to do. 112 | }, 113 | "incidence" = { 114 | n <- mean(vapply(data, function(obj) mean(obj$n), numeric(1L))) ### PAS TOP 115 | new_param <- list( 116 | Ai = estimate_param(model, bquote(.(log_base)^x1)), 117 | ai = estimate_param(model, bquote(.(log_base)^x1 * .(n)^(-x2))), 118 | AI = estimate_param(model, bquote(.(log_base)^x1 * .(n)^(2 * (1 - x2)))), 119 | aI = estimate_param(model, bquote(.(log_base)^x1 * .(n)^(2 - x2))) 120 | ) 121 | param <- rbind(param, do.call(rbind, lapply(new_param, unlist))) # TODO: Not clean 122 | } 123 | ) 124 | 125 | # Return the following object: 126 | structure(list(call = call, # TODO: Add more information about the transformation? 127 | data_class = object_class, 128 | name = name, 129 | data = data, # TODO: Useful ??? (not in spatial_hier) 130 | model = model, 131 | param = param, # TODO: Where in n???? 132 | log_base = log_base, 133 | coord_obs = coord_obs, 134 | coord_the = coord_the), 135 | class = "power_law") 136 | } 137 | 138 | #------------------------------------------------------------------------------# 139 | #' @export 140 | #------------------------------------------------------------------------------# 141 | print.power_law <- function(x, ...) { 142 | cat(x$name, ":\n", 143 | "Power law analysis for '", x$data_class[1L], "' data.\n", sep = "") 144 | cat("\nCoefficients:\n") 145 | print(coef(x$model)) 146 | cat("\n") 147 | invisible(x) 148 | } 149 | 150 | #------------------------------------------------------------------------------# 151 | #' @export 152 | #------------------------------------------------------------------------------# 153 | summary.power_law <- function(object, ...) { 154 | structure(list(call = object$call, 155 | model = object$model, 156 | coefficients = object$param 157 | ), class = "summary.power_law") 158 | } 159 | 160 | #------------------------------------------------------------------------------# 161 | #' @method print summary.power_law 162 | #' @export 163 | #------------------------------------------------------------------------------# 164 | print.summary.power_law <- function(x, ...) { 165 | res <- summary.lm(x$model, ...) 166 | res$call <- x$call 167 | res$coefficients <- x$coefficients 168 | print(res) 169 | invisible(res) 170 | } 171 | 172 | #------------------------------------------------------------------------------# 173 | # Plot results of a power law analysis 174 | # 175 | # Plot results of a power law analysis. 176 | # 177 | # @param x A \code{\link{power_law}} object. 178 | # @param scale Logarithmic or standard linear scale to display the results? 179 | # @param observed Logical. 180 | # @param model Logical. 181 | # @param random Logical. Theoretical Random distribution. 182 | # @param ... Additional arguments to be passed to other methods. 183 | # TODO: Dashed lines indicate the cases where both variances are equal, which suggests an absence of aggregation. 184 | # Points should lie on this line if ... 185 | # 186 | #' @export 187 | #------------------------------------------------------------------------------# 188 | plot.power_law <- function(x, ..., scale = c("logarithmic", "linear"), 189 | observed = TRUE, model = TRUE, random = TRUE) { 190 | scale <- match.arg(scale) 191 | log_base <- x$log_base 192 | gg <- ggplot() 193 | 194 | switch (scale, 195 | "logarithmic" = { 196 | log_base_name <- ifelse(log_base == exp(1), "e", 197 | as.character(log_base)) 198 | gg <- gg + 199 | labs(x = bquote(log[.(log_base_name)] * "(binomial variance)"), 200 | y = bquote(log[.(log_base_name)] * "(observed variance)")) 201 | if (observed) { 202 | gg <- gg + geom_point(data = log(x$coord_obs, base = log_base), 203 | aes(x, y), ...) 204 | } 205 | if (model) { 206 | gg <- gg + geom_line(data = log(x$coord_the, base = log_base), 207 | aes(x, y), ...) 208 | } 209 | if (random) { 210 | gg <- gg + geom_line(data = log(x$coord_the, base = log_base), 211 | aes(x, x), linetype = "dashed", ...) 212 | } 213 | }, 214 | "linear" = { 215 | gg <- gg + labs(x = "Binomial variance", y = "Observed variance") 216 | if (observed) { 217 | gg <- gg + geom_point(data = x$coord_obs, aes(x, y), ...) 218 | } 219 | if (model) { 220 | gg <- gg + geom_line(data = x$coord_the, aes(x, y), ...) 221 | } 222 | if (random) { 223 | gg <- gg + geom_line(data = x$coord_the, aes(x, x), 224 | linetype = "dashed", ...) 225 | } 226 | } 227 | ) 228 | gg <- gg + theme_bw() 229 | print(gg) 230 | invisible(NULL) 231 | } 232 | 233 | 234 | #==============================================================================# 235 | # a2a 236 | #==============================================================================# 237 | 238 | #------------------------------------------------------------------------------# 239 | #' Easily switch between different power law formulations. 240 | #' 241 | #' \code{a2a} was designed to avoid headaches that are likely to occur when 242 | #' working with different formulations of the binomial power law analysis. 243 | #' 244 | #' The binomial power law can be expressed as: \eqn{s_y^2 = (intercept)(s_{bin}^2)^b}. 245 | #' But different forms of (intercept) are possible depending on the formulation of the 246 | #' binomial power law. 247 | #' \tabular{ccccc}{ 248 | #' \tab Ai \tab ai \tab AI \tab aI \cr 249 | #' Ai \tab 1 \tab n^b \tab n^(2(b-1)) \tab n^(b-2) \cr 250 | #' ai \tab n^(-b) \tab 1 \tab n^(b-2) \tab n^(-2) \cr 251 | #' AI \tab n^(2(1-b)) \tab n^(2-b) \tab 1 \tab n^(-b) \cr 252 | #' aI \tab n^(2-b) \tab n^2 \tab n^b \tab 1 \cr 253 | #' } 254 | #' 255 | #' @param x Intercept parameter to be converted or a named list with the 256 | #' parameter to be converted ("Ai", "ai", "AI" or "aI"), the slope 257 | #' ("slope"), and the number of individual per sampling unit ("n"). 258 | #' @param from Kind of the input intercept parameter ("Ai", "ai", "AI" or "aI"). 259 | #' @param to Desired kind for the ouput intercept parameter ("Ai", "ai", "AI" or 260 | #' "aI"). 261 | #' @param slope Slope parameter. 262 | #' @param n Number of individuals per sampling unit. 263 | #' @param ... Additional arguments to be passed to other methods. 264 | #' 265 | #' @returns A numeric vector. 266 | #' 267 | #' @examples 268 | #' # Values from the power_law() example: 269 | #' Ai <- 38.6245 270 | #' slope <- 1.9356 271 | #' n <- 9 272 | #' 273 | #' # Usual function call syntax: 274 | #' a2a(Ai, slope, n, from = "Ai", to = "ai") 275 | #' 276 | #' # Other syntaxes: 277 | #' inputs <- list(Ai = Ai, slope = slope, n = n) 278 | #' a2a(inputs, "ai") 279 | #' require(magrittr) 280 | #' inputs %>% a2a("ai") 281 | #' 282 | #' @export 283 | #------------------------------------------------------------------------------# 284 | a2a <- function(x, ...) UseMethod("a2a") 285 | 286 | #------------------------------------------------------------------------------# 287 | #' @keywords internal 288 | #------------------------------------------------------------------------------# 289 | a2a_internal <- function(intercept, b, n, from, to) { 290 | dico <- expand.grid(from = c("Ai", "ai", "AI", "aI"), 291 | to = c("Ai", "ai", "AI", "aI"), 292 | KEEP.OUT.ATTRS = FALSE, stringsAsFactors = FALSE) 293 | #-----------------------------------------------------------------# 294 | # | col Ai |col ai | col AI | col aI 295 | dico$coef <- c(1, n^b, n^(2*(b-1)), n^(b-2), # row Ai 296 | n^(-b), 1, n^(b-2), n^(-2), # row ai 297 | n^(2*(1-b)), n^(2-b), 1, n^(-b), # row AI 298 | n^(2-b), n^2, n^b, 1) # row aI 299 | #-----------------------------------------------------------------# 300 | item <- dico[which(dico$from == from & dico$to == to), ] 301 | res <- intercept * item[["coef"]] 302 | attr(res, "param") <- c(intercept = intercept, 303 | coef = item[["coef"]], 304 | slope = b, 305 | n = n) 306 | attr(res, "class") <- c("a2a", "numeric") 307 | res 308 | } 309 | 310 | #------------------------------------------------------------------------------# 311 | #' @rdname a2a 312 | #' @export 313 | #------------------------------------------------------------------------------# 314 | a2a.numeric <- function(x, slope, n, # Here, x = intercept 315 | from = c("Ai", "ai", "AI", "aI"), 316 | to = c("Ai", "ai", "AI", "aI"), ...) { 317 | 318 | # Checks and variable allocation: 319 | from <- match.arg(from) 320 | to <- match.arg(to) 321 | intercept <- x 322 | b <- slope 323 | 324 | # Perform the conversion: 325 | a2a_internal(intercept, b, n, from, to) 326 | } 327 | 328 | #------------------------------------------------------------------------------# 329 | #' @rdname a2a 330 | #' @export 331 | #------------------------------------------------------------------------------# 332 | a2a.list <- function(x, # Here, x: list with 3 elements: intercept, slope and n. 333 | to = c("Ai", "ai", "AI", "aI"), ...) { 334 | 335 | # Checks and variable allocation: 336 | stopifnot(length(x) == 3) 337 | stopifnot(all(c("slope", "n") %in% names(x))) 338 | cases <- c("Ai", "ai", "AI", "aI") 339 | from <- cases[cases %in% names(x)] 340 | stopifnot(length(from) == 1) 341 | to <- match.arg(to) 342 | intercept <- x[[from]] 343 | b <- x[["slope"]] 344 | n <- x[["n"]] 345 | 346 | # Perform the conversion: 347 | a2a_internal(intercept, b, n, from, to) 348 | } 349 | 350 | #------------------------------------------------------------------------------# 351 | #' @export 352 | #------------------------------------------------------------------------------# 353 | print.a2a <- function(x, ...) cat(x, "\n", sep = "") 354 | 355 | --------------------------------------------------------------------------------