8) Using the predict function for a one way linear regression one.

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

Deploying a linear regression model manually is rather simple, however, there is an even simpler method available in calling the predict() function which takes a model and a data frame as its parameter,  returning a prediction vector.

AutomaticLinearRegression <- predict.lm(LinearRegression,FDX)

Run the line of script to console:

Add the newly created vector to the FDX data frame:

FDX <- mutate(FDX, AutomaticlLinearRegression)

Run the line of script to console:

To view the last two columns of the data frame, containing a manually derived prediction and automatically derived prediction:

View(FDX[,203:204])

Run the line of script to console:

The manual and automatic prediction shown side by side are identical to each other.  It follows that the automatic prediction is a much more concise means to execute the prediction based upon a linear regression model created in R:

6) Creating a One Way Linear Regression Model.

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

Beforehand the lm() function was used inside the stat_smooth() function of ggplo2 to create a linear regression solution,  rather line of best fit. Naturally the lm() function can also be used to create linear regression model which can be deployed as a predictive model in its own right.

To create a linear regression model with one dependent variable and one independent variable:

LinearRegression <- lm(Dependent ~ Median_4,FDX)

Run the line of script to console:

Once the model has been computed it can be output:

LinearRegression

Run the line of script to console:

The summary() function can be used to expand on the validity and performance of the model:

summary(LinearRegression)

Run the line of script to console:

A more traditional Linear Regression model has now been written out.   It is worth checking the precision of the coefficients to ensure that they have not been truncated, as this can lead to a profound change in the predicted values:

coeefeicents(LinearRegression)

Run the line of script to console:

It can be seen that the coefficients written out have rather more decimal places, or precision, which will be extremely important when seeking to make accurate predictions.