Regression - Diagnostic - Plot - Residuals vs Leverage extension

From Q
Jump to navigation Jump to search

Create a plot of residuals versus leverage values

A chart of residual versus leverage values from a regression model. See this Cross Validated post for a discussion of the interpretation of this diagnostic.

Example

An example output from running this QScript on a Regression - Linear Regressionlinear regression output is shown below. Contours on the plot represent Cook's distanceCook's distance.

Acknowledgements

Uses the plot.lm or plot.glm functions from the stats R package.

Code

includeWeb("QScript R Output Functions");

var is_displayr = (!!Q.isOnTheWeb && Q.isOnTheWeb());
if (!is_displayr)
{
    if (Q.fileFormatVersion() >= 17.13)
        main();
    else
        alert("Please update Q to use this feature from the extension button, or run it from the menu via Create > Regression > Diagnostic > Plot > Residuals vs Leverage.");
}
else
{
    main();
}

function main() {

    // The following 2 variables contain information specific to this diagnostic.
    var required_class = "Regression";
    var output_name_suffix = "residuals.vs.leverage";
    
    var item = checkSelectedItemClass(required_class);
    if (item == null)
        return false;
    var r_name = stringToRName(item.referenceName);

    // The following lines contain the R code to run
    var expression = "plot(" + r_name + ", which = 5)"

    return createROutput(item, expression, output_name_suffix);
}