Giter Club home page Giter Club logo

Comments (7)

dmcglinn avatar dmcglinn commented on May 31, 2024

Here is a better proof of concept and a function to carry out the computation of the individual swapped community by site matrix. The code also performs a quick test at the bottom that checks that species sums have not changed

S = 6
N = 100
nplots = 10
rand_comm = matrix(rpois(S*nplots, 1),
                   ncol=S, nrow=nplots)

intra_sp_swap = function(comm, groups) {
  group_levels = unique(groups)
  comm_group_noagg = matrix(NA, ncol=ncol(comm), nrow=nrow(comm))
  for(i in seq_along(group_levels)) {
    row_indices = groups == group_levels[i]
    comm_group = comm[row_indices, ]
    sp_sums = colSums(comm_group)
    tmp_comm = sapply(sp_sums, function(x) 
                      table(c(sample(1:nrow(comm_group), x,
                                    replace=T),
                              1:nrow(comm_group))) - 1)
    comm_group_noagg[row_indices, ] = tmp_comm
  }  
  comm_group_noagg
}

# test species sums for equality
replicate(20, all.equal(colSums(rand_comm), 
          colSums(intra_sp_swap(rand_comm, rep(1:2, each=5)))))

from mobr.

dmcglinn avatar dmcglinn commented on May 31, 2024

here is the best form of the two functions needed for incoperating this into our code base. There are two functions: 1) computes shuffled comm matrix, 2) computes averaged sample based rarefaction curve. These two functions can just be called before normal sample based rarefaction for both the observed and null perms. it is very fast luckily

intra_sp_swap = function(comm, groups) {
    group_levels = unique(groups)
    comm_group_noagg = matrix(NA, ncol=ncol(comm), nrow=nrow(comm))
    for(i in seq_along(group_levels)) {
        row_indices = groups == group_levels[i]
        comm_group = comm[row_indices, ]
        sp_sums = colSums(comm_group)
        tmp_comm = sapply(sp_sums, function(x) 
                          table(c(sample(1:nrow(comm_group), x,
                                        replace=T),
                                 1:nrow(comm_group))) - 1)
        comm_group_noagg[row_indices, ] = tmp_comm
    }  
    comm_group_noagg
}

avg_swap_rare = function(comm, groups, nperm=1000, effort=NULL){
    S = replicate(nperm, 
                  rarefaction(intra_sp_swap(comm, groups),
                              'samp', effort))
    Savg = apply(S, 1, mean)
    Savg
}

from mobr.

dmcglinn avatar dmcglinn commented on May 31, 2024

Bummer minor bug in code not quite as fast as I thought maybe we could turn down the default 1000 replicates to 500 or so.

from mobr.

dmcglinn avatar dmcglinn commented on May 31, 2024

I should note though that the functions I provide above appear to be working

from mobr.

rueuntal avatar rueuntal commented on May 31, 2024

Looks great, thanks!

from mobr.

rueuntal avatar rueuntal commented on May 31, 2024

Forget about what I said this morning - let's keep the functions as they are for now. It's actually easier to fit them into our original framework this way.

from mobr.

dmcglinn avatar dmcglinn commented on May 31, 2024

Yea with my functions you can supply them just a single group

Dan

On Jul 15, 2016, at 11:07 AM, Xiao Xiao [email protected] wrote:

Forget about what I said this morning - let's keep the functions as they are for now. It's actually easier to fit them into our original framework this way.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

from mobr.

Related Issues (20)

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.