-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
84 lines (67 loc) · 2.38 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
output: github_document
---
<!--
README.md is generated from README.Rmd. Please edit that file
rmarkdown::render("README.Rmd")
-->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "tools/README-"
)
```
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/lognorm)](http://cran.r-project.org/package=lognorm)
[![Travis-CI Build Status](https://travis-ci.org/bgctw/lognorm.svg?branch=master)](https://travis-ci.org/bgctw/lognorm)
## Overview
The `lognorm` package provides support for the univariate
[lognormal
distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).
It helps
- estimating distribution parameters from observations statistics
- computing moments and other statistics
- approximate the sum of several lognormally distributed random variables
## Installation
```{r, eval = FALSE}
# From CRAN
install.packages("lognorm")
# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("bgctw/lognorm")
```
## Usage
A simple example computes the distribution parameters of the sum of
two correlated lognormal parameters.
```{r example}
require(lognorm)
means <- c(110,100)
sigmaStar <- c(1.5,1.5)
corr <- setMatrixOffDiagonals(diag(nrow = 2), value = 0.6, isSymmetric = TRUE)
#
# estimate paramters of terms
coefTerms <- getParmsLognormForExpval(means, sigmaStar)
# approximate sum of the two correlated term
coefSum <- estimateSumLognormal( coefTerms[,"mu"], coefTerms[,"sigma"], corr = corr )
#
# plot statistics of distribution
x <- seq(50,500,length.out = 100)
density <- dlnorm(x, coefSum["mu"], coefSum["sigma"])
plot(density ~ x, type = "l")
# confidence intervals
abline(v = qlnorm(c(0.025, 0.975), coefSum["mu"], coefSum["sigma"]), lty = "dotted")
# expected value
abline(v = getLognormMoments(coefSum["mu"], coefSum["sigma"])[1,"mean"])
# mode
abline(v = getLognormMode(coefSum["mu"], coefSum["sigma"]), lty = "dashed")
# median
abline(v = getLognormMedian(coefSum["mu"], coefSum["sigma"]), lty = "dotdash")
```
The sum of the expected values is conserved, while the multiplicative standard
deviation decreases during aggregation:
```{r}
c(
mean = unname(getLognormMoments(coefSum["mu"], coefSum["sigma"])[1,"mean"])
, sigmaStar = unname(exp(coefSum["sigma"])))
```
See the [package vignettes](https://github.com/bgctw/lognorm/tree/master/vignettes) (*.md) for further examples.