Skip to content

Commit 17c474f

Browse files
author
Raphael Sonabend
authored
Merge pull request #165 from alan-turing-institute/dev
Added c.Distribution
2 parents ab18e51 + 9904520 commit 17c474f

File tree

10 files changed

+200
-13
lines changed

10 files changed

+200
-13
lines changed

.appveyor.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ environment:
5656
- R_VERSION: release
5757
R_ARCH: x64
5858

59-
- R_VERSION: devel
60-
R_ARCH: x64
59+
# - R_VERSION: devel
60+
# R_ARCH: x64
6161

62-
- R_VERSION: oldrel
63-
R_ARCH: x64
62+
# - R_VERSION: oldrel
63+
# R_ARCH: x64
6464

6565
cache:
6666
- C:\RLibrary\

.travis.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ branches:
77

88
matrix:
99
include:
10+
# - os: linux
11+
# r: oldrel
1012
- os: linux
11-
r: oldrel
12-
- os: linux
13-
r: release
14-
- os: linux
15-
r: devel
16-
- os: osx
17-
r: oldrel
18-
- os: osx
1913
r: release
14+
# - os: linux
15+
# r: devel
16+
# - os: osx
17+
# r: oldrel
18+
# - os: osx
19+
# r: release
2020
# - os: osx
2121
# r: devel
2222

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: distr6
22
Title: The Complete R6 Probability Distributions Interface
3-
Version: 1.3.0
3+
Version: 1.3.1
44
Authors@R:
55
c(person(given = "Raphael",
66
family = "Sonabend",
@@ -170,6 +170,7 @@ Collate:
170170
'Wrapper_TruncatedDistribution.R'
171171
'Wrapper_VectorDistribution.R'
172172
'assertions.R'
173+
'c.Distribution.R'
173174
'decomposeMixture.R'
174175
'decorate.R'
175176
'distr6-deprecated.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ S3method("[",VectorDistribution)
77
S3method("^",SetInterval)
88
S3method(as.ParameterSet,data.table)
99
S3method(as.ParameterSet,list)
10+
S3method(c,Distribution)
1011
S3method(huberize,Distribution)
1112
S3method(lines,Distribution)
1213
S3method(mean,Distribution)

NEWS.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
# distr6 1.3.1
2+
3+
## Major Updates
4+
5+
- None
6+
7+
## Minor Updates
8+
9+
### Added Functions and Classes
10+
11+
- None
12+
13+
### Deprecated Functions
14+
15+
- None
16+
17+
### Updated Functions
18+
19+
- None
20+
21+
## Patches
22+
23+
- Added wrapper for VectorDistribution for quick concatenation of constructed distributions, `c.Distribution`
24+
- `VectorDistribution` `print` method more in line with base R vectors
25+
26+
127
# distr6 1.3.0
228

329
## Major Updates

R/Wrapper_VectorDistribution.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ VectorDistribution$set("public","modelTable", function(){
257257
private$.wrappedModels
258258
})
259259

260+
VectorDistribution$set("public", "strprint", function(n = 100){
261+
names <- as.character(self$modelTable()$shortname)
262+
lng <- length(names)
263+
if(lng >(2*n))
264+
names = c(names[1:n], "...", names[(lng-n+1):lng])
265+
266+
return(names)
267+
})
260268
VectorDistribution$set("public", "getParameterValue", function(...){
261269
message("Vector Distribution should not be used to get/set parameters. Try to use '[' first.")
262270
return(NULL)

R/c.Distribution.R

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#' @title Combine Distributions into a VectorDistribution
2+
#' @description Helper function for quickly combining distributions into a \code{\link{VectorDistribution}}.
3+
#' @param ... distributions to be concatenated.
4+
#' @param name,short_name,description,decorators See \code{\link{VectorDistribution}}
5+
#' @return A VectorDistribution
6+
#' @seealso \code{\link{VectorDistribution}}
7+
#' @examples
8+
#' # Construct and combine
9+
#' c(Binomial$new(), Normal$new())
10+
#'
11+
#' # More complicated distributions
12+
#' b = truncate(Binomial$new(), 2, 6)
13+
#' n = huberize(Normal$new(), -1, 1)
14+
#' c(b, n)
15+
#'
16+
#' # Concatenate VectorDistributions
17+
#' v1 = VectorDistribution$new(list(Binomial$new(), Normal$new()))
18+
#' v2 = VectorDistribution$new(distribution = "Gamma",
19+
#' params = data.table::data.table(shape = 1:2, rate = 1:2))
20+
#' c(v1, v2)
21+
#'
22+
#' @export
23+
c.Distribution <- function(..., name = NULL, short_name = NULL, description = NULL, decorators = NULL){
24+
# Get list of inputs and assert all distributions
25+
distlist = list(...)
26+
assertDistributionList(distlist)
27+
28+
# If all distributions in the list are VectorDistributions then try and return a VectorDistribution
29+
# with distribution/params constructor.
30+
if (all(sapply(distlist, getR6Class) %in% "VectorDistribution")) {
31+
if (any(sapply(distlist, function(x) x$distlist)))
32+
return(VectorDistribution$new(unlist(lapply(distlist, function(x) x$wrappedModels()))))
33+
else {
34+
distribution = unlist(lapply(distlist, function(x) as.character(unlist(x$modelTable()$distribution))))
35+
params = lapply(distlist, function(x) x$modelTable()$params)
36+
if (length(params) != length(distribution))
37+
params = unlist(params, FALSE)
38+
return(VectorDistribution$new(distribution = distribution, params = params))
39+
}
40+
}
41+
42+
# If any are VectorDistributions then get the wrapped list
43+
distlist = unlist(lapply(distlist, function(x)
44+
ifelse(getR6Class(x) == "VectorDistribution", list(x$wrappedModels()), list(x))))
45+
46+
# Create VectorDistribution
47+
return(VectorDistribution$new(distlist, name = name, short_name = short_name,
48+
description = description, decorators = decorators))
49+
}

man/c.Distribution.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-VectorDistribution.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ test_that("shared d/p/q/r",{
138138
Binom3 = qbinom(0.42,6,0.2)))
139139
})
140140

141+
test_that("print",{
142+
a = VectorDistribution$new(distribution = "Binomial", params = list(list(prob = 0.1, size = 2),
143+
list(prob = 0.6, size = 4),
144+
list(prob = 0.2, size = 6)))
145+
expect_equal(a$strprint(), c("Binom1","Binom2","Binom3"))
146+
expect_equal(a$strprint(1), c("Binom1","...","Binom3"))
147+
expect_output(a$print(), "Binom1")
148+
})
149+

tests/testthat/test-cDistribution.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
library(testthat)
2+
3+
context("c.Distribution")
4+
5+
test_that("non-distlist",{
6+
expect_error(c(Binomial$new(), Binomial), "One or more...")
7+
expect_silent(expect_length(c(Binomial, Binomial$new()), 2))
8+
})
9+
10+
test_that("SDistributions",{
11+
expect_silent(c(Binomial$new(), Normal$new()))
12+
expect_equal(getR6Class(c(Binomial$new(), Normal$new())), "VectorDistribution")
13+
expect_equal(c(Binomial$new(), Normal$new())$short_name, "BinomVecNorm")
14+
})
15+
16+
test_that("VectorDistributions",{
17+
v1 = VectorDistribution$new(list(Binomial$new(), Normal$new()))
18+
v2 = VectorDistribution$new(distribution = "Gamma", params = data.table::data.table(shape = 1:2, rate = 1:2))
19+
expect_silent(c(v1, v2))
20+
expect_silent(c(v1, v2, Normal$new(), truncate(Binomial$new(), 2, 6)))
21+
})
22+
23+
test_that("distribution/param VectorDistributions",{
24+
v1 = VectorDistribution$new(distribution = c("Binomial","Normal"),
25+
params = list(list(size = 2), list(mean = 0, var = 2)))
26+
v2 = VectorDistribution$new(distribution = "Gamma", params = data.table::data.table(shape = 1:2, rate = 1:2))
27+
expect_silent(c(v1, v2))
28+
v3 = c(v1, v2)
29+
expect_false(v3$distlist)
30+
expect_equal(v3$modelTable()$distribution, c("Binomial","Normal","Gamma","Gamma"))
31+
expect_equal(v3$modelTable()$shortname, c("Binom1","Norm1","Gamma1","Gamma2"))
32+
expect_equal(v3$modelTable()$params, list(list(size = 2), list(mean = 0, var = 2),
33+
list(shape = 1, rate = 1), list(shape = 2, rate = 2)))
34+
})
35+
36+
test_that("weighteddiscrete vec",{
37+
v1 = VectorDistribution$new(distribution = "WeightedDiscrete",
38+
params = list(data = data.frame(x = 1, pdf = 1),
39+
data = data.frame(x = 2, pdf = 1)))
40+
v2 = VectorDistribution$new(distribution = "WeightedDiscrete",
41+
params = list(data = data.frame(x = 3, pdf = 1),
42+
data = data.frame(x = 4, pdf = 1)))
43+
expect_silent(c(v1, v2))
44+
})
45+
46+
test_that("different lengths",{
47+
v1 = VectorDistribution$new(distribution = "WeightedDiscrete",
48+
params = list(list(data = data.frame(x = 1, pdf = 1),
49+
data = data.frame(x = 2, pdf = 1))))
50+
v2 = VectorDistribution$new(distribution = "WeightedDiscrete",
51+
params = list(data = data.frame(x = 3, pdf = 1),
52+
data = data.frame(x = 4, pdf = 1)))
53+
expect_silent(c(v1, v2))
54+
})

0 commit comments

Comments
 (0)