Hi,

I would like to make a raster, based on the regression coefficients with 5 other rasters (Predictors). In theory this should be easy using the predict function of the raster package (http://cran.r-project.org/web/packages/raster/raster.pdf). But I fail to get it to work.

First I fit a glm logit model using 5 cregressors:


> model <- glm(c3p$t0~c3p$ycoord+c3p$prec+c3p$tempmean+c3p$tempmax+c3p$tempmin, family=binomial(link="logit"))

> logregt0



Call:  glm(formula = c3p$t0 ~ c3p$ycoord + c3p$prec + c3p$tempmean +

    c3p$tempmax + c3p$tempmin, family = binomial(link = "logit"))



Coefficients:

 (Intercept)    c3p$ycoord      c3p$prec  c3p$tempmean   c3p$tempmax   c3p$tempmin

  -7.179e+01     7.380e-06    -1.621e-03    -2.261e-01     7.630e-02     1.787e-01

Then I make a raster stack of the 5 prediction surfaces:

> newgrid = raster("newgrid.txt")
> Predictors <- stack(newgrid)
> Predictors <- addLayer(ycoord,prec,tempmean,tempmax,tempmin)
> layerNames(Predictors)
[1] "ycoord"   "prec"     "tempmean" "tempmax"  "tempmin"

Then I try to make a new raster based on the reg. coeeficients of the 5 prediction surfaces:


> PredRegSurface <- predict(object=Predictors, model=logregt0)





Error in v[cells, ] <- predv :

  number of items to replace is not a multiple of replacement length

In addition: Warning message:

'newdata' had 40000 rows but variable(s) found have 1350 rows

In above error message:
1st part I do not understand, 2nd part refers to the Predictors raster (that has more than 40.000 rows) and the datafile on which the glm model is based v(which has 1350 rows). I must be doing something wrong in the syntax, which I have been changing for hours already without any result. Does anybody have any ideas or better even some script of this probably quite common thing (prediction from multiple regressors)?



Hein Bouwmeester
Wageningen University



	[[alternative HTML version deleted]]

