Navigation Menu

Skip to content

Commit

Permalink
new function obs_exp is a diagnostic function to compare observed and…
Browse files Browse the repository at this point in the history
… expected counts at different covariate aggregations
  • Loading branch information
David Lawrence Miller committed Jun 12, 2018
1 parent 5302c35 commit f99224e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
1 change: 0 additions & 1 deletion .Rbuildignore
@@ -1,6 +1,5 @@
.travis.yml
LICENSE
R/obs_exp.R
^\.travis\.yml$
^appveyor\.yml$
^tic\.R$
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -21,6 +21,7 @@ export(generate.ds.uncertainty)
export(generate.mb.sample)
export(latlong2km)
export(make.soapgrid)
export(obs_exp)
export(plot_pred_by_term)
export(rqgam.check)
export(trim.var)
Expand All @@ -47,6 +48,7 @@ importFrom(graphics,segments)
importFrom(mgcv,uniquecombs)
importFrom(mrds,DeltaMethod)
importFrom(numDeriv,grad)
importFrom(plyr,ddply)
importFrom(statmod,qres.binom)
importFrom(statmod,qres.nbinom)
importFrom(statmod,qres.tweedie)
Expand Down
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -5,6 +5,7 @@ dsm news!
* gamma option removed, since it now is not ignored by gam(). If using method="GCV.Cp" then gamma=1.4 should still be used (but generally we advise using REML rather than GCV for smoothing parameter selection)
* dsm now requires mgcv version 1.8-23 or higher.
* dsm.var.prop (and dsm_varprop) now throw an error when there are no covariates in the detection function as it can't do anything useful
* new function obs_exp is a diagnostic function to compare observed and expected counts at different covariate aggregations


# 2.2.15
Expand Down
48 changes: 48 additions & 0 deletions R/obs_exp.R
@@ -0,0 +1,48 @@
#' Observed versus expected diagnostics for fitted DSMs
#'
#' Given a covariate, calculate the observed and expected counts for each unique value of the covariate. This can be a useful goodness of fit check for DSMs.
#'
#' One strategy for model checking is to calculate observed and expected counts at different aggregations of the variable. If these match well then the model fit is good.
#'
#' @param model a fitted \code{dsm} model object
#' @param covar covariate to aggregate by (character)
#' @param cut vector of cut points to aggregate at. If not supplied, the unique values of \code{covar} are used.
#' @importFrom plyr ddply
#' @export
#' @author David L Miller, on the suggestion of Mark Bravington.
#' @export
#' @return \code{data.frame} with values of observed and expected counts.
#' @examples
#' library(Distance)
#' library(dsm)
#'
#' # example with the Gulf of Mexico dolphin data
#' data(mexdolphins)
#' hr.model <- ds(distdata, max(distdata$distance),
#' key = "hr", adjustment = NULL)
#' mod1 <- dsm(count~s(x,y), hr.model, segdata, obsdata)
obs_exp <- function(model, covar, cut=NULL){

# get data
oe <- model$data
# add in predictions
oe$N <- predict(model)

# do the aggregation if necessary
if(!is.null(cut)){
oe[[covar]] <- cut(oe[[covar]], breaks=cut)
}

# for each unique value of term, sum observed and expected
oe <- plyr::ddply(oe, covar, function(x){
data.frame(Observed = sum(x$count),
Expected = sum(x$N))
})

# format the table
cn <- oe[,1]
oe <- t(oe[,2:3])
colnames(oe) <- cn

return(oe)
}
37 changes: 37 additions & 0 deletions man/obs_exp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f99224e

Please sign in to comment.