2017-03-17 128 views
0

我想在我的三维依赖图中添加颜色渐变色,后面是拟合值(例如较高的拟合值较暗的颜色,较低的拟合值较浅的颜色)。BRT:使用gbm.perspec为交互图添加渐变颜色

我已经使用在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的代码,如果我正确理解的拟合值是式为“预测”内部呼叫,后来就是“pred.matrix”的一部分这是传递给最终的绘图:persp(x = x.var,y = y.var,z = pred.matrix ...),但我没有设法从gbm.perspec公式访问它们。我尝试通过在函数内部的persp()中添加“col = heat.colors(100)[round(pred.matrix * 100,0)]”来修改gbm.perpec函数,但它不会执行我的操作寻找:

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功能,你知道怎么样?

谢谢你的时间!

回答

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轴,你应该能够产生正是你想要的。