I think for your purpose the predict method is more appropriate than predictDsurface - that is for evaluating the model at every pixel across a habitat mask, whereas you are really interested only in the three levels of the habitat factor. I suspect predictDsurface (which generates the error message) is tripping up on the groups, so it's a prompt for me to actually test that part of the code, but let's bypass predictDsurface for now.
Your challenge is to construct a dataframe 'newdata' that has the requisite group, habitat and session predictors (if the default is not adequate), and then to sum the resulting predicted habitat-specific densities across groups.
I tried this with the ovenCH dataset with these results
- Code: Select all
ovenmask <- make.mask(traps(ovenCH), buffer=400, spacing=15)
testfit <- secr.fit(ovenCH, mask = ovenmask, D ~ session + g, groups='Sex')
## squash the output of predict into a single dataframe
pred <- do.call(rbind, predict(testfit))
## extract the density estimates (every third row)
## two groups, so two columns
sumD <- matrix(pred$estimate[seq(1,30,3)], ncol = 2)
dimnames(sumD) <- list(2005:2009, c('F','M'))
sumD
apply(sumD,1,sum)
F M
2005 0.4019269 0.5180388
2006 0.4207779 0.5423356
2007 0.4972829 0.6409419
2008 0.3633985 0.4683800
2009 0.3060198 0.3944253
apply(sumD,1,sum)
2005 2006 2007 2008 2009
0.9199657 0.9631135 1.1382248 0.8317785 0.7004452
sumDse <- matrix(pred$SE.estimate[seq(1,30,3)], ncol = 2)
dimnames(sumDse) <- list(2005:2009, c('F','M'))
apply(sumDse,1, function(x) sqrt(sum(x^2)))
2005 2006 2007 2008 2009
0.1790869 0.1874864 0.2215747 0.1619198 0.1363535
The SE might better be combined on the link (default: log) scale, but that's a hassle. An alternative might be to change the link function for D to 'identity' in your call to secr.fit. You might want to populate a 3-D array with the predicted densities, rather than a matrix, as you have habitat, session and group as margins. Also, it is probably possible to construct the model so that it estimates the overall density directly using appropriate contrasts, but I haven't tried.
Hope this is somewhat useful, if not a complete solution.
Murray