Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions R/All_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pre_associndex_UNISITE_UNI <- function(int_data = NULL) {

# associndex_UNISITE_UNI()

associndex_UNISITE_UNI <- function(int_data = NULL, threshold_density=100) {
associndex_UNISITE_UNI <- function(int_data = NULL, threshold_density = NULL) {

if (!"Open" %in% int_data$Canopy) stop("tests cannot be conducted because your data does not contain a node named Open or it is spelled differently.")

Expand Down Expand Up @@ -1106,7 +1106,7 @@ pre_associndex_UNISITE_BI_COMP <- function(int_data = NULL) {
#remove line to remove a column named "Frequency" that does not exist

associndex_UNISITE_BI <- function(int_data = NULL,
threshold_density = 100) {
threshold_density = NULL) {

if (!"Open" %in% int_data$Canopy) stop("tests cannot be conducted because your data does not contain a node named Open or it is spelled differently.")

Expand Down Expand Up @@ -1164,7 +1164,7 @@ associndex_UNISITE_BI <- function(int_data = NULL,
#remove line to remove a column named "Frequency" that does not exist

associndex_UNISITE_BI_COMP <- function(int_data = NULL,
threshold_density = 100) {
threshold_density = NULL) {

if (!"Open" %in% int_data$Canopy) stop("tests cannot be conducted because your data does not contain a node named Open or it is spelled differently.")

Expand Down Expand Up @@ -1370,7 +1370,7 @@ node_degrees_BI <- function(int_data,cover_data){

matrix_Fcr<-RN_to_matrix(int_data,cover_data, int_type="fac", weight="Fcr")
matrix_Pcr <-ifelse(matrix_Fcr>0,1,0)
p<-associndex(int_data,cover_data,expand="no",rm_sp_no_cover="onlycanopy" ,threshold_density = 100)
p<-associndex(int_data,cover_data,expand="no",rm_sp_no_cover="onlycanopy", threshold_density = NULL)
node_abund <-unique(p[,c("Canopy","Ac")])
node_abund <-node_abund[node_abund$Canopy%in%colnames(matrix_Fcr),]
rownames(node_abund)<-node_abund$Canopy
Expand Down Expand Up @@ -1421,7 +1421,7 @@ node_degrees_BI_COMP <- function(int_data,cover_data){

matrix_Fcr<-RN_to_matrix(int_data,cover_data, int_type="comp", weight="RII")
matrix_Pcr <-ifelse(matrix_Fcr<0,1,0)
p<-associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="onlycanopy" ,threshold_density = 100)
p<-associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="onlycanopy", threshold_density = NULL)
node_abund <-unique(p[,c("Canopy","Ac")])
node_abund <-node_abund[node_abund$Canopy%in%colnames(matrix_Fcr),]
rownames(node_abund)<-node_abund$Canopy
Expand Down
64 changes: 33 additions & 31 deletions R/associndex.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,13 @@
#' - *onlycanopy*: removes only canopy species lacking cover data, retaining recruit
#' species even if they lack cover data.
#'
#' @param threshold_density **threshold_density**: indicates a threshold to retain only
#' those interactions in which the observed density is below the threshold
#' (i.e. Dcr < thr & Dro < thr). This is required to avoid the use of interactions
#' based on very few recruits which may have occurred by chance under a very scarce
#' canopy species. These cases will result in a large overestimation of Dcr, likely
#' being spurious outlayers. If no threshold value is provided or it is set to NULL,
#' the function estimates a threshold value based on recruitment density outliers
#' according to a Weibull distribution: a value is an outlier if it is above the limit
#' where less than 1 observation is expected. Explanation of its options:
#' - A positive value. High values provide more chance to include potentially spurious
#' interactions, unless such a high density is reliable based on the knowledge of the
#' study system. A way to decide the threshold value would consist in identifying possible
#' outlayers in the distribution of recruitment densities (*Dcr* and *Dro*).
#' - NULL: use package **extremevalues** to find the density threshold value (*K*)
#' above which, according to the Weibull distribution, less than 1 observation is
#' expected given the number of observations in the sample.
#' @param threshold_density **threshold_density**. Possible values are:
#' - The default option is *NULL* if no threshold is required.
#' - A *positive number* if a particular threshold is to be used.
#' - *"Weibull"* Can be used to exclude possible outlayers according to a Weibull
#' distribution. The function uses package **extremevalues** to find the density
#' threshold value (*K*) above which, according to the Weibull distribution, less
#' than 1 observation is expected, given the number of observations in the sample.
#'
#' @returns a data frame with the following information for each canopy-recruit
#' interaction with the following information:
Expand Down Expand Up @@ -84,7 +75,7 @@
#' rm_sp_no_cover = "onlycanopy", threshold_density=NULL)
#' associndex (Amoladeras_int, Amoladeras_cover, expand = "no",
#' rm_sp_no_cover = "allsp", threshold_density=NULL)
#' associndex (Amoladeras_int, Amoladeras_cover, expand = "yes",
#' associndex (Amoladeras_int, Amoladeras_cover, expand = "no",
#' rm_sp_no_cover = "onlycanopy", threshold_density=NULL)

associndex<- function(int_data, cover_data, expand=c("yes","no"),
Expand All @@ -98,24 +89,35 @@ associndex<- function(int_data, cover_data, expand=c("yes","no"),
differently. Data for recruitment in Open is required to calculate the indices."
)

# Suggest a threshold value based on recruitment density outliers according
# to a Weibull distribution. Uses "extremevalues" package.
int_data_0<-comm_to_RN_UNI(int_data, cover_data)
db_inter_0 <- pre_associndex_UNISITE_UNI(int_data_0)
db_inter_0$Dcr <- db_inter_0$Fcr/db_inter_0$Ac
db_inter_0$Dro <- db_inter_0$Fro/db_inter_0$Ao
y <- rbind(db_inter_0$Dcr,db_inter_0$Dro)
y <- y[which(y>0)] # Zero densities are common, so we will check only for
#suspiciously high density values.
K <- extremevalues::getOutliers(y, method = "I", distribution = "weibull", rho=c(1,1)) # We
#consider that a value is an outlier if it is above the limit where less
#than 1 observation is expected.
int_data_0<-comm_to_RN_UNI(int_data, cover_data)
db_inter_0 <- pre_associndex_UNISITE_UNI(int_data_0)
db_inter_0$Dcr <- db_inter_0$Fcr/db_inter_0$Ac
db_inter_0$Dro <- db_inter_0$Fro/db_inter_0$Ao
# Dro has repeated values for each recruit species. The next line removes repeated
# values.
unique_Dro <- stats::aggregate(Dro ~ Recruit, data = db_inter_0[, c("Recruit", "Dro")], mean)
y <- c(db_inter_0$Dcr,unique_Dro$Dro)
Comment on lines +92 to +99

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Hola, la indentación en este bloque es inconsistente. Para mejorar la legibilidad, sería bueno ajustarla a 2 espacios. También he añadido espacios alrededor de los operadores (/, ~) y he homogeneizado el uso de <- para las asignaciones, siguiendo las guías de estilo comunes en R.

  int_data_0 <- comm_to_RN_UNI(int_data, cover_data)
  db_inter_0 <- pre_associndex_UNISITE_UNI(int_data_0)
  db_inter_0$Dcr <- db_inter_0$Fcr / db_inter_0$Ac
  db_inter_0$Dro <- db_inter_0$Fro / db_inter_0$Ao
  # Dro has repeated values for each recruit species. The next line removes repeated
  # values.
  unique_Dro <- stats::aggregate(Dro ~ Recruit, data = db_inter_0[, c("Recruit", "Dro")], mean)
  y <- c(db_inter_0$Dcr, unique_Dro$Dro)


if(is.null(threshold_density)){
threshold_density = K$limit[[2]]
message("Based on Weibull distribution fitted to the observed values, the threshold density has been set to: ", threshold_density, ".")
# To effectively avoid the use of a threshold, we set its value to the maximum
# observed density.
threshold_density = max(y)
}
Comment on lines 101 to 105

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Hola, al establecer threshold_density = max(y), las interacciones con la densidad máxima serán filtradas debido a que la condición de filtrado es un menor estricto (<). Para desactivar el umbral de forma efectiva, como se indica en la documentación, sería mejor usar Inf. Esto asegurará que no se filtre ninguna interacción cuando threshold_density es NULL. También he aprovechado para mejorar el estilo del bloque.

  if (is.null(threshold_density)) {
    # To effectively avoid the use of a threshold, we set its value to infinity
    # so no interactions are filtered out.
    threshold_density <- Inf
  }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pues es verdad!


if(threshold_density == "Weibull"){

# Suggest a threshold value based on recruitment density outliers according
# to a Weibull distribution. Uses "extremevalues" package.
# Zero densities are common, so we will check only for suspiciously high densities.
y <- y[which(y>0)]
K <- extremevalues::getOutliers(y, method = "I", distribution = "weibull", rho=c(1,1)) # We
#consider that a value is an outlier if it is above the limit where less
#than 1 observation is expected.

threshold_density = K$limit[[2]]
n_excluded <- length(K$iRight)
message("Based on Weibull distribution fitted to the observed values, the threshold density has been set to: ", threshold_density, ". ", n_excluded, " cases were excluded.")
}
Comment on lines +107 to +120

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Hola, en este bloque también hay algunas inconsistencias de estilo. Para mejorar la legibilidad, sugiero ajustar la indentación, usar <- para las asignaciones y añadir espacios alrededor de los operadores. Te dejo una sugerencia con estos cambios.

  if (threshold_density == "Weibull") {
    # Suggest a threshold value based on recruitment density outliers according
    # to a Weibull distribution. Uses "extremevalues" package.
    # Zero densities are common, so we will check only for suspiciously high densities.
    y <- y[which(y > 0)]
    K <- extremevalues::getOutliers(y, method = "I", distribution = "weibull", rho = c(1, 1)) # We
    # consider that a value is an outlier if it is above the limit where less
    # than 1 observation is expected.

    threshold_density <- K$limit[[2]]
    n_excluded <- length(K$iRight)
    message("Based on Weibull distribution fitted to the observed values, the threshold density has been set to: ", threshold_density, ". ", n_excluded, " cases were excluded.")
  }


if(expand=="yes" & rm_sp_no_cover=="allsp"){int_type ="rec"}
if(expand=="yes" & rm_sp_no_cover=="onlycanopy"){int_type ="comp"}
Expand Down
4 changes: 2 additions & 2 deletions R/canopy_service_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#' canopy_service_test(Amoladeras_int, Amoladeras_cover)
#'

canopy_service_test <- function(int_data,cover_data){
canopy_service_test <- function(int_data, cover_data){

df<-associndex(int_data,cover_data, expand="yes", rm_sp_no_cover="onlycanopy")
df<-associndex(int_data,cover_data, expand="yes", rm_sp_no_cover="onlycanopy", threshold_density = NULL)
sp_Fc <- stats::aggregate(Fcr ~ Canopy, data = df, FUN = sum)
sp_Ac <- stats::aggregate(Ac ~ Canopy, data = df, FUN = max)
sp_Fro <- stats::aggregate(Fro ~ Canopy, data = df, FUN = sum)
Expand Down
2 changes: 1 addition & 1 deletion R/recruitment_niche_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#'
recruitment_niche_test <- function(int_data,cover_data){

df <- associndex(int_data,cover_data, expand="yes", rm_sp_no_cover="onlycanopy")
df <- associndex(int_data,cover_data, expand="yes", rm_sp_no_cover="onlycanopy", threshold_density = NULL)
sp_Fr <- stats::aggregate(Fcr ~ Recruit, data = df, FUN = sum)
sp_Av <- stats::aggregate(Ac ~ Recruit, data = df, FUN = sum)
sp_Fro <- stats::aggregate(Fro ~ Recruit, data = df, FUN = max)
Expand Down
Loading