Learn R Programming

lessR (version 1.9.0)

reg: Regression Analysis

Description

Automatically provides a comprehensive regression analysis from a single, simple function call with many default settings. By default the data should exist as a dataframe called mydata, such as provided by the rad function included in this package for reading and processing data in preparation for analysis. If all the variables in the model are not in the same dataframe, the analysis will not be complete.

The default analysis provides the model's parameter estimates and corresponding hypothesis tests and confidence intervals, goodness of fit indices, the ANOVA table, correlation matrix of the model's variables, collinearity analysis of the predictor variables, adjusted R-squared for the corresponding models defined by each possible subset of the predictor variables, and, for each observation in the model, analysis of residuals and influence as well as the confidence and prediction intervals. By default the residual analysis lists the data and fitted value for each observation as well as the residual, Studentized residual and Cook's distance, with the first 25 observations listed and sorted by Cook's distance. The output for the confidence and prediction intervals also provides the data and fitted value for each observations, as well as the lower and upper bounds for each of the two intervals. The observations are sorted by the lower bound of each prediction interval.

Three default graphs are also provided. A histogram is provided with superimposed normal and general density plots from the color.density function included in this package. A scatterplot of the residuals with the fitted values is also provided from the color.plot function included in this package. For models with a single predictor variable, a scatterplot of the data is produced, along with the regression line and corresponding confidence and prediction intervals. For multiple regression models, a scatterplot matrix of the variables in the model is produced.

Overriding the default settings can turn off features and reduce the number of provided analyses.

Usage

reg(my.formula, dframe=mydata, cor=TRUE,
         res.rows=NULL, res.sort=c("cooks","rstudent","off"), 
         pred=TRUE, pred.all=FALSE, pred.sort=c("predint", "off"),
         subsets=TRUE, collinear=TRUE, sig.digits=4, cook.cut=1,
         results=c("full", "brief"), show.R=FALSE)

Arguments

my.formula
Standard R formula for specifying a model. For example, for a response variable named Y and two predictor variables, X1 and X2, specify the corresponding linear model as Y ~ X1 + X2.
dframe
Default is mydata, the name of the data frame that contains the data. The default name is consistent with the name given by the rad function for reading the data, also available in this pack
cor
Default is TRUE, which prints a correlation matrix of the model variables.
res.rows
Default is 25, which lists the first 25 rows of data sorted by the specified sort criterion. To turn this option off, specify a value of 0. To see the output for all observations, specify a value of "all".
res.sort
Default is "cooks", for specifying Cook's distance as the sort criterion for the display of the rows of data and associated residuals. Other values are "rstudent" for Studentized residuals, and "off" to not pr
pred
Default is TRUE, which, produces confidence and prediction intervals for each row of data.
pred.all
Default is FALSE, which, produces prediction intervals only for the first, middle and last five rows of data.
pred.sort
Default is "predint", which sorts the rows of data and associated intervals by the lower bound of each prediction interval. Turn off this sort by specifying a value of "off".
subsets
Default is TRUE, for producing an analysis from the leaps package for the adjusted R-squared of all possible models from the set of predictor variables.
collinear
Default is TRUE, for producing a collinearity analysis from the car package.
sig.digits
Provides the same functionality as the standard options function regarding the digits option. The distinction is that this value applies selectively to portions of the output, the different t
cook.cut
Cutoff value of Cook's Distance at which observations with a larger value are flagged in red and labeled in the resulting scatterplot of Residuals and Fitted Values.
results
Verbosity of displayed results. Default is "full".
show.R
Display the R instructions that yielded the lessR output, albeit without the additional lessR formatting.

Details

Regression analysis automatically provides a variety of regression analyses. The basic analysis successively invokes three standard R functions lm, summary and confint. The residual analysis invokes fitted, resid, rstudent, and cooks.distance. The option for prediction intervals calls the standard R function predict, once with the argument interval="confidence" and once with interval="prediction". If there is only one predictor variable in the model, a scatterplot of the data with regression line is produced, along with the plotted confidence and prediction intervals.

The output for the residual analysis displays by default just the first 25 observations with the largest values of Cook's distance, sorted by this criterion. The output of the prediction intervals is re-organized so that each row's computed fitted value and prediction interval are listed adjacent to the corresponding values of the predictor variables and response variable. Each row of information, the data and corresponding intervals, is by default sorted by the lower bound of the prediction interval.

The options function is called to turn off the stars for different significance levels (show.signif.stars=FALSE) and to turn off scientific notation for the output (scipen=30).

The purpose of reg is to combine these function calls into one, and provide ancillary analyses such as sorting where appropriate to assist in interpretation, and the analysis of the adjusted R-squared for the models defined by all possible subsets of the predictor variables.

See Also

formula, lm, summary.lm, anova, confint, fitted, resid, rstudent, cooks.distance

Examples

Run this code
# Generate random data, place in dataframe mydata
X1 <- rnorm(20)
X2 <- rnorm(20)
Y <- .7*X1 + .2*X2 + .6*rnorm(20)
mydata <- data.frame(X1, X2, Y)

# Call reg for a one-predictor regression
# Provide all default analyses including scatterplot etc.
reg(Y ~ X1)

# Call reg according to a multiple regression model
# Provide the full range of default analyses
reg(Y ~ X1 + X2)
# Provide only the brief analysis
reg(Y ~ X1 + X2, results="brief")

# Call reg and modify the default settings as specified
reg(Y ~ X1 + X2, res.row=8, res.sort="rstudent", sig.digits=8, pred=FALSE)

Run the code above in your browser using DataLab