predict()

predict()

The predict() function will generate the prediction(s) from a model. 

Example 1:

# Calculate the predictions from the Sex model of Thumb
# Use the lm() function to specify the model
predict(lm(Thumb ~ Sex, data = Fingers))

# Alt: Save the model into an object first
# then specify the name of the object as the argument
# (this method will produce the same output as the method above)
sex_model <- lm(Thumb ~ Sex, data = Fingers)
predict(sex_model

Example output (truncated):

Output of 'predict' function for Sex model of Thumb

Example 2:

To see each prediction in context, you might consider saving the predictions into the data frame as a new column to see more closely what the predict() function is doing.

# Save the predictions back into the data frame
sex_model <- lm(Thumb ~ Sex, data = Fingers)
Fingers$Thumb_predict <- predict(sex_model)
# Select a few rows and the relevant columns to compare
head(select(Fingers, Thumb, Sex, Thumb_predict))

Example output:

Output of a few rows of Thumb, Sex, and Thumb_predict


    • Related Articles

    • statistical model

      A statistical model can help us (1) understand patterns in data, (2) predict what will happen in the future, and (3) improve the functioning of complex systems; there are many kinds of models, but the statistical model we focus on here generates a ...
    • resid()

      The resid() function will calculate the residuals (error) from a model. That is, when given a model, it will take each case and calculate how far away the observed value is from the prediction of the model. Example 1: # Calculate the residuals from ...
    • SS Model

      SS model is the reduction in error (measured in sums of squares) due to the model; the area of all the squared deviations based on the distance between the complex model predictions and the null model predictions.
    • SS Error

      SS error is the amount of error left unexplained by the model; the area of all the squared residuals based on the distance of each score from the model prediction.
    • residual

      Residual is the difference between our model prediction and an actual observed score.