lm_fit: lm_fit

Description Usage Arguments Value Examples

View source: R/lm_fit.R

Description

Fit a linear regression model.

Usage

1
lm_fit(x, y, add.intercept = FALSE, method = "qr", tol = 1e-07)

Arguments

x

a n * p matrix of predictor variables.

y

a length n vector(q = 1) or a n * q matrix of response variables.

add.intercept

logical. If TRUE, add intercept term to the linear regression model.

method

the method to fit the model(only method "qr" and "inv" is supported). Default is qr.

tol

the tolerance for detecting linear dependencies in the columns of x. Default is 1e-7.

Value

lm_fit returns a list containing following response values.

coefficients

q named vectors of coefficients.

residuals

q vectors of residuals, which equals response minus fitted values.

rank

the numeric rank of the fitted linear model.

fitted.values

q vectors of the fitted mean values.

df.residual

the degrees of freedom of residuals.

method

the method to be used for fitting the model.

The following response values are returned and printed only when method = "qr".

effects

q vectors of orthogonal single-df effects.

qr

the QR decomposition of matrix predictor variables.

The functions lm_summary and lm_anova are used to obtain a summary and analysis of variance table of the results.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
n = 10; p = 5; q = 2;
x = matrix(rnorm(n * p), n, p) # no intercept
y1 = rnorm(n)
y2 = matrix(rnorm(n * q), n, q)

# no intercept, using method "qr"
z = lm_fit(x = x, y = y1)
# no intercept, using method "inv"
z = lm_fit(x = x, y = y1, method ="inv")
# with intercept, using method "qr"
z = lm_fit(x = x, y = y2, add.intercept = TRUE)
# with intercept, using method "inv"
z = lm_fit(x = x, y = y2, add.intercept = TRUE, method = "inv")

leyaozh/lm.hw4 documentation built on Dec. 3, 2019, 7:18 a.m.