Giter Site home page Giter Site logo

yangxhcaf / ngnn Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phytomosaic/ngnn

0.0 1.0 0.0 52 KB

Nonlinear Gradient Nearest Neighbors -- a method for predicting multivariate species compositions from environmental variables based on individualistic but possibly coordinated species responses.

License: GNU General Public License v3.0

R 100.00%

ngnn's Introduction

ngnn

Nonlinear Gradient Nearest Neighbors, a method for predicting multivariate species compositions from environmental variables based on individualistic but possibly coordinated species responses. Nonlinear responses are possible, which improves upon existing gradient nearest neighbors methods. Package also includes methods for other ecological tasks, including in-sample NMS ordination, out-of-sample NMS prediction, Nonparametric Constrained Ordination (NCO), Nonparametric Multiplicative Regression (NPMR), and simplified Gradient Nearest Neighbors (GNN).

Motivation

Given a set of sample units where species abundances and corresponding predictor values are both known, how does one infer which species should appear in 'new' sample units where only the predictors are known? NGNN (nonlinear gradient nearest neighbors) approaches the problem of species imputation in the following way:

Regress species individualistically on predictors -> Feed fitted values to NMS ordination -> Find nearest neighbors in ordination space, and assign those species. This retains realistic communities of co-occurring species, since they've already been observed in at least one other sample unit.

Installation

Install the package from github as follows:

install.packages('devtools')
devtools::install_github('phytomosaic/foggy')
devtools::install_github('phytomosaic/ecole')

Examples

Create datasets (here split one artificially)

# set up
set.seed(978)
require(vegan)
data(varespec, varechem)
spe <- varespec ; id  <- varechem
i   <- sample(1:nrow(spe), size=floor(0.75*nrow(spe))) # sample
spe <- spe[i, ]          # in-sample species
idi <- id[i, ]           # in-sample predictors
ido <- id[-i, ]          # out-of-sample predictors
nm  <- c('Al', 'K')      # select 1 or 2 gradients of interest

Basic usage

# perform NGNN
res <- ngnn(spe, idi, ido, nm, nmulti=5, method='bray',
            thresh=0.90, neighb=5, maxits=999, k=1)
summary(res)
str(res, 1)

# plot the species response curves
ngnn_plot_spp(res, pick=1:9, nm=nm)

# plot the NCO gradient space
ngnn_plot_nco(res)

# predicted (imputed) species composition for out-of-sample sites
ngnn_get_spp(res)

# how close were predicted species composition to 'true' values?
spe_append <- rbind(spe, res$spp_imputed)   # append to existing
heatmap(t(as.matrix(spe_append)), Rowv=NA, Colv=NA)

# check composition of 'hold-out' data
heatmap(t(as.matrix(varespec[-i,])), Rowv=NA, Colv=NA)
# ... vs new species from NGNN
heatmap(t(as.matrix(res$spp_imputed)), Rowv=NA, Colv=NA)

# Prediction error: Root Mean Square Error
`rmse` <- function(y, ypred, ...){
     sqrt(mean((y-ypred)^2, ...))
}
rmse(varespec[-i,], res$spp_imputed)

Can do this manually, avoiding the 'ngnn' wrapper function:

# step 1: NPMR
res_npmr <- npmr(spe, idi, ido, nm, nmulti=5)

# step 2: NCO (NMS)
res_nco  <- nco(res_npmr, method='bray', thresh=0.90)

# step 3: NCOpredict (NMSpredict)
res_nmsp <- nco_predict(res_nco, method='bray', neighb=5, maxits=999)

# step 4: GNN
res_gnn  <- gnn(obj=res_nmsp, k=1)
summary(res_gnn)

References

Kruskal, J. B. 1964. Multidimensional scaling by optimizing goodness of fit to a nonmetric hypothesis. Psychometrika 29: 1-27. McCune, B. 2006. Non-parametric habitat models with automatic interactions. Journal of Vegetation Science 17(6):819-830. McCune, B., and H. T. Root. 2012. Nonparametric constrained ordination. 97th ESA Annual Meeting. Ecological Society of America, Portland, OR. McCune, B., and H. T. Root. 2017. Nonparametric constrained ordination to describe community and species relationships to environment. Unpublished ms. Ohmann, J.L., and M.J. Gregory. 2002. Predictive mapping of forest composition and structure with direct gradient analysis and nearest-neighbor imputation in coastal Oregon, U.S.A. Canadian Journal of Forest Research 32:725-741.

ngnn's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.