Learn R Programming

Greg (version 2.0.2)

printCrudeAndAdjustedModel: Output crude and adjusted model data

Description

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")

Usage

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 printCrudeAndAdjusted rbind(..., alt.names, deparse.level = 1)

# S3 method for printCrudeAndAdjusted print(x, ...)

# S3 method for printCrudeAndAdjusted htmlTable(x, ...)

# S3 method for printCrudeAndAdjusted [(x, i, j, ...)

# S3 method for printCrudeAndAdjusted cbind(..., alt.names, deparse.level = 1)

# S3 method for printCrudeAndAdjusted knit_print(x, ...)

# S3 method for printCrudeAndAdjusted latex(object, ...)

Value

matrix Returns a matrix of class printCrudeAndAdjusted that has a default print method associated with

Arguments

model

A regression model fit, i.e. the returned object from your regression function, or the output from getCrudeAndAdjustedModelData()

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 > -1000 for a value of 1001. This gives a prettier table when you have very wide confidence intervals.

sprintf_ci_str

A string according to sprintf() to write the confidence interval where the first %s is the lower and the second the upper. You can choose to set this through setting the option sprintf_ci_str, e.g. options(sprintf_ci_str = "%s - %s").

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 age_groups as add_references_pos = list(age_groups = 2) so that you have the second group as the position for the reference.

reference_zero_effect

Used with references, tells if zero effect is in exponential form, i.e. exp(0) = 1, or in regular format, i.e. 0 = 0 (can be set to any value)

groups

If you wish to have other than the default rgroup names for the grouping parameter

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 caDescribeOpts function.

impute_args

A list with additional arguments if the provided input is a imputed object. Currently the list options coef_change and variance.inflation are supported. If you want both columns then the simplest way is to provide the list: list(coef_change = TRUE, variance.inflation = TRUE). The coef_change adds a column with the change in coefficients due to the imputation, the the "raw" model is subtracted from the imputed results. The "raw" model is the unimputed model, coef(imputed_model) - coef(raw_model). The variance.inflation adds the variance.inflation.impute from the fit.mult.impute() to a separate column. See the description for the variance.inflation.impute in in the fit.mult.impute() description. Both arguments can be customized by providing a list. The list can have the elements type, name, out_str, and/or digits. The type can for coef_change/variance.impute be either "percent" or "ratio", note that variance.inflation.impute was not originally intended to be interpreted as %. The default for coef_change is to have "diff", that gives the absolute difference in the coefficient. The name provides the column name, the out_str should be a string that is compatible with sprintf() and also contains an argument for accepting a float value, e.g. " column. The digits can be used if you are not using the out_str argument, it simply specifies the number of digits to show. See the example for how for a working example. Note that currently only the fit.mult.impute() is supported by this option.

...

outputs from printCrudeAndAdjusted. If mixed then it defaults to rbind.data.frame

alt.names

If you don't want to use named arguments for the tspanner attribute in the rbind or the cgroup in the cbind but a vector with names then use this argument.

deparse.level

backward compatibility

x

The output object from the printCrudeAndAdjustedModel function

object

The output object from the printCrudeAndAdjustedModel function

Warning

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!

See Also

latex() for details.

Other crudeAndAdjusted functions: getCrudeAndAdjustedModelData()

Examples

Run this code
# 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"))

Run the code above in your browser using DataLab