Operating linear regression and multivariate analysis

Linear regression is a method to assess the association between dependent and independent variables. In this recipe, we will cover how to conduct linear regression for multivariate analysis.

Getting ready

Ensure that mtcars has already been loaded into a data frame within an R session.

How to do it...

Perform the following steps:

  1. To fit variables into a linear model, you can use the lm function:
    > lmfit = lm(mtcars$mpg ~ mtcars$cyl)
    > lmfit
    lm(formula = mtcars$mpg ~ mtcars$cyl)
    (Intercept)   mtcars$cyl  
         37.885       -2.876 
  2. To get detailed information on the fitted model, you can use the summary function:
    > summary(lmfit)
    lm(formula = mtcars$mpg ~ mtcars$cyl)
     Min 1Q ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.