BRT:使用gbm.perspec为交互图添加渐变颜色

时间:2017-03-17 09:54:13

标签: r machine-learning tree gbm dismo

我想在我的三维依赖图中按照拟合值(例如,更高的拟合值,更暗的颜色,更低的拟合值,更浅的颜色)添加渐变颜色。

我使用了dismo包中提供的示例:

library(dismo) 
data(Anguilla_train)
angaus.tc5.lr01 <- gbm.step(data=Anguilla_train, gbm.x = 3:13, gbm.y = 2,
family = "bernoulli", tree.complexity = 5, learning.rate = 0.01, 
bag.fraction = 0.5)

# Find interactions in the gbm model:
find.int <- gbm.interactions( angaus.tc5.lr01)
find.int$interactions
find.int$rank.list

我只是设法为整个情节添加相同的颜色:

gbm.perspec( angaus.tc5.lr01, 7, 1,
            x.label = "USRainDays",
            y.label = "SegSumT", 
            z.label = "Fitted values",
            z.range=c(0,0.435),
            col="blue")

Interaction plot all in one colour

或者添加渐变颜色但不遵循拟合值:

    gbm.perspec( angaus.tc5.lr01, 7, 1,
             x.label = "USRainDays",
             y.label = "SegSumT", 
             z.label = "Fitted values",
             col=heat.colors(50),
             z.range=c(0,0.435))

Interaction plot with different colours not following fitted values

我还检查了函数gbm.perspec的代码,如果我理解正确,则在公式内调用拟合值为&#34;预测&#34;以及稍后是&#34; pred的一部分。矩阵&#34;传递给最终的绘图:persp(x = x.var,y = y.var,z = pred.matrix ...),但我没有设法从gbm.perspec公式访问它们。我尝试通过添加&#34; col = heat.colors(100)[round(pred.matrix * 100,0)]&#34;来修改gbm.perpec函数。进入函数内部的persp(),但它没有做我想要的:

persp(x = x.var, y = y.var, z = pred.matrix, zlim = z.range, 
      xlab = x.label, ylab = y.label, zlab = z.label, 
      theta = theta, phi = phi, r = sqrt(10), d = 3, 
      ticktype = ticktype,
      col=heat.colors(100)[round(pred.matrix*100, 0)], 
      mgp = c(4, 1, 0), ...)

Interaction plot coloured following predicted values (?) but not showing the colours properly

我相信解决方案可能来自修改gbm.perpec函数,你知道吗?

感谢您的时间!

2 个答案:

答案 0 :(得分:0)

修改gbm.perspec函数当然是一个选项,但是如果你使用gbm模型中的预测值并将它们绘制到另一个包的3D散点图上,你也应该能够实现它。

这是一个使用plot3Drgl包的选项,原始代码由@Fabrice提供。

library(dismo); library(plot3Drgl); library(devEMF)
data(Anguilla_train)
angaus.tc5.lr01 <- gbm.step(data=Anguilla_train, gbm.x = 3:13, gbm.y = 2,
                            family = "bernoulli", tree.complexity = 5, learning.rate = 0.01, 
                            bag.fraction = 0.5)

# Find interactions in the gbm model:
find.int <- gbm.interactions( angaus.tc5.lr01)
find.int$interactions
find.int$rank.list

d<-plot(angaus.tc5.lr01,c(1,7),return.grid=T)


x <- d$SegSumT
y <- d$USRainDays
z <- d$y


grid.lines = 30
elevation.site = loess(z ~ x*y, data=d, span=1, normalize = FALSE) 
x.pred <- seq(min(x), max(x), length.out = grid.lines) # x grid
y.pred <- seq(min(y), max(y), length.out = grid.lines) # y grid
xy <- expand.grid( x = x.pred, y = y.pred)  # final grid combined
z.site=matrix(predict(elevation.site, newdata = xy), nrow = grid.lines, ncol = grid.lines) # predicedt matrix

scatter3D(x, y, z, theta = 160, phi = 35, # x y z coords and angle of plot
          clab = c(""), # Needs moving - label legend
          colkey = list(side = 4, length = 0.65, 
                        adj.clab = 0.15, dist = -0.15, cex.clab = 0.6, cex.axis = 0.6), # change the location and length of legend, change position of label and legend
          clim = c(-4,0.1),
          bty = "b", # type of box
          col = ramp.col(col = c("grey", "blue"), 200),
          pch = 19, cex = 0.55, # shape and size of points
          xlab = "SegSumT", 
          xlim=c(10,20),ylim=c(0,3.5), zlim=c(-4,0.1), d= 2,
          ylab = "USRaindays",
          zlab= "Fitted values", #axes labels
          cex.lab = 0.8, font.lab = 1, cex.axis = 0.6, font.axis= 1, # size and font of axes and ticks
          ticktype = "detailed", nticks = 5, # ticks and numer of ticks
          #type = "h", # vertical lines
          surf = list(x = x.pred, y = y.pred, z = z.site,  
                      facets = NA, CI=NULL))

enter image description here

通过使用grid.lines调整并反转x轴,您应该能够准确地生成您想要的内容。

答案 1 :(得分:0)

通过将here中找到的一些代码合并到gbm.perspec()源代码中,您可以创建所需的效果。

首次运行

# Color palette (100 colors)
col.pal<-colorRampPalette(c("blue", "red"))
colors<-col.pal(100)

然后,在z.facet.center之后将gbm.perspec()添加到else源代码中,并将代码中的z更改为pred.matrix,如下所示,

# and finally plot the result
#
if (!perspective) {
  image(x = x.var, y = y.var, z = pred.matrix, zlim = z.range)
} else {
  z.facet.center <- (pred.matrix[-1, -1] + pred.matrix[-1, -ncol(pred.matrix)] + 
                       pred.matrix[-nrow(pred.matrix), -1] + pred.matrix[-nrow(pred.matrix), -ncol(pred.matrix)])/4
  # Range of the facet center on a 100-scale (number of colors)
  z.facet.range<-cut(z.facet.center, 100)
  persp(x=x.var, y=y.var, z=pred.matrix, zlim= z.range,      # input vars
        xlab = x.label, ylab = y.label, zlab = z.label,   # labels
        theta=theta, phi=phi, r = sqrt(10), d = 3,
        col=colors[z.facet.range],# viewing pars
        ticktype = ticktype, mgp = c(4,1,0), ...) #

这将为您提供这样的图(请注意,这不是使用示例数据集绘制的,这就是为什么交互效果与问题中的图不同的原因)。 enter image description here