Title: | Advanced Plotting of Ordinal Regression Models |
---|---|
Description: | An extension to the Regression Modeling Strategies package that facilitates plotting ordinal regression model predictions together with confidence intervals for each dependent variable level. It also adds a functionality to plot the model summary as a modifiable object. |
Authors: | Richard Meitern [aut, cre] |
Maintainer: | Richard Meitern <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.6 |
Built: | 2024-10-31 03:35:23 UTC |
Source: | https://github.com/rix133/ormplot |
Function to convert any input to string vector
convert_arg(x)
convert_arg(x)
x |
string, object name or vector of these |
vector of strings
A dataset containing the standardized residuals of Estonian schoolchildren.
educ_data
educ_data
A data frame with 11032 rows and 10 variables:
highest obtained educational level
location of school (rural or urban)
gender of the child)
highest parental profession
number of children in the family
cranial volume (residuals to age an birth date)
height (residuals to age an birth date)
face width (residuals to age an birth date)
year of birth
centered but not scaled year of birth (YOB)
Function to get aligned table of two ggplot objects
join_ggplots( leftplot, rightplot, plot.widths = c(0.5, 0.5), title = "Odds Ratio" )
join_ggplots( leftplot, rightplot, plot.widths = c(0.5, 0.5), title = "Odds Ratio" )
leftplot |
the left side plot |
rightplot |
the plot on the right |
plot.widths |
the relative widths of the left and right plot
should be a vector ( |
title |
the tile row of the drawn plot |
set.seed(123) #load the libraries library(rms) library(ormPlot) library(ggplot2) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ YOBc + sex + height_rzs + n_siblings + cran_rzs, data = educ_data) #the antilog true produces odd ratios (default value for orm and lrm) s<-summary(cran_model, antilog = TRUE) #set the plotting default theme (optional) theme_set(theme_classic()) #return modifiable ggplots plots<-forestplot(s, return_ggplots = TRUE ) #modify like any ggplot2 object table<-plots[[1]] + theme(axis.text=element_text(size = 12), axis.line.x = element_line(color = "red", size = 1), axis.text.y = element_blank()) graph<-plots[[2]] + theme(axis.line = element_line(color = "red", size = 1), axis.text.y = element_text()) #join the graphs join_ggplots( graph, table, title = "", plot.widths = c(0.6,0.4))
set.seed(123) #load the libraries library(rms) library(ormPlot) library(ggplot2) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ YOBc + sex + height_rzs + n_siblings + cran_rzs, data = educ_data) #the antilog true produces odd ratios (default value for orm and lrm) s<-summary(cran_model, antilog = TRUE) #set the plotting default theme (optional) theme_set(theme_classic()) #return modifiable ggplots plots<-forestplot(s, return_ggplots = TRUE ) #modify like any ggplot2 object table<-plots[[1]] + theme(axis.text=element_text(size = 12), axis.line.x = element_line(color = "red", size = 1), axis.text.y = element_blank()) graph<-plots[[2]] + theme(axis.line = element_line(color = "red", size = 1), axis.text.y = element_text()) #join the graphs join_ggplots( graph, table, title = "", plot.widths = c(0.6,0.4))
Get row names from odd an values form even columns
oddstable(x)
oddstable(x)
x |
a |
Function to get a ggplot table from a matrix
oddstable_graph( x, digits = 3, theme = ggplot2::theme_get(), header = NULL, row.names.y = NULL )
oddstable_graph( x, digits = 3, theme = ggplot2::theme_get(), header = NULL, row.names.y = NULL )
x |
a |
digits |
the number of significant digits to display |
theme |
the desired ggplot2 theme |
header |
names of the table columns |
row.names.y |
new names for the variable rows |
Function to get a ggplot figure from a matrix x
orm_graph( x, theme = ggplot2::theme_get(), header = NULL, row.names.y = NULL, shape = 19, limits = NULL, breaks = c(0.5, 1, 1.5, 2, 3, 4) )
orm_graph( x, theme = ggplot2::theme_get(), header = NULL, row.names.y = NULL, shape = 19, limits = NULL, breaks = c(0.5, 1, 1.5, 2, 3, 4) )
x |
a |
theme |
the desired ggplot2 theme |
header |
names of the table columns |
row.names.y |
new names for the variable rows |
shape |
point shape, see |
limits |
the x axis limits as a vector, see also:
|
breaks |
the x axis breaks as a vector,help see also:
|
rms
The package is an extension to the rms
package that
facilitates plotting the ordinal regression orm
model
objects. The aim is to get ggplot2 plots that are modifiable
The ormPlot package provides two categories of important functions: forestplotting the summary and plotting the predictions
The forestplot function facilitates plotting the
summary.rms
objects resulting from the
orm
or lrm
model
See exported methods for more details:
The prediction plotting function facilitates plotting the
orm
objects using the results got from
Predict
function. In particular it adds confidence
intervals to orm prediction plots.
See exported methods for more details:
educ_data
data about morfometrics of schoolchildren
born between 1937-1962 in Estonian territory. see also the
citation("ormPlot") article
This function plots the model predictions given that all variables that are
not included in the plot are kept constant. Hence it requires at least one
variable to produce a plot.
returns a ggplot
object that can be further customized like any
other ggplot
## S3 method for class 'lrm' plot(x, ...)
## S3 method for class 'lrm' plot(x, ...)
x |
an object created by |
... |
additional parameters that will be passed to |
a ggplot
plot object
#load the libraries library(rms) library(ormPlot) #make the datadist dd<-datadist(educ_data) options(datadist='dd') #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #plot the predictions of the model for varying one variable only plot(cran_model, cran_rzs) #customize the plotting varying all variables plot(cran_model, cran_rzs, plot_cols = max_SEP_3, plot_rows = c(Rural, sex), #setting new x-label (optional) xlab = "Cranial volume (residuals to age an birth date)", #setting new facet labels (optional) facet_labels = list(Rural = c("Urban", "Rural"), sex = c("Boys","Girls")) )
#load the libraries library(rms) library(ormPlot) #make the datadist dd<-datadist(educ_data) options(datadist='dd') #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #plot the predictions of the model for varying one variable only plot(cran_model, cran_rzs) #customize the plotting varying all variables plot(cran_model, cran_rzs, plot_cols = max_SEP_3, plot_rows = c(Rural, sex), #setting new x-label (optional) xlab = "Cranial volume (residuals to age an birth date)", #setting new facet labels (optional) facet_labels = list(Rural = c("Urban", "Rural"), sex = c("Boys","Girls")) )
This function plots the model predictions given that all variables that are
not included in the plot are kept constant. Hence it requires at least one
variable to produce a plot.
returns a ggplot
object that can be further customized like any
other ggplot
## S3 method for class 'orm' plot( x, xval, plot_cols = c(), plot_rows = c(), label_with_colname = TRUE, facet_labels = NULL, xlab = NULL, ylab = NULL, np = 100, fun = stats::plogis, boot.type = "bca", conf.int = 0.95, ... )
## S3 method for class 'orm' plot( x, xval, plot_cols = c(), plot_rows = c(), label_with_colname = TRUE, facet_labels = NULL, xlab = NULL, ylab = NULL, np = 100, fun = stats::plogis, boot.type = "bca", conf.int = 0.95, ... )
x |
an object created by |
xval |
The model value plotted on the x axis |
plot_cols |
A vector of strings with other model components that should be plotted. These are put on columns. |
plot_rows |
A vector of strings with other model components that should be plotted. These are put on rows. |
label_with_colname |
Should he variable name also be included on plot row and column names |
facet_labels |
A named list of new names for variables on rows and columns |
xlab |
A custom x-axis value (if specified) |
ylab |
A custom y-axis value (if specified) |
np |
the number of equally-spaced points computed for continuous
predictors that vary, i.e., when the specified value is |
fun |
an optional transformation of the linear predictor.
Specify |
boot.type |
set to |
conf.int |
confidence level (highest posterior density interval probability for
Bayesian models). Default is 0.95. Specify |
... |
additional parameters that will be passed to |
a ggplot
plot object
#load the libraries library(rms) library(ormPlot) #make the datadist dd<-datadist(educ_data) options(datadist='dd') #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #plot the predictions of the model for varying one variable only plot(cran_model, cran_rzs) #customize the plotting varying all variables plot(cran_model, cran_rzs, plot_cols = max_SEP_3, plot_rows = c(Rural, sex), #setting new x-label (optional) xlab = "Cranial volume (residuals to age an birth date)", #setting new facet labels (optional) facet_labels = list(Rural = c("Urban", "Rural"), sex = c("Boys","Girls")) )
#load the libraries library(rms) library(ormPlot) #make the datadist dd<-datadist(educ_data) options(datadist='dd') #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #plot the predictions of the model for varying one variable only plot(cran_model, cran_rzs) #customize the plotting varying all variables plot(cran_model, cran_rzs, plot_cols = max_SEP_3, plot_rows = c(Rural, sex), #setting new x-label (optional) xlab = "Cranial volume (residuals to age an birth date)", #setting new facet labels (optional) facet_labels = list(Rural = c("Urban", "Rural"), sex = c("Boys","Girls")) )
Convenience function to create a plot of the orm
model
summary. For further customizing the plots use return_ggplots = TRUE
This will create 2 ggplot2
objects that can be joined with the
join_ggplots
commands.
## S3 method for class 'summary.rms' plot(x, ...) forestplot( x, return_ggplots = FALSE, plot.widths = c(0.5, 0.5), title = "Odds ratio", digits = 3, shape = 19, header = NULL, limits = NULL, breaks = c(0.5, 1, 1.5, 2, 3, 4), theme = ggplot2::theme_get(), row.names.y = NULL ) forestplot.default(x, ...) forestplot.summary.rms(x, ...)
## S3 method for class 'summary.rms' plot(x, ...) forestplot( x, return_ggplots = FALSE, plot.widths = c(0.5, 0.5), title = "Odds ratio", digits = 3, shape = 19, header = NULL, limits = NULL, breaks = c(0.5, 1, 1.5, 2, 3, 4), theme = ggplot2::theme_get(), row.names.y = NULL ) forestplot.default(x, ...) forestplot.summary.rms(x, ...)
x |
result of a |
... |
see parameters of method |
return_ggplots |
if |
plot.widths |
the relative widths of the left and right plot
should be a vector ( |
title |
the tile row of the drawn plot |
digits |
the number of significant digits to display |
shape |
point shape, see |
header |
names of the table columns |
limits |
the x axis limits as a vector, see also:
|
breaks |
the x axis breaks as a vector,help see also:
|
theme |
the desired ggplot2 theme |
row.names.y |
new names for the variable rows |
set.seed(123) #load the libraries library(rms) library(ormPlot) library(ggplot2) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ YOBc + sex + height_rzs + n_siblings + cran_rzs, data = educ_data) #the antilog true produces odd ratios (default value for orm and lrm) s<-summary(cran_model, antilog = TRUE) #set the plotting default theme (optional) theme_set(theme_classic()) #show simply the result forestplot(s) #return modifiable ggplots forestplot(s, return_ggplots = TRUE ) #new row names and header newnames <- c("Year of birth", "Height", "Number of children", "Cranial volume", "Sex" ) newhead <- c("Odds Ratio", "CI 5%", "CI 95%" ) #adjust also the relative plot widths and change the color and shape newtheme <- theme_classic() + theme(text = element_text(color = "red", size = 12), line = element_line(color= "red"), rect = element_rect(color="red")) forestplot(s, row.names.y = newnames, header = newhead, plot.widths = c(0.6,0.4), shape = 17, theme = newtheme)
set.seed(123) #load the libraries library(rms) library(ormPlot) library(ggplot2) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ YOBc + sex + height_rzs + n_siblings + cran_rzs, data = educ_data) #the antilog true produces odd ratios (default value for orm and lrm) s<-summary(cran_model, antilog = TRUE) #set the plotting default theme (optional) theme_set(theme_classic()) #show simply the result forestplot(s) #return modifiable ggplots forestplot(s, return_ggplots = TRUE ) #new row names and header newnames <- c("Year of birth", "Height", "Number of children", "Cranial volume", "Sex" ) newhead <- c("Odds Ratio", "CI 5%", "CI 95%" ) #adjust also the relative plot widths and change the color and shape newtheme <- theme_classic() + theme(text = element_text(color = "red", size = 12), line = element_line(color= "red"), rect = element_rect(color="red")) forestplot(s, row.names.y = newnames, header = newhead, plot.widths = c(0.6,0.4), shape = 17, theme = newtheme)
returns a data.frame
object similar to the Predict
however it adds a column dependent that lists all factor levels with
appropriate confidence intervals calculated for each level. It is similar to
predict.lrm
with type="fitted.ind"
but also generates
selected confidence intervals.
predict_with_ci( x, ..., np = 100, fun = stats::plogis, conf.int = 0.95, boot.type = "bca" )
predict_with_ci( x, ..., np = 100, fun = stats::plogis, conf.int = 0.95, boot.type = "bca" )
x |
an object created by |
... |
One or more variables to vary, or single-valued adjustment values.
Specify a variable name without an equal sign to use the default
display range, or any range
you choose (e.g. |
np |
the number of equally-spaced points computed for continuous
predictors that vary, i.e., when the specified value is |
fun |
an optional transformation of the linear predictor.
Specify |
conf.int |
confidence level (highest posterior density interval probability for
Bayesian models). Default is 0.95. Specify |
boot.type |
set to |
a data.frame
set.seed(123) #load the libraries library(rms) library(ormPlot) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #get the predictions of the orm model with confidence intervals for all levels predictiondf<-predict_with_ci(cran_model, cran_rzs, Rural, sex, max_SEP_3) #show the predictions head head(predictiondf) #get the predictions of the orm model with confidence intervals for sex only predictiondf_sex<-predict_with_ci(cran_model, sex) #show the predictions head head(predictiondf_sex)
set.seed(123) #load the libraries library(rms) library(ormPlot) #make the datadist dd<-rms::datadist(educ_data) options(datadist="dd") #create the model cran_model <- orm(educ_3 ~ Rural + sex + max_SEP_3 + cran_rzs, data = educ_data) #get the predictions of the orm model with confidence intervals for all levels predictiondf<-predict_with_ci(cran_model, cran_rzs, Rural, sex, max_SEP_3) #show the predictions head head(predictiondf) #get the predictions of the orm model with confidence intervals for sex only predictiondf_sex<-predict_with_ci(cran_model, sex) #show the predictions head head(predictiondf_sex)