Exercise

The linear model

It is rather cumbersome to try to get the correct least squares line, i.e. the line that minimizes the sum of squared residuals, through trial and error. Instead we can use the lm() function in R to fit the linear model (a.k.a. regression line).

model <- lm(formula, data = your_dataframe)

The first argument in the function lm() is a formula that takes the form y ~ x. Here it can be read that you want to make a linear model of y as a function of x. The second argument specifies that R should look in the my_dataframe data frame to find the x and y variables.

Instructions

100 XP

Estimate a linear model with runs as the dependent variable and at_bats as the explanatory variable. Use the lm() function and assign the output to m1.