Learn R Programming

lessR (version 4.5.6)

Model: Regression, Logit Regression, ANOVA, or t-test from a Model Specification

Description

Abbreviation: model, model_brief

Specify a model with a standard R formula, and Model selects and runs the appropriate statistical procedure: OLS regression, logit regression, ANOVA, or a t-test. The selection follows from the analytic structure of the model's variables -- whether the response and predictor variables are numerical or categorical, and how many unique values they have. The analysis itself, with its full text and graphics output, is produced by the corresponding lessR function: Regression, Logit, ANOVA, or ttest. Any parameter of the selected function can be passed through the Model call.

By default the data exist in the data frame d, such as data read by the lessR Read function.

Usage

Model(my_formula, data=d, brief=getOption("brief"), xlab=NULL, ...)

model_brief(..., brief=TRUE)

model(...)

Value

The value returned by the selected analysis function, invisibly. The primary output is the text and graphics of the selected analysis.

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.

data

The default name of the data frame that contains the data for analysis is d, otherwise explicitly specify.

brief

If set to TRUE, reduced text output. Can change system default with the style function.

xlab

x-axis label for the graphics output of the logit and t-test analyses, defaults to the variable name, or, if present, the variable label.

...

Parameter values passed to the selected analysis function, Regression, Logit, ANOVA, or ttest.

Author

David W. Gerbing (Portland State University; gerbing@pdx.edu)

Details

OVERVIEW
The purpose of Model is to select the appropriate statistical procedure from the specified model and properties of the data. The analyst specifies the model, the analytic question of interest; Model identifies and runs the procedure that answers it. A message that precedes the output of each analysis reports which procedure was selected and why.

SELECTION OF THE PROCEDURE
Classify the response variable (before the tilde in the formula) and the predictor variables (after the tilde) as categorical -- an R factor, character, or logical variable -- or as numerical. The analysis follows from that classification.

For a categorical response variable with exactly two categories, run a logit regression with Logit, regardless of the type of the predictor variables. A categorical response variable with more than two categories is not supported.

For a numerical response variable:

  • If the values of the response variable are only 0 and 1, run a logit regression with Logit.

  • If there is a single categorical predictor variable with exactly two unique values, compare the two corresponding group means with ttest.

  • If all predictor variables are categorical, compare the group means with ANOVA.

  • Otherwise, that is, for predictor variables that are all numerical, or a mix of numerical and categorical variables, run an OLS regression with Regression, which for one numerical and one categorical predictor variable provides an analysis of covariance.

A numerical response variable with exactly two unique values other than 0 and 1 is intended as binary, so first transform its values to 0 and 1 with recode before running Model.

For further details of each analysis, and the additional parameters available for each, see the corresponding function: Regression, Logit, ANOVA, and ttest.

See Also

Regression, Logit, ANOVA, ttest, formula, lm, glm

Examples

Run this code
# Generate random data, place in data frame d
n <- 200
X1 <- rnorm(n)
X2 <- rnorm(n)
Y <- .7*X1 + .2*X2 + .6*rnorm(n)
Ybin <- cut(Y, breaks=2, labels=FALSE)
#  instead, if read data with the Read function
#   then the result is the data frame called d
d <- round(data.frame(X1, X2, Y, Ybin),2)
rm(Y); rm(Ybin); rm(X1); rm(X2)

# One-predictor regression
# Provide all default analyses including scatterplot etc.
Model(Y ~ X1)
# alternate form
model(Y ~ X1)

# Multiple regression model
# Provide all default analyses
Model(Y ~ X1 + X2)

# Logit analysis from a numeric response variable of 0's and 1's
d <- recode(Ybin, old=c(1,2), new=c(0,1), quiet=TRUE)
Model(Ybin ~ X1)

# t-test
Model(breaks ~ wool, data=warpbreaks)

# ANOVA analysis
# from a data frame other than the default d
# breaks is numerical, wool and tension are categorical
Model(breaks ~ wool + tension, data=warpbreaks)

# Analysis of covariance: one numerical and one categorical predictor
d <- Read("Employee", quiet=TRUE)
Model(Salary ~ Years + Gender)

# Logit analysis from a two-category response variable
Model(Gender ~ Salary)

Run the code above in your browser using DataLab