Skip to content

Commit 753781f

Browse files
committed
update examples
1 parent a0c6e11 commit 753781f

10 files changed

Lines changed: 115 additions & 35 deletions

File tree

R/gen_prec_sbm.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#' }
6767
#'
6868
#' @param weight.paras A list
69-
#' (default = \code{list(c(shape = 1e4, rate = 1e2), c(min = 0, max = 5))})
69+
#' (default = \code{list(c(shape = 100, rate = 10), c(min = 0, max = 5))})
7070
#' specifying the parameters associated with \code{weight.dists}. It must follow
7171
#' the same length rules as \code{weight.dists}. Each element should be a named
7272
#' vector or list suitable for the corresponding sampler.
@@ -144,7 +144,7 @@ gen_prec_sbm <- function(d,
144144
prob.mat = NULL, within.prob = 0.25, between.prob = 0.05,
145145
weight.mat = NULL,
146146
weight.dists = list("gamma", "unif"),
147-
weight.paras = list(c(shape = 1e4, rate = 1e2),
147+
weight.paras = list(c(shape = 100, rate = 10),
148148
c(min = 0, max = 5)),
149149
cond.target = 100) {
150150

R/performance.R

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#' (typically true) precision matrix.
1212
#'
1313
#' @return
14-
#' A data frame with one row per performance metric and two columns:
14+
#' A data frame of S3 class \code{"performance"}, with one row per performance
15+
#' metric and two columns:
1516
#' \describe{
1617
#' \item{measure}{The name of each performance metric. The reported metrics
1718
#' include: sparsity, Frobenius norm loss, Kullback-Leibler divergence,
@@ -89,6 +90,9 @@
8990
#' \tab Negative predictive value (NPV) = TN / (TN + FN) = 1 - FOR \tab \tab \cr
9091
#' }
9192
#'
93+
#' @example
94+
#' inst/example/ex-performance.R
95+
#'
9296
#' @export
9397

9498
performance <- function(hatOmega, Omega) {
@@ -143,7 +147,7 @@ performance <- function(hatOmega, Omega) {
143147
## FPR <- FP / sum(Omega_edge == 0)
144148
F1 <- 2 * TP / (2*TP + FN + FP)
145149
## F1 <- 2 * precision * recall / (precision + recall)
146-
MCC <- (TP * TN - FP * FN) / sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN))
150+
MCC <- (TP * TN - FP * FN) / (sqrt(TP + FP) * sqrt(TP + FN) * sqrt(TN + FP) * sqrt(TN + FN))
147151
## MCC <- (TP * TN - FP * FN) / sqrt(
148152
## sum(hatOmega_edge != 0) * sum(Omega_edge != 0) * sum(Omega_edge == 0) * sum(hatOmega_edge == 0)
149153
## )
@@ -154,5 +158,14 @@ performance <- function(hatOmega, Omega) {
154158
value = c(sparsity, FL, KL, QL, SL,
155159
TP, TN, FP, FN, TPR, FPR, F1, MCC)
156160
)
161+
class(result) <- c("performance", class(result))
157162
return(result)
158163
}
164+
165+
166+
#' @noRd
167+
168+
print.performance <- function(x, ...) {
169+
x$value <- round(x$value, 4)
170+
NextMethod("print", x)
171+
}

inst/example/ex-gen_prec_sbm.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ set.seed(1234)
66
## block-structured precision matrix based on SBM
77
#### case 1: base R distribution
88
sim1 <- gen_prec_sbm(d = 100, K = 5,
9-
within.prob = 0.5, between.prob = 0.05,
9+
within.prob = 0.25, between.prob = 0.1,
1010
weight.dists = list("gamma", "unif"),
11-
weight.paras = list(c(shape = 20, scale = 5),
12-
c(min = 0, max = 1)),
11+
weight.paras = list(c(shape = 100, scale = 1e2),
12+
c(min = 0, max = 10)),
1313
cond.target = 100)
1414
#### visualization
1515
plot(sim1)
1616

1717
#### case 2: user-defined sampler
1818
my_gamma <- function(n) {
19-
rgamma(n, shape = 10, scale = 5)
19+
rgamma(n, shape = 1e4, scale = 1e2)
2020
}
2121
sim2 <- gen_prec_sbm(d = 100, K = 5,
22-
within.prob = 0.5, between.prob = 0.05,
22+
within.prob = 0.2, between.prob = 0.05,
2323
weight.dists = list(my_gamma, "unif"),
2424
weight.paras = list(NULL,
2525
c(min = 0, max = 1)),

inst/example/ex-grasps.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ library(grasps)
44
set.seed(1234)
55

66
## block-structured precision matrix based on SBM
7-
sim <- gen_prec_sbm(d = 60, K = 3,
8-
within.prob = 0.5, between.prob = 0.05,
7+
sim <- gen_prec_sbm(d = 30, K = 3,
8+
within.prob = 0.25, between.prob = 0.05,
99
weight.dists = list("gamma", "unif"),
10-
weight.paras = list(c(shape = 20, scale = 5), c(min = 0, max = 1)),
10+
weight.paras = list(c(shape = 100, rate = 10),
11+
c(min = 0, max = 5)),
1112
cond.target = 100)
1213
## visualization
1314
plot(sim)
1415

1516
## n-by-d data matrix
1617
library(MASS)
17-
X <- mvrnorm(n = 30, mu = rep(0, 60), Sigma = sim$Sigma)
18+
X <- mvrnorm(n = 20, mu = rep(0, 30), Sigma = sim$Sigma)
1819

19-
## lasso, BIC
20-
res <- grasps(X = X, membership = sim$membership, penalty = "lasso", crit = "BIC")
20+
## adapt, BIC
21+
res <- grasps(X = X, membership = sim$membership, penalty = "adapt", crit = "BIC")
2122

2223
## visualization
2324
plot(res)
25+
26+
## performance
27+
performance(hatOmega = res$hatOmega, Omega = sim$Omega)

inst/example/ex-performance.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library(grasps)
2+
3+
## reproducibility for everything
4+
set.seed(1234)
5+
6+
## block-structured precision matrix based on SBM
7+
sim <- gen_prec_sbm(d = 30, K = 3,
8+
within.prob = 0.25, between.prob = 0.05,
9+
weight.dists = list("gamma", "unif"),
10+
weight.paras = list(c(shape = 10, scale = 5),
11+
c(min = 0, max = 5)),
12+
cond.target = 100)
13+
## visualization
14+
plot(sim)
15+
16+
## n-by-d data matrix
17+
library(MASS)
18+
X <- mvrnorm(n = 20, mu = rep(0, 30), Sigma = sim$Sigma)
19+
20+
## adapt, BIC
21+
res <- grasps(X = X, membership = sim$membership, penalty = "adapt", crit = "BIC")
22+
23+
## visualization
24+
plot(res)
25+
26+
## performance
27+
performance(hatOmega = res$hatOmega, Omega = sim$Omega)

inst/example/ex-plot.blkmat.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ library(grasps)
44
set.seed(1234)
55

66
## block-structured precision matrix based on SBM
7-
sim <- gen_prec_sbm(d = 100, K = 5,
8-
within.prob = 0.5, between.prob = 0.05,
7+
sim <- gen_prec_sbm(d = 100, K = 10,
8+
within.prob = 0.2, between.prob = 0.05,
99
weight.dists = list("gamma", "unif"),
10-
weight.paras = list(c(shape = 20, scale = 5), c(min = 0, max = 1)),
10+
weight.paras = list(c(shape = 100, scale = 10),
11+
c(min = 0, max = 5)),
1112
cond.target = 100)
1213

1314
## visualization

man/gen_prec_sbm.Rd

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

man/grasps.Rd

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

man/performance.Rd

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot.blkmat.Rd

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

0 commit comments

Comments
 (0)