Error when using predict() to get effective areas

47 views
Skip to first unread message

Iris Berger

unread,
Mar 1, 2024, 9:52:21 AMMar 1
to distance-sampling
Dear all,
I have fit numerous distance functions to point count observations of around 200 bird species. I have extracted the best-fitting model for each species at each site class (forest or farmland) (since I tried lots of different detection functions, some species-specific ones, some with observations across species combined & species identity as a covariate, some with the proportion of closed habitat around each point as a covariate etc.).
Now, I am trying to use predict() to get the effective areas surveyed for the points where I did not record an individual. However, I keep getting this error:

Error in predict.ds(af.farm.mod, newdata = data.frame(name_latin = filled_data4$name_latin), :
fields or factor levels in `newdata` do not match data used in fitted model


This happens even when I extract the data directly from the model (so it is literally the data that was used to fit the model), eg:

af.farm.data <- (functm_chosen6[["Acridotheres_fuscus.farm"]][["model"]][["data"]])

##get ds object
af.farm.mod <- functm_chosen6[["Acridotheres_fuscus.farm"]][["model"]]

predict(af.farm.mod, newdata = NULL, esw = TRUE) ###works

predict(af.farm.mod, newdata = af.farm.data, esw = TRUE) ##does not work

functm_chosen6 is a large list containing the best fitting detection models for each species-site class combination.

I have also attached my R code & happy to provide more information or data.

Does anyone have any idea what has gone wrong?

Thank you so much for your help,
Iris

predict_effArea_test_new.R

Eric Rexstad

unread,
Mar 1, 2024, 10:56:04 AMMar 1
to Iris Berger, distance-sampling
Hello Iris.

You've described distance sampling analysis on an industrial scale, performing estimation for ~200 species.

I don't have an answer to your problem, which you see from your error message is coming from the function predict.ds​ in the mrds​ package.

I think you are describing two different instances of problems.
  • The error message you provide seems to have been generated from executing lines 249-251 of the script you provided. In those lines of code, the newdata​ argument you are passing consists of only a single column name_latin​, rather than a complete data frame with detection distances. Hence the error message should be taken literally—you haven't provided predict.ds​ with data necessary to fit a detection function from which it would be able to integrate the area under the fitted detection function to estimate ESW.
  • Your other instance (lines 200-216 of your script) appears to be a different issue. Line 214 seems to do what you want. This is because when the newdata​ argument is set to NULL, predict.ds​ simply plucks the fitted​ values from the dsmodel​ object; no refitting is performed.
    • When newdata​ is not NULL, refitting takes place before estimating ESW. The same estimate of ESW should result from lines 214 and 216 if af.farm.data​ contains the same data. I don't know what you mean when you say line 216 "doesn't work". Is an error produced, or a value you know to be incorrect?
Finally, I wonder if your basic premise is the heart of the problem. You say (line 201) you are trying to produce ESW value for stations where there are no detections. If there are no detections, then there aren't any detection distances with which to determine the range of integration. Lots of code within predict.ds​ leads up to performing the integration of the detection function at line 253. I would presume ESW is undefined in those instances.

That's my best diagnosis.


From: distance...@googlegroups.com <distance...@googlegroups.com> on behalf of Iris Berger <irisber...@gmail.com>
Sent: 01 March 2024 14:52
To: distance-sampling <distance...@googlegroups.com>
Subject: [distance-sampling] Error when using predict() to get effective areas
 
--
You received this message because you are subscribed to the Google Groups "distance-sampling" group.
To unsubscribe from this group and stop receiving emails from it, send an email to distance-sampl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/distance-sampling/f14a8af7-d7b9-4507-9688-9977258a95ccn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages