test.linear: Tests for Linearity in a Linear Model

View source: R/test.linear.R

test.linearR Documentation

Tests for Linearity in a Linear Model

Description

This function performs the regression equation specification error (RESET) test (Remsey, 1969), the Harvey-Collier test (Harvey & Collier, 1977), and the rainbow test for linearity (Utts, 1982). By default, the function performs the RESET test.

Usage

test.linear(model, method = c("reset", "harv", "rain"), power = c(2, 3),
            frac = 0.5, order = c("none", "all", "mahalanobis"), p.adj = "none",
            digits = 2, p.digits = 3, plot = FALSE,
            geom = c("all", "none", "lm", "loess", "ci.lm", "ci.loess"),
            filename = NULL, width = NA, height = NA, dpi = 600,
            write = NULL, append = TRUE, check = TRUE, output = TRUE, ...)

Arguments

model

a fitted model of class lm.

method

a character string indicating the method used for testing linearity, i.e., "reset" (default) for the regression equation specification error (RESET) test, "harv" for the Harvey-Collier test, and "rain" for the rainbow test for linearity.

power

a positive integer vector indicating the powers of the fitted values that should be included in the auxiliary regression model. By default, the test includes quadratic and cubic terms in the model, i.e., c(2, 3). Note that this argument is only used for the RESET test.

frac

a numeric value between 0 and 1 indicating the proportion of observations used in the central subset model. Note that this argument is only used for the rainbow test.

order

a character string indicated how the observations are ordered, i.e., "none" for observations assumed to be already ordered, "all" for ordering each predictor in the model specified in the "model" argument separately, "mahalanobis" for ordering observations by their Mahalanobis distances from the mean regressor. Note that this argument is only used for the Harvey-Collier test and the rainbow test.

p.adj

a character string indicating an adjustment method for multiple testing based on p.adjust, i.e., none (default), bonferroni, holm, hochberg, hommel, BH, BY, or fdr.

digits

an integer value indicating the number of decimal places to be used for displaying results.

p.digits

an integer value indicating the number of decimal places to be used for displaying the p-value.

plot

logical: if TRUE, partial residual plots or component-plus-residual plots are drawn. For additional plotting arguments, see Details in the help page of the function plot.misty.object.

geom

character vector indicating which plot elements to draw, i.e., "all" (default) for drawing all plot elements, "none" for drawing no regression or smoothing line, "lm" for drawing the regression line, "loess" for drawing the LOESS smoothing curve, "ci.lm" for drawing confidence bands around the regression line, "ci.loess" for drawing confidence bands around the LOESS smoothing curve (i.e., se = TRUE).

filename

a character string indicating the filename argument including the file extension in the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument. Note that plots can only be saved when plot = TRUE.

width

a numeric value indicating the width argument (default is the size of the current graphics device) for the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) for the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) for the ggsave function.

write

a character string naming a text file with file extension ".txt" (e.g., "Output.txt") for writing the output into a text file.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is shown.

...

additional plotting arguments from the plot.misty.object() function, see 'Details in the plot.misty.object function.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

model

model specified in model

plot

ggplot2 object for plotting the results

args

specification of function arguments

result

data frame with results

Note

This function is based on the resettest(), harvtest(), and raintest() function in the lmtest package by Achim Zeileis and Torsten Hothorn.

Author(s)

Takuya Yanagida

References

Harvey, A., & Collier, P. (1977). Testing for functional misspecification in regression analysis. Journal of Econometrics, 6(1), 103-119.

Ramsey, J. B. (1969). Tests for specification errors in classical linear least-squares regression analysis. Journal of the Royal Statistical Society: Series B (Methodological), 31(2), 350-371.

Utts, J. M. (1982). The rainbow test for lack of fit in regression. Communications in Statistics - Theory and Methods, 11(24), 2801-2815.

Zeileis, A., & Hothorn, T. (2002). Diagnostic checking in regression relationships. R News, 2(3), 7-10.

See Also

check.resid, test.homo, test.norm, coeff.robust

Examples

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Tests for Linearity in a Linear Model

# Estimate linear model
mod <- lm(mpg ~ disp + hp + drat, data = mtcars)

# Example 1a: RESET test (default)
test.linear(mod)

# Example 1b: Harvey-Collier test
test.linear(mod, method = "harv")

# Example 1c: Rainbow test
test.linear(mod, method = "rain")

#————————————————————————————————————————————————————————————————————————————
# 'order' Argument

# Example 2a: Harvey-Collier test, order observations by 'hp'
test.linear(mod, method = "harv", order = "hp")

# Example 2a: Rainbow test, order by Mahalanobis distances
test.linear(mod, method = "rain", order = "mahalanobis")

#————————————————————————————————————————————————————————————————————————————
# Plot

# Example 3a: Partial residual plots
test.linear(mod, plot = TRUE)

# Example 3b: Plot with smoothing line only
test.linear(mod, plot = TRUE, geom = "loess")

# Example 3c: Partial residual plots, additional arguments
# see Details in the help page of the function plot.misty.object
test.linear(mod, plot = TRUE, point.size = 1, point.shape = 2)

# Example 3d: Plot results using the plot() function, additional arguments
# see Details in the help page of the function plot.misty.object
object <- test.linear(mod)
plot(object, point.size = 1, point.shape = 2)

#————————————————————————————————————————————————————————————————————————————
# Write Results

# Example 4a: Write results into a text file
test.linear(mod, write = "Test_Linear.txt")

# Example 4b: Write results into an Excel file
test.linear(mod, write = "Test_Linear.xlsx")

# Example 4c: Save plot as PNG file
test.linear(mod, plot = TRUE, filename = "Linear.png", width = 15, height = 5)

## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to test.linear in misty...