Skip to content

Commit dff9bc1

Browse files
committed
Add ability to pass moments in directly to mommb. Need to change documentation and add tests.
1 parent 5f1f2cb commit dff9bc1

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

R/mom.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
# Copyright Avraham Adler (c) 2024
22
# SPDX-License-Identifier: MPL-2.0+
33

4-
mommb <- function(x, maxit = 100L, tol = .Machine$double.eps ^ 0.5,
4+
mommb <- function(x, m = FALSE, maxit = 100L, tol = .Machine$double.eps ^ 0.5,
55
na.rm = TRUE) {
66

7-
if (!na.rm && anyNA(x)) {
8-
stop("There are NAs in the data yet na.rm was passed as FALSE.")
7+
if (m) {
8+
if (length(x) != 2L) {
9+
stop("Was expecting first and second moment but something other than 2 ",
10+
"parameters were passed.")
11+
}
12+
13+
mu <- x[1L]
14+
mu2 <- x[2L]
15+
} else {
16+
if (!na.rm && anyNA(x)) {
17+
stop("There are NAs in the data yet na.rm was passed as FALSE.")
18+
}
19+
20+
mu <- mean(x, na.rm = na.rm)
21+
mu2 <- mean(x ^ 2, na.rm = na.rm)
922
}
1023

1124
findb <- function(mu, g, tol) {
@@ -25,8 +38,6 @@ mommb <- function(x, maxit = 100L, tol = .Machine$double.eps ^ 0.5,
2538
}
2639
}
2740

28-
mu <- mean(x, na.rm = na.rm)
29-
mu2 <- mean(x ^ 2, na.rm = na.rm)
3041
g <- 1 / mu2
3142
b <- findb(mu, g, tol)
3243
converged <- FALSE

0 commit comments

Comments
 (0)