7) Deploying a One Way Linear Regression Manually with vector arithmetic.

This Blog entry is from the Linear Regression section in Learn R.

The deployment formula for a linear regression model is quite straightforward and is simply a matter of taking the intercept then adding, in this example, the Median_4 value multiplied by the coefficient:

ManualLinearRegression <- 0.01758027 + (FDX$Median_4 * -0.05595731)
1.png

Run the line of script to console:

2.png

As vector arithmetic, has been performed, the formula has been applied to every row of the data frame.  To add this vector to the FDX data frame, use the mutate function of dplyr():

FDX <- mutate(FDX, ManualLinearRegression)
3.png

Run the line of script to console:

4.png

The mutate() function appends the vector to the FDX data frame.  To verify the column has been appended, view the FDX data frame:

View(FDX[,203])
5.png

Run the line of script to console:

6.png

The use of subsetting in the call to the View() function is far less than ideal and it is to compensate for the inability of RStudio to display more than 100 columns in the grid.  In this example, prior to calling the mutate() function there were 202 columns,  after which there were 203:

7.png

The call to the View() function in this manner yields evidence that column has been successfully added:

8.png