View source: R/printCrudeAndAdjustedModel.R
printCrudeAndAdjustedModel | R Documentation |
Prints table for a fitted object. It prints by default a latex table but can
also be converted into a HTML table that should be more compatible with common
word processors. For details run vignette("printCrudeAndAdjustedModel")
printCrudeAndAdjustedModel(
model,
order,
digits = 2,
ci_lim = c(-Inf, Inf),
sprintf_ci_str = getOption("sprintf_ci_str", "%s to %s"),
add_references,
add_references_pos,
reference_zero_effect,
groups,
rowname.fn,
use_labels = TRUE,
desc_column = FALSE,
desc_args = caDescribeOpts(digits = digits),
impute_args,
...
)
## S3 method for class 'printCrudeAndAdjusted'
rbind(..., alt.names, deparse.level = 1)
## S3 method for class 'printCrudeAndAdjusted'
print(x, ...)
## S3 method for class 'printCrudeAndAdjusted'
htmlTable(x, ...)
## S3 method for class 'printCrudeAndAdjusted'
x[i, j, ...]
## S3 method for class 'printCrudeAndAdjusted'
cbind(..., alt.names, deparse.level = 1)
## S3 method for class 'printCrudeAndAdjusted'
knit_print(x, ...)
## S3 method for class 'printCrudeAndAdjusted'
latex(object, ...)
model |
A regression model fit, i.e. the returned object from your
regression function, or the output from |
order |
A vector with regular expressions for each group, use if youe want to reorder the groups in another way than what you've used in your original function. You can also use this in order to skip certain variables from the output. |
digits |
The number of digits to round to |
ci_lim |
A limit vector number that specifies if any values should be
abbreviated above or below this value, for instance a value of 1000
would give a value of |
sprintf_ci_str |
A string according to |
add_references |
True if it should use the data set to look for references, otherwise supply the function with a vector with names. Sometimes you want to indicate the reference row for each group. This needs to be just as many as the groups as the order identified. Use NA if you don't want to have a reference for that particular group. |
add_references_pos |
The position where a reference should be added.
Sometimes you don't want the reference to be at the top, for instance
if you have age groups then you may have < 25, 25-39, 40-55, > 55 and
you have the reference to be 25-39 then you should set the reference
list for |
reference_zero_effect |
Used with references, tells if zero effect
is in exponential form, i.e. |
groups |
If you wish to have other than the default |
rowname.fn |
A function that takes a row name and sees if it needs beautifying. The function has only one parameter the coefficients name and should return a string or expression. |
use_labels |
If the rowname.fn function doesn't change the name then the label should be used instead of the name, that is if there is a label and it isn't a factor. |
desc_column |
Add descriptive column to the crude and adjusted table |
desc_args |
The description arguments that are to be used for the
the description columns. The options/arguments should be generated by the
|
impute_args |
A list with additional arguments if the provided input is
a imputed object. Currently the list options |
... |
outputs from |
alt.names |
If you don't want to use named arguments for the |
deparse.level |
backward compatibility |
x |
The output object from the |
object |
The output object from the printCrudeAndAdjustedModel function |
matrix
Returns a matrix of class printCrudeAndAdjusted that
has a default print method associated with
If you call this function and you've changed any of the variables used in the original call, i.e. the premises are changed, this function will not remember the original values and the statistics will be faulty!
latex()
for details.
Other crudeAndAdjusted functions:
getCrudeAndAdjustedModelData()
# simulated data to use
set.seed(10)
ds <- data.frame(
ftime = rexp(200),
fstatus = sample(0:1, 200, replace = TRUE),
Variable1 = runif(200),
Variable2 = runif(200),
Variable3 = runif(200),
Variable4 = factor(sample(LETTERS[1:4], size = 200, replace = TRUE))
)
library(rms)
dd <- datadist(ds)
options(datadist = "dd")
fit <- cph(Surv(ftime, fstatus) ~ Variable1 + Variable3 + Variable2 + Variable4,
data = ds, x = TRUE, y = TRUE
)
printCrudeAndAdjustedModel(fit, order = c("Variable[12]", "Variable3"))
printCrudeAndAdjustedModel(fit,
order = c("Variable3", "Variable4"),
add_references = TRUE,
desc_column = TRUE
)
# Now to a missing example
n <- 500
ds <- data.frame(
x1 = factor(sample(LETTERS[1:4], size = n, replace = TRUE)),
x2 = rnorm(n, mean = 3, 2),
x3 = factor(sample(letters[1:3], size = n, replace = TRUE))
)
ds$Missing_var1 <- factor(sample(letters[1:4], size = n, replace = TRUE))
ds$Missing_var2 <- factor(sample(letters[1:4], size = n, replace = TRUE))
ds$y <- rnorm(nrow(ds)) +
(as.numeric(ds$x1) - 1) * 1 +
(as.numeric(ds$Missing_var1) - 1) * 1 +
(as.numeric(ds$Missing_var2) - 1) * .5
# Create a messy missing variable
non_random_missing <- sample(which(ds$Missing_var1 %in% c("b", "d")),
size = 150, replace = FALSE
)
# Restrict the non-random number on the x2 variables
non_random_missing <- non_random_missing[non_random_missing %in%
which(ds$x2 > mean(ds$x2) * 1.5) &
non_random_missing %in%
which(ds$x2 > mean(ds$y))]
ds$Missing_var1[non_random_missing] <- NA
# Simple missing variable
ds$Missing_var2[sample(1:nrow(ds), size = 50)] <- NA
# Setup the rms environment
ddist <- datadist(ds)
options(datadist = "ddist")
impute_formula <-
as.formula(paste(
"~",
paste(colnames(ds),
collapse = "+"
)
))
imp_ds <- aregImpute(impute_formula, data = ds, n.impute = 10)
fmult <- fit.mult.impute(y ~ x1 + x2 + x3 +
Missing_var1 + Missing_var2,
fitter = ols, xtrans = imp_ds, data = ds
)
printCrudeAndAdjustedModel(fmult,
impute_args = list(
variance.inflation = TRUE,
coef_change = list(
type = "diff",
digits = 3
)
)
)
# Use some labels and style to prettify the output
# fro the mtcars dataset
data("mtcars")
label(mtcars$mpg) <- "Gas"
units(mtcars$mpg) <- "Miles/(US) gallon"
label(mtcars$wt) <- "Weight"
units(mtcars$wt) <- "10^3 kg" # not sure the unit is correct
mtcars$am <- factor(mtcars$am, levels = 0:1, labels = c("Automatic", "Manual"))
label(mtcars$am) <- "Transmission"
mtcars$gear <- factor(mtcars$gear)
label(mtcars$gear) <- "Gears"
# Make up some data for making it slightly more interesting
mtcars$col <- factor(sample(c("red", "black", "silver"), size = NROW(mtcars), replace = TRUE))
label(mtcars$col) <- "Car color"
require(splines)
fit_mtcar <- lm(mpg ~ wt + gear + col, data = mtcars)
printCrudeAndAdjustedModel(fit_mtcar,
add_references = TRUE,
ctable = TRUE,
desc_column = TRUE,
digits = 1,
desc_args = caDescribeOpts(
digits = 1,
colnames = c("Avg.")
)) |>
htmlTable::addHtmlTableStyle(css.rgroup = "",
css.header = "font-weight: normal")
printCrudeAndAdjustedModel(fit_mtcar,
add_references = TRUE,
desc_column = TRUE,
order = c("Interc", "gear")
)
# Alterntive print - just an example, doesn't make sense to skip reference
printCrudeAndAdjustedModel(fit_mtcar,
order = c("col", "gear"),
groups = c("Color", "Gears"),
add_references = c("black", NA),
ctable = TRUE
)
# Now we can also combine models into one table using rbind()
mpg_model <- printCrudeAndAdjustedModel(lm(mpg ~ wt + gear + col, data = mtcars),
add_references = TRUE,
ctable = TRUE,
desc_column = TRUE,
digits = 1,
desc_args = caDescribeOpts(
digits = 1,
colnames = c("Avg.")
)
)
wt_model <- printCrudeAndAdjustedModel(lm(wt ~ mpg + gear + col, data = mtcars),
add_references = TRUE,
ctable = TRUE,
desc_column = TRUE,
digits = 1,
desc_args = caDescribeOpts(
digits = 1,
colnames = c("Avg.")
)
)
library(htmlTable)
rbind(Miles = mpg_model, Weight = wt_model) |>
addHtmlTableStyle(pos.caption = "bottom") |>
htmlTable(caption = paste("Combining models together with a table spanner element",
"separating each model"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.