[R] apply model predictions over larger area with predict()

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 15 09:50:28 CEST 2008

K. Fleischer wrote:
> Dear all,
> I have built glm models based on presences/absences and a number of 
> predictor maps and would like to compute habitat suitability based on 
> the modelled coefficients. 
> I thought this is pretty straight forward and wanted to use predict() 
> and supply the new data in a data frame, with one column for each 
> predictor. 
> However, I do get an error msg warning me that the number of rows for 
> old and new data do not match. 
> the script looks like that:
> model<-glm(species~temp+prec+elev,family=binomial(link=logit))
> #whereby temp,prec,elev are in vector format and contain the elements 
> on species presence/absence; species is vector of 0's and 1's 
> (length=319)
> wholearea<-data.frame(cbind(as.vector(temperature),as.vector
> (precipitation),as.vector(elevation))  # (length=7526)
> predict(model, newdata=wholearea,type="response")
> Warning message: 'newdata' had 7526 rows but variable(s) found have 
> 319 rows.
> Ive searched quite a while for the answer now, has anyone encountered 
> that problem before?? thanx in advance.

Your newdata does not contain the variables named temp, prec, and elev 
that your model refers to. (And you should get rid of that cbind, but 
that is not the problem.)

    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907

More information about the R-help mailing list