Skip to content

Commit 0de1677

Browse files
committed
inst/example
1 parent 025566f commit 0de1677

19 files changed

Lines changed: 147 additions & 109 deletions

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ RoxygenNote: 7.3.3
4242
RdMacros: Rdpack
4343
Suggests:
4444
knitr,
45+
MASS,
4546
rmarkdown
4647
VignetteBuilder: knitr

R/draw_sample.R

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,8 @@
3333
#' @return
3434
#' A numeric vector of length \eqn{n} containing simulated data.
3535
#'
36-
#' @examples
37-
#' ## reproducibility for everything
38-
#' set.seed(1234)
39-
#'
40-
#' ## user-defined sampler
41-
#' my_gamma <- function(n) {
42-
#' rgamma(n, shape = 10, scale = 0.5)
43-
#' }
44-
#' draw_sample(my_gamma, n = 10)
45-
#'
46-
#' my_unif <- function(n, min, max) {
47-
#' runif(n, min = 0, max = 1)
48-
#' }
49-
#' draw_sample(my_unif, para = list(min = 1, max = 5), n = 10)
50-
#'
51-
#' ## base R distribution
52-
#' draw_sample("gamma", para = list(shape = 10, scale = 0.5), n = 10)
53-
#' draw_sample("unif", para = list(min = 1, max = 5), n = 10)
36+
#' @example
37+
#' inst/example/ex-draw_sample.R
5438
#'
5539
#' @noRd
5640

R/expand_spec.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
#' correspond to within-group blocks, and the remaining \eqn{K(K-1)/2}
2222
#' correspond to between-group blocks.
2323
#'
24-
#' @examples
25-
#' my_spec <- list("gamma", "unif")
26-
#' K <- 3
27-
#' expand_spec(my_spec, K)
24+
#' @example
25+
#' inst/example/ex-expand_spec.R
2826
#'
2927
#' @noRd
3028

R/gen_prec_sbm.R

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,8 @@
127127
#' \item{membership}{An integer vector specifying the group membership.}
128128
#' }
129129
#'
130-
#' @examples
131-
#' ## reproducibility for everything
132-
#' set.seed(1234)
133-
#'
134-
#' ## user-defined sampler
135-
#' my_gamma <- function(n) {
136-
#' rgamma(n, shape = 10, scale = 0.5)
137-
#' }
138-
#'
139-
#' sim <- gen_prec_sbm(d = 20, K = 3,
140-
#' within.prob = 0.25, between.prob = 0.05,
141-
#' weight.dists = list(my_gamma, "unif"),
142-
#' weight.paras = list(NULL, c(min = 0, max = 5)),
143-
#' cond.target = 100)
144-
#' plot(sim)
130+
#' @example
131+
#' inst/example/ex-gen_prec_sbm.R
145132
#'
146133
#' @export
147134

R/grasps.R

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,8 @@
143143
#' @references
144144
#' \insertAllCited{}
145145
#'
146-
#' @examples
147-
#' ## reproducibility for everything
148-
#' set.seed(1234)
149-
#'
150-
#' ## n-by-d data matrix
151-
#' X <- matrix(rnorm(200), 10, 20)
152-
#'
153-
#' ## group membership
154-
#' membership <- c(rep(1,5), rep(2,5), rep(3,4), rep(4,6))
155-
#'
156-
#' ## lasso, BIC
157-
#' res <- grasps(X = X, membership = membership, penalty = "lasso", crit = "BIC")
158-
#' plot(res)
146+
#' @example
147+
#' inst/example/ex-grasps.R
159148
#'
160149
#' @export
161150

R/plot.grasps.R

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,8 @@
1717
#' A \code{ggplot2} heatmap showing the matrix entries. Dashed lines indicate
1818
#' group boundaries. The plot title also reports matrix dimension and sparsity.
1919
#'
20-
#' @examples
21-
#' ## reproducibility for everything
22-
#' set.seed(1234)
23-
#'
24-
#' ## user-defined sampler
25-
#' my_gamma <- function(n) {
26-
#' rgamma(n, shape = 20, scale = 5)
27-
#' }
28-
#'
29-
#' ## block-structured precision matrix based on SBM
30-
#' sim <- gen_prec_sbm(d = 100, K = 5,
31-
#' within.prob = 0.5, between.prob = 0.05,
32-
#' weight.dists = list(my_gamma, "unif"),
33-
#' weight.paras = list(NULL, c(min = 0, max = 1)),
34-
#' cond.target = 100)
35-
#'
36-
#' ## visualization
37-
#' plot(sim)
20+
#' @example
21+
#' inst/example/ex-plot.grasps.R
3822
#'
3923
#' @export
4024
#' @noRd

R/sparsify_block_banded.R

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,8 @@
2222
#' \item{membership}{An integer vector specifying the group membership.}
2323
#' }
2424
#'
25-
#' @examples
26-
#' ## reproducibility for everything
27-
#' set.seed(1234)
28-
#'
29-
#' ## precision matrix estimation
30-
#' X <- matrix(rnorm(200), 10, 20)
31-
#' membership <- c(rep(1,5), rep(2,5), rep(3,4), rep(4,6))
32-
#' est <- grasps(X, membership = membership, penalty = "lasso", crit = "BIC")
33-
#'
34-
#' ## default: keep blocks within ±1 of each group
35-
#' res1 <- sparsify_block_banded(est$hatOmega, membership, neighbor.range = 1)
36-
#' plot(res1)
37-
#'
38-
#' ## wider band: keep blocks within ±2 of each group
39-
#' res2 <- sparsify_block_banded(est$hatOmega, membership, neighbor.range = 2)
40-
#' plot(res2)
41-
#'
42-
#' ## special case: block-diagonal matrix
43-
#' res3 <- sparsify_block_banded(est$hatOmega, membership, neighbor.range = 0)
44-
#' plot(res3)
25+
#' @example
26+
#' inst/example/ex-sparsify_block_banded.R
4527
#'
4628
#' @export
4729

README.Rmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,20 @@ library(grasps)
7575
set.seed(1234)
7676
7777
## block-structured precision matrix based on SBM
78-
sim <- gen_prec_sbm(d = 100, K = 5,
78+
sim <- gen_prec_sbm(d = 60, K = 5,
7979
within.prob = 0.5, between.prob = 0.05,
8080
weight.dists = list("gamma", "unif"),
8181
weight.paras = list(c(shape = 20, scale = 5), c(min = 0, max = 1)),
8282
cond.target = 100)
8383
8484
## synthetic data
8585
library(MASS)
86-
X <- MASS::mvrnorm(n = 50, mu = rep(0, ncol(sim$Sigma)), Sigma = sim$Sigma)
86+
X <- MASS::mvrnorm(n = 30, mu = rep(0, 60), Sigma = sim$Sigma)
8787
8888
## solution
8989
res <- grasps(X = X, membership = sim$membership, penalty = "lasso", crit = "BIC")
90+
91+
## visualization
9092
plot(res)
9193
```
9294

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ library(grasps)
4747
set.seed(1234)
4848

4949
## block-structured precision matrix based on SBM
50-
sim <- gen_prec_sbm(d = 100, K = 5,
50+
sim <- gen_prec_sbm(d = 60, K = 5,
5151
within.prob = 0.5, between.prob = 0.05,
5252
weight.dists = list("gamma", "unif"),
5353
weight.paras = list(c(shape = 20, scale = 5), c(min = 0, max = 1)),
5454
cond.target = 100)
5555

5656
## synthetic data
5757
library(MASS)
58-
X <- MASS::mvrnorm(n = 50, mu = rep(0, ncol(sim$Sigma)), Sigma = sim$Sigma)
58+
X <- MASS::mvrnorm(n = 30, mu = rep(0, 60), Sigma = sim$Sigma)
5959

6060
## solution
6161
res <- grasps(X = X, membership = sim$membership, penalty = "lasso", crit = "BIC")
62+
63+
## visualization
6264
plot(res)
6365
```
6466

inst/example/ex-draw_sample.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library(grasps)
2+
3+
## reproducibility for everything
4+
set.seed(1234)
5+
6+
## user-defined sampler
7+
#### Case 1
8+
my_gamma <- function(n) {
9+
rgamma(n, shape = 10, scale = 0.5)
10+
}
11+
draw_sample(my_gamma, n = 10)
12+
## Case 2
13+
my_unif <- function(n, min, max) {
14+
runif(n, min = 0, max = 1)
15+
}
16+
draw_sample(my_unif, para = list(min = 1, max = 5), n = 10)
17+
18+
## base R distribution
19+
#### Case 1
20+
draw_sample("gamma", para = list(shape = 10, scale = 0.5), n = 10)
21+
#### Case 2
22+
draw_sample("unif", para = list(min = 1, max = 5), n = 10)

0 commit comments

Comments
 (0)