-
Notifications
You must be signed in to change notification settings - Fork 4
Add the rest of functions and tests #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #' Title | ||
| #' | ||
| #' @param int_data | ||
| #' @param cover_data | ||
| #' @param int_type | ||
| #' | ||
| #' @returns | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' | ||
| #' # RN_dims_UNI() modified to enter the interaction and cover data | ||
| #The size of a network can be described by the number of nodes (*N*) and links (*L*), and its complexity is #proportional to network connectance. *C* can be estimated on different ways depending on the type of #network, but it always measures the proportion of links relative to the maximum number of links that could #be possible in the network. In the case of recruitment networks we use the formula $C = L /(N^2-N)$ since #the node "Open" does not act as a recruit (i.e. Open is represented by a row of zeroes in the adjacency #matrix). | ||
| #For facilitation and competition that are bipartite networks, the nodes in eahc guild are #provided( nurse/facilitated, Canopy (depressor)/recruit) seprately, and open is not a node | ||
| #' | ||
| RN_dims <- function(int_data,cover_data, int_type=c("rec","fac","comp")){ | ||
|
|
||
| if(int_type=="rec"){ | ||
|
|
||
| int_data <-comm_to_RN_UNI(int_data,cover_data) | ||
|
|
||
| # FUNCTION | ||
| df <- int_data | ||
| n_nodes <- length(unique(c(df$Canopy, df$Recruit))) | ||
| n_links <- sum(df$Pcr) | ||
| connectance <- n_links/(n_nodes^2 - n_nodes) | ||
|
|
||
| out <- data.frame(c(n_nodes, n_links, connectance)) | ||
| colnames(out) <- c("Value") | ||
| rownames(out) <- c("Num. Nodes", "Num. Links", "Connectance") | ||
| return(out) | ||
|
|
||
| } | ||
|
|
||
| if(int_type=="fac"){ | ||
|
|
||
| int_data <-suppressWarnings(RN_to_matrix(int_data,cover_data, int_type="fac",weight="Pcr")) | ||
|
|
||
| df <-int_data | ||
| n_nodes_nurse <-dim(df)[2] | ||
| n_nodes_facilitated <-dim(df)[1] | ||
| n_links <- sum(df) | ||
| connectance <-n_links/(n_nodes_nurse*n_nodes_facilitated) | ||
|
|
||
| out <- data.frame(c(n_nodes_nurse,n_nodes_facilitated, n_links, connectance)) | ||
| colnames(out) <- c("Value") | ||
| rownames(out) <- c("Num. Nurse sp","Num. Facilitated sp", "Num. Links", "Connectance") | ||
| return(out) | ||
|
|
||
| } | ||
|
|
||
|
|
||
| if(int_type=="comp"){ | ||
|
|
||
| int_data <-suppressWarnings(RN_to_matrix(int_data,cover_data, int_type="comp",weight="Pcr")) | ||
|
|
||
| df <-int_data | ||
| n_nodes_canopy_depressing <-dim(df)[2] | ||
| n_nodes_recruit_depressed <-dim(df)[1] | ||
| n_links <- sum(df) | ||
| connectance <-n_links/(n_nodes_canopy_depressing*n_nodes_recruit_depressed) | ||
|
|
||
| out <- data.frame(c(n_nodes_canopy_depressing, n_nodes_recruit_depressed, n_links, connectance)) | ||
| colnames(out) <- c("Value") | ||
| rownames(out) <- c("Num. Canopy depressing sp","Num. Recruit depressed sp", "Num. Links", "Connectance") | ||
| return(out) | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #' Title | ||
| #' | ||
| #' @param int_data | ||
| #' @param cover_data | ||
| #' @param int_type | ||
| #' @param weight | ||
| #' @param scale_top | ||
| #' | ||
| #' @returns | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' | ||
| #' | ||
| RN_heatmap <- function(int_data,cover_data,int_type=c("rec","fac","comp"), weight = c("Pcr","Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII"), scale_top = 1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function parameter Additionally, using RN_heatmap <- function(int_data,cover_data,int_type=c("rec","fac","comp"), weight = c("Pcr","Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII")) { |
||
|
|
||
| if(int_type=="rec"){ | ||
|
|
||
| index<-suppressWarnings(associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="allsp")) | ||
| data<-comm_to_RN_UNI(int_data,cover_data) | ||
| data$Dcr<-data$Fcr/data$Ac | ||
|
|
||
| # calculate Dro for each recuit species | ||
| open_rows <- data[data$Canopy == "Open", ] | ||
| ratios <- tapply(open_rows$Fcr / open_rows$Ac, open_rows$Recruit, mean) | ||
| data$Dro <- ratios[data$Recruit] | ||
|
|
||
|
|
||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| index$int<-paste(index$Canopy,index$Recruit, sep="_") | ||
| db<-merge(data[,c("int","Canopy","Recruit","Fcr","Icr","Pcr","Dcr","Dro")],index[,c("int","Ns", "NintC", "NintA", "RII")], by="int", all.x=T) | ||
| db[is.na(db)]<-0 | ||
|
|
||
| int_data<-db | ||
|
|
||
| if (weight %in% c("Ns", "NintC", "NintA", "RII")) { | ||
| warning("Since the index specified in the 'weight' argument is defined relative to 'Open', rows or columns labeled 'Open' are mathematically zero and not biologically meaningful")} | ||
|
|
||
| require(ggplot2) | ||
| # manually set node order | ||
| canopy_order <- unique(int_data$Canopy) | ||
| canopy_order <- canopy_order[!canopy_order %in% c('Open')] | ||
| canopy_order <- c("Open", canopy_order) | ||
| int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order) | ||
| recruit_order <- sort(unique(int_data$Canopy), decreasing = TRUE) | ||
| recruit_order <- recruit_order[!recruit_order %in% c('Open')] | ||
| recruit_order <- c(recruit_order, "Open") | ||
| int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order) | ||
|
|
||
|
|
||
| # Make weight variable | ||
| int_data$weight_v <- as.numeric(int_data[[weight]]) | ||
|
|
||
| # Lowest (non-zero) and highest values of the weighting variable | ||
| highest_W <- max(int_data$weight_v) | ||
| lowest_W <- min(int_data$weight_v[int_data$weight_v>0]) | ||
|
|
||
| # Plot the heatmap | ||
| p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) + | ||
| geom_tile(aes(height = 1, alpha = weight_v != 0), | ||
| colour = "gray", linewidth = 0.25) + | ||
| scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") + | ||
| scale_x_discrete(position = "top") + | ||
| labs(fill = weight,x = "Canopy", y = "Recruit") + | ||
| theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0)) | ||
|
|
||
| return(p) | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| if(int_type=="fac"){ | ||
|
|
||
| data<-comm_to_RN_BI(int_data,cover_data) | ||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| df<-suppressWarnings(int_significance_BI (data)) | ||
| df<-df[df$Effect_int=="Enhancing",] | ||
| fac_int<-paste(df$Canopy,df$Recruit, sep="_") | ||
| db<-suppressWarnings(associndex_UNISITE_BI(comm_to_RN_BI(int_data,cover_data))) | ||
| db$int<-paste(db$Canopy,db$Recruit, sep="_") | ||
| db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T) | ||
| db<-db[db$int%in%fac_int,] | ||
|
|
||
| int_data<-db | ||
|
|
||
| require(ggplot2) | ||
| # manually set node order | ||
| canopy_order <- unique(int_data$Canopy) | ||
| int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order) | ||
| recruit_order <- sort(unique(int_data$Recruit), decreasing = TRUE) | ||
| int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order) | ||
|
|
||
| # Add recruitment density as another weighting variable | ||
| int_data$Dcr <- int_data$Fcr/int_data$Ac | ||
|
|
||
| # Make weight variable | ||
| int_data$weight_v <- as.numeric(int_data[[weight]]) | ||
|
|
||
| # Lowest (non-zero) and highest values of the weighting variable | ||
| highest_W <- max(int_data$weight_v) | ||
| lowest_W <- min(int_data$weight_v[int_data$weight_v>0]) | ||
|
|
||
| # Plot the heatmap | ||
| p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) + | ||
| geom_tile(aes(height = 1, alpha = weight_v != 0), | ||
| colour = "gray", linewidth = 0.25) + | ||
| scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") + | ||
| scale_x_discrete(position = "top") + | ||
| labs(fill = weight,x = "Canopy", y = "Recruit") + | ||
| theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0)) | ||
|
|
||
| return(p) | ||
|
|
||
| } | ||
|
|
||
|
|
||
| if(int_type=="comp"){ | ||
|
|
||
|
|
||
| data<-comm_to_RN_UNI_COMP(int_data,cover_data) | ||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| df<-suppressWarnings(int_significance_BI_COMP (data)) | ||
| df<-df[df$Effect_int=="Depressing",] | ||
| comp_int<-paste(df$Canopy,df$Recruit, sep="_") | ||
| db<-suppressWarnings(associndex_UNISITE_BI_COMP(comm_to_RN_UNI_COMP(int_data,cover_data))) | ||
| db$int<-paste(db$Canopy,db$Recruit, sep="_") | ||
| db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T) | ||
| db<-db[db$int%in%comp_int,] | ||
|
|
||
| int_data<-db | ||
|
|
||
| require(ggplot2) | ||
| # manually set node order | ||
| canopy_order <- unique(int_data$Canopy) | ||
| int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order) | ||
| recruit_order <- sort(unique(int_data$Recruit), decreasing = TRUE) | ||
| int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order) | ||
|
|
||
| # Add recruitment density as another weighting variable | ||
| int_data$Dcr <- int_data$Fcr/int_data$Ac | ||
|
|
||
| # Make weight variable | ||
| int_data$weight_v <- as.numeric(int_data[[weight]]) | ||
|
|
||
| if(weight%in% c("Fcr","Icr","Pcr","Dcr","Dro")){ | ||
| # Lowest (non-zero) and highest values of the weighting variable | ||
| highest_W <- max(int_data$weight_v) | ||
| lowest_W <- min(int_data$weight_v[int_data$weight_v>0]) | ||
| } | ||
|
|
||
|
|
||
| if(weight %in% c("Ns", "NintC", "NintA", "RII")){ | ||
| nonzero_vals <- int_data$weight_v[int_data$weight_v != 0] | ||
| highest_W <- max(nonzero_vals) | ||
| lowest_W <- min(nonzero_vals) | ||
| } | ||
| # Plot the heatmap | ||
| p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) + | ||
| geom_tile(aes(height = 1, alpha = weight_v != 0), | ||
| colour = "gray", linewidth = 0.25) + | ||
| scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") + | ||
| scale_x_discrete(position = "top") + | ||
| labs(fill = weight,x = "Canopy", y = "Recruit") + | ||
| theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0)) | ||
|
|
||
| return(p) | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #' Title | ||
| #' | ||
| #' @param int_data | ||
| #' @param cover_data | ||
| #' @param int_type | ||
| #' @param weight | ||
| #' | ||
| #' @returns | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' #This function returns a matrix with canopy species as columns and recruits as rows, containing the association index specified in the weight argument. If int_type is set to "rec," all possible interactions between species with known coverage are displayed. If set to "fac" (or "comp"), a nonzero value appears when recruits associate with canopy plants more (or less) than expected based on their coverage; otherwise, the value is zero. | ||
| #' | ||
| RN_to_matrix<- function(int_data,cover_data, int_type=c("rec", "fac","comp"), weight = c("Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII")){ | ||
|
|
||
| if (!"Open" %in% int_data$Canopy) stop("ERROR: your data does not contain a node named Open or it is spelled differently.") | ||
|
|
||
| if(int_type=="rec"){ | ||
|
|
||
| index<-suppressWarnings(associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="allsp")) | ||
| data<-comm_to_RN_UNI(int_data,cover_data) | ||
| data$Dcr<-data$Fcr/data$Ac | ||
|
|
||
| # calculate Dro for each recruit species | ||
| open_rows <- data[data$Canopy == "Open", ] | ||
| ratios <- tapply(open_rows$Fcr / open_rows$Ac, open_rows$Recruit, mean) | ||
| data$Dro <- ratios[data$Recruit] | ||
|
|
||
|
|
||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| index$int<-paste(index$Canopy,index$Recruit, sep="_") | ||
| db<-merge(data[,c("int","Canopy","Recruit","Fcr","Icr","Pcr","Dcr","Dro")],index[,c("int","Ns", "NintC", "NintA", "RII")], by="int", all.x=T) | ||
| db[is.na(db)]<-0 | ||
| net<-suppressWarnings(RN_to_matrix_UNI(db, weight)) | ||
|
|
||
| if (weight %in% c("Ns", "NintC", "NintA", "RII")) { | ||
| warning("Since the index specified in the 'weight' argument is defined relative to 'Open', rows or columns labeled 'Open' are mathematically zero and not biologically meaningful") | ||
| } | ||
| } | ||
|
|
||
| if(int_type=="fac"){ | ||
|
|
||
| data<-comm_to_RN_BI(int_data,cover_data) | ||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| df<-suppressWarnings(int_significance_BI (data)) | ||
|
|
||
| if(nrow(df[df$Effect_int=="Enhancing",])==0){ | ||
|
|
||
| stop("There is not any recruitment enhancing interaction") | ||
| }else{ | ||
|
|
||
| df<-df[df$Effect_int=="Enhancing",] | ||
| fac_int<-paste(df$Canopy,df$Recruit, sep="_") | ||
| db<-suppressWarnings(associndex_UNISITE_BI(comm_to_RN_BI(int_data,cover_data))) | ||
| db$int<-paste(db$Canopy,db$Recruit, sep="_") | ||
| db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T) | ||
| db<-db[db$int%in%fac_int,] | ||
| net<-suppressWarnings(RN_to_matrix_BI(db, weight)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if(int_type=="comp"){ | ||
|
|
||
| data<-comm_to_RN_UNI_COMP(int_data,cover_data) | ||
| data$int<-paste(data$Canopy,data$Recruit, sep="_") | ||
| df<-suppressWarnings(int_significance_BI_COMP (data)) | ||
|
|
||
| if(nrow(df[df$Effect_int=="Depressing",])==0){ | ||
|
|
||
| stop("There is not any recruitment depressing interaction") | ||
| }else{ | ||
|
|
||
| df<-df[df$Effect_int=="Depressing",] | ||
| comp_int<-paste(df$Canopy,df$Recruit, sep="_") | ||
| db<-suppressWarnings(associndex_UNISITE_BI_COMP(comm_to_RN_UNI_COMP(int_data,cover_data))) | ||
| db$int<-paste(db$Canopy,db$Recruit, sep="_") | ||
| db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T) | ||
| db<-db[db$int%in%comp_int,] | ||
| net<-suppressWarnings(RN_to_matrix_BI(db, weight)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return(net) | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better code structure and robustness, it's recommended to validate the
int_typeargument usingmatch.arg()at the beginning of the function. Additionally, the series ofifstatements should be converted to anif/else ifchain. This is more efficient as it avoids unnecessary checks once a condition is met, and it more clearly expresses the mutually exclusive nature of the code blocks.