lr: Linear Regression Fitting

Description Usage Arguments Details Value Examples

View source: R/LinearRegressionFitting.R

Description

lr is used for fitting linear regression models with numeric or categorical covariates. It can be used to estimate regression coefficients with least square method and make inference based on relevant t-test and F-test (sequential F-test can be realized by F_test).

Usage

1
lr(formula, data, coding = "reference", intercept = TRUE, reference = 1)

Arguments

formula

an object of class formula: a symbolic form of the model to be fitted which should contain both respond and covariate variables. The details of model specification form are given under "Details".

data

an optional data frame,list or environment containing the variables in the model. If not found in data or by default, the variables would be taken from environment where ls is called.

coding

optional,the method to be used for fitting categorical covaraites in the model. There are two options: coding = "reference" (by default) or "means" (see "Details").

intercept

logical. If TRUE (by default), the corresponding fitting model will contain intercept term.

reference

an optional categorical covariate group which would be considered as reference group when coding = "reference" (by default). If not specified, model fitting will take first group occured among the corresponding covariate as reference.

Details

Models for lr are specified symbolically like "y ~ x1 + x2". A typical model has the form "response ~ covariates" where response is the numeric response vector and covariates are a series of numeric or categorical terms which specifies a linear predictor for response. A covariate specification of the form "x1 + x2" indicates covariates will contain all the observations in "x1" and "x2".

In addition, regarding to the variable in a data frame contianed in model formula, they can be either expressed as mtcars$mpg or mpg with data = mtcars where "mtcars" is the name of a data frame, and "mpg" is the variable name in "mtcars".

coding method is used to cope with model containing categorical covariates. Two common methods: "cell reference coding" and "cell means coding" are supported by setting coding = "reference" (by default) or "means". Former one takes one group of the corresponding categorical covariate as a reference group and reserve intercept in the model, while latter one just eliminates intercept in the model.

Value

linear regression fitting coefficients and reference results.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## A example for numeric respond and covariate variables
y = c(23, 24, 26, 37, 38, 25, 36, 40)
x1 = c(1, 2, 3, 4, 5, 6, 7, 8)
x2 = c(23, 32, 34, 20, 24, 56, 34, 24)
result = lr(y ~ x1 + x2)

## A example for numeric respond and categorical variables
x3 = c("M", "F", "F", "U", "M", "F", "F", "U")
result = lr(y ~ x1 + x2 + x3)

## A example for variables in data frame
result = lr(mpg ~ cyl + disp + hp, data = mtcars)
result = lr(mtcars$mpg ~ mtcars$cyl + mtcars$disp + mtcars$hp)

XuelinGu/LinearRegression documentation built on Dec. 24, 2019, 9:45 a.m.