[R] apply model predictions over larger area with predict()
Greg Snow
Greg.Snow at imail.org
Wed Oct 15 16:23:12 CEST 2008
You did not read Peters response close enough.
Do:
> names(areadata)
And you will see that the names of your areadata object do not match with what the predict function is expecting. When you create areadata, you need to name the columns (or name them afterwards), it does not automatically generate the correct names. Try something like:
> areadata<- data.frame(veld=as.vector(veld), water=as.vector(water), moeras=as.vector(moeras))
Or use the same definition of areadata, but follow with the command:
> names(areadata) <- c('veld','water','moeras')
Whenever R complains about not finding something in your data, it is a good idea to take a close look at your data to make sure that it matches what it should (and predict is very picky about names matching exactly). Functions that help with this include names, head, tail, View, str, and print.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of K. Fleischer
> Sent: Wednesday, October 15, 2008 4:06 AM
> To: ONKELINX, Thierry
> Cc: r-help at r-project.org
> Subject: Re: [R] apply model predictions over larger area with
> predict()
>
> Dear all,
>
> thanx for the quick reply but your ideas have not gotten me further
> unfortuantely..
> after implementing the idea from Thierry my code looks like that now:
>
> #b00all is the dataframe with only element used for model fitting
> #variables .._zui contain the whole are over which to predict
> veld<-b00all$veld00all_zui
> water<-b00all$kmoi_water_zui
> moeras<-b00all$kveg_moeras_zui
> specvec<-b00all$specvec
> mydata<-data.frame(specvec,veld,water,moeras)
>
> b00all_mdl6<-glm(specvec~veld+water+moeras,
> data=mydata, family=binomial(link=logit))
> veld<-veld00all_zui
> water<-kmoi_water_zui
> moeras<-kveg_moeras_zui
> areadata<-
> data.frame(as.vector(veld),as.vector(water),as.vector(moeras))
> zuid<-predict(b00all_mdl6,newdata=areadata,type="response")
>
> Error: variables 'veld', 'water', 'moeras' were specified with
> different types from the fit
> In addition: Warning message:
> 'newdata' had 7526 rows but variable(s) found have 106 rows
>
> So again the problem is as follows:
>
> I have a dataframe with each column representing one predictor, whereby
> first columns contains species presence/absences and every row is one
> observation.
>
> now I want to take the model output and apply it over the whole area
> for which I have predictor values for, which are stored in .asc maps.
>
> That cannot be too difficult I am sure!!
>
> thanx again,
> Katrin
>
> MSc student
> Computational Geo-Ecology
> University of Amsterdam
>
>
>
>
> ----- Original Message -----
> From: "ONKELINX, Thierry" <Thierry.ONKELINX at inbo.be>
> Date: Wednesday, October 15, 2008 9:56 am
> Subject: RE: [R] apply model predictions over larger area with
> predict()
> To: "K. Fleischer" <K.Fleischer at student.uva.nl>, r-help at r-project.org
>
> > Dear Katrin,
> >
> > Store the "old" data in a dataframe instead of vectors and supply the
> > name of that dataframe to your model. eg
> > Model <- glm(species ~ temp + prec + elev, data = your.data.frame,
> > family = binomial(link = logit))
> >
> > Notice that I've added some spaces which makes your code more easy to
> > read.
> >
> > I think this will solve your problem.
> >
> > HTH,
> >
> > Thierry
> >
> >
> > -------------------------------------------------------------------
> > -----
> > ----
> > ir. Thierry Onkelinx
> > Instituut voor natuur- en bosonderzoek / Research Institute for
> Nature
> > and Forest
> > Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
> > methodology and quality assurance
> > Gaverstraat 4
> > 9500 Geraardsbergen
> > Belgium
> > tel. + 32 54/436 185
> > Thierry.Onkelinx at inbo.be
> > www.inbo.be
> >
> > To call in the statistician after the experiment is done may be no
> > morethan asking him to perform a post-mortem examination: he may
> > be able to
> > say what the experiment died of.
> > ~ Sir Ronald Aylmer Fisher
> >
> > The plural of anecdote is not data.
> > ~ Roger Brinner
> >
> > The combination of some data and an aching desire for an answer
> > does not
> > ensure that a reasonable answer can be extracted from a given body of
> > data.
> > ~ John Tukey
> >
> > -----Oorspronkelijk bericht-----
> > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org]Namens K. Fleischer
> > Verzonden: woensdag 15 oktober 2008 9:34
> > Aan: r-help at r-project.org
> > Onderwerp: [R] apply model predictions over larger area with
> predict()
> >
> > 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.
> >
> > Katrin Fleischer
> > MSc Student
> > Computational Geo-ecology
> > University of Amsterdam
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> > Dit bericht en eventuele bijlagen geven enkel de visie van de
> > schrijver weer
> > en binden het INBO onder geen enkel beding, zolang dit bericht
> > niet bevestigd is
> > door een geldig ondertekend document. The views expressed in this
> > message
> > and any annex are purely those of the writer and may not be
> > regarded as stating
> > an official position of INBO, as long as the message is not
> > confirmed by a duly
> > signed document.
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list