R/defaultextractfn.R

Defines functions defaultextractfn

Documented in defaultextractfn

defaultextractfn <- function(x, ...) {
    
    if (inherits(x, 'try-error')) {
        ## null output: dataframe of 0 rows and 0 columns
        data.frame()
    }
    else if (inherits(x, 'capthist')) {
        ## summarised raw data
        if (ms(x))
            unlist(lapply(x, counts))
        else {
            gp <- covariates(x)$group
            if (is.null(gp))
                counts(x)
            else
                unlist(lapply(split(x,gp,dropnullocc=TRUE), counts))
        }
    }
    else if (
        (inherits(x, 'secr') && !is.null(x$fit)) ||
        (inherits(x, 'ipsecr') && x$code == 1)
    ) {
        
        keepdensity <- FALSE
        args <- list(...)
        if ('keepdensity' %in% names(args)) keepdensity <- args$keepdensity
        ## fitted model:
        ## default predictions of 'real' parameters
        out <- predict(x)
        if (!is.data.frame(out)) {
            warning ("summarising only first session, group or mixture class")
            out <- out[[1]]
        }
        attr(out, 'counts')   <- counts(x$capthist)
        attr(out, 'start')    <- x$start
        attr(out, 'method')   <- x$method
        attr(out, 'proctime') <- x$proctime
        attr(out, 'chat')     <- x$details$chat
        if (keepdensity) {
            attr(out, 'D')    <- attr(x$capthist, 'D')    # usually NULL
        }
        
        # ipsecr.fit completion 'code' 1 successful, 2 target not within final box, 3 exceeded maximum simulations
        # nlm() completion 'code' 
        # optim() completion code 'convergence'
        if (!is.null(x$fit) && !is.null(x$fit$code))
            attr(out, 'code')   <- x$fit$code
        if (!is.null(x$fit) && !is.null(x$fit$convergence))
            attr(out, 'convergence') <- x$fit$convergence
        
        out
    }
    else if (inherits(x, 'secrlist') ||
        (is.list(x) && length(x) > 0 && !is.data.frame(x) &&
         all(sapply(x, inherits, c('secr', 'ipsecr', 'try-error'))))) {
        ## fit = "multifit": several models fitted to the same data;
        ## named list of the usual estimate tables, one per model
        lapply(x, defaultextractfn, ...)
    }
    else {
        ## null output: dataframe of 0 rows and 0 columns
        data.frame()
    }
}

Try the secrdesign package in your browser

Any scripts or data that you put into this service are public.

secrdesign documentation built on Dec. 1, 2026, 9:06 a.m.