# Goodness-of-Fit Test

### Description

Simple Monte-Carlo goodness-of-fit tests for full-likelihood SECR models. The approach is to calculate a statistic from either the raw data or a fitted model, and to relate this to the distribution of the statistic under the original fitted model. The distribution is estimated by simulating data from the model, and possibly re-fitting the model to each simulated dataset.

The suitability of different test statistics has yet to be assessed.

### Usage

1 2 |

### Arguments

`object` |
a fitted secr model |

`nsim` |
integer number of replicates |

`statfn` |
function to compute a numeric vector of one or more statistics from a single-session ‘capthist’ object or from a fitted model (see Details) |

`fit` |
logical; if TRUE the model is re-fitted to each simulated dataset |

`seed` |
value for setting .Random.seed - either NULL or an integer |

`ncores` |
integer number of cores available for parallel processing |

`tracelevel` |
see |

### Details

The test statistic(s) may be computed either on a dataset or on a
fitted model, as determined by the argument `fit`

. The single
argument expected by `statfn`

should be either a ‘capthist’ object
(`fit = FALSE`

) or an ‘secr’ object (`fit = TRUE`

).

The default statistic when `fit = FALSE`

is the proportion of
individuals observed on only one occasion, which is equivalent to
```
statfn = function(CH) c(f1 = sum(apply(abs(CH) > 0,1,sum) == 1) /
nrow(CH))
```

. Repeat detections on one occasion at the same or different
detectors are not counted. The default statistic is therefore not
appropriate for some data, specifically from ‘count’ or ‘polygon’
detectors with few occasions or only one.

The default statistic when `fit = TRUE`

is the deviance divided by
the residual degrees of freedom (i.e., ```
statfn = function(object)
c(devdf = deviance(object) / df.residual(object))
```

).

The reported probability (p) is the rank of the observed value in the
vector combining the observed value and simulated values, divided by
(nsim + 1). Ranks are computed with `rank`

using the default
`ties.method = "average"`

.

Simulations take account of the usage attribute of detectors in the original capthist object, given that usage was defined and ignoreusage was not set.

`statfn`

may return a vector of statistics for each observed or
simulated dataset or model: then the value of each statistic will be
calculated for every simulated dataset, and summarised. If ```
fit =
TRUE
```

the vector of statistics may include both those computed on the
raw data (object$capthist) and the fitted model.

### Value

An object of class `c('secrtest','list')`

with components:

`object` |
as input |

`nsim` |
as input |

`statfn` |
as input |

`fit` |
as input |

`seed` |
as input |

`output` |
list comprising the simulated values, the observed value(s), and estimated probabilities |

For multi-session input when `fit = FALSE`

, ‘output’ is a list
in which each session provides one component.

Print and plot methods are provided for ‘secrtest’ objects.

### Note

`simulate.secr`

is always used to simulate the raw data, but
`simulate.secr`

does not work for all types of fitted
model. Models fitted by maximizing the likelihood conditional on
*n* (`CL = TRUE`

in `secr.fit`

) potentially include
individual covariates whose distribution in the population is
unknown. This precludes simulation, and conditional-likelihood models
in general are therefore not covered by `secr.test`

.

Other exclusions include exotic non-binary behavioural responses (“bn", “bkn", “bkc", “Bkc" - but these are generally undocumented in any case).

If `fit = TRUE`

then `sim.secr`

is used.

At each simulation a new population is generated across the extent of the original mask. If the extent is unduly large then time will be wasted simulating the possibility of detection for many essentially undetectable animals. This is an argument for keeping the mask tight - large enough only to avoid mask-induced bias.

### See Also

`print.secrtest`

,
`plot.secrtest`

,
`simulate.secr`

,
`sim.secr`

,
`deviance.secr`

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ```
## Not run:
secr.test(secrdemo.0, nsim = 99)
secr.test(ovenbird.model.1, nsim = 20)
## example combining raw data summary and model fit
## assumes single-session
bothfn <- function(object) {
CH <- object$capthist
f1 <- sum(apply(abs(CH) > 0, 1, sum) == 1) / nrow(CH)
devdf <- deviance(object) / df.residual(object)
c(f1 = f1, devdf = devdf)
}
test <- secr.test (secrdemo.0, nsim = 19, statfn = bothfn, fit = TRUE)
test
plot(test, main = '')
## End(Not run)
``` |