Skip to content

Commit

Permalink
Merge pull request #8 from bgctw/issue07
Browse files Browse the repository at this point in the history
Issue07
  • Loading branch information
bgctw authored Sep 7, 2018
2 parents e9566ae + ff5dcf5 commit 12e1c41
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: lognorm
Title: Functions for the Lognormal Distribution
Version: 0.1.2
Version: 0.1.3
Author: Thomas Wutzler
Maintainer: Thomas Wutzler <[email protected]>
Description: Provides Moments and other statistics of the lognormal distribution.
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# lognorm 0.1.3

estimateSumLognormal by default now returns NA if there are NA values in terms.
New argument na.rm = TRUE allows for previous behaviour of neglecting those terms.

# lognorm 0.1.2

- computation of effective number of observations
Expand Down
5 changes: 3 additions & 2 deletions R/autocorr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
computeEffectiveNumObs <- function(
### compute the effective number of observations taking into account autocorrelation
res ##<< numeric of autocorrelated numbers, usually observation - model residuals
, effAcf = computeEffectiveAutoCorr(res) ##<< may provide precomputed for efficiency
## the first entry is fixed at 1 for zero distance
, effAcf = computeEffectiveAutoCorr(res) ##<< autocorrelation coefficients.
## The first entry is fixed at 1 for zero distance.
## May provide precomputed for efficiency or computed from a larger sample.
, na.rm = FALSE ##<< a logical value indicating whether NA values should be
## stripped before the computation proceeds.
){
Expand Down
15 changes: 11 additions & 4 deletions R/lognormalSum.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ estimateSumLognormalSample <- function(
## of resLog is too small) set to 1 to assume uncorrelated sample
, isGapFilled = logical(0) ##<< logical vector whether entry is gap-filled
## rather than an original measurement, see details
, na.rm = TRUE ##<< neglect terms with NA vlaues in mu or sigma
){
# only one term, return the parameters
if (length(mu) == 1 ) return(
c(mu = as.vector(mu), sigma = as.vector(sigma), nEff = 1)
)
if (length(sigma) == 1) sigma <- rep(sigma, length(mu))
nEff <- computeEffectiveNumObs(resLog, effAcf = effAcf)
nEff <- computeEffectiveNumObs(resLog, effAcf = effAcf, na.rm = na.rm)
##details<<
## If there are no gap-filled values, i.e. \code{all(!isGapFilled)} or
## \code{!length(isGapFilled)} (the default), distribution parameters
Expand All @@ -39,19 +40,20 @@ estimateSumLognormalSample <- function(
ans1 <- estimateSumLognormalSample(
muMeas, sigmaMeas, resLogMeas
,effAcf = effAcf, isGapFilled = logical(0)
,na.rm = na.rm
)
ans1["sigma"]
}
p <- estimateSumLognormal(
# sigma will not be used only checked for is.finite()
mu, sigma = sigma, sigmaSum = sigmaSum, effAcf = effAcf)
mu, sigma = sigma, sigmaSum = sigmaSum, effAcf = effAcf, na.rm = na.rm)
return(c(p , nEff = nEff))
}
##value<< numeric vector with components "mu", "sigma", and "nEff"
## the parameters of the lognormal distribution at log scale
## (Result of \code{link{estimateSumLognormal}})
## and the number of effective observations.
p <- estimateSumLognormal( mu, sigma, effAcf = effAcf)
p <- estimateSumLognormal( mu, sigma, effAcf = effAcf, na.rm = na.rm)
return(c(p , nEff = nEff))
}

Expand All @@ -65,7 +67,7 @@ estimateSumLognormal <- function(
## of correlations between the random variables
, sigmaSum = numeric(0) ##<< numeric scalar: possibility to specify
## of a precomputed scale parameter
, corrLength = if(inherits(corr, "ddiMatrix")) 0 else nTerm ##<< integer
, corrLength = if (inherits(corr, "ddiMatrix")) 0 else nTerm ##<< integer
## scalar: set correlation length to smaller values
## to speed up computation by neglecting correlations among terms
## further apart.
Expand All @@ -75,6 +77,9 @@ estimateSumLognormal <- function(
## Set this to TRUE to issue an error instead.
, effAcf ##<< numeric vector of effective autocorrelation
## This overides arguments \code{corr} and \code{corrLength}
, na.rm = isStopOnNoTerm ##<< if there are terms with NA values in mu or sigma
## by default also the sum coefficients are NA. Set to TRUE to
## neglect such terms in the sum.
){
##references<<
## Lo C (2013) WKB approximation for the sum of two correlated lognormal
Expand All @@ -84,6 +89,8 @@ estimateSumLognormal <- function(
lengthMu <- length(mu)
if (length(sigma) == 1) sigma <- rep(sigma, lengthMu)
iFinite <- which( is.finite(mu) & is.finite(sigma))
if (!isTRUE(na.rm) && length(iFinite) != lengthMu)
return(c(mu = NA_real_, sigma = NA_real_))
muFin <- mu[iFinite]
sigmaFin <- sigma[iFinite]
nTerm = length(muFin)
Expand Down
5 changes: 3 additions & 2 deletions man/computeEffectiveNumObs.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
na.rm = FALSE)}
\arguments{
\item{res}{numeric of autocorrelated numbers, usually observation - model residuals}
\item{effAcf}{may provide precomputed for efficiency
the first entry is fixed at 1 for zero distance}
\item{effAcf}{autocorrelation coefficients.
The first entry is fixed at 1 for zero distance.
May provide precomputed for efficiency or computed from a larger sample.}
\item{na.rm}{a logical value indicating whether NA values should be
stripped before the computation proceeds. }
}
Expand Down
5 changes: 4 additions & 1 deletion man/estimateSumLognormal.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\usage{estimateSumLognormal(mu, sigma, corr = Diagonal(length(mu)),
sigmaSum = numeric(0), corrLength = if (inherits(corr,
"ddiMatrix")) 0 else nTerm, isStopOnNoTerm = FALSE,
effAcf)}
effAcf, na.rm = isStopOnNoTerm)}
\arguments{
\item{mu}{numeric vector of center parameters of terms at log scale}
\item{sigma}{numeric vector of variance parameter of terms at log scale}
Expand All @@ -23,6 +23,9 @@ default, NA is returned for the sum.
Set this to TRUE to issue an error instead.}
\item{effAcf}{numeric vector of effective autocorrelation
This overides arguments \code{corr} and \code{corrLength}}
\item{na.rm}{if there are terms with NA values in mu or sigma
by default also the sum coefficients are NA. Set to TRUE to
neglect such terms in the sum.}
}

\value{numeric vector with two components mu and sigma
Expand Down
3 changes: 2 additions & 1 deletion man/estimateSumLognormalSample.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
\description{Estimate the parameters of the lognormal approximation to the sum}
\usage{estimateSumLognormalSample(mu, sigma, resLog,
effAcf = computeEffectiveAutoCorr(resLog),
isGapFilled = logical(0))}
isGapFilled = logical(0), na.rm = TRUE)}
\arguments{
\item{mu}{numeric vector of center parameters of terms at log scale}
\item{sigma}{numeric vector of variance parameter of terms at log scale}
Expand All @@ -15,6 +15,7 @@ coefficients (may provide precomputed for efficiency or if the sample
of resLog is too small) set to 1 to assume uncorrelated sample}
\item{isGapFilled}{logical vector whether entry is gap-filled
rather than an original measurement, see details}
\item{na.rm}{neglect terms with NA vlaues in mu or sigma}
}
\details{If there are no gap-filled values, i.e. \code{all(!isGapFilled)} or
\code{!length(isGapFilled)} (the default), distribution parameters
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_sumLognormal.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.tmp.f <- function(){
require(testthat)
#
require(Matrix)
require(tidyr)
require(dplyr)
require(ggplot2)
Expand Down Expand Up @@ -103,13 +104,17 @@ test_that("estimateSumLognormal few terms",{
sigma = log(rep(1.2, length(mu)))
# no finite case
sigma0 <- sigma; sigma0[] <- NA
coefSum <- estimateSumLognormal(mu, sigma0, na.rm = TRUE)
expect_equal(coefSum, c(mu = NA_real_, sigma = NA_real_))
coefSum <- estimateSumLognormal(mu, sigma0)
expect_equal(coefSum, c(mu = NA_real_, sigma = NA_real_))
expect_error(
coefSum <- estimateSumLognormal(mu, sigma0, isStopOnNoTerm = TRUE))
# one finite case
sigma1 <- sigma; sigma1[-1] <- NA
coefSum <- estimateSumLognormal(mu, sigma1)
expect_equal(coefSum, c(mu = NA_real_, sigma = NA_real_))
coefSum <- estimateSumLognormal(mu, sigma1, na.rm = TRUE)
expect_equal(coefSum, c(mu = mu[1], sigma = sigma1[1]))
})

Expand Down

0 comments on commit 12e1c41

Please sign in to comment.