-
Notifications
You must be signed in to change notification settings - Fork 3
/
misc_functions.R
83 lines (71 loc) · 2.37 KB
/
misc_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Load packages
library("bnlearn")
library("gRain")
library("tools")
########## functions used in the Ecological Integrity BN modelling workflow
##### BN specification
# Replace spaces and points by "_", remove file extensions
fixNames = function(string_vector){
string_vector = file_path_sans_ext(string_vector)
string_vector = gsub(" ", "_", string_vector)
string_vector = gsub("\\.", "_", string_vector)
return(string_vector)
}
# In a matrix:
# fix column and row names
fixDimnames = function(adj_mat){
colnames(adj_mat)=fixNames(colnames(adj_mat))
rownames(adj_mat)=fixNames(rownames(adj_mat))
return(adj_mat)
}
# Initialize empty graph with appropriate node names
# the names are read from the row names of a adjacency matrix
# read from a file (e.g. a csv) or a specified vector
initAdj = function(adj_mat, custom_names=NULL){
if (is.null(custom_names)){
bngraph = empty.graph(row.names(adj_mat))
}
else{
bngraph = empty.graph(custom_names)
}
return(bngraph)
}
# For a group of nodes set arcs based on a adjacency matrix
setArcs = function(bngraph,
adj_mat){
amat(bngraph) = as.matrix(adj_csv,row.names=TRUE)
return(bngraph)
}
##### BN fit
# Produce a raster brick where first variable
# is the dependent variable (Ecological Integrity proxy)
# and the rest independent variables
# All raster are assumed harmonized (same projection, res, extent)
bnBrick = function(dep_path,
indep_paths){
bnbrik = brick()
bnbrik = addLayer(bnbrik,raster(dep_path))
for (i in 1:length(indep_paths)){
bnbrik = addLayer(bnbrik,raster(indep_paths[i]))
}
return(bnbrik)
}
# Coerce list of integer valued variables to factor
factorCols = function(bnbrik_df,categorical_var_vec){
for (i in 1:length(categorical_var_vec)){
bnbrik_df[,
categorical_var_vec[i]]=as.factor(bnbrik_df[,
categorical_var_vec[i]])
}
return(bnbrik_df)
}
# Discretize list of numeric valued variables
discretizeCols = function(bnbrik_df,
numeric_var_vec,
breaks_vec=rep(5,length(numeric_var_vec)),
method="interval"){
bnbrik_df[,numeric_var_vec] = bnlearn::discretize(bnbrik_df[,numeric_var_vec],
breaks=breaks_vec,
method=method)
return(bnbrik_df)
}