-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.Rmd
More file actions
270 lines (194 loc) · 7.54 KB
/
README.Rmd
File metadata and controls
270 lines (194 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# bregr: Easy and Efficient Batch Processing of Regression Models in R <a href="https://wanglabcsu.github.io/bregr/"><img src="man/figures/logo.png" align="right" height="139" alt="bregr website" /></a>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=bregr)
[](https://cran.r-project.org/package=bregr)
[](https://github.com/WangLabCSU/bregr/actions/workflows/R-CMD-check.yaml)
[](https://deepwiki.com/WangLabCSU/bregr)
<!-- badges: end -->
The **bregr** package revolutionizes batch regression modeling in R, enabling you to run **hundreds of models simultaneously** with a clean, intuitive workflow. Designed for both univariate and multivariate analyses, it delivers **tidy-formatted results** and publication-ready visualizations, transforming cumbersome statistical workflows into efficient pipelines.
## Key Features
- 🚀 **Batch Processing**: Automate regression modeling across multiple dependent/independent variables.
- 📊 **Tidy Output**: Structured results compatible with `tidyverse` for seamless downstream analysis.
- 📈 **Integrated Visualization**: One-command forest plots and model diagnostics.
- ⚡️ **Unified Workflow**: Chain operations with native R pipes (`|>`).
- 📦 **Model Agnostic**: Supports linear models, Cox regression, and more.
## Batch Regression Modeling Overview
Batch regression streamlines analyses where:
- Each model shares **identical control variables** ($c_1$, $c_2$, ...).
- **Focal predictors** ($x_1$, $x_2$, ...) or **response variables** ($y_1$, $y_2$, ...) vary systematically.
A simplified overview of batch regression modeling is given below for illustration:
<p align="center">
<img src="man/figures/breg_formula.svg">
</p>
## Installation
You can install the stable version of bregr from CRAN with:
```r
install.packages("bregr")
```
Alternatively, install the development version from [r-universe](https://wanglabcsu.r-universe.dev/bregr) with:
``` r
install.packages('bregr', repos = c('https://wanglabcsu.r-universe.dev', 'https://cloud.r-project.org'))
```
or from [GitHub](https://github.com/) with:
``` r
#install.packages("remotes")
remotes::install_github("WangLabCSU/bregr")
```
## Usage
Load package(s):
```{r load-package}
library(bregr)
```
Load data:
```{r load-data}
lung <- survival::lung |>
dplyr::filter(ph.ecog != 3)
lung$ph.ecog <- factor(lung$ph.ecog)
```
bregr is designed and implemented following [Tidy design principles](https://design.tidyverse.org/) and [Tidyverse style guide](https://style.tidyverse.org/), making it intuitive and user-friendly.
### Core workflow
Define and construct batch models:
```{r}
mds <- breg(lung) |> # Init breg object
br_set_y(c("time", "status")) |> # Survival outcomes
br_set_x(colnames(lung)[6:10]) |> # Focal predictors
br_set_x2(c("age", "sex")) |> # Controls
br_set_model("coxph") |> # Cox Proportional Hazards
br_run() # Execute models
```
### One-Step Pipeline
```{r eval=FALSE}
mds <- br_pipeline(
lung,
y = c("time", "status"),
x = colnames(lung)[6:10],
x2 = c("age", "sex"),
method = "coxph"
)
```
Run in parallel:
```{r warning=FALSE}
mds_p <- br_pipeline(
lung,
y = c("time", "status"),
x = colnames(lung)[6:10],
x2 = c("age", "sex"),
method = "coxph",
n_workers = 3
)
```
```{r}
all.equal(mds, mds_p)
```
Two global options have been introduced to control whether models are saved as local files (`bregr.save_model`, default is `FALSE`) and where they should be saved (`bregr.path`, default uses a temporary path).
### Output Inspection
Use `br_get_*()` function family to access attributes and data of result `breg` object.
```{r}
br_get_models(mds) # Raw model objects
br_get_results(mds) # Comprehensive estimates
br_get_results(mds, tidy = TRUE) # Tidy-formatted coefficients
```
### Visualization
#### Forest Plot (Key Results)
bregr mainly provides `br_show_forest()` for plotting data table of modeling results.
```{r dpi=150}
br_show_forest(mds)
```
We can tune the plot to only keep focal variables and adjust the limits of x axis.
```{r dpi=150}
br_show_forest(
mds,
rm_controls = TRUE, # Focus on focal predictors
xlim = c(0, 3), # Custom axis scaling
# Use x_trans = "log" to transform the axis
# Use log_first = TRUE to transform both
# the axis and estimate table
drop = 1 # Remove redundant columns
)
```
We also provide some interfaces from other packages for plotting constructed model(s), e.g., `br_show_forest_ggstats()`, `br_show_forest_ggstatsplot()`, `br_show_fitted_line()`,
and `br_show_fitted_line_2d()`.
#### Comparing Univariate vs Multivariate Models
A common analysis task is to compare how predictor effects change when modeled individually (univariate) versus together (multivariate). The `br_compare_models()` function builds both types of models and displays them side-by-side:
```{r dpi=150, fig.height=5}
# Compare univariate and multivariate models
comparison <- br_compare_models(
lung,
y = c("time", "status"),
x = c("ph.ecog", "ph.karno", "pat.karno"),
x2 = c("age", "sex"),
method = "coxph"
)
# Show forest plot with both models
br_show_forest_comparison(comparison)
```
This allows you to see how adjusting for other predictors affects the estimates for each variable.
For Cox-PH modeling results (focal variables must be continuous type), we provide a risk network plotting function.
```{r}
mds2 <- br_pipeline(
survival::lung,
y = c("time", "status"),
x = colnames(survival::lung)[6:10],
x2 = c("age", "sex"),
method = "coxph"
)
```
```{r dpi=150, fig.height=8}
br_show_risk_network(mds2)
```
#### Model Score Prediction and Survival Curves
For Cox-PH models, you can generate model predictions (risk scores) and create survival curves grouped by these scores:
```{r}
# Generate model predictions
scores <- br_predict(mds2, idx = "ph.ecog")
head(scores)
```
```{r dpi=150, fig.height=6}
# Create survival curves based on model scores
br_show_survival_curves(
mds2,
idx = "ph.ecog",
n_groups = 3,
title = "Survival Curves by 'ph.ecog' Model Risk Score"
)
```
### Table
Show tidy table result as pretty table:
```{r}
br_show_table(mds)
```
As markdown table:
```{r}
br_show_table(mds, export = TRUE)
```
As HTML table:
```{r eval=FALSE}
br_show_table(mds, export = TRUE, args_table_export = list(format = "html"))
```
## Documentation
All functions are documented in the [package reference](https://wanglabcsu.github.io/bregr/reference/), with full documentation available on the [package site](https://wanglabcsu.github.io/bregr/).
## Coverage
```{r eval=FALSE}
covr::package_coverage()
```
## Citation
If you use **bregr** in academic field, please cite:
- Wang S, Peng Y, Shu C, et al. bregr: An R Package for Streamlined Batch Processing and Visualization of Biomedical Regression Models[J]. Med Research, 2025. https://doi.org/10.1002/mdr2.70028
## Related/Similar Project(s)
- [ezcox: Easily Process a Batch of Cox Models](https://github.com/ShixiangWang/ezcox/)
- [autoReg](https://github.com/cardiomoon/autoReg/)
- [riskRegression](https://github.com/tagteam/riskRegression/)
## LICENSE
(GPL-3) Copyright (c) 2025 Shixiang Wang & WangLabCSU team