Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor speed-up to logistic code when distance is a covariate
- Loading branch information
David Lawrence Miller
committed
Jul 15, 2015
1 parent
5f893ed
commit 49da34a
Showing
7 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
integratelogisticdup <- function(x1, x2, models, beta, lower=0, width, point){ | ||
# numerical integral of product of logistic detection functions | ||
|
||
integrate(logisticdupbyx, lower=lower, upper=width, | ||
subdivisions=10, rel.tol=0.01, abs.tol=0.01, | ||
x1=x1, x2=x2, models=models, beta=beta, point=point)$value | ||
# computation speed-up when there is distance in the formula | ||
# but only if there are no interactions with distance | ||
if(sum(grepl("distance",names(beta)))==1){ | ||
|
||
# set parameter to be zero for distance | ||
beta_distance <- beta[grepl("distance",names(beta))] | ||
beta[grepl("distance",names(beta))] <- 0 | ||
|
||
# calculate the rest of the linear predictor | ||
x1 <- setcov(x1,models$g0model)%*%beta | ||
x2 <- setcov(x2,models$g0model)%*%beta | ||
|
||
# do some integration | ||
integrate(logisticdupbyx_fast, lower=lower, upper=width, | ||
subdivisions=10, rel.tol=0.01, abs.tol=0.01, | ||
x1=x1, x2=x2, models=models, beta=beta, point=point, | ||
beta_distance=beta_distance)$value | ||
}else{ | ||
# Otherwise just go ahead and do the numerical integration | ||
integrate(logisticdupbyx, lower=lower, upper=width, | ||
subdivisions=10, rel.tol=0.01, abs.tol=0.01, | ||
x1=x1, x2=x2, models=models, beta=beta, point=point)$value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' Logistic for duplicates as a function of covariates (fast) | ||
#' | ||
#' As \code{\link{logisticdupbyx}}, but faster when distance is a covariate (but no interactions with distance occur. | ||
#' | ||
#' @inheritParams logisticdupbyx | ||
#' @param beta_distance parameter for distance | ||
#' @param x1 linear predictor for 1, without distance | ||
#' @param x2 linear predictor for 2, without distance | ||
#' @author David L Miller | ||
logisticdupbyx_fast <- function(distance, x1, x2, models, beta, point, beta_distance){ | ||
|
||
# function to calculate p/(1+p) | ||
ologit <- function(p) p/(1+p) | ||
|
||
# first part of the function | ||
gx1 <- ologit(exp(x1 + distance*beta_distance)) | ||
|
||
# calculate second and return | ||
if(!point){ | ||
return(gx1 * ologit(exp(x2 + distance*beta_distance))) | ||
}else{ | ||
return(gx1 * ologit(exp(x2 + distance*beta_distance))*2*distance) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
% Generated by roxygen2 (4.1.1): do not edit by hand | ||
% Please edit documentation in R/logisticdupbyx_fast.R | ||
\name{logisticdupbyx_fast} | ||
\alias{logisticdupbyx_fast} | ||
\title{Logistic for duplicates as a function of covariates (fast)} | ||
\usage{ | ||
logisticdupbyx_fast(distance, x1, x2, models, beta, point, beta_distance) | ||
} | ||
\arguments{ | ||
\item{distance}{vector of distance values} | ||
|
||
\item{x1}{linear predictor for 1, without distance} | ||
|
||
\item{x2}{linear predictor for 2, without distance} | ||
|
||
\item{models}{model list} | ||
|
||
\item{beta}{logistic parameters} | ||
|
||
\item{point}{\code{TRUE} for point transect data} | ||
|
||
\item{beta_distance}{parameter for distance} | ||
} | ||
\description{ | ||
As \code{\link{logisticdupbyx}}, but faster when distance is a covariate (but no interactions with distance occur. | ||
} | ||
\author{ | ||
David L Miller | ||
} | ||
|