0

我想在我的三维依赖图中的拟合值之后添加颜色渐变(例如,拟合值越高,颜色越深,拟合值越低,颜色越浅)。

我使用了 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")

一种颜色的交互图

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

    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))

不同颜色的交互图不遵循拟合值

我还检查了函数 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), ...)

交互图在预测值 (?) 之后着色,但未正确显示颜色

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

感谢您的时间!

4

2 回答 2

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))

在此处输入图像描述

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

于 2017-10-17T22:17:25.317 回答
0

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

第一次运行

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

然后,添加z.facet.centergbm.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), ...) #

这将为您提供这样的图(请注意,这不是使用示例数据集绘制的,这就是交互效果与问题中的图不同的原因)。在此处输入图像描述

于 2020-06-28T14:46:57.060 回答