model.frame.default(terms(reformulate(object $ var.names)),newdata中的错误:变量'X2'的无效类型(S4)

时间:2019-03-01 09:24:55

标签: r machine-learning prediction

我已经为R中的物种分布建立了一个预测模型,现在我想在.asc图像中进行相同的预测以了解空间分布。它以某种方式显示错误。为什么这会发生错误?我的代码如下:

#Data previously added and breakdown to train_set and valid_set
>head(train_set)
                Y          X1     X2       X3
    1            0   7.076299 26.525 23.137705
    2            0   6.219646 25.720 26.406429
    3            0  12.922848 27.110 41.971050
    4            1   7.419030 26.060 42.125389
    5            1  11.445523 26.735 50.397018
    6            1  12.288206 26.860 47.862190
    >library(dismo)
    >library(gbm)
    > library(maptools)
    > ribbon.tc3.lr005 <- gbm.step(data=train_set, gbm.x = 2:4, gbm.y = 1,family = "bernoulli",tree.complexity = 3,learning.rate = 0.005, bag.fraction = 0.6)


    #prediction with valid_set data from model
    > preds <- predict.gbm(ribbon.tc3.lr005, valid_set,n.trees=ribbon.tc3.lr005$gbm.call$best.trees, type="response")
    > calc.deviance(obs=valid_set$Y, pred=preds, calc.mean=TRUE)
    [1] 0.9814844

    #evaluation of model
    > d <- cbind(valid_set$Y, preds)
    > pres <- d[d[,1]==1, 2]
    > abs <- d[d[,1]==0, 2]
    > e <- evaluate(p=pres, a=abs)
    > e

    #import the asc image for making prediction map
    >X1 <-  raster("H:/SAC/predictor_var/X1.asc")
    >X2 <-  raster("H:/SAC/predictor_var/X2.asc")
    >X3 <-  raster("H:/SAC/predictor_var/X3.asc")

    >raster.stack<-stack(X1,X2,X3)

    >raster.stack
        # class       : RasterStack 
        # dimensions  : 322, 294, 94668, 3  (nrow, ncol, ncell, nlayers)
        # resolution  : 0.04167014, 0.04167014  (x, y)
        # extent      : 63.27028, 75.5213, 12.32039, 25.73818  (xmin, xmax, ymin, ymax)
        # coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
        # names       : X1,X2,X3 

    p <- predict(raster.stack, ribbon.tc3.lr005, filename='april', progress='text',
           n.tree=ribbon.tc3.lr005$n.trees, type='response', format='GTiff')

    Returns:

        Error in model.frame.default(terms(reformulate(object$var.names)),
        newdata,  :    invalid type (S4) for variable 'X2'

0 个答案:

没有答案