-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvariable_importance.R
More file actions
42 lines (26 loc) · 1.65 KB
/
variable_importance.R
File metadata and controls
42 lines (26 loc) · 1.65 KB
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
var_importance <- function(pur_res,X, plot=TRUE, left_margin=7){
my_components <- unique(unlist(sapply(1:length(pur_res), function(s)
pur_res[[s]]$my.functions[sapply(1:length(pur_res[[s]]$my.functions), function(z) sum(abs(pur_res[[s]]$values[[z]]))>0)]
),recursive = FALSE))
my_components <- my_components[order(sapply(my_components, function(x){ if (length(x)>1) {length(x)+10} else x } ))]
var_names <- sapply(1:length(my_components), function(k){
paste(
paste(colnames(X)[as.numeric(my_components[[k]])], collapse = ","),"(",
paste(as.numeric(my_components[[k]]), collapse = ","),")",sep = "")
}
)
#var <- sapply(1:length(my_components), function(k){paste(as.numeric(my_components[[k]]), collapse = ",")})
importance <- sapply(1:length(my_components), function(k){
mean( sapply(1:length(pur_res), function(s){
my.var.tree.index <- (1:length(pur_res[[s]]$my.functions))[ sapply(1:length(pur_res[[s]]$my.functions), function(i) setequal(as.numeric(pur_res[[s]]$my.functions[[i]]),my_components[[k]]))] ### all trees equal function component
if(length(my.var.tree.index )==0) return(0)
if (sum(my_components[[k]])==0) return(abs(pur_res[[s]]$values[[my.var.tree.index]]))
return(sum(pur_res[[s]]$individuals[[my.var.tree.index]]*abs(pur_res[[s]]$values[[my.var.tree.index]]))/sum(pur_res[[1]]$individuals[[1]]))
}))
})
res <- cbind(var_names,importance )
res2 <- res[order(as.numeric(res[,2])),]
par(mar = c(3, left_margin, 3, 3)) # Set the margin on all sides to 6
if (plot==TRUE) barplot(height=as.numeric(res2[,2]),main="Variable importance", horiz=TRUE,names.arg=res2[,1],las=1)
res[order(-as.numeric(res[,2])),]
}