Regression - Diagnostic - Normality (Shapiro-Wilk) extension

From Q
Jump to: navigation, search

Conducts the Shapiro-Wilk test of normality on the (deviance) residuals of a Regression output.

Example

An example output appears below.

Technical details

A small p-value indicates a departure from normality. For large samples, a departure from normality will almost always be detected.

The test is performed on the deviance residuals in a model, which are automatically extracted from a model using resid. For more information on the use of residuals in regression modeling, see this blog post.

Acknowledgements

Uses the R function shapiro.test in the stats package.

References

Shapiro, S. S.; Wilk, M. B. (1965). "An analysis of variance test for normality (complete samples)". Biometrika. 52 (3–4): 591–611. DOI: 10.1093/biomet/52.3-4.591.

Royston, P. (1982). An extension of Shapiro and Wilk's W test for normality to large samples. Applied Statistics. 31: 115-124. DOI: 10.2307/2347973.

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.");
}
else
{
    main();
}

function main() {

    // The following 2 variables contain information specific to this diagnostic.
    var required_class = "Regression";
    var output_name_suffix = "normality";
    
    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 = "flipFormat::SignificanceTest(\n    shapiro.test(resid(" + r_name 
        + ")),\n    'Test of Residual Normality (Shapiro-Wilk)',\n    vars = NULL,\n    reg.name = '"
        + item.name + "',\n    reg.sample.description = " + r_name + "$sample.description)";

    return createROutput(item, expression, output_name_suffix);
}