R/mom_function.R

Defines functions mom2

Documented in mom2

#' Method of Moments Estimating of Migration Paramter
#'
#' This function estimates the migration parameter between a local and meta community for a single individual.
#' This method is an extension from the paper "Taxa-Abundance Distributions in Microbial Communities Using Environmental
#' Sequence Data" by Sloan et al.
#' @param local A local community of OTU reads. Rows should be OTUs, Columns should be Samples. A column of taxon indexes should be included at the beginning. Note that if this is not included, the first sample in your data will not be used.
#' @param meta A meta community of OTU reads. Rows should be OTUs, Columns should be Samples. A column of taxon indexes should be included at the beginning. Note that if this is not included, the first sample in your data will not be used.
#' @import vegan 
#' @return a list of 1 element. Element 1 is the estimate for the migration parameter. 
#' @export

mom2 <- function(local, meta)
{

  NT=sum(local[,2])
  d=1/NT
  oralMn=t(t(meta[,2])/sum(meta[,2]))
  
  lungFr=ifelse(local[,2] == 0, 0, 1)
  
  minFcn <- function(m){
    sum((lungFr-1+pbeta(d,NT*m*oralMn, NT*m*(1-oralMn)))^2) 
  }
  
  f1Oral=optimize(f=minFcn,lower=1.0e-7,upper=1-1.0e-7)
  
  return(list(f1Oral$min))
}
kathalexknuts/ntsims documentation built on May 4, 2019, 6:40 p.m.