forked from iandurbach/ml-for-ecology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable-importance.R
56 lines (39 loc) · 1.24 KB
/
variable-importance.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
#### Assessing variable importance and variable effects
# - variable importance plots
# - partial dependence plots
library(randomForest)
library(gbm)
source("utils.R")
# load data
load("data/aloe.RData")
head(aloe_pa)
# load previous models
load("output/aloe_models.RData")
##### variable importance and partial dependence plots
## for bagging
varImpPlot(bag, main = "")
partialPlot(bag,
pred.data = subset(aloe_pa,
train_id != 3),
x.var = Longitude,
which.class = 1,
n.pt = 50)
## for random forests
varImpPlot(rf, main = "")
partialPlot(rf,
pred.data = subset(aloe_pa,
train_id != 3),
x.var = Longitude,
which.class = 1,
n.pt = 50)
## for boosting
best.iter <- gbm.perf(boost, method="cv")
# plot variable influence
summary(boost, n.trees = best.iter, par(las=1)) # based on the estimated best number of trees
# plot marginal effects (watch out for scale!)
plot(boost, 1, best.iter, type = "response")
plot(boost, 2, best.iter, type = "response")
plot(boost, 3, best.iter, type = "response")
# lattice plot of Lat-Long effects
par(mfrow=c(1,1))
plot(boost, 1:2, best.iter, type = "response")