mrCoOccurNet_bootstrap: Generate a MrIML co-occurrence network

View source: R/mrCoOccurNet_bootstrap.R

mrCoOccurNet_bootstrapR Documentation

Generate a MrIML co-occurrence network

Description

This function generates a co-occurrence network from a provided list and calculates strength and directionality of the relationships.

Usage

mrCoOccurNet_bootstrap(mrPD_obj, Y)

Arguments

mrPD_obj

A list of model predictions.

Y

The response data.

Value

A dataframe representing the co-occurrence network with strength and directionality.

Examples

## Not run: 
# Example usage:
#set up analysis
Y <- dplyr::select(Bird.parasites, -scale.prop.zos)%>% 
dplyr::select(sort(names(.)))#response variables eg. SNPs, pathogens, species....
X <- dplyr::select(Bird.parasites, scale.prop.zos) # feature set
X1 <- Y %>%
dplyr::select(sort(names(.)))
model_rf <- 
rand_forest(trees = 100, mode = "classification", mtry = tune(), min_n = tune()) %>% #100 trees are set for brevity. Aim to start with 1000
set_engine("randomForest")
yhats_rf <- mrIMLpredicts(X=X, Y=Y,
X1=X1,'Model=model_rf , 
balance_data='no',mode='classification',
tune_grid_size=5,seed = sample.int(1e8, 1),'morans=F,
prop=0.7, k=5, racing=T) #

bs_analysis <- mrBootstrap(yhats=yhats_rf,Y=Y, num_bootstrap = 5)
assoc_net<- mrCoOccurNet_bootstrap (mrPD_obj=pds , Y=Y)

assoc_net_filtered <-  assoc_net %>% 
filter(mean_strength > 0.05)
g <- graph_from_data_frame(assoc_net_filtered, directed=TRUE, vertices=names(Y)) #matching Y data
E(g)$Value <- assoc_net_filtered$mean_strength###chnge this as needed
E(g)$Color <- ifelse(assoc_net_filtered$direction == "negative", "blue", "red")
gg <- ggnetwork(g)
ggplot(gg, aes(x = x, y = y, xend = xend, yend = yend)) +
 geom_edges(aes(color = Color, linewidth = (Value)), curvature = 0.2,
            arrow = arrow(length = unit(5, "pt"), type = "closed")) + #makes arrows bigger
 geom_nodes(color = "gray", size = degree(g, mode = "out")/2)+#, fill='black', stroke =2) +
scale_color_identity() +
 theme_void() +
 theme(legend.position = "none")  +
 geom_nodelabel_repel(aes(label = name),
                     box.padding = unit(0.5, "lines"),
                      data = gg,
                      size=2,
                     segment.colour = "black",
                     colour = "white", fill = "grey36")
## End(Not run) 

nfj1380/mrIML documentation built on May 17, 2024, 7:41 a.m.