Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_bins when there is binned data #116

Closed
erex opened this issue Feb 10, 2022 · 0 comments
Closed

create_bins when there is binned data #116

erex opened this issue Feb 10, 2022 · 0 comments

Comments

@erex
Copy link
Member

erex commented Feb 10, 2022

Email from Charlotte Spira today, with following error message:

hn0_bushpig <- ds(bushpig, transect = "point", key = "hn", adjustment = NULL, cutpoints = c(seq(0,8,1)), formula = ~as.factor(stratum))
Error in if (!all(in.cp.ind)) { :
  missing value where TRUE / FALSE is needed.

Error comes from create_bins(). Her data were binned (0.5, 1.5, 2.5, 3.5,...) with lots of NA distances from camera traps without detections, causing NAs to appear in in.cp.ind, generating the error

Temporary work-around the problem by reverting to distbegin and distend

library(Distance)
bushpig <- read.csv("E:/troubleshoot/charlotte/bushpig_distances.csv", sep=";", dec=",")
#hn0_bushpig <- ds(bushpig, transect = "point", key = "hn", adjustment = NULL,
#                  cutpoints = seq(0,8,1))#, formula = ~as.factor(stratum))
hist(bushpig$distance)

mypig <- bushpig
names(mypig)[2] <- "Region.Label"
mypig$Area <- mypig$Area / 1e6
mypig$distbegin <- c(rep(0,2), rep(1,36), rep(2, 29), rep(3, 17), rep(4,62), rep(5,27),rep(6,11), rep(7,7), rep(NA,102))
mypig$distend <- c(rep(1,2), rep(2,36), rep(3, 29), rep(4, 17), rep(5,62), rep(6,27),rep(7,11), rep(8,7), rep(NA,102))

units <- convert_units("meter", NULL, "square kilometer")

hn0_bushpig <- ds(mypig, transect = "point", key = "hn", adjustment = NULL, convert.units = units,
                  formula = ~as.factor(Region.Label))
summary(hn0_bushpig)
plot(hn0_bushpig, pdf=TRUE)
withmult <- dht2(hn0_bushpig, flatfile=mypig, strat_formula = ~Region.Label, convert_units = units,
                 sample_fraction = mypig$multiplier[1])
print(withmult, report="both")

You will see I read in your original data, then renamed the resulting data frame and made some modifications. I changed the field "stratum" to "Region.Label" because ds() is fussy about that field name. I converted the area you had provide from (what I think) was square meters, to square kilometers for more sensible output. Did I get that right?

The cause of your original error message was that your data were already binned. It seems reasonable that the "cutpoints" argument to ds() should have done what you wanted, but it did not. Another way to accomplish the same job is to add fields "distbegin" and "distend" to the data frame. By adding those two columns, I removed the cutpoints argument and the error went away. It is something we need to investigate further. But this fix with serve your purposes for the moment.

@dill dill closed this as completed in c7ed64f Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant