Tutorial by Examples

The built-in mtcars data frame contains information about 32 cars, including their weight, fuel efficiency (in miles-per-gallon), speed, etc. (To find out more about the dataset, use help(mtcars)). If we are interested in the relationship between fuel efficiency (mpg) and weight (wt) we may start p...
Continuing on the mtcars example, here is a simple way to produce a plot of your linear regression that is potentially suitable for publication. First fit the linear model and fit <- lm(mpg ~ wt, data = mtcars) Then plot the two variables of interest and add the regression line within the d...
Sometimes we want the model to give more weight to some data points or examples than others. This is possible by specifying the weight for the input data while learning the model. There are generally two kinds of scenarios where we might use non-uniform weights over the examples: Analytic Weights...
Sometimes when working with linear regression we need to check for non-linearity in the data. One way to do this is to fit a polynomial model and check whether it fits the data better than a linear model. There are other reasons, such as theoretical, that indicate to fit a quadratic or higher order ...
After building a regression model it is important to check the result and decide if the model is appropriate and works well with the data at hand. This can be done by examining the residuals plot as well as other diagnostic plots. # fit the model fit <- lm(mpg ~ wt, data = mtcars) # par(mfro...
Once a model is built predict is the main function to test with new data. Our example will use the mtcars built-in dataset to regress miles per gallon against displacement: my_mdl <- lm(mpg ~ disp, data=mtcars) my_mdl Call: lm(formula = mpg ~ disp, data = mtcars) Coefficients: (Intercep...

Page 1 of 1