diff options
author | Ken Kellner <ken@kenkellner.com> | 2024-01-20 09:31:36 -0500 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2024-01-20 09:31:36 -0500 |
commit | c80c1a2ccfce129f6070713c55ca5a0eb5211255 (patch) | |
tree | 48bd295bb260392e920a8c8f03a83d4692db15d6 | |
parent | 62ec0f1e8d7338f9f3919d8d9d5ddb82c730babf (diff) |
Update umf brackets
-rw-r--r-- | DESCRIPTION | 1 | ||||
-rw-r--r-- | R/brackets.R | 446 | ||||
-rw-r--r-- | R/gdistremoval.R | 12 | ||||
-rw-r--r-- | R/occuCOP.R | 94 | ||||
-rw-r--r-- | R/unmarkedFrame.R | 411 | ||||
-rw-r--r-- | man/extract-methods.Rd | 93 | ||||
-rw-r--r-- | man/unmarkedEstimateList-class.Rd | 1 | ||||
-rw-r--r-- | man/unmarkedFit-class.Rd | 1 | ||||
-rw-r--r-- | man/unmarkedFrame-bracket-methods.Rd | 47 | ||||
-rw-r--r-- | man/unmarkedFrame-class.Rd | 12 | ||||
-rw-r--r-- | tests/testthat/test_occuCOP.R | 4 |
11 files changed, 516 insertions, 606 deletions
diff --git a/DESCRIPTION b/DESCRIPTION index 352015a..f4073e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,6 +45,7 @@ License: GPL (>=3) LazyLoad: yes LazyData: yes Collate: 'classes.R' 'unmarkedEstimate.R' 'mapInfo.R' 'unmarkedFrame.R' + 'brackets.R' 'unmarkedFit.R' 'utils.R' 'getDesign.R' 'colext.R' 'distsamp.R' 'multinomPois.R' 'occu.R' 'occuRN.R' 'occuMulti.R' 'pcount.R' 'gmultmix.R' 'pcountOpen.R' 'gdistsamp.R' 'unmarkedFitList.R' 'unmarkedLinComb.R' diff --git a/R/brackets.R b/R/brackets.R new file mode 100644 index 0000000..03d8f16 --- /dev/null +++ b/R/brackets.R @@ -0,0 +1,446 @@ +# i is the vector of sites to extract + +# Select a subset of sites i--------------------------------------------------- +# i is a numeric vector of sites + +setMethod("[", c("unmarkedFrame", "numeric", "missing", "missing"), + function(x, i, j){ + select_sites_internal(x, i) +}) + +setGeneric("select_sites_internal", function(x, i){ + standardGeneric("select_sites_internal") +}) + +setMethod("select_sites_internal", "unmarkedFrame", + function(x, i){ + + M <- numSites(x) + if(length(i) == 0) return(x) + if(any(i < 0) && any(i > 0)) + stop("i must be all positive or all negative indices.") + if(all(i < 0)) { # if i is negative, then convert to positive + i <- (1:M)[i] + } + y <- getY(x)[i,] + if (length(i) == 1) { + y <- t(y) + } + siteCovs <- siteCovs(x) + obsCovs <- obsCovs(x) + if (!is.null(siteCovs)) { + siteCovs <- siteCovs(x)[i, , drop = FALSE] + } + if (!is.null(obsCovs)) { + R <- obsNum(x) + .site <- rep(1:M, each = R) + oc <- lapply(i, function(ind){ + obsCovs[.site==ind,,drop=FALSE] + }) + obsCovs <- do.call(rbind, oc) + } + umf <- x + umf@y <- y + umf@siteCovs <- siteCovs + umf@obsCovs <- obsCovs + umf +}) + + +setMethod("select_sites_internal", "unmarkedFrameOccuMulti", + function(x, i) +{ + if(length(i) == 0) return(x) + M <- numSites(x) + + ylist <- lapply(x@ylist,function(x) x[i,,drop=F]) + siteCovs <- siteCovs(x) + obsCovs <- obsCovs(x) + if (!is.null(siteCovs)) { + siteCovs <- siteCovs(x)[i, , drop = FALSE] + } + if (!is.null(obsCovs)) { + R <- obsNum(x) + .site <- rep(1:M, each = R) + oc <- lapply(i, function(ind){ + obsCovs[.site==ind,,drop=FALSE] + }) + obsCovs <- do.call(rbind, oc) + } + umf <- x + umf@y <- ylist[[1]] + umf@ylist <- ylist + umf@siteCovs <- siteCovs + umf@obsCovs <- obsCovs + umf +}) + + +setMethod("select_sites_internal", "unmarkedMultFrame", + function(x, i) +{ + M <- numSites(x) + if(length(i) == 0) return(x) + if(any(i < 0) && any(i > 0)) + stop("i must be all positive or all negative indices.") + if(all(i < 0)) { # if i is negative, then convert to positive + i <- (1:M)[i] + } + oldy <- getY(x) + y <- oldy[i,] + siteCovs <- siteCovs(x) + obsCovs <- obsCovs(x) + if (!is.null(siteCovs)) { + siteCovs <- siteCovs(x)[i, , drop = FALSE] + } + if (!is.null(obsCovs)) { + R <- obsNum(x) + .site <- rep(1:M, each = obsNum(x)) #NULL ## testing + oc <- lapply(i, function(ind){ + obsCovs[.site==ind,,drop=FALSE] + }) + obsCovs <- do.call(rbind, oc) + } + u <- unmarkedMultFrame(y=matrix(y, ncol=ncol(oldy)), + siteCovs=siteCovs, + obsCovs=obsCovs, + numPrimary=x@numPrimary) + ysc <- x@yearlySiteCovs + if(!is.null(ysc)) { + T <- x@numPrimary + sites <- rep(1:M, each=T) + keep <- as.vector(sapply(i, function(x) which(sites %in% x))) + ysc <- ysc[keep,, drop=FALSE] + u@yearlySiteCovs <- ysc + } + u + +}) + + +setMethod("select_sites_internal", "unmarkedFrameOccuMS", + function(x, i) +{ + multf <- callNextMethod(x, i) + unmarkedFrameOccuMS(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + obsCovs=obsCovs(multf), + numPrimary=x@numPrimary) +}) + + +setMethod("select_sites_internal", "unmarkedFrameGMM", + function(x, i) +{ + M <- nrow(x@y) + y <- x@y[i,,drop=FALSE] + R <- obsNum(x) + T <- x@numPrimary + + sc <- siteCovs(x)[i,,drop=FALSE] + + ysc_ind <- rep(1:M, each=T) + ysc <- do.call("rbind", lapply(i, function(ind){ + yearlySiteCovs(x)[ysc_ind == ind,,drop=FALSE] + })) + + oc_ind <- rep(1:M, each=R) + oc <- do.call("rbind", lapply(i, function(ind){ + obsCovs(x)[oc_ind == ind,,drop=FALSE] + })) + + unmarkedFrameGMM(y=y, siteCovs=sc, + yearlySiteCovs=ysc, + obsCovs=oc, + piFun=x@piFun, type=x@samplingMethod, + obsToY=x@obsToY, numPrimary=x@numPrimary) +}) + + +setMethod("select_sites_internal", "unmarkedFrameGPC", + function(x, i) +{ + multf <- callNextMethod(x, i) # unmarkedMultFrame + class(multf) <- "unmarkedFrameGPC" + multf +}) + + +setMethod("select_sites_internal", "unmarkedFrameGPC", + function(x, i) +{ + multf <- as(x, "unmarkedMultFrame") + out <- callNextMethod(multf, i) # unmarkedMultFrame + as(out, "unmarkedFrameGPC") +}) + + +setMethod("select_sites_internal", "unmarkedFrameGDS", + function(x, i) +{ + multf <- callNextMethod(x, i) # unmarkedMultFrame + sur <- x@survey + if(sur=="line") + unmarkedFrameGDS(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + numPrimary=x@numPrimary, + dist.breaks=x@dist.breaks, + tlength=x@tlength[i], + survey=sur, + unitsIn=x@unitsIn) + else if(sur=="point") + unmarkedFrameGDS(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + numPrimary=x@numPrimary, + dist.breaks=x@dist.breaks, + survey=sur, + unitsIn=x@unitsIn) +}) + + +setMethod("select_sites_internal", "unmarkedFramePCO", + function(x, i) +{ + multf <- callNextMethod(x, i) # unmarkedMultFrame + unmarkedFramePCO(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + obsCovs=obsCovs(multf), + numPrimary=x@numPrimary, + primaryPeriod=x@primaryPeriod[i,,drop=FALSE]) +}) + + +setMethod("select_sites_internal", "unmarkedFrameOccuTTD", + function(x, i) +{ + multf <- callNextMethod(x, i) # unmarkedMultFrame + unmarkedFrameOccuTTD(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + obsCovs=obsCovs(multf), + numPrimary=x@numPrimary, + surveyLength=x@surveyLength[i,,drop=FALSE]) +}) + + +setMethod("select_sites_internal", "unmarkedFrameDSO", + function(x, i) +{ + multf <- callNextMethod(x, i) # unmarkedMultFrame + sur <- x@survey + pp <- x@primaryPeriod[i,,drop=FALSE] + if(sur=="line") + unmarkedFrameDSO(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + numPrimary=x@numPrimary, + dist.breaks=x@dist.breaks, + tlength=x@tlength[i], + survey=sur, + unitsIn=x@unitsIn, + primaryPeriod=pp) + else if(sur=="point") + unmarkedFrameDSO(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + numPrimary=x@numPrimary, + dist.breaks=x@dist.breaks, + survey=sur, + unitsIn=x@unitsIn, + primaryPeriod=pp) +}) + + + + + +# Select a subset of observations j ------------------------------------------- +# j is a numeric vector + +### RBC: Why??? this doesn't allow umf[,c(1,1)] +setMethod("[", c("unmarkedFrame", "missing", "numeric", "missing"), + function(x, i, j){ + select_obs_internal(x, j) +}) + +setGeneric("select_obs_internal", function(x, j){ + standardGeneric("select_obs_internal") +}) + + +setMethod("select_obs_internal", "unmarkedFrame", + function(x, j) +{ + y <- getY(x) + obsCovs <- obsCovs(x) + obsToY <- obsToY(x) + obs.remove <- rep(TRUE, obsNum(x)) + obs.remove[j] <- FALSE + y.remove <- t(obs.remove) %*% obsToY > 0 + y <- y[,!y.remove, drop=FALSE] + obsCovs <- obsCovs[!rep(obs.remove, numSites(x)),, drop=FALSE] + x@obsCovs <- obsCovs + x@y <- y + x@obsToY <- obsToY[!obs.remove,!y.remove, drop=FALSE] + x +}) + + +setMethod("select_obs_internal", "unmarkedFrameOccuMulti", + function(x, j) +{ + y <- getY(x) + obsCovs <- obsCovs(x) + obsToY <- obsToY(x) + obs.remove <- rep(TRUE, obsNum(x)) + obs.remove[j] <- FALSE + y.remove <- t(obs.remove) %*% obsToY > 0 + ylist <- lapply(x@ylist, function(z) z[,!y.remove, drop=F]) + obsCovs <- obsCovs[!rep(obs.remove, numSites(x)),, drop=FALSE] + + x@obsCovs <- obsCovs + x@y <- ylist[[1]] + x@ylist <- ylist + x@obsToY <- obsToY[!obs.remove,!y.remove, drop=FALSE] + x +}) + + +## for multframes, must remove years at a time +setMethod("select_obs_internal", "unmarkedMultFrame", + function(x, j) +{ + J <- obsNum(x)/x@numPrimary + obs <- rep(1:x@numPrimary, each = J) + years <- 1:x@numPrimary + numPrimary <- length(j) + obsj <- match(obs, j) + j2 <- which(!is.na(obsj)) + u <- callNextMethod(x, j2) + ysc <- yearlySiteCovs(x) + if(!is.null(ysc)) { + ysc <- ysc[rep(!is.na(match(years, j)), nrow(getY(x))),, drop=FALSE] + u@yearlySiteCovs <- ysc + } + u@numPrimary <- numPrimary + return(u) +}) + + +setMethod("select_obs_internal", "unmarkedFramePCO", + function(x, j) +{ + multf <- callNextMethod(x, j) # unmarkedMultFrame + unmarkedFramePCO(y=getY(multf), siteCovs=siteCovs(multf), + yearlySiteCovs=yearlySiteCovs(multf), + obsCovs=obsCovs(multf), + numPrimary=length(j), + primaryPeriod=x@primaryPeriod[,j,drop=FALSE]) +}) + + + + +setMethod("select_obs_internal", "unmarkedFrameOccuTTD", + function(x, j) +{ + + if(any(j>x@numPrimary)) stop("Can't select primary periods that don't exist", call.=FALSE) + if(!all(j>0)) stop("All indices must be positive", call.=FALSE) + + R <- ncol(getY(x))/x@numPrimary + pp_vec <- rep(1:x@numPrimary, each=R) + keep_cols <- which(pp_vec%in%j) + y <- getY(x)[,keep_cols,drop=FALSE] + sl <- x@surveyLength[,keep_cols,drop=FALSE] + + pp_vec2 <- rep(1:x@numPrimary, numSites(x)) + keep_rows <- which(pp_vec2 %in% j) + ysc <- yearlySiteCovs(x)[keep_rows,,drop=FALSE] + + obs_vec <- rep(rep(1:x@numPrimary, each = R), numSites(x)) + keep_rows <- which(obs_vec %in% j) + oc <- obsCovs(x)[keep_rows,,drop=FALSE] + + unmarkedFrameOccuTTD(y=y, surveyLength=sl, siteCovs=siteCovs(x), + yearlySiteCovs=ysc, obsCovs=oc, + numPrimary=length(j)) +}) + + +# Select a subset of both sites i and observations j -------------------------- +# i and j are numeric vectors +# many unmarkedFrames do not support this, I think + + +# i is as before and j is the obsNum to remove and corresponding y's +setMethod("[", c("unmarkedFrame","numeric", "numeric", "missing"), + function(x, i, j) +{ + ## first remove sites + umf <- x[i,] + umf <- umf[,j] + umf +}) + + +# Select a subset of sites as a list------------------------------------------- + +### list is a ragged array of indices (y's) to include for each site. +### Typically useful for multilevel boostrapping. +setMethod("[", c("unmarkedFrame","list", "missing", "missing"), + function(x, i, j) +{ + m <- numSites(x) + J <- R <- obsNum(x) + o2y <- obsToY(x) + if (!identical(o2y, diag(R))) + stop("Ragged subsetting of unmarkedFrames is only valid for diagonal obsToY.") + J <- ncol(o2y) + if (m != length(i)) stop("list length must be same as number of sites.") + siteCovs <- siteCovs(x) + y <- cbind(.site=1:m, getY(x)) + obsCovs <- obsCovs(x) + site_idx <- rep(1:m, each=R) + stopifnot(length(site_idx) == nrow(obsCovs)) + + oc <- lapply(1:m, function(ind){ + df <- obsCovs[site_idx==ind,,drop=FALSE] + obs <- i[[ind]] + if (length(obs) > R) + stop("All elements of list must be less than or equal to R.") + obs <- c(obs, rep(NA, R-length(obs))) + df[obs,,drop=FALSE] + }) + obsCovs <- do.call(rbind, oc) + rownames(obsCovs) <- NULL + + y <- apply(y, 1, function(row) { + site <- row[1] + row <- row[-1] + obs <- i[[site]] + obs <- c(obs, rep(NA, R-length(obs))) + row[obs] + }) + + if(!is.null(obsCovs(x))){ + obsCovs(x) <- obsCovs + } + x@y <- t(y) + x +}) + + +# Select subset of sites using logical vector---------------------------------- + +setMethod("[", c("unmarkedFrame", "logical", "missing", "missing"), + function(x, i, j) { + i <- which(i) + x[i, ] +}) + +# Get first few rows of an unmarkedFrame--------------------------------------- + +setMethod("head", "unmarkedFrame", function(x, n) { + if(missing(n)) n <- 10 + umf <- x[1:n,] + umf +}) + diff --git a/R/gdistremoval.R b/R/gdistremoval.R index d4b1b39..f230433 100644 --- a/R/gdistremoval.R +++ b/R/gdistremoval.R @@ -64,7 +64,7 @@ setAs("unmarkedFrameGDR", "data.frame", function(from){ data.frame(yDistance, yRemoval, out) }) -setMethod("[", c("unmarkedFrameGDR", "numeric", "missing", "missing"), +setMethod("select_sites_internal", "unmarkedFrameGDR", function(x, i) { M <- numSites(x) T <- x@numPrimary @@ -119,14 +119,8 @@ setMethod("[", c("unmarkedFrameGDR", "numeric", "missing", "missing"), umf }) -setMethod("[", c("unmarkedFrameGDR", "logical", "missing", "missing"), - function(x, i) { - i <- which(i) - x[i, ] -}) - -setMethod("[", c("unmarkedFrameGDR", "missing", "numeric", "missing"), - function(x, i, j){ +setMethod("select_obs_internal", "unmarkedFrameGDR", + function(x, j){ M <- numSites(x) T <- x@numPrimary diff --git a/R/occuCOP.R b/R/occuCOP.R index d42ab07..432771a 100644 --- a/R/occuCOP.R +++ b/R/occuCOP.R @@ -248,86 +248,22 @@ setMethod("summary", "unmarkedFrameOccuCOP", function(object,...) { }) -## umf[i, j] ---- -setMethod("[", c("unmarkedFrameOccuCOP", "numeric", "numeric", "missing"), - function(x, i, j) { - # Gey dimensions of x - M <- numSites(x) - J <- obsNum(x) - - if (length(i) == 0 & length(j) == 0) { - return(x) - } - - # Check i - if (any(i < 0) && - any(i > 0)) { - stop("i must be all positive or all negative indices.") - } - if (all(i < 0)) { - i <- (1:M)[i] - } - - # Check j - if (any(j < 0) && - any(j > 0)) { - stop("j must be all positive or all negative indices.") - } - if (all(j < 0)) { - j <- (1:J)[j] - } - - # y observation count data subset - y <- getY(x)[i, j, drop = FALSE] - if (min(length(i), length(j)) == 1) { - y <- t(y) - } - - # L subset - L <- x@L[i, j, drop = FALSE] - if (min(length(i), length(j)) == 1) { - L <- t(L) - } - - # siteCovs subset - siteCovs <- siteCovs(x) - if (!is.null(siteCovs)) { - siteCovs <- siteCovs(x)[i, , drop = FALSE] - } - - # obsCovs subset - obsCovs <- obsCovs(x) - if (!is.null(obsCovs)) { - MJ_site <- rep(1:M, each = J) - MJ_obs <- rep(1:J, times = M) - obsCovs <- obsCovs[((MJ_obs %in% j) & (MJ_site %in% i)), , drop = FALSE] - rownames(obsCovs) <- NULL - } - - # Recreate umf - new( - Class = "unmarkedFrameOccuCOP", - y = y, - L = L, - siteCovs = siteCovs, - obsCovs = obsCovs, - obsToY = diag(length(j)), - mapInfo = x@mapInfo - ) - }) - - -## umf[i, ] ---- -setMethod("[", c("unmarkedFrameOccuCOP", "numeric", "missing", "missing"), - function(x, i) { - x[i, 1:obsNum(x)] - }) +## Bracket selectors ---- +setMethod("select_sites_internal", "unmarkedFrameOccuCOP", + function(x, i){ + out <- callNextMethod(x, i) + L <- x@L[i,,drop=FALSE] + unmarkedFrameOccuCOP(y=getY(out), L = L, siteCovs=siteCovs(out), + obsCovs=obsCovs(out)) +}) -## umf[, j] ---- -setMethod("[", c("unmarkedFrameOccuCOP", "missing", "numeric", "missing"), - function(x, j) { - x[1:numSites(x), j] - }) +setMethod("select_obs_internal", "unmarkedFrameOccuCOP", + function(x, j){ + out <- callNextMethod(x, j) + L <- x@L[,j,drop=FALSE] + unmarkedFrameOccuCOP(y=getY(out), L = L, siteCovs=siteCovs(out), + obsCovs=obsCovs(out)) +}) ## fl_getY ---- diff --git a/R/unmarkedFrame.R b/R/unmarkedFrame.R index c936bdf..48aad33 100644 --- a/R/unmarkedFrame.R +++ b/R/unmarkedFrame.R @@ -1118,417 +1118,6 @@ setMethod("hist", "unmarkedFrameDS", function(x, ...) }) - -################################# SELECTORS ############################## - -# i is the vector of sites to extract - -setMethod("[", c("unmarkedFrame", "numeric", "missing", "missing"), - function(x, i) -{ - M <- numSites(x) - if(length(i) == 0) return(x) - if(any(i < 0) && any(i > 0)) - stop("i must be all positive or all negative indices.") - if(all(i < 0)) { # if i is negative, then convert to positive - i <- (1:M)[i] - } - y <- getY(x)[i,] - if (length(i) == 1) { - y <- t(y) - } - siteCovs <- siteCovs(x) - obsCovs <- obsCovs(x) - if (!is.null(siteCovs)) { - siteCovs <- siteCovs(x)[i, , drop = FALSE] - } - if (!is.null(obsCovs)) { - R <- obsNum(x) - .site <- rep(1:M, each = R) - oc <- lapply(i, function(ind){ - obsCovs[.site==ind,,drop=FALSE] - }) - obsCovs <- do.call(rbind, oc) - } - umf <- x - umf@y <- y - umf@siteCovs <- siteCovs - umf@obsCovs <- obsCovs - umf -}) - - -## remove obs only -### RBC: Why??? this doesn't allow umf[,c(1,1)] -setMethod("[", c("unmarkedFrame", "missing", "numeric", "missing"), - function(x, i, j) -{ - y <- getY(x) - obsCovs <- obsCovs(x) - obsToY <- obsToY(x) - obs.remove <- rep(TRUE, obsNum(x)) - obs.remove[j] <- FALSE - y.remove <- t(obs.remove) %*% obsToY > 0 - y <- y[,!y.remove, drop=FALSE] - obsCovs <- obsCovs[!rep(obs.remove, numSites(x)),, drop=FALSE] - x@obsCovs <- obsCovs - x@y <- y - x@obsToY <- obsToY[!obs.remove,!y.remove, drop=FALSE] - x -}) - - -# i is as before and j is the obsNum to remove and corresponding y's -setMethod("[", c("unmarkedFrame","numeric", "numeric", "missing"), - function(x, i, j) -{ - ## first remove sites - umf <- x[i,] - umf <- umf[,j] - umf -}) - - - -### list is a ragged array of indices (y's) to include for each site. -### Typically useful for multilevel boostrapping. -setMethod("[", c("unmarkedFrame","list", "missing", "missing"), - function(x, i, j) -{ - m <- numSites(x) - J <- R <- obsNum(x) - o2y <- obsToY(x) - if (!identical(o2y, diag(R))) - stop("Ragged subsetting of unmarkedFrames is only valid for diagonal obsToY.") - J <- ncol(o2y) - if (m != length(i)) stop("list length must be same as number of sites.") - siteCovs <- siteCovs(x) - y <- cbind(.site=1:m, getY(x)) - obsCovs <- obsCovs(x) - site_idx <- rep(1:m, each=R) - stopifnot(length(site_idx) == nrow(obsCovs)) - - oc <- lapply(1:m, function(ind){ - df <- obsCovs[site_idx==ind,,drop=FALSE] - obs <- i[[ind]] - if (length(obs) > R) - stop("All elements of list must be less than or equal to R.") - obs <- c(obs, rep(NA, R-length(obs))) - df[obs,,drop=FALSE] - }) - obsCovs <- do.call(rbind, oc) - rownames(obsCovs) <- NULL - - y <- apply(y, 1, function(row) { - site <- row[1] - row <- row[-1] - obs <- i[[site]] - obs <- c(obs, rep(NA, R-length(obs))) - row[obs] - }) - - if(!is.null(obsCovs(x))){ - obsCovs(x) <- obsCovs - } - x@y <- t(y) - x -}) - - -#[ Methods for multispecies occupancy frames -setMethod("[", c("unmarkedFrameOccuMulti", "numeric", "missing", "missing"), - function(x, i) -{ - if(length(i) == 0) return(x) - M <- numSites(x) - - ylist <- lapply(x@ylist,function(x) x[i,,drop=F]) - siteCovs <- siteCovs(x) - obsCovs <- obsCovs(x) - if (!is.null(siteCovs)) { - siteCovs <- siteCovs(x)[i, , drop = FALSE] - } - if (!is.null(obsCovs)) { - R <- obsNum(x) - .site <- rep(1:M, each = R) - oc <- lapply(i, function(ind){ - obsCovs[.site==ind,,drop=FALSE] - }) - obsCovs <- do.call(rbind, oc) - } - umf <- x - umf@y <- ylist[[1]] - umf@ylist <- ylist - umf@siteCovs <- siteCovs - umf@obsCovs <- obsCovs - umf -}) - -setMethod("[", c("unmarkedFrameOccuMulti", "missing", "numeric", "missing"), - function(x, i, j) -{ - y <- getY(x) - obsCovs <- obsCovs(x) - obsToY <- obsToY(x) - obs.remove <- rep(TRUE, obsNum(x)) - obs.remove[j] <- FALSE - y.remove <- t(obs.remove) %*% obsToY > 0 - ylist <- lapply(x@ylist, function(z) z[,!y.remove, drop=F]) - obsCovs <- obsCovs[!rep(obs.remove, numSites(x)),, drop=FALSE] - - x@obsCovs <- obsCovs - x@y <- ylist[[1]] - x@ylist <- ylist - x@obsToY <- obsToY[!obs.remove,!y.remove, drop=FALSE] - x -}) - - -## for multframes, must remove years at a time -setMethod("[", c("unmarkedMultFrame", "missing", "numeric", "missing"), - function(x, i, j) -{ - J <- obsNum(x)/x@numPrimary - obs <- rep(1:x@numPrimary, each = J) - years <- 1:x@numPrimary - numPrimary <- length(j) - obsj <- match(obs, j) - j2 <- which(!is.na(obsj)) - u <- callNextMethod(x, i, j2) - ysc <- yearlySiteCovs(x) - if(!is.null(ysc)) { - ysc <- ysc[rep(!is.na(match(years, j)), nrow(getY(x))),, drop=FALSE] - u@yearlySiteCovs <- ysc - } - u@numPrimary <- numPrimary - return(u) -}) - - - -## for multframes, must remove years at a time -setMethod("[", c("unmarkedMultFrame", "numeric", "missing", "missing"), - function(x, i, j) -{ - M <- numSites(x) - if(length(i) == 0) return(x) - if(any(i < 0) && any(i > 0)) - stop("i must be all positive or all negative indices.") - if(all(i < 0)) { # if i is negative, then convert to positive - i <- (1:M)[i] - } - oldy <- getY(x) - y <- oldy[i,] - siteCovs <- siteCovs(x) - obsCovs <- obsCovs(x) - if (!is.null(siteCovs)) { - siteCovs <- siteCovs(x)[i, , drop = FALSE] - } - if (!is.null(obsCovs)) { - R <- obsNum(x) - .site <- rep(1:M, each = obsNum(x)) #NULL ## testing - oc <- lapply(i, function(ind){ - obsCovs[.site==ind,,drop=FALSE] - }) - obsCovs <- do.call(rbind, oc) - } - u <- unmarkedMultFrame(y=matrix(y, ncol=ncol(oldy)), - siteCovs=siteCovs, - obsCovs=obsCovs, - numPrimary=x@numPrimary) - ysc <- x@yearlySiteCovs - if(!is.null(ysc)) { - T <- x@numPrimary - sites <- rep(1:M, each=T) - keep <- as.vector(sapply(i, function(x) which(sites %in% x))) - ysc <- ysc[keep,, drop=FALSE] - u@yearlySiteCovs <- ysc - } - u - -}) - - -setMethod("[", c("unmarkedFrameOccuMS", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) - unmarkedFrameOccuMS(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - obsCovs=obsCovs(multf), - numPrimary=x@numPrimary) -}) - -setMethod("[", c("unmarkedFrameGMM", "numeric", "missing", "missing"), - function(x, i, j) -{ - M <- nrow(x@y) - y <- x@y[i,,drop=FALSE] - R <- obsNum(x) - T <- x@numPrimary - - sc <- siteCovs(x)[i,,drop=FALSE] - - ysc_ind <- rep(1:M, each=T) - ysc <- do.call("rbind", lapply(i, function(ind){ - yearlySiteCovs(x)[ysc_ind == ind,,drop=FALSE] - })) - - oc_ind <- rep(1:M, each=R) - oc <- do.call("rbind", lapply(i, function(ind){ - obsCovs(x)[oc_ind == ind,,drop=FALSE] - })) - - unmarkedFrameGMM(y=y, siteCovs=sc, - yearlySiteCovs=ysc, - obsCovs=oc, - piFun=x@piFun, type=x@samplingMethod, - obsToY=x@obsToY, numPrimary=x@numPrimary) -}) - - -setMethod("[", c("unmarkedFrameGPC", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - class(multf) <- "unmarkedFrameGPC" - multf -}) - - -setMethod("[", c("unmarkedFrameGPC", "missing", "numeric", "missing"), - function(x, i, j) -{ - multf <- as(x, "unmarkedMultFrame") - out <- callNextMethod(multf, i, j) # unmarkedMultFrame - as(out, "unmarkedFrameGPC") -}) - - - - - -setMethod("[", c("unmarkedFrameGDS", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - sur <- x@survey - if(sur=="line") - unmarkedFrameGDS(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - numPrimary=x@numPrimary, - dist.breaks=x@dist.breaks, - tlength=x@tlength[i], - survey=sur, - unitsIn=x@unitsIn) - else if(sur=="point") - unmarkedFrameGDS(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - numPrimary=x@numPrimary, - dist.breaks=x@dist.breaks, - survey=sur, - unitsIn=x@unitsIn) -}) - - - -setMethod("[", c("unmarkedFramePCO", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - unmarkedFramePCO(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - obsCovs=obsCovs(multf), - numPrimary=x@numPrimary, - primaryPeriod=x@primaryPeriod[i,,drop=FALSE]) -}) - - -setMethod("[", c("unmarkedFramePCO", "missing", "numeric", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - unmarkedFramePCO(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - obsCovs=obsCovs(multf), - numPrimary=length(j), - primaryPeriod=x@primaryPeriod[,j,drop=FALSE]) -}) - - -setMethod("[", c("unmarkedFrameOccuTTD", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - unmarkedFrameOccuTTD(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - obsCovs=obsCovs(multf), - numPrimary=x@numPrimary, - surveyLength=x@surveyLength[i,,drop=FALSE]) -}) - - -setMethod("[", c("unmarkedFrameDSO", "numeric", "missing", "missing"), - function(x, i, j) -{ - multf <- callNextMethod(x, i, j) # unmarkedMultFrame - sur <- x@survey - pp <- x@primaryPeriod[i,,drop=FALSE] - if(sur=="line") - unmarkedFrameDSO(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - numPrimary=x@numPrimary, - dist.breaks=x@dist.breaks, - tlength=x@tlength[i], - survey=sur, - unitsIn=x@unitsIn, - primaryPeriod=pp) - else if(sur=="point") - unmarkedFrameDSO(y=getY(multf), siteCovs=siteCovs(multf), - yearlySiteCovs=yearlySiteCovs(multf), - numPrimary=x@numPrimary, - dist.breaks=x@dist.breaks, - survey=sur, - unitsIn=x@unitsIn, - primaryPeriod=pp) -}) - - -setMethod("[", c("unmarkedFrameOccuTTD", "missing", "numeric", "missing"), - function(x, i, j) -{ - - if(any(j>x@numPrimary)) stop("Can't select primary periods that don't exist", call.=FALSE) - if(!all(j>0)) stop("All indices must be positive", call.=FALSE) - - R <- ncol(getY(x))/x@numPrimary - pp_vec <- rep(1:x@numPrimary, each=R) - keep_cols <- which(pp_vec%in%j) - y <- getY(x)[,keep_cols,drop=FALSE] - sl <- x@surveyLength[,keep_cols,drop=FALSE] - - pp_vec2 <- rep(1:x@numPrimary, numSites(x)) - keep_rows <- which(pp_vec2 %in% j) - ysc <- yearlySiteCovs(x)[keep_rows,,drop=FALSE] - - obs_vec <- rep(rep(1:x@numPrimary, each = R), numSites(x)) - keep_rows <- which(obs_vec %in% j) - oc <- obsCovs(x)[keep_rows,,drop=FALSE] - - unmarkedFrameOccuTTD(y=y, surveyLength=sl, siteCovs=siteCovs(x), - yearlySiteCovs=ysc, obsCovs=oc, - numPrimary=length(j)) -}) - - - - -setMethod("head", "unmarkedFrame", function(x, n) { - if(missing(n)) n <- 10 - umf <- x[1:n,] - umf -}) - ############################### COERCION ################################# setAs("data.frame", "unmarkedFrame", function(from) diff --git a/man/extract-methods.Rd b/man/extract-methods.Rd deleted file mode 100644 index a891b9d..0000000 --- a/man/extract-methods.Rd +++ /dev/null @@ -1,93 +0,0 @@ -\name{[-methods} -\docType{methods} -\alias{[-methods} -\alias{[,unmarkedEstimateList,ANY,ANY,ANY-method} -\alias{[,unmarkedFit,ANY,ANY,ANY-method} -\alias{[,unmarkedFrame,missing,numeric,missing-method} -\alias{[,unmarkedFrame,numeric,missing,missing-method} -\alias{[,unmarkedFrame,numeric,numeric,missing-method} -\alias{[,unmarkedMultFrame,missing,numeric,missing-method} -\alias{[,unmarkedMultFrame,numeric,missing,missing-method} -\alias{[,unmarkedFrameGMM,numeric,missing,missing-method} -\alias{[,unmarkedFrameGDS,numeric,missing,missing-method} -\alias{[,unmarkedFramePCO,numeric,missing,missing-method} -\alias{[,unmarkedFramePCO,missing,numeric,missing-method} -\alias{[,unmarkedFrameGPC,numeric,missing,missing-method} -\alias{[,unmarkedFrameGPC,missing,numeric,missing-method} -\alias{[,unmarkedFrame,list,missing,missing-method} -\title{Methods for bracket extraction [ in Package `unmarked'} -\description{ -Methods for bracket extraction \code{[} in Package `unmarked' -} -\usage{ -\S4method{[}{unmarkedEstimateList,ANY,ANY,ANY}(x, i, j, drop) -\S4method{[}{unmarkedFit,ANY,ANY,ANY}(x, i, j, drop) -\S4method{[}{unmarkedFrame,numeric,numeric,missing}(x, i, j) -\S4method{[}{unmarkedFrame,list,missing,missing}(x, i, j) -\S4method{[}{unmarkedMultFrame,missing,numeric,missing}(x, i, j) -\S4method{[}{unmarkedMultFrame,numeric,missing,missing}(x, i, j) -\S4method{[}{unmarkedFrameGMM,numeric,missing,missing}(x, i, j) -\S4method{[}{unmarkedFrameGDS,numeric,missing,missing}(x, i, j) -\S4method{[}{unmarkedFramePCO,numeric,missing,missing}(x, i, j) -} -\arguments{ - \item{x}{Object of appropriate S4 class} - \item{i}{Row numbers} - \item{j}{Observation numbers (eg occasions, distance classes, etc...)} - \item{drop}{Not currently used} - } -\section{Methods}{ -\describe{ - -\item{x = "unmarkedEstimateList", i = "ANY", j = "ANY", drop = "ANY"}{Extract a - unmarkedEstimate object from an unmarkedEstimateList by name (either 'det' - or 'state')} - -\item{x = "unmarkedFit", i = "ANY", j = "ANY", drop = "ANY"}{Extract a - unmarkedEstimate object from an unmarkedFit by name (either 'det' - or 'state')} - -\item{x = "unmarkedFrame", i = "missing", j = "numeric", drop = "missing"}{ - Extract observations from an unmarkedFrame.} - -\item{x = "unmarkedFrame", i = "numeric", j = "missing", drop = "missing"}{ - Extract rows from an unmarkedFrame } - -\item{x = "unmarkedFrame", i = "numeric", j = "numeric", drop = "missing"}{ - Extract rows and observations from an unmarkedFrame } - -\item{x = "unmarkedMultFrame", i = "missing", j = "numeric", drop = "missing"}{ - Extract primary sampling periods from an unmarkedMultFrame} - -\item{x = "unmarkedFrame", i = "list", j = "missing", drop = "missing"}{ - List is the index of observations to subset for each site. } - -\item{x = "unmarkedMultFrame", i = "numeric", j = "missing", drop = "missing"}{ - Extract rows (sites) from an unmarkedMultFrame} - -\item{x = "unmarkedGMM", i = "numeric", j = "missing", drop = "missing"}{ - Extract rows (sites) from an unmarkedFrameGMM object} - -\item{x = "unmarkedGDS", i = "numeric", j = "missing", drop = "missing"}{ - Extract rows (sites) from an unmarkedFrameGDS object} - -\item{x = "unmarkedPCO", i = "numeric", j = "missing", drop = "missing"}{ - Extract rows (sites) from an unmarkedFramePCO object} - -}} - - -\examples{ - -data(mallard) -mallardUMF <- unmarkedFramePCount(mallard.y, siteCovs = mallard.site, - obsCovs = mallard.obs) -summary(mallardUMF) - -mallardUMF[1:5,] -mallardUMF[,1:2] -mallardUMF[1:5, 1:2] - -} -\keyword{methods} - diff --git a/man/unmarkedEstimateList-class.Rd b/man/unmarkedEstimateList-class.Rd index 41f1778..3aa42b0 100644 --- a/man/unmarkedEstimateList-class.Rd +++ b/man/unmarkedEstimateList-class.Rd @@ -5,6 +5,7 @@ \alias{names,unmarkedEstimateList-method} \alias{show,unmarkedEstimateList-method} \alias{summary,unmarkedEstimateList-method} +\alias{[,unmarkedEstimateList,ANY,ANY,ANY-method} \title{Class "unmarkedEstimateList"} \description{Class to hold multiple unmarkedEstimates in an \code{\link{unmarkedFit}} diff --git a/man/unmarkedFit-class.Rd b/man/unmarkedFit-class.Rd index e9d1c01..5fa6528 100644 --- a/man/unmarkedFit-class.Rd +++ b/man/unmarkedFit-class.Rd @@ -64,6 +64,7 @@ \alias{getY,unmarkedFitColExt-method} \alias{getY,unmarkedFitOccuRN-method} \alias{getY,unmarkedFitOccuMulti-method} +\alias{[,unmarkedFit,ANY,ANY,ANY-method} \title{Class "unmarkedFit" } \description{Contains fitted model information which can be manipulated or diff --git a/man/unmarkedFrame-bracket-methods.Rd b/man/unmarkedFrame-bracket-methods.Rd new file mode 100644 index 0000000..1bc9f33 --- /dev/null +++ b/man/unmarkedFrame-bracket-methods.Rd @@ -0,0 +1,47 @@ +\name{[-methods for unmarkedFrames} +\alias{[,unmarkedFrame,numeric,missing,missing-method} +\alias{[,unmarkedFrame,missing,numeric,missing-method} +\alias{[,unmarkedFrame,numeric,numeric,missing-method} +\alias{[,unmarkedFrame,list,missing,missing-method} +\alias{[,unmarkedFrame,logical,missing,missing-method} + +\title{Methods for bracket extraction [ of unmarkedFrames} + +\description{ + These methods extract subsets of sites, primary periods, or observations + from \code{unmarkedFrame} objects using numeric vectors of indices, + a list of indices, or a logical vector. +} + +\usage{ +\S4method{[}{unmarkedFrame,numeric,missing,missing}(x, i, j) +\S4method{[}{unmarkedFrame,missing,numeric,missing}(x, i, j) +\S4method{[}{unmarkedFrame,numeric,numeric,missing}(x, i, j) +\S4method{[}{unmarkedFrame,list,missing,missing}(x, i, j) +\S4method{[}{unmarkedFrame,logical,missing,missing}(x, i, j) +} + +\arguments{ + \item{x}{An object of class \code{unmarkedFrame}} + \item{i}{Sites to select} + \item{j}{Observation numbers (eg occasions, distance classes, etc.) to so select + for single-season models, and primary periods to select for multi-season and + temporary emigration models} +} + +\value{ + A new \code{unmarkedFrame} object of the class as the input. +} + +\examples{ + +data(mallard) +mallardUMF <- unmarkedFramePCount(mallard.y, siteCovs = mallard.site, + obsCovs = mallard.obs) +summary(mallardUMF) + +mallardUMF[1:5,] +mallardUMF[,1:2] +mallardUMF[1:5, 1:2] + +} diff --git a/man/unmarkedFrame-class.Rd b/man/unmarkedFrame-class.Rd index ab41506..ddcd347 100644 --- a/man/unmarkedFrame-class.Rd +++ b/man/unmarkedFrame-class.Rd @@ -58,18 +58,6 @@ \alias{summary,unmarkedFrameOccuMulti-method} \alias{summary,unmarkedFrameOccuTTD-method} \alias{summary,unmarkedFrameOccuCOP-method} -\alias{[,unmarkedFrameOccuMulti,missing,numeric,missing-method} -\alias{[,unmarkedFrameOccuTTD,missing,numeric,missing-method} -\alias{[,unmarkedFrameGDR,missing,numeric,missing-method} -\alias{[,unmarkedFrameOccuMS,numeric,missing,missing-method} -\alias{[,unmarkedFrameOccuTTD,numeric,missing,missing-method} -\alias{[,unmarkedFrameOccuMulti,numeric,missing,missing-method} -\alias{[,unmarkedFrameDSO,numeric,missing,missing-method} -\alias{[,unmarkedFrameGDR,numeric,missing,missing-method} -\alias{[,unmarkedFrameGDR,logical,missing,missing-method} -\alias{[,unmarkedFrameOccuCOP,missing,numeric,missing-method} -\alias{[,unmarkedFrameOccuCOP,numeric,missing,missing-method} -\alias{[,unmarkedFrameOccuCOP,numeric,numeric,missing-method} \title{Class "unmarkedFrame" } \description{Methods for manipulating, summarizing and viewing diff --git a/tests/testthat/test_occuCOP.R b/tests/testthat/test_occuCOP.R index ca880bd..26c72f0 100644 --- a/tests/testthat/test_occuCOP.R +++ b/tests/testthat/test_occuCOP.R @@ -79,7 +79,7 @@ test_that("unmarkedFrameOccuCOP is constructed correctly", { # Create subsets expect_no_error(umf_sub_i <- umf[1:3, ]) expect_no_error(umf_sub_j <- umf[, 1:2]) - expect_no_error(umf_sub_ij <- umf[1:3, 1:2]) + expect_no_error(expect_warning(umf_sub_ij <- umf[1:3, 1:2])) # unmarkedFrameOccuCOP organisation ---------------------------------------------- expect_true(inherits(umf, "unmarkedFrameOccuCOP")) @@ -95,7 +95,7 @@ test_that("unmarkedFrameOccuCOP is constructed correctly", { expect_output(print(umf_sub_i), "Data frame representation of unmarkedFrame object") expect_output(print(umf[1,]), "Data frame representation of unmarkedFrame object") expect_output(print(umf[,1]), "Data frame representation of unmarkedFrame object") - expect_output(print(umf[1,1]), "Data frame representation of unmarkedFrame object") + expect_output(expect_warning(print(umf[1,1])), "Data frame representation of unmarkedFrame object") # summary method for unmarkedFrameOccuCOP expect_output(summary(umf), "unmarkedFrameOccuCOP Object") |