DistanceDevelopment / mrds

R package for mark-recapture-distance-sampling analysis
GNU General Public License v3.0
4 stars 4 forks source link

Strange result from optimisation? #98

Open LHMarshall opened 10 months ago

LHMarshall commented 10 months ago

While generating some test data I have seen some strange model fitting results from mrds. Something to come back to when we look to improve the optimiser perhaps? Recording here so we have code to reproduce. It's strange because the data are generated from a half normal and it is not a particularly complex model. There is no association between distance and size.

image

set.seed(112)
# generate some data to test on
dat <- data.frame(Sample.Label = 1:10)
dat$Region.Label <- c(rep(1, 5), rep(2, 5))
set.seed(123)
dat2 <- dat[sample(1:nrow(dat), nrow(dat), replace=TRUE), ]
dat2$object <- 1:nrow(dat2)
dat <- dat[!(dat$Sample.Label %in% dat2$Sample.Label),]
dat$object <- NA
dat <- rbind(dat, dat2)
dat$save.ind <- 1:nrow(dat)

#set.seed(127) or set.seed(112) here to see stange results
dat2 <- dat
dat2$Region.Label <- paste("YEAR", as.character(dat2$Region.Label), sep = "")
dat2 <- rbind(dat2, dat2, dat2)
dat2$distance <- abs(rnorm(nrow(dat2), 0, 25))
dat2$Effort <- 1000
dat2$Area <- 100
dat2$size <- rpois(nrow(dat2), 20)
dat2$object <- 1:nrow(dat2)

conversion.factor <- convert_units("meter", "meter", "square kilometer")

fit.ds <- ds(data=dat2,
             truncation=50,
             key="hn",
             adjustment=NULL,
             convert_units=conversion.factor,
             formula=~size)

plot(fit.ds)
fit.ds$ddf$par
erex commented 6 months ago

Ran this verbatim code using

> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Distance_1.0.9 mrds_2.3.0   

and produced this result

> fit.ds$ddf$par
X.Intercept.         size 
  3.67491262  -0.02612638

image