Exercise

# Linear regression algorithm

To truly understand linear regression, it is helpful to know how the algorithm works. The code for `lm()`

is hundreds of lines because it has to work with any formula and any dataset. However, in the case of simple linear regression for a single dataset, you can implement a linear regression algorithm in just a few lines of code.

The workflow is

- Write a script to calculate the sum of squares.
- Turn this into a function.
- Use R's general purpose optimization functions find the coefficients that minimize this.

The explanatory values (the `n_convenience`

column of `taiwan_real_estate`

) are available as `x_actual`

.
The response values (the `price_twd_msq`

column of `taiwan_real_estate`

) are available as `y_actual`

.

Instructions 1/3

**undefined XP**

- Set the intercept to ten.
- Set the slope to one.
- Calculate the predicted y-values as the intercept plus the slope times the actual x-values.
- Calculate the differences between predicted and actual y-values.
- Calculate the sum of squares. Get the sum of the differences in y-values, squaring each value.