Statistical Computing and Graphics in R

# Statistical Models

Statistical Data Analysis

## lm Function in R

Many generic functions are available for the computation of regression coefficients, for the testing of coefficients, for computation of residuals or predictions values, etc. Therefore, a good grasp of lm() function is necessary. Suppose, we have performed the regression analysis using lm() function as done in the previous lesson.

mod <- lm(mpg ~ hp, data = mtcars)

The object returned by the lm() function has a class of “lm”. The objects associated with the “lm” class have mode as a list.

class(mod)

The name of the objects related to the “lm” class can be queried via

names(mod)

All the components of the “lm” class can be assessed directly. For example,

mod$rank mod$coef   # or mod$coefficients The following is the list of some generic functions for the fitted “lm” model. It is better to save objects from the summary() function. The summary() function returns an object of class “summy.lm()” and its components can be queried via sum_mod <- summary(mod) names(sum_mod) names( summary(mod) ) The objects from the summary() function can be obtained as sum_mod$residuals
sum_mod$r.squared sum_mod$adj.r.squared
sum_mod$df sum_mod$sigma

Scroll to top