knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(linreg)

This vignette is a documentation of how to use the linreg package. For this purpose, we will use the mtcars data set.

usethis::use_vignette("my-vignette")
data(mtcars)
head(mtcars)

Fitting a Linear Regression Model

We use the mtcars data set to fit a linear regression model. In this example, we are interested to investigate the impact of weight (1000 lbs) and number of cylinders on Miles/(US) gallon, for cars in the mtcars data set. Thus, the independent variables are wt and cyl and the dependent variable is mpg, and can be expressed as $\text{mpg} \sim \text{wt} + \text{cyl}$.

A linear regression model is fitted by using the linreg function, which returns an object of an S3 class.

model <- linreg(formula = mpg ~ wt + cyl, data = mtcars, QR = TRUE)

The linreg function calculates the coefficient estimates as well as the variance of the estimates using QR decomposition if QR = TRUE, otherwise the estimates are calculated using ordinary least squares.

Methods

The following methods are able to take an object of class linreg as an argument: print(), plot(), resid(), pred(), coef() and summary()

Let us begin by printing the modelobject using the print() method, which returns the argument and the estimates of the coefficient.

print(model)

Now let us visualize some diagnostics for the linreg object. The plot() method returns two plots with the graphical profile of Linköping University: a plot of residuals against fitted values and a Scale-Location plot of the standardized residuals against fitted values.

plot(model)

To use the LiU theme, simply apply theme_liu() to your ggplot.

Further, we can extract the model residuals from the linreg object and get the coefficients by using resid()and coef(), respectively.

resid(model)

coef(model)

To get a nice overview, we can summarize the linear model fit with summary().

summary(model)


hankOlofs/linreg documentation built on Nov. 2, 2020, 11:25 a.m.