[R-sig-Geo] predict function of regressors in the raster package

2013-04-04 Thread Bouwmeester, Hein
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 4 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]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] predict function of regressors in the raster package

2013-04-04 Thread Jesse Berman
Hi Hein,

I'm not sure if this will help, but one thing to check is that your
prediction grid has covariate data for each of the 40,000 cells.  If a large
number of cells have 'NA' as data values, then sometimes the prediction will
not work.  Offhandedly, it strikes me that ycoord may be limited to only
your t0 locations.

Jesse



--
View this message in context: 
http://r-sig-geo.2731867.n2.nabble.com/predict-function-of-regressors-in-the-raster-package-tp7583201p7583205.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] predict function of regressors in the raster package

2013-04-04 Thread Robert J. Hijmans
Hein,

I think this problem goes away if you use a clean formula, like this:

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

Robert



On Thu, Apr 4, 2013 at 7:42 AM, Jesse Berman berman.je...@gmail.com wrote:

 Hi Hein,

 I'm not sure if this will help, but one thing to check is that your
 prediction grid has covariate data for each of the 40,000 cells.  If a
 large
 number of cells have 'NA' as data values, then sometimes the prediction
 will
 not work.  Offhandedly, it strikes me that ycoord may be limited to only
 your t0 locations.

 Jesse



 --
 View this message in context:
 http://r-sig-geo.2731867.n2.nabble.com/predict-function-of-regressors-in-the-raster-package-tp7583201p7583205.html
 Sent from the R-sig-geo mailing list archive at Nabble.com.

 ___
 R-sig-Geo mailing list
 R-sig-Geo@r-project.org
 https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo