| Title: | Regression Model Processing Port |
|---|---|
| Description: | Provides R6 classes, methods and utilities to construct, analyze, summarize, and visualize regression models. |
| Authors: | Shixiang Wang [aut, cre] (ORCID: <https://orcid.org/0000-0001-9855-7357>) |
| Maintainer: | Shixiang Wang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1 |
| Built: | 2026-05-20 10:33:51 UTC |
| Source: | https://github.com/ShixiangWang/regport |
Contains fields storing data and methods to build, process and visualize a regression model. Currently, this class is designed for CoxPH and GLM regression models.
dataa data.table storing modeling data.
recipean R formula storing model formula.
termsall terms (covariables, i.e. columns) used for building model.
argsother arguments used for building model.
modela constructed model.
typemodel type (class).
resultmodel result, a object of parameters_model. Can be converted into
data.frame with as.data.frame() or data.table::as.data.table().
forest_datamore detailed data used for plotting forest.
new()
Build a REGModel object.
REGModel$new(
data,
recipe,
...,
f = c("coxph", "binomial", "gaussian", "Gamma", "inverse.gaussian", "poisson", "quasi",
"quasibinomial", "quasipoisson"),
exp = NULL,
ci = 0.95
)dataa data.table storing modeling data.
recipean R formula or a list with two elements 'x' and 'y',
where 'x' is for covariables and 'y' is for label. See example for detail
operation.
...other parameters passing to corresponding regression model function.
fa length-1 string specifying modeling function or family of glm(), default is 'coxph'.
Other options are members of GLM family, see stats::family().
'binomial' is logistic, and 'gaussian' is linear.
explogical, indicating whether or not to exponentiate the the coefficients.
ciconfidence Interval (CI) level. Default to 0.95 (95%).
e.g. survival::coxph().
a REGModel R6 object.
get_forest_data()
get tidy data for plotting forest.
REGModel$get_forest_data(separate_factor = FALSE, global_p = FALSE)
separate_factorseparate factor/class as a blank row.
global_pif TRUE, return global p value.
plot_forest()
plot forest.
REGModel$plot_forest(ref_line = NULL, xlim = NULL, ...)
ref_linereference line, default is 1 for HR.
xlimlimits of x axis.
...other plot options passing to forestploter::forest().
Also check https://github.com/adayim/forestploter to see more complex adjustment of the result plot.
plot()
print the REGModel$result with default plot methods from see package.
REGModel$plot(...)
...other parameters passing to plot() in see:::plot.see_parameters_model function.
print()
print the REGModel object
REGModel$print(...)
...unused.
clone()
The objects of this class are cloneable with this method.
REGModel$clone(deep = FALSE)
deepWhether to make a deep clone.
library(survival) test1 <- data.frame( time = c(4, 3, 1, 1, 2, 2, 3), status = c(1, 1, 1, 0, 1, 1, 0), x = c(0, 2, 1, 1, 1, 0, 0), sex = c(0, 0, 0, 0, 1, 1, 1) ) test1$sex <- factor(test1$sex) # -------------- # Build a model # -------------- # way 1: mm <- REGModel$new( test1, Surv(time, status) ~ x + strata(sex) ) mm as.data.frame(mm$result) if (require("see")) mm$plot() mm$print() # Same as print(mm) # way 2: mm2 <- REGModel$new( test1, recipe = list( x = c("x", "strata(sex)"), y = c("time", "status") ) ) mm2 # Add other parameters, e.g., weights # For more, see ?coxph mm3 <- REGModel$new( test1, recipe = list( x = c("x", "strata(sex)"), y = c("time", "status") ), weights = c(1, 1, 1, 2, 2, 2, 3) ) mm3$args # ---------------------- # Another type of model # ---------------------- library(stats) counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12) outcome <- gl(3, 1, 9) treatment <- gl(3, 3) data <- data.frame(treatment, outcome, counts) mm4 <- REGModel$new( data, counts ~ outcome + treatment, f = "poisson" ) mm4 mm4$plot_forest() mm4$get_forest_data() mm4$plot_forest()library(survival) test1 <- data.frame( time = c(4, 3, 1, 1, 2, 2, 3), status = c(1, 1, 1, 0, 1, 1, 0), x = c(0, 2, 1, 1, 1, 0, 0), sex = c(0, 0, 0, 0, 1, 1, 1) ) test1$sex <- factor(test1$sex) # -------------- # Build a model # -------------- # way 1: mm <- REGModel$new( test1, Surv(time, status) ~ x + strata(sex) ) mm as.data.frame(mm$result) if (require("see")) mm$plot() mm$print() # Same as print(mm) # way 2: mm2 <- REGModel$new( test1, recipe = list( x = c("x", "strata(sex)"), y = c("time", "status") ) ) mm2 # Add other parameters, e.g., weights # For more, see ?coxph mm3 <- REGModel$new( test1, recipe = list( x = c("x", "strata(sex)"), y = c("time", "status") ), weights = c(1, 1, 1, 2, 2, 2, 3) ) mm3$args # ---------------------- # Another type of model # ---------------------- library(stats) counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12) outcome <- gl(3, 1, 9) treatment <- gl(3, 3) data <- data.frame(treatment, outcome, counts) mm4 <- REGModel$new( data, counts ~ outcome + treatment, f = "poisson" ) mm4 mm4$plot_forest() mm4$get_forest_data() mm4$plot_forest()
Contains fields storing data and methods to build, process and visualize a list of regression model. Currently, this class is designed for CoxPH and GLM regression models.
dataa data.table storing modeling data.
xfocal variables (terms).
ypredicted variables or expression.
covarscovariables.
mlista list of REGModel.
argsother arguments used for building model.
typemodel type (class).
resultmodel result, a object of parameters_model. Can be converted into
data.frame with as.data.frame() or data.table::as.data.table().
forest_datamore detailed data used for plotting forest.
new()
Create a REGModelList object.
REGModelList$new(data, y, x, covars = NULL)
dataa data.table storing modeling data.
ypredicted variables or expression.
xfocal variables (terms).
covarscovariables.
a REGModelList R6 object.
build()
Build REGModelList object.
REGModelList$build(
f = c("coxph", "binomial", "gaussian", "Gamma", "inverse.gaussian", "poisson", "quasi",
"quasibinomial", "quasipoisson"),
exp = NULL,
ci = 0.95,
parallel = FALSE,
...
)fa length-1 string specifying modeling function or family of glm(), default is 'coxph'.
Other options are members of GLM family, see stats::family().
'binomial' is logistic, and 'gaussian' is linear.
explogical, indicating whether or not to exponentiate the the coefficients.
ciconfidence Interval (CI) level. Default to 0.95 (95%).
e.g. survival::coxph().
parallelif TRUE, use N-1 cores to run the task.
...other parameters passing to corresponding regression model function.
a REGModel R6 object.
plot_forest()
plot forest.
REGModelList$plot_forest( ref_line = NULL, xlim = NULL, vars = NULL, p = NULL, ... )
ref_linereference line, default is 1 for HR.
xlimlimits of x axis.
varsselected variables to show.
pselected variables with level' pvalue lower than p.
...other plot options passing to forestploter::forest().
Also check https://github.com/adayim/forestploter to see more complex adjustment of the result plot.
print()
print the REGModelList object
REGModelList$print(...)
...unused.
clone()
The objects of this class are cloneable with this method.
REGModelList$clone(deep = FALSE)
deepWhether to make a deep clone.
ml <- REGModelList$new( data = mtcars, y = "mpg", x = c("factor(cyl)", colnames(mtcars)[3:5]), covars = c(colnames(mtcars)[8:9], "factor(gear)") ) ml ml$print() ml$plot_forest() ml$build(f = "gaussian") ## Not run: ml$build(f = "gaussian", parallel = TRUE) ## End(Not run) ml$print() ml$result ml$forest_data ml$plot_forest()ml <- REGModelList$new( data = mtcars, y = "mpg", x = c("factor(cyl)", colnames(mtcars)[3:5]), covars = c(colnames(mtcars)[8:9], "factor(gear)") ) ml ml$print() ml$plot_forest() ml$build(f = "gaussian") ## Not run: ml$build(f = "gaussian", parallel = TRUE) ## End(Not run) ml$print() ml$result ml$forest_data ml$plot_forest()