resid()

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 the empty model (the mean) of Thumb
# Use the lm() function to specify the model
resid(lm(Thumb ~ NULL, data = Fingers))

# Alt: Save the empty 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)
empty_model <- lm(Thumb ~ NULL, data = Fingers)
resid(empty_model

Example output (truncated):

Output of 'resid' function for empty model of Thumb

Example 2:

To see each residual in context, you might consider saving the predictions and the residuals into the data frame as new columns to see more closely what the resid() function is computing.

# Save the predictions and the residuals back into the data frame
Fingers$Thumb_predict <- predict(empty_model)
Fingers$Thumb_resid <- resid(empty_model)
# Select a few rows and the relevant columns to compare
head(select(Fingers, Thumb, Thumb_predict, Thumb_resid))

Example output:

Output of saved predictions and residuals

    • Related Articles

    • sqrt()

      The sqrt() function computes the square root of a value. Example 1: # Calculate the square root of 157 sqrt(157) Example output: Example 2: # Use sqrt() to get the standard deviation around the mean of Thumb empty_model <- lm(Thumb ~ NULL, data = ...
    • abs()

      The abs() function will produce the absolute value for a number. Example: The code below will take the residuals from the empty model for Thumb in the Fingers data frame, and give back their absolute value. empty_model <- lm(Thumb ~ NULL, data = ...
    • residual

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

      SS Total is the amount of error revealed by the empty model (the mean); it is the total area of the squared residuals based on the distance of each score from the mean.
    • 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.