#' Calculation of directional beta diversity based on Baselga's beta decomposition (turnover)
#'
#' @param presab a presence/absence matrix with species in columns and "communities" in rows
#' @param names.countries a character vector with country names. Ideally the names must be equal to the ones in the rows of presab matrix
#'
#' @return A data frame with six columns containing turnover values for native and types
#' and also sucesses and fails (realizations) for types and native
#' @export
#'
#' @examples
<-
beta_types function(presab, names.countries){
# calculating components of beta
<- betapart::betapart.core(presab)
beta_core_all
# splitting shared and not shared components of beta
<-
m_all_notshare lapply(names.countries, function(x){
<- grep(pattern = gsub("\\s*\\([^\\)]+\\)", "", x), rownames(beta_core_all$not.shared))
row_country <- grep(pattern = gsub("\\s*\\([^\\)]+\\)", "", x), rownames(beta_core_all$not.shared))
column_country $not.shared[row_country, column_country]
beta_core_all
})<-
m_all_share lapply(names.countries, function(x){
<- grep(pattern = gsub("\\s*\\([^\\)]+\\)", "", x), rownames(beta_core_all$shared))
row_country <- grep(pattern = gsub("\\s*\\([^\\)]+\\)", "", x), rownames(beta_core_all$shared))
column_country $shared[row_country, column_country]
beta_core_all
})
# calculating native and holotype portions
<-
native_all lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} 2, 1] / (m_all_share[[x]][2, 1] + m_all_notshare[[x]][2, 1])
m_all_notshare[[x]][
}# native - underrepresentation)
})
<-
native_success lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} 2, 1]
m_all_notshare[[x]][
}
})
<-
native_fail lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} 2, 1] + m_all_notshare[[x]][2, 1]) - (m_all_notshare[[x]][2, 1])
(m_all_share[[x]][
}
})
<-
type_all lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} if(m_all_notshare[[x]][1, 2] == 0){
0
else{
} 1, 2] / (m_all_share[[x]][2, 1] + m_all_notshare[[x]][1, 2])
m_all_notshare[[x]][
}
}
})
<-
type_success lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} if(m_all_notshare[[x]][1, 2] == 0){
0
else{
} 1, 2]
m_all_notshare[[x]][
}
}
})
<-
type_fail lapply(1:length(m_all_notshare), function(x){
if(!is.matrix(m_all_notshare[[x]]) | length(m_all_notshare[[x]]) == 0){
NA
else{
} if(m_all_notshare[[x]][1, 2] == 0){
0
else{
} 2, 1] + m_all_notshare[[x]][1, 2]) - (m_all_notshare[[x]][1, 2])
(m_all_share[[x]][
}
}
})
# joining all in a df
<- do.call(rbind, native_all)
df_native_beta <- do.call(rbind, type_all)
df_type_beta <- do.call(rbind, native_success)
df_native_success <- do.call(rbind, native_fail)
df_native_fail <- do.call(rbind, type_success)
df_type_success <- do.call(rbind, type_fail)
df_type_fail <- data.frame(countries = names.countries,
df_all_beta native.beta = df_native_beta,
type.beta = df_type_beta,
native.success = df_native_success,
native.fail = df_native_fail,
type.success = df_type_success,
type.fail = df_type_fail)
return(df_all_beta)
}
Functions
0011_Functions.qmd
Function for beta diversity
beta_types (function_beta_types_success_fail.R)
Calculation of directional beta diversity based on Baselga’s beta decomposition (turnover)
Scale function
scale_back (function_scale_back.R)
Auxiliar function to scale back standardized variables
#' Auxiliar function to scale back standardized variables
#'
#' @param data data frame. An object from ggpredict
#' @param scaled data frame. An object from scale
#'
#' @return a new data frame with the variable x_original that corresponds
#' to the standardized variable scaled back
#' @export
#'
#' @examples
<-
scale_back function(data, scaled){
<- data.frame(data)
data2 <-
res |>
data2 ::mutate(x_original = as.matrix(x) * attr(scaled, 'scaled:scale') +
dplyrattr(scaled, 'scaled:center'))
return(res)
}