spatial-ews / spatialwarnings Goto Github PK
View Code? Open in Web Editor NEWAn R package to compute spatial early-warning signals of ecosystem degradation
License: Other
An R package to compute spatial early-warning signals of ecosystem degradation
License: Other
Indicator functions should return a warning when a matrix looks like a binary matrix while not being one as it has big consequences on the way indicators are computed.
Maybe the right test could be length(unique(states)) <= 1
Make sure the spectral ews workflow works: the following functions should be implemented and working.
Here are the bifurcation diagrams with two cross-sections (Figure in the same style as Flo's Fig. 2).
In top row plots, areas shaded in gray indicate that density of mussels/veg/trees is zero. Contour lines indicate the density.
I also put this figure in the dropbox with the corresponding code (in ./figures/bifurcation_diagrams)
Having all the single-indicator functions, we need a user-friendly function now that computes all the indicators and reports about their values. I suggest following the usual pattern of creating a new object class "shift_indicator" and add usual methods.
A user workflow could look this way (a bit similar to a glm workflow basically) :
as.*
function family)indicators
function to create the result object containing all the appropriate indicator values (naming is to be discussed)summary
function on the indicators
object, see what indicators show an interesting valueWhat I have in mind for a summary function is something with an output like :
> summary(glm(Sepal.Length ~ Sepal.Width + Species, data = iris))
Call:
glm(formula = Sepal.Length ~ Sepal.Width + Species, data = iris)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.30711 -0.25713 -0.05325 0.19542 1.41253
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.2514 0.3698 6.089 9.57e-09 ***
Sepal.Width 0.8036 0.1063 7.557 4.19e-12 ***
Speciesversicolor 1.4587 0.1121 13.012 < 2e-16 ***
Speciesvirginica 1.9468 0.1000 19.465 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Thoughts ? Ideas for the plot function ?
Make sure the spectral ews workflow is documented the following functions should have their respective roxygen comments:
Make sure the generic ews workflow works: the following functions should be implemented and working.
It is unclear what the indictest method should be (if it is needed).
Most of the code is shared between those three functions, most of the code can be factorized. (not a high priority though)
It seems a function to do coarsed grain
We need to wrap this into a valid R package.
The globally accepted input should be a binary matrix, A
.
F
with the focal state "+" can be easily converted into such by A <- F == "+"
.I
. A <- I == 1
Many of the present functions can be run on that directly. Others need patch counts. Thus a core function label()
should provide a fast labelling of patches and return a matrix with integer patch IDs.
Then a secondary function could take in the binary matrix as input and return a vector of patch sizes and/or a table of cumulative patch sizes.
Make sure the generic ews workflow works: the following functions should be implemented and working.
This function is called in the case of spectral analysis, right ?
The package needs to do its best try at converting data to binary matrices. The best way to do this I think is to create a binary_matrix
class and as.binary_matrix.*
S3 methods :
as.binary_matrix.data.frame
as.binary_matrix.matrix
(for non-binary matrices)as.binary_matrix.list
(for lists of stuff, determines what kind of objects the list is made of and apply the appropriate as.*
function to them)as.binary_matrix.ca_run
(not to be defined in this package but in caspr)It would be great to have the list of all the indicator functions that we need for the package in this respository.
Some R script contain source()
command. We don't need those commands since when the package is loaded, all the scripts of the R
folder are sourced.
A grep
command in the repository gives that 20 source()
command are present, we need to delete them.
To work locally, you can install the package by doing
install_github("fdschneider/spatial_warnings")
library(spatialwarnings)
Normally, it should work and you don't need to source manually individual scripts.
If you modify a function locally and you want to test it, you can run devtools::load_all()
which load all the scripts which are in the R/
folder.
Add a workflow functions (spectral_spews + print/summary/plot methods) that informs the use about :
In more details :
This function should print the ic_value ~ coarse_graining size plot so the user can decide on an optimal coarse-graining size.
When the input is several matrices along a gradient, then we don't know on which snapshot this curve should be displayed. Moreover they are not comparable across values of the gradient as density changes.
A rule of thumb could be using (1) something not too close to the tipping point and (2) with a density that is not too high.
input is a binary matrix or a list of binary matrices (using S3 methods)
See discussion at fdschneider/caspr#4
e.g. This code produces an error
ex <- summary(generic_spews(forestdat$matrices), null_replicates = 0)
print(ex)
If we plan to submit our package on the CRAN, we need to pass all the required automatic tests. CRAN reviewers are very strict about this so all NOTEs/WARNINGs/ERRORs must be squashed.
The package can be checked within R using the devtools::check()
function. Pass the argument cran=TRUE
to run a CRAN-like check.
Note that this is not to be confused with the unit-tests run using test()
. What check()
does is checking whether the package will be runnable on other computers and whether it follows the CRAN guidelines. (check() run the unit tests though and stops if those fail).
Example output:
* using log directory ‘<snip>/spatial_warnings.Rcheck’
* using R version 3.2.1 (2015-06-18)
<snip>
Status: 1 ERROR, 10 WARNINGs, 6 NOTEs
See
Right now skewness returns NA if the vector of values has a std. deviation of zero. It should return zero instead.
Make sure the patch-based ews workflow is documented: the following functions should have their respective roxygen comments:
fitpsd.R et patchsizedistr_ews.R
patchsizedistr_ews.R seems to be obsolete.
May be we have also to think to the structure of this function ? Cumulative patch size distribution could be separated from power law fitting function ?
As put in http://r-pkgs.had.co.nz/vignettes.html :
A vignette is like a book chapter or an academic paper: it can describe the problem that your package is designed to solve, and then show the reader how to solve it. A vignette should divide functions into useful categories, and demonstrate how to coordinate multiple functions to solve problems. Vignettes are also useful if you want to explain the details of your package. For example, if you have implemented a complex statistical algorithm, you might want to describe all the details in a vignette so that users of your package can understand what’s going on under the hood, and be confident that you’ve implemented the algorithm correctly.
Following this idea, I suggest dividing our vignette into several parts :
Make sure the generic ews workflow is documented the following functions should have their respective roxygen comments:
Use one of the templates provided in [1] to add a function that returns the largest patch size (with user documentation).
[1] https://github.com/fdschneider/spatial_warnings/blob/master/R/indicator_example.R
There are a couple of data files in the original repo, but no documentation on how to interpret them.
Could we add a couple of valid binary matrices as data objects that we can use for writing examples?
input should be a vector of patch sizes.
returns a list of fitting results for all candidate distributions and a general summary of the best fitting function. see issue fdschneider/caspr#2
After running all the indicators the user is likely to want to export the results to a more standard object like a data.frame (so it can do beautiful plots in ggplot for example).
To do this we need functions that take the output object:
list(variance = list(value = xx, ...), # OR variance = xx
skewness = list(value = xx, ...),
psd = `psd object`,
...)
(class: c('spatial_indicators', 'list'))
and convert it to something like this
> as.data.frame(indic_object)
data.frame of x rows and x columns
replicate value
variance 1 xx
variance 2 yy
variance 3 zz
skewness 1 aa
skewness 2 bb
(etc.)
class: c('data.frame', 'list')
Non numerical indicators cannot fit into this data structure so I suggest dicarding them.
The model results are starting to arrive !
As usual these files contain two variables: upper_branch and lower_branch. Both are lists of three components :
access them with upper_branch[[1]]
(or 2 or 3)
For all of them we compute a null distribution of indicator values based on shuffled matrices, but the way we test against this distribution might have to differ from indicator to indicator:
When using detrending, means can be negative due to floating point errors, so a minus sign is added in front of the number => an extra column is added and the layourt breaks. Similarly, plots show a bogus trend.
Solution : set means to zero when detrending has been used.
Reproducible example:
a <- generic_spews(forestdat$matrices, detrend = TRUE)
a.summary <- summary(a)
a.summary # broken layout
plot(a.summary) # broken mean trend
We should use the following ranges:
Add a check in coarse_grain() to catch this ? (or maybe do it at higher level to avoid exceptions in highly-repeated code).
Some indicators will not run on the example datasets :
indicator_powerspectrum
(cannot find function rspec_ews
)indicator_corrfunc
(missing value where TRUE/FALSE needed)These functions will be available to users as they can be useful, and need documentation:
Create a function that will compute all the indicators and produce a summarizable/plottable object.
# We need a catchy short name for that
all_indicators <- function(data, # `binary_matrix` object
indicators = list(variance = indicator_variance, psd = indicator_psd, ...), # list of functions
?)
It should return an object of class indicators
with the following structure:
list(variance = list(value = xx, ...), # OR variance = xx
skewness = list(value = xx, ...),
psd = `psd object`,
...) # Component names are taken from the list of functions above.
(class: c('spatial_indicators', 'list'))
Numerical indicators (e.g. variance, skewness, moran's I) should have a value=
in their output list so they get recognized by the summarize
function.
Currently there is no standard naming for the output of the indicator_*
functions: we need that so the summary
function can produce the right output for each of them (e.g. a table with values for numerical indicators, see #19).
In particular, all indicators that return "a single number" and not a complex object (e.g. a curve fit) should return EITHER :
value
Here is a list of the numerical functions that need to be modified :
These functions should explicitely add a specific class to their output. For example, indicator_fitpsd sets the class of its output to psdfit
.
There should be a summary
method available for this class that will be called within the global summary of all the indicators. (e.g. summary.psdfit
).
These ones are :
I might have forgotten some indicators so please add them below :o)
Error:
unexpected end of input in spatial_indicators_main.R
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.