summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormikemeredith <mike@mmeredith.net>2021-10-12 09:17:12 +0800
committermikemeredith <mike@mmeredith.net>2021-10-12 09:17:12 +0800
commitdbb30e3ae07861e05eccd7fbfb8c9ffa141f459d (patch)
tree3be00b3c1efbe73347b64524a963c2b3451b8769
parent05e29e540ad7847aa998e3c316abb74d3979a1fd (diff)
Modify marrayAge
-rw-r--r--.local_checks/@making_IPMbook_package.R2
-rw-r--r--DESCRIPTION4
-rw-r--r--NEWS6
-rw-r--r--R/groupfreq.R28
-rw-r--r--R/marrayAge.R10
-rw-r--r--man/marrayAge.Rd4
6 files changed, 17 insertions, 37 deletions
diff --git a/.local_checks/@making_IPMbook_package.R b/.local_checks/@making_IPMbook_package.R
index 62578d1..48bdcf9 100644
--- a/.local_checks/@making_IPMbook_package.R
+++ b/.local_checks/@making_IPMbook_package.R
@@ -20,7 +20,7 @@ devtools::load_all("D:/Github/IPMbook_package/IPMbook")
# ==========================
unlink(list.files(pattern="Rplots.pdf", recursive=TRUE))
system("R CMD build IPMbook") # Produces the .tar.gz
-pkg <- "IPMbook_0.1.2.9006.tar.gz" # <-- fix version number here
+pkg <- "IPMbook_0.1.2.9007.tar.gz" # <-- fix version number here
# Pick one to check:
## on desktop
diff --git a/DESCRIPTION b/DESCRIPTION
index a472a79..8ea9e91 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
Package: IPMbook
Type: Package
Title: Functions and Data for the Book 'Integrated Population Models'
-Version: 0.1.2.9006
-Date: 2021-10-04
+Version: 0.1.2.9007
+Date: 2021-10-11
Depends: R (>= 2.10)
Imports: stats, abind
Suggests:
diff --git a/NEWS b/NEWS
index 7bfb296..1effce7 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,8 @@
-CHANGES in version 0.1.2.9005 (2021-10-04)
+CHANGES in version 0.1.2.9007 (2021-10-11)
+
+ * `marrayAge` now accepts a scalar value for `age`.
+
+CHANGES in version 0.1.2.9006 (2021-10-04)
* `marray*` functions gain a `groups` argument, as an alternative to columns in `freq`.
diff --git a/R/groupfreq.R b/R/groupfreq.R
index 26752ad..7eae54d 100644
--- a/R/groupfreq.R
+++ b/R/groupfreq.R
@@ -1,5 +1,5 @@
-# Function to combine a vector of frequncies and a vector of group IDs
+# Function to combine a vector of frequencies and a vector of group IDs
# into a matrix with frequencies for each group
# Input:
@@ -18,29 +18,3 @@ groupfreq <- function(freq, groups) {
out[cbind(1:nind, as.numeric(groups))] <- freq
return(out)
}
-
-
-# For testing:
-if(FALSE) {
-library(wiqid)
-data(dippers)
-CH <- dippers[, 1:7]
-groups <- dippers$sex
-CHf <- CH[groups=="F", ]
-CHm <- CH[groups=="M", ]
-
-freq <- rep(1, length(groups))
-
-library(IPMbook)
-
-mf <- marray(CHf)
-mm <- marray(CHm)
-
-f2 <- groupfreq(freq, groups)
-mg <- marray(CH, freq=f2)
-mg
-all(mg[,,1] == mf)
-all(mg[,,2] == mm)
-
-
-}
diff --git a/R/marrayAge.R b/R/marrayAge.R
index c397a73..250065a 100644
--- a/R/marrayAge.R
+++ b/R/marrayAge.R
@@ -20,7 +20,7 @@
################################################
-marrayAge <- function(ch, age, mAge = 1, freq = 1, groups = NULL){
+marrayAge <- function(ch, age = 1, mAge = 1, freq = 1, groups = NULL){
if(is.data.frame(ch))
ch <- as.matrix(ch)
if (!is.matrix(ch))
@@ -29,6 +29,8 @@ marrayAge <- function(ch, age, mAge = 1, freq = 1, groups = NULL){
stopifNegative(ch, allowNA=TRUE, allowZero=TRUE)
age <- round(age)
stopifNegative(age, allowNA=FALSE, allowZero=FALSE)
+ if(length(age) == 1)
+ age <- rep(age, nrow(ch))
mAge <- round(mAge[1])
stopifNegative(mAge, allowNA=FALSE, allowZero=FALSE)
if(is.data.frame(freq))
@@ -49,7 +51,7 @@ marrayAge <- function(ch, age, mAge = 1, freq = 1, groups = NULL){
# stopifnotLength(freq, nind, allow1=FALSE) ## FIXME
n.occasions <- ncol(ch)
ng <- ncol(freq) # number of groups, can be 1
-
+
# Remove capture histories of individuals that are marked at last occasion
first <- getFirst(ch)
# last <- which(first == n.occasions)
@@ -75,7 +77,7 @@ marrayAge <- function(ch, age, mAge = 1, freq = 1, groups = NULL){
# Create empty m-array, add dimnames
marr <- array(0, dim = c(n.occasions-1, n.occasions, maxAge, ng))
-
+
gNames <- colnames(freq)
if(is.null(gNames))
gNames <- paste0("G", 1:ng)
@@ -92,7 +94,7 @@ marrayAge <- function(ch, age, mAge = 1, freq = 1, groups = NULL){
cap.occ <- which(ch[i,]!=0)
capID <- paste0("Y", cap.occ)
cap.age <- age.matrix[i, cap.occ]
-
+
for(g in 1:ng) {
if (length(capID) == 1 && !traploss[i,g]) { # no recaptures
marr[capID[1], 'never', cap.age[1], g] <- marr[capID[1], 'never', cap.age[1],g] + absfreq[i,g]
diff --git a/man/marrayAge.Rd b/man/marrayAge.Rd
index 4ee33c1..0d65341 100644
--- a/man/marrayAge.Rd
+++ b/man/marrayAge.Rd
@@ -9,14 +9,14 @@ Creates age-dependent m-arrays
Converts single-state capture-recapture data to age-dependent m-arrays.
}
\usage{
-marrayAge(ch, age, mAge = 1, freq = 1, groups = NULL)
+marrayAge(ch, age = 1, mAge = 1, freq = 1, groups = NULL)
}
\arguments{
\item{ch}{
an individuals x time matrix with capture histories (0: not captured; 1: captured). See Details.
}
\item{age}{
-vector with the age class at first capture for each individual.
+vector with the age class at first capture for each individual, or a scalar that will be used for all individuals.
}
\item{mAge}{
maximum number of age classes for which m-arrays are constructed; ignored if \code{max(age) > mAge}. Only required if the \code{age} vector has fewer age classes than we want to separate (e.g. capture histories \code{ch} contains only individuals marked as juveniles, and we want 2 age classes).