Skip to content

Commit 4e6487b

Browse files
committed
add rm_control option & set basic readme
1 parent 37d943a commit 4e6487b

6 files changed

Lines changed: 520 additions & 43 deletions

File tree

R/04-show.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#' If `TRUE`, remove "Group" or "Focal" variable column when the values in the result table
2121
#' are same (before performing `subset` and `drop`),
2222
#' and reduce repeat values in column "Group", "Focal", and "Variable".
23+
#' @param rm_controls If `TRUE`, remove control terms.
2324
#' @param ... Additional arguments passed to `forestploter::forest()`, run `vignette("forestploter-post", "forestploter")`
2425
#' to see more plot options.
2526
#' @param subset Expression for subsetting the results data.
@@ -43,18 +44,23 @@
4344
br_show_forest <- function(
4445
breg,
4546
clean = TRUE,
47+
rm_controls = FALSE,
4648
...,
4749
subset = NULL,
4850
drop = NULL,
4951
tab_headers = NULL) {
50-
assert_breg_obj_with_results(breg)
52+
assert_breg_obj_with_results
53+
assert_bool(rm_controls)
5154

5255
# TODO: grouped (compared) forestplot for group_by???
5356
dots <- rlang::list2(...)
5457

5558
dt <- br_get_results(breg)
5659
x2 <- br_get_x2(breg)
57-
dt <- dt
60+
61+
if (rm_controls) {
62+
dt = dt |> dplyr::filter(.data$Focal_variable == .data$variable)
63+
}
5864

5965
has_group <- !is.null(br_get_group_by(breg))
6066
dt <- dt |>

README.Rmd

Lines changed: 138 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,173 @@ knitr::opts_chunk$set(
1313
)
1414
```
1515

16-
# bregr
16+
# bregr: Easy and Efficient Batch Processing of Regression Models in R
1717

1818
<!-- badges: start -->
1919
[![CRAN status](https://www.r-pkg.org/badges/version/bregr)](https://CRAN.R-project.org/package=bregr)
2020
[![](https://cranlogs.r-pkg.org/badges/grand-total/bregr?color=blue)](https://cran.r-project.org/package=bregr)
2121
[![R-CMD-check](https://github.com/WangLabCSU/bregr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/WangLabCSU/bregr/actions/workflows/R-CMD-check.yaml)
2222
<!-- badges: end -->
2323

24-
The goal of bregr is to ...
24+
The **bregr** package is designed to simplify and streamline the process of **b**atch processing **reg**ression models in **R**. Whether you're working with univariate or multivariate models, bregr makes it easy to handle multiple analyses at once. The package returns results in a tidy format, ensuring that the output is organized and accessible for further analysis or reporting. Additionally, bregr generates clear visualization plots that aid in the straightforward interpretation of model results. This makes it an invaluable tool for anyone working with regression models in R, allowing you to focus more on deriving insights and less on the mechanics of setting up and processing models.
25+
26+
27+
## A overview of regression modeling in batch
28+
29+
Regression modeling in batch involves constructing a series of regression models
30+
with same or different dependent variables $y_i$ in each model, different independent variables $x_i$
31+
of interest (focal) in each model, and constant variables (constant) in all models.
32+
33+
$$
34+
y_1 = \alpha_1 x_1 + \beta_1 c_1 + \gamma_1 c_2 \\
35+
y_2 = \alpha_2 x_1 + \beta_2 c_1 + \gamma_2 c_2 \\
36+
y_3 = \alpha_3 x_1 + \beta_3 c_1 + \gamma_3 c_2 \\
37+
... \\
38+
y_4 = \alpha_n x_n + \beta_n c_1 + \gamma_4 c_2 \\
39+
$$
40+
2541

2642
## Installation
2743

44+
You can install the stable version of bregr from CRAN with:
45+
46+
```r
47+
install.packages("bregr")
48+
```
49+
2850
You can install the development version of bregr from [GitHub](https://github.com/) with:
2951

3052
``` r
3153
# install.packages("pak")
3254
pak::pak("WangLabCSU/bregr")
3355
```
3456

35-
## Example
57+
## Usage
3658

37-
This is a basic example which shows you how to solve a common problem:
59+
Load package(s):
3860

39-
```{r example}
61+
```{r load-package}
4062
library(bregr)
41-
## basic example code
4263
```
4364

44-
What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so:
65+
Load data:
66+
67+
```{r load-data}
68+
lung = survival::lung
69+
lung$ph.ecog = factor(lung$ph.ecog)
70+
```
71+
72+
The design and implementation of bregr follow [Tidy design principles](https://design.tidyverse.org/) and [Tidyverse style guide](https://style.tidyverse.org/), which is easy to understand and use.
73+
74+
### Basics and workflow
75+
76+
1. Construct a `breg` object:
77+
78+
```{r}
79+
breg(lung)
80+
```
81+
82+
2. Set model response (i.e., dependent) variable(s):
83+
84+
```{r}
85+
breg(lung) |>
86+
br_set_y(c("time", "status"))
87+
```
88+
89+
2. Set model predictor (i.e., independent) variables. In batch analysis, this is
90+
divided into two parts: 1) focal variables and 2) control variables.
91+
92+
Focal variables are injected into constructed models one by one, while
93+
control variables remain constant across all constructed models.
94+
95+
```{r}
96+
breg(lung) |>
97+
br_set_y(c("time", "status")) |>
98+
br_set_x(colnames(lung)[6:10]) |> # set focal terms
99+
br_set_x2(c("age", "sex")) # set control terms, this is optional
100+
```
101+
> `terms` is desribed in the result object as bregr supports a more broader scope of
102+
> predictors. e.g., `x + poly(x, 2)` has one variable x, but two terms `x` and `poly(x, 2)`.
103+
45104

46-
```{r cars}
47-
summary(cars)
105+
3. Set model configuration.
106+
107+
Here, "coxph" (Cox-PH) for survival analysis is given.
108+
109+
```{r}
110+
mds = breg(lung) |>
111+
br_set_y(c("time", "status")) |>
112+
br_set_x(colnames(lung)[6:10]) |>
113+
br_set_x2(c("age", "sex")) |>
114+
br_set_model("coxph")
115+
mds
116+
```
117+
We can check constructed model formulas in text format.
118+
119+
```{r}
120+
br_get_models(mds)
121+
# or mds@models
122+
```
123+
4. The final step is constructing modeling with data and options defined above.
124+
125+
```{r}
126+
mds = breg(lung) |>
127+
br_set_y(c("time", "status")) |>
128+
br_set_x(colnames(lung)[6:10]) |>
129+
br_set_x2(c("age", "sex")) |>
130+
br_set_model("coxph") |>
131+
br_run()
132+
mds
133+
```
134+
135+
Now the `models` slot has been updated to real model objects:
136+
137+
```{r}
138+
br_get_models(mds)
139+
```
140+
141+
Tidy and comprehensive modeling results can be obtained from slots `results_tidy` and `results`,
142+
respectively.
143+
144+
```{r}
145+
br_get_results(mds, tidy = TRUE)
146+
br_get_results(mds)
147+
```
148+
149+
We can see that the whole workflow is easy to read and they are combined by R native operators.
150+
151+
```
152+
breg(lung) |>
153+
br_set_y(c("time", "status")) |>
154+
br_set_x(colnames(lung)[6:10]) |>
155+
br_set_x2(c("age", "sex")) |>
156+
br_set_model("coxph") |>
157+
br_run()
158+
```
159+
160+
### Pipeline function
161+
162+
Given the develop experience in [ezcox](https://github.com/ShixiangWang/ezcox/),
163+
we create a pipeline function for common one-step use.
164+
165+
```r
166+
br_pipeline(
167+
lung,
168+
y = c("time", "status"),
169+
x = colnames(lung)[6:10],
170+
x2 = c("age", "sex"),
171+
method = "coxph"
172+
)
48173
```
49174

50-
You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date. `devtools::build_readme()` is handy for this.
175+
### Visualization
51176

52-
You can also embed plots, for example:
177+
bregr mainly provides `br_show_forest()` for result data.
53178

54-
```{r pressure, echo = FALSE}
55-
plot(pressure)
179+
```{r fig.width=8, fig.height=6}
180+
br_show_forest(mds)
56181
```
57182

58-
In that case, don't forget to commit and push the resulting figure files, so they display on GitHub and CRAN.
59183

60184

61185
## Coverage

0 commit comments

Comments
 (0)