-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalVarImpRF.R
39 lines (25 loc) · 1.04 KB
/
LocalVarImpRF.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
# Required package
library(ranger)
### Function to determine local importance of covariates
LocalVarImp <- function(ranger.rf.object, obs){
nodeIDs <- ranger.rf.object$forest$child.nodeIDs
varIDs <- ranger.rf.object$forest$split.varIDs
predNodeID <- predict(object = ranger.rf.object, data = obs, type = "terminalNodes")$predictions
# We count the number of times each covariate has been used to allocate the observed data
impx <- rep(0, ranger.rf.object$num.independent.variables)
ntree <- rf.ranger$forest$num.trees
for( b in 1:ntree ){
motherNode <- predNodeID[b]
while(motherNode != 0){
if(motherNode%%2 == 1){
motherNode <- which(nodeIDs[[b]][[1]] == motherNode)-1 # Odd
}
else{
motherNode <- which(nodeIDs[[b]][[2]] == motherNode)-1 # Even
}
impx[varIDs[[b]][motherNode+1]] <- impx[varIDs[[b]][motherNode+1]] + 1 # Increment
}
}
impxStand <- impx/sum(impx)
return(stdImp = impxStand)
}