Skip to content

Commit

Permalink
version 0.3-0
Browse files Browse the repository at this point in the history
  • Loading branch information
jun-yan authored and cran-robot committed Oct 1, 2016
1 parent 81bc4e8 commit 5abc903
Show file tree
Hide file tree
Showing 15 changed files with 1,134 additions and 269 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
2016-09-30 Jun Yan <jun.yan@uconn.edu>

* added full likelihood based hidden Markov model
* full likelihood and composite likelihood are now done with Rcpp


2013-11-04 Jun Yan <jun.yan@uconn.edu>

* added Depends: R (>= 3.0.0).
Expand Down
19 changes: 11 additions & 8 deletions DESCRIPTION
@@ -1,16 +1,19 @@
Package: smam
Type: Package
Title: Statistical Modeling of Animal Movements
Version: 0.2-2
Date: 2013-11-4
Author: Jun Yan <jun.yan@uconn.edu> and
Vladimir Pozdnyakov <vladimir.pozdnyakov@uconn.edu>
Version: 0.3-0
Date: 2016-09-02
Author: Jun Yan <jun.yan@uconn.edu> and
Vladimir Pozdnyakov <vladimir.pozdnyakov@uconn.edu>
Maintainer: Jun Yan <jun.yan@uconn.edu>
Depends: R (>= 3.0.0), Matrix
Depends: R (>= 3.2.0)
Imports: stats, methods, Matrix, numDeriv, Rcpp (>= 0.12.2)
LinkingTo: Rcpp
Description: Animal movement models including moving-resting process
with embedded Brownian motion, Brownian motion with measurement error.
with embedded Brownian motion, Brownian motion with measurement error.
License: GPL (>= 3.0)
Packaged: 2013-11-04 10:58:18 UTC; jyan
RoxygenNote: 5.0.1
NeedsCompilation: yes
Packaged: 2016-09-30 23:24:03 UTC; junyan
Repository: CRAN
Date/Publication: 2013-11-04 14:11:39
Date/Publication: 2016-10-01 14:33:22
24 changes: 14 additions & 10 deletions MD5
@@ -1,11 +1,15 @@
991ea0e90141bf8d7ebad24fe21d838f *ChangeLog
4b69281a07fce53e8e711024a7a9e406 *DESCRIPTION
e71ec683464d8db6d3a6f8acc790c7f1 *NAMESPACE
332261d90bf856cc4528300c7ba4605e *R/bmme.R
e0a92a8a2e9b6aebaa0002a1fedf6b2c *R/movres.R
9ad7a258de2dd391fcfde01984b91379 *man/dtm.Rd
318af21c2f7491de1943b4a889d9eb9e *man/fitBmme.Rd
2d90fb6d5ad7a963ce33801f67a5e166 *man/fitMovRes.Rd
cf8a771958de0ff7de8fecce8129f6bd *man/rMovRes.Rd
8e6a097f8cc5a6877e8314a5ab1a9532 *man/rbmme.Rd
2646fcacec898b2a7a9bb413a409941e *ChangeLog
a40a384a2cb164533fc094ea96173991 *DESCRIPTION
040cb528d6afd61d17fe93146e093c94 *NAMESPACE
873fe7bd0943f1171119a6c66f19bd99 *R/RcppExports.R
1266d5adc42c04a88dd503a6bb66d863 *R/bmme.R
20bdc0be21ed0c9cfdb28e5c0fa53390 *R/movres.R
3abc5257ac9f082752ec82d220c5b0a9 *man/dtm.Rd
d1b689ceb76908d3f23d69044ca95b41 *man/fitBmme.Rd
c4e7b9a5e92d4858e31367c0e14897e9 *man/fitMovRes.Rd
043d6f9bc4b2ac8fe8a0e88b7b145445 *man/integr.control.Rd
ea9d2018e85318cf41746a325c74e3f3 *man/rMovRes.Rd
247b7abd6e1172bb545a52f6ba3268ec *man/rbmme.Rd
f886589f1bb47e97e5e42118c9bb052b *src/RcppExports.cpp
49ec4ce0b9a60c3a44525f67bad626a0 *src/altb.c
d31a7edd7834fe1d9820bd8ff801a0d5 *src/movres-llk.cpp
30 changes: 17 additions & 13 deletions NAMESPACE
@@ -1,14 +1,18 @@
useDynLib(smam)


## exportPattern(".")
# Generated by roxygen2: do not edit by hand

## BM with ME
export(fitBmme,
rbmme)

## Moving-Resting with embedded BM
export(fitMovRes, rMovRes, cllk.m1,
dtm,
dtr)

export(dtm)
export(dtr)
export(fitBmme)
export(fitMovRes)
export(integr.control)
export(rMovRes)
export(rbmme)
importFrom(Rcpp,evalCpp)
importFrom(methods,is)
importFrom(numDeriv,hessian)
importFrom(stats,dnorm)
importFrom(stats,integrate)
importFrom(stats,optim)
importFrom(stats,rexp)
importFrom(stats,rnorm)
useDynLib(smam)
43 changes: 43 additions & 0 deletions R/RcppExports.R
@@ -0,0 +1,43 @@
# This file was generated by Rcpp::compileAttributes
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

vp11 <- function(vw, t, lambda1, lambda0) {
.Call('smam_vp11', PACKAGE = 'smam', vw, t, lambda1, lambda0)
}

vp10 <- function(vw, t, lambda1, lambda0) {
.Call('smam_vp10', PACKAGE = 'smam', vw, t, lambda1, lambda0)
}

vp00 <- function(vw, t, lambda1, lambda0) {
.Call('smam_vp00', PACKAGE = 'smam', vw, t, lambda1, lambda0)
}

vp01 <- function(vw, t, lambda1, lambda0) {
.Call('smam_vp01', PACKAGE = 'smam', vw, t, lambda1, lambda0)
}

h11 <- function(x, t, theta, integrControl) {
.Call('smam_h11', PACKAGE = 'smam', x, t, theta, integrControl)
}

h10 <- function(x, t, theta, integrControl) {
.Call('smam_h10', PACKAGE = 'smam', x, t, theta, integrControl)
}

h00 <- function(x, t, theta, integrControl) {
.Call('smam_h00', PACKAGE = 'smam', x, t, theta, integrControl)
}

h01 <- function(x, t, theta, integrControl) {
.Call('smam_h01', PACKAGE = 'smam', x, t, theta, integrControl)
}

ncllk_m1_inc <- function(theta, data, integrControl, logtr) {
.Call('smam_ncllk_m1_inc', PACKAGE = 'smam', theta, data, integrControl, logtr)
}

nllk_inc <- function(theta, data, integrControl, logtr) {
.Call('smam_nllk_inc', PACKAGE = 'smam', theta, data, integrControl, logtr)
}

83 changes: 78 additions & 5 deletions R/bmme.R
Expand Up @@ -9,6 +9,37 @@
#### delta: vector of dim, measurement error sd, recyclable
#### Output:
#### a matrix of dim + 1 columns: time, location coordinates


#' Sampling from Brown Motion with Measurement Error
#'
#' Given the volatility parameters of a Brownian motion and normally
#' distributed measurement errors, generate the process at discretely
#' observed time points of a given dimension.
#'
#' @param time vector of time points at which observations are to be sampled
#' @param dim (integer) dimension of the Brownian motion
#' @param sigma volatility parameter (sd) of the Brownian motion
#' @param delta sd parameter of measurement error
#'
#' @return
#' A \code{data.frame} whose first column is the time points and whose
#' other columns are coordinates of the locations.
#'
#' @references
#' Pozdnyakov V., Meyer, TH., Wang, Y., and Yan, J. (2013)
#' On modeling animal movements using Brownian motion with measurement
#' error. Ecology 95(2): p247--253. doi:doi:10.1890/13-0532.1.
#'
#' @examples
#' tgrid <- seq(0, 10, length = 1001)
#' ## make it irregularly spaced
#' tgrid <- sort(sample(tgrid, 800))
#' dat <- rbmme(tgrid, 1, 1)
#' plot(dat[,1], dat[,2], xlab="t", ylab="X(t)", type="l")
#'
#' @export

rbmme <- function(time, dim = 2, sigma = 1, delta = 1) {
n <- length(time)
dat <- matrix(NA_real_, n, dim)
Expand All @@ -29,11 +60,12 @@ rbmme <- function(time, dim = 2, sigma = 1, delta = 1) {
#### param: vector of (sigma, delta)
#### Output:
#### a banded sparse covariance matrix of the increments

getSparseSigma <- function(tinc, param) {
n <- length(tinc)
d0 <- c(tinc * param[1]^2 + 2 * param[2]^2)
d1 <- rep(- param[2]^2, n - 1)
bandSparse(n, k = c(0, 1), diagonals = list(d0, d1), symmetric = TRUE)
Matrix::bandSparse(n, k = c(0, 1), diagonals = list(d0, d1), symmetric = TRUE)
}

#### multivariate density using sparse covariance matrix
Expand Down Expand Up @@ -92,12 +124,53 @@ bmme.start <- function(dat) {
#### Output
#### vector containing parameter estimate, standard error, and convergence code

fitBmme <- function(dat, start = NULL, method = "Nelder-Mead",

#' Fit a Brownian Motion with Measurement Error
#'
#' Given discretely observed animal movement locations, fit a Brownian
#' motion model with measurement errors.
#'
#' @param data a data.frame whose first column is the observation time, and other
#' columns are location coordinates.
#' @param start starting value of the model, a vector of two component, one for
#' sigma (sd of BM) and the other for delta (sd for measurement error).
#' If unspecified (NULL), a moment estimator will be used assuming equal
#' sigma and delta.
#' @param method the method argument to feed \code{optim}.
#' @param optim.control a list of control that is passed down to \code{optim}.
#'
#' @details
#' The joint density of the increment data is multivariate normal with a
#' sparse (tri-diagonal) covariance matrix. Sparse matrix operation from
#' package Matrix is used for computing efficiency in handling large data.
#'
#' @return
#' A list of the following components:
#' \item{estimate }{the esimated parameter vector}
#' \item{var.est }{variance matrix of the estimator}
#' \item{loglik }{loglikelihood evaluated at the estimate}
#' \item{convergence}{convergence code from optim}
#'
#' @references
#' Pozdnyakov V., Meyer, TH., Wang, Y., and Yan, J. (2013)
#' On modeling animal movements using Brownian motion with measurement
#' error. Ecology 95(2): p247--253. doi:doi:10.1890/13-0532.1.
#' @seealso
#' \code{\link{fitMovRes}}
#' @examples
#' set.seed(123)
#' tgrid <- seq(0, 500, by = 1)
#' dat <- rbmme(tgrid, sigma = 1, delta = 0.5)
#' fit <- fitBmme(dat)
#' fit
#' @export

fitBmme <- function(data, start = NULL, method = "Nelder-Mead",
optim.control = list()) {
if (is.null(start)) start <- bmme.start(dat)
dinc <- apply(dat, 2, diff)
if (is.null(start)) start <- bmme.start(data)
dinc <- apply(data, 2, diff)
fit <- optim(start, nllk.bmme, dinc = dinc, hessian = TRUE, method=method, control = optim.control)
## Sigma <- getSparseSigma(dat[,1], fit$par)
## Sigma <- getSparseSigma(data[,1], fit$par)
ans <- list(estimate = fit$par,
var.est = solve(fit$hessian),
loglik = - fit$value,
Expand Down

0 comments on commit 5abc903

Please sign in to comment.