当因子变量包含在模型

时间:2018-04-28 00:02:52

标签: r session prediction r-raster gbm

这个问题与我在一年半前发布的这个问题有关:Reproducibility of results from predict() function - raster package。但由于它没有一个例子,我已经用更新的信息创建了一个新问题。

在将我的预测复制到栅格时,我有一个模糊的问题。我正在创建一个带有数值变量和单因子变量的gbm模型。然后,我使用光栅包使用我训练过的模型预测到栅格。预测会话会话不同,但在单个R会话中重现。如果我删除因子变量,结果会重新创建会话会话。此外,在我的下面的示例中,如果我在训练数据中的因子级别比在栅格变量版本中有更多的因子级别,我可以让它重现会话到会话。导致这种情况的原因以及如何在包含因子变量的同时将结果会话重现为会话?

# This code will not reproduce session to session, but does if I leave many many factor levels in newwine with the
# commented out code

library(breakDown)
library(gbm)
library(dplyr)
library(raster)

# leave in many levels and code will reproduce session to session
#newwine <- wine[1:500,c(1:3,6)]

# specify only levels which are in the below raster and code will not reproduce session to session
newwine <- wine[,c(1:3,6)] %>% 
           filter(free.sulfur.dioxide == 3 | free.sulfur.dioxide == 10 | free.sulfur.dioxide == 15 |
                  free.sulfur.dioxide == 37 | free.sulfur.dioxide == 76)

head(newwine)

# make free.sulfur.dioxide as factor variable
newwine$free.sulfur.dioxide <- as.factor(newwine$free.sulfur.dioxide)
levels(newwine$free.sulfur.dioxide)

set.seed(123)
model <- gbm(fixed.acidity ~ ., data = newwine, 
             distribution = "gaussian",
             bag.fraction = 0.50,
             n.trees = 1000, 
             interaction.depth = 16, 
             shrinkage = 0.016, 
             n.minobsinnode = 10, verbose = FALSE)

summary(model)
plot(model, i.var = 3, n.trees = 1000)


# make some rasters for the predictor variables
free.sulfur.dioxide <- c(rep(3,times=10), rep(10, times = 10), 
                         rep(15, times = 10), rep(37, times = 10), 
                         rep(76, times = 10))

free.sulfur.dioxide.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(free.sulfur.dioxide.r) <- free.sulfur.dioxide

set.seed(123)
volatile.acidity <- newwine %>% 
                    dplyr::select(volatile.acidity) %>% 
                    sample_n(50) 
volatile.acidity <- as.vector(volatile.acidity)[,1]
volatile.acidity.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(volatile.acidity.r) <- volatile.acidity

set.seed(123)
citric.acid <- newwine %>% 
               dplyr::select(citric.acid) %>% 
               sample_n(50) 
citric.acid <- as.vector(citric.acid)[,1]
citric.acid.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(citric.acid.r) <- citric.acid

# create a raster stack
r <- stack(free.sulfur.dioxide.r, volatile.acidity.r, citric.acid.r)
names(r) <- c("free.sulfur.dioxide", "volatile.acidity", "citric.acid")

###########################################################################################################################

# predict to a raster with raster predict
pred <- predict(r, model, n.trees = model$n.trees, format="GTiff")
writeRaster(pred, "prediction1.tif", overwrite = TRUE)

###########################################################################################################################

# close the session and reopen, run until line 61, then run below to make a new prediction, called prediction 2
pred <- predict(r, model, n.trees = model$n.trees, format="GTiff")
writeRaster(pred, "prediction2.tif", overwrite = TRUE)

# read in the previous prediction
prediction1 <- raster("prediction1.tif")
prediction2 <- raster("prediction2.tif")

# compare rasters built across sessions
compareRaster(prediction1, prediction2, values = TRUE)
summary(prediction1-prediction2)

# compare rasters built within same session
pred2 <- predict(r, model, n.trees = model$n.trees, format="GTiff")
compareRaster(pred, pred2, values = TRUE)

但是,以下代码不使用因子变量,并将会话重现会话。

### Same exercise but without setting the free sulfur dioxide to factor
## this code will reproduce session to session

library(breakDown)
library(gbm)
library(dplyr)
library(raster)

newwine <- wine[1:500,c(1:3)]

head(newwine)

set.seed(123)
model <- gbm(fixed.acidity ~ ., data = newwine, 
             distribution = "gaussian",
             bag.fraction = 0.50,
             n.trees = 1000, 
             interaction.depth = 16, 
             shrinkage = 0.016, 
             n.minobsinnode = 10, verbose = FALSE)

summary(model)

set.seed(123)
volatile.acidity <- newwine %>% 
  dplyr::select(volatile.acidity) %>% 
  sample_n(50) 
volatile.acidity <- as.vector(volatile.acidity)[,1]
volatile.acidity.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(volatile.acidity.r) <- volatile.acidity

set.seed(123)
citric.acid <- newwine %>% 
  dplyr::select(citric.acid) %>% 
  sample_n(50) 
citric.acid <- as.vector(citric.acid)[,1]
citric.acid.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(citric.acid.r) <- citric.acid

# create a raster stack
r <- stack( volatile.acidity.r, citric.acid.r)
names(r) <- c( "volatile.acidity", "citric.acid")

#######################################################################################################################

# predict to a raster with raster predict
pred <- predict(r, model, n.trees = model$n.trees, format="GTiff")
writeRaster(pred, "prediction1.tif", overwrite = TRUE)

#######################################################################################################################

# close the session and reopen to make a new prediction, called prediction 2
pred <- predict(r, model, n.trees = model$n.trees, format="GTiff")
writeRaster(pred, "prediction2.tif", overwrite = TRUE)

# read in the previous prediction
prediction1 <- raster("prediction1.tif")
prediction2 <- raster("prediction2.tif")

# compare rasters built across sessions
compareRaster(prediction1, prediction2, values = TRUE)
summary(prediction1-prediction2)

# compare rasters built within same session
pred2 <- predict(r, model, n.trees = model$n.trees, format="GTiff")
compareRaster(pred, pred2, values = TRUE)
summary(pred-pred2)

2 个答案:

答案 0 :(得分:1)

看来这个问题不是由raster包引起的,而是由gbm包引起的。经过一番挖掘后,我发现gbm包已于2017年3月成为孤儿,并且在github上有一个名为gbm3的新gbm包(在CRAN上尚未提供)https://github.com/gbm-developers/gbm3。当您预测到栅格时,您可以使用模型类型所需的任何预测方法(例如predict.gbm() gbmpredict.GBMFit() gbm3predict.gbm()只是不能正确处理来自模型中栅格的因素。它可能是也可能不是错误,但在任何一种情况下,都不再维护此包。gbm3可以解决问题并且可以重现。

# This code will reproduce session to session for the gbm3 model, but not for old gbm model

library(breakDown)
# install gbm3 from github
library(gbm3)
library(dplyr)
library(raster)

# specify only levels which are in the below raster 
newwine <- wine[,c(1:3,6)] %>% 
           filter(free.sulfur.dioxide == 3 | free.sulfur.dioxide == 10 | free.sulfur.dioxide == 15 |
                  free.sulfur.dioxide == 37 | free.sulfur.dioxide == 76)

head(newwine)

# make free.sulfur.dioxide as factor variable
newwine$free.sulfur.dioxide <- as.factor(newwine$free.sulfur.dioxide)
levels(newwine$free.sulfur.dioxide)

#set.seed(123)
# model <-  gbm(fixed.acidity ~ ., data = newwine, #gbm.fit(x = newwine[,2:4], y = newwine[,1], 
#              distribution = "gaussian",
#              bag.fraction = 0.50,
#              n.trees = 1000, 
#              interaction.depth = 16, 
#              shrinkage = 0.016, 
#              n.minobsinnode = 10, verbose = FALSE)
set.seed(123)
model <- gbmt(fixed.acidity ~ ., data = newwine, distribution = gbm_dist("Gaussian")) 

summary(model)
plot(model, var_index = 3, num_trees = 1000)

# make some rasters for the predictor variables
free.sulfur.dioxide <- c(rep(3,times=10), rep(10, times = 10), 
                         rep(15, times = 10), rep(37, times = 10), 
                         rep(76, times = 10))

free.sulfur.dioxide.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(free.sulfur.dioxide.r) <- free.sulfur.dioxide

set.seed(123)
volatile.acidity <- newwine %>% 
                    dplyr::select(volatile.acidity) %>% 
                    sample_n(50) 
volatile.acidity <- as.vector(volatile.acidity)[,1]
volatile.acidity.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(volatile.acidity.r) <- volatile.acidity

set.seed(123)
citric.acid <- newwine %>% 
               dplyr::select(citric.acid) %>% 
               sample_n(50) 
citric.acid <- as.vector(citric.acid)[,1]
citric.acid.r <- raster(ext = extent(-10, 5, -10, 5), nrows = 5, ncols = 10)
values(citric.acid.r) <- citric.acid

# create a raster stack
r <- stack(free.sulfur.dioxide.r, volatile.acidity.r, citric.acid.r)
names(r) <- c("free.sulfur.dioxide", "volatile.acidity", "citric.acid")

###########################################################################################################################

# predict to a raster with raster predict
pred <- raster::predict(r, model, n.trees = 2000, format="GTiff")
writeRaster(pred, "prediction1.tif", overwrite = TRUE)

# predict to a vector with predict
v <- values(r)
v <- data.frame(v)
v$free.sulfur.dioxide <- as.factor(v$free.sulfur.dioxide)
vpred <- predict(model, v, n.trees = 2000)
write.table(vpred, "vector_predict.txt", row.names = FALSE, col.names = TRUE)

###########################################################################################################################

# close the session and reopen, run until #### line, then run below to make a new prediction, called prediction 2
pred <- raster::predict(r, model, n.trees = 2000, format="GTiff")
writeRaster(pred, "prediction2.tif", overwrite = TRUE)

# predict to a vector with predict
v <- values(r)
v <- data.frame(v)
v$free.sulfur.dioxide <- as.factor(v$free.sulfur.dioxide)
vpred <- predict(model, v, n.trees = 2000)
write.table(vpred, "vector_predict2.txt", row.names = FALSE, col.names = TRUE)

# read in the previous prediction
prediction1 <- raster("prediction1.tif")
prediction2 <- raster("prediction2.tif")

# compare rasters built across sessions
compareRaster(prediction1, prediction2, values = TRUE)
summary(prediction1-prediction2)

# compare rasters built within same session
pred2 <- raster::predict(r, model, n.trees = 2000, format="GTiff", factors = f)
compareRaster(pred, pred2, values = TRUE)

# compare the vector predictions
p1 <- read.delim("vector_predict.txt")
p2 <- read.delim("vector_predict2.txt")

plot(p1$x,p2$x)

summary(p1$x - p2$x)

答案 1 :(得分:0)

这不是解决方案,而是试图解决问题。在我看来,这与raster无关。

当我这样做时:

v <- values(r)
pred <- predict(model, data.frame(v), n.trees = model$n.trees)
rpred <- predict(r, model, n.trees = model$n.trees)

退出,保存会话,开始新会话并执行:

library(gbm)
library(raster)
pred2 <- predict(model, data.frame(v), n.trees = model$n.trees )
rpred2 <- predict(r, model, n.trees = model$n.trees)

我发现predpred2的值并不完全相同。 (请参阅plot(pred, pred2)。但是,pred2rpred2的值相同:plot(values(rpred2), pred2)

或者,当我保存predsaveRDS(pred, 'pred.rds'),并将其加载到新会话pred1 <- readRDS(pred.rds)时,结果并不完全相同。

它向我建议,gbm中的某个地方会发生一些不受set.seed控制的随机化。