R: Function for calibration plot and Hosmer-Lemeshow goodness of...

plotCalibration {PredictABEL}R Documentation

Function for calibration plot and Hosmer-Lemeshow goodness of fit test.


The function produces a calibration plot and provides Hosmer-Lemeshow goodness of fit test statistics.


plotCalibration(data, cOutcome, predRisk, groups, rangeaxis, 
plottitle, xlabel, ylabel, filename, fileplot, plottype)



Data frame or numeric matrix that includes the outcome and predictor variables.


Column number of the outcome variable.


Vector of predicted risks of all individuals in the dataset.


Number of groups considered in Hosmer-Lemeshow test. Specification of groups is optional (default groups is 10).


Range of x-axis and y-axis. Specification of rangeaxis is optional. Default is c(0,1).


Title of the plot. Specification of plottitle is optional. Default is "Calibration plot".


Label of x-axis Default. Specification of xlabel is optional. Default is "Predicted risk".


Label of y-axis. Specification of ylabel is optional. Default is "Observed risk".


Name of the output file in which the calibration table is saved. The file is saved as a txt file in the working directory. When no filename is specified, the output is not saved. Example: filename="calibration.txt"


Name of the file that contains the calibation plot. The file is saved in the working directory in the format specified under plottype. Example: fileplot="plotname". Note that the extension is not specified here. When fileplot is not specified, the plot is not saved.


The format in which the plot is saved. Available formats are wmf, emf, png, jpg, jpeg, bmp, tif, tiff, ps, eps or pdf. Foe example, plottype="eps" will save the plot in eps format. When plottype is not specified, the plot will be saved in jpg format.


Hosmer-Lemeshow test statistic is a measure of the fit of the model, comparing observed and predicted risks across subgroups of the population. The default number of groups is 10.

The function requires the outcome of interest and predicted risks of all individuals. Predicted risks can be obtained from the functions fitLogRegModel and predRisk or be imported from other packages or methods.


The function creates a calibration plot and returns the following measures:


Chi square value of Hosmer-Lemeshow test


Degrees of freedom, which is (groups-2) where groups: number of groups


p-value of Hosmer-Lemeshow test for goodness of fit


Hosmer DW, Hosmer T, Le Cessie S, Lemeshow S. A comparison of goodness-of-fit tests for the logistic regression model. Stat Med 1997; 16:965-980.

See Also



# specify dataset with outcome and predictor variables 
# specify column number of the outcome variable
cOutcome <- 2

# fit a logistic regression model
# all steps needed to construct a logistic regression model are written in a function
# called 'ExampleModels', which is described on page 4-5
riskmodel <- ExampleModels()$riskModel2

# obtain predicted risks
predRisk <- predRisk(riskmodel)

# specify range of x-axis and y-axis  
rangeaxis <- c(0,1) 
# specify number of groups for Hosmer-Lemeshow test            
groups <- 10 

# compute calibration measures and produce calibration plot                          
plotCalibration(data=ExampleData, cOutcome=cOutcome, predRisk=predRisk, 
groups=groups, rangeaxis=rangeaxis)