Skip to content

Commit

Permalink
fix varianceBySigma bracket error
Browse files Browse the repository at this point in the history
  • Loading branch information
bgctw committed Mar 12, 2019
1 parent 7d942e8 commit 05fa662
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 11 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
^CRAN-RELEASE$
^.*\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ vignettes/*.pdf
.Rproj.user
*.Rproj
README.html
CRAN-RELEASE
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Suggests: testthat, knitr, dplyr, ggplot2, mvtnorm, purrr, tidyr
VignetteBuilder: knitr
License: GPL-2
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
URL: https://github.com/bgctw/lognorm
8 changes: 4 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- Sum of lognormals: if all values are gap-filled, assume multiplicative
standard deviation (sigmaStar) of the sum to be the mean of given sigmaStar.
Before took it assumed to be the maximum. But this led to larger confidence
boundsas compared to using the normal assumption.
bounds compared to using the normal assumption.

- Estimate parameters from various statistics, such as mean and an
upper quantile value, i.e. a practical maximum.
Expand All @@ -15,12 +15,12 @@ function `seCor`.

# 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.
estimateSumLognormal by default now returns NA if there are NA values in terms.
New argument na.rm = TRUE allows for previous behavior of neglecting those terms.

# lognorm 0.1.2

- Computate of effective number of observations
- Compute effective number of observations
- fixed bracketing bug in formula
- better dealing with NA at begin or end

Expand Down
2 changes: 1 addition & 1 deletion R/autocorr.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ seCor <- function(
##details<< Computation follows
## https://stats.stackexchange.com/questions/274635/calculating-error-of-mean-of-time-series.
##
##details<< The default uses empiricial autocorrelation
##details<< The default uses empirical autocorrelation
## estimates from the supplied data up to first negative component.
## For short series of \code{x} it is strongly recommended to to
## provide \code{effCov} that was estimated on a longer time series.
Expand Down
8 changes: 4 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## Note
Added support for the common use case of computing standard error of an
autocorrelated time series.
Added support for computing moments and statistics.

## Test environments
* local R 3.4.4 on Mint17 64bit
* Ubuntu 14.04.5 LTS (on travis-ci), 14.04
* win-builder (devel)
* R-Hub

## R CMD check results
There were no ERRORs nor WARNINGs

One note on Possibly mis-spelled words in DESCRIPTION
caused by correct "et al." of a citation.
Note on wrong doi: I checked that the doi correctly resolves
http://dx.doi.org/10.1641%2F0006-3568(2001)051[0341%3Alndats]2.0.co%3B2



Expand Down
60 changes: 60 additions & 0 deletions inst/docu/varianceBySigmaAndExpected.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: "testing variance computation with expected value and sigma"
output: html_notebook
---

Formula for Variance given sigma at log scale and expected value

We start with formulas for variance and mean.
$$
V = (e^{\sigma^2} - 1) e^{2\mu + \sigma^2} \\
m = e^{\mu + \sigma^2/2}
$$
mean formula resolved for $\mu$ and substituted into V.
$$
\mu = log_m - \sigma^2/2 \\
V = (e^{\sigma^2} - 1) e^{2(log_m - \sigma^2/2) + \sigma^2} \\
V = (e^{\sigma^2} - 1) e^{2log_m - \sigma^2 + \sigma^2} \\
V = (e^{\sigma^2} - 1) e^{2log_m} \\
V = (e^{\sigma^2} - 1) (e^{log_m})^2 \\
V = (e^{\sigma^2} - 1) m^2 \\
$$


```{r}
n = 1e4
sigma = log(1.2)
#sigma = log(1.41)
mu = log(10)
logR = rnorm(n, mu, sigma)
R = exp(logR)
meanR = mean(R)
sdR = sd(R)
V2 = (exp(sigma^2) - 1)*exp(2*mu + sigma^2)
#
m = exp(mu + sigma^2/2)
V = (exp(sigma^2) - 1)*m^2
c(meanR, m)
c(sdR, sqrt(V), sqrt(V2))
```
```{r}
xPred <- seq(-2,22,length.out = 101)
plot(density(R), xlim = c(-2,22), lty = "dotted")
abline(v = meanR)
lines(dnorm(xPred, m, sqrt(V))~xPred, col = "blue", lty = "dashed")
lines(dlnorm(xPred, mu, sigma)~xPred, col = "green")
```
```{r}
df <- data.frame(nu = c(0.05,0.1,0.2,0.5,1,2,5,10,20))
df$sigmaStar = sqrt(df$nu^2 + 1)
df
```
```{r}
plot(sigmaStar ~ nu, df[1:3,])
```

```{r}
sigmaStar <- 1.2
(nu <- sqrt(sigmaStar^2 - 1))
```

372 changes: 372 additions & 0 deletions inst/docu/varianceBySigmaAndExpected.nb.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion man/seCor.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ first entry is the variance. See \code{\link{computeEffectiveAutoCorr}}}
https://stats.stackexchange.com/questions/274635/calculating-error-of-mean-of-time-series.


The default uses empiricial autocorrelation
The default uses empirical autocorrelation
estimates from the supplied data up to first negative component.
For short series of \code{x} it is strongly recommended to to
provide \code{effCov} that was estimated on a longer time series.}
Expand Down

0 comments on commit 05fa662

Please sign in to comment.