Why is this regression plot only plotting 2 of the 4 regression coefficients?

Viewed 10362

I have the following set of data: https://archive.ics.uci.edu/ml/datasets/abalone

I am trying to plot a regression for the whole weight against the diameter.

A scatter plot of the data is clearly not a linear function. (I am unable to attach it for some reason.)

Consider a quadratic regression model. I set it up like so:

abalone <- read.csv("abalone.data")
diameter <- abalone$Diameter
diameter2 <- diameter^2
whole <- abalone$Whole.weight

quadraticModel <- lm( whole ~ diameter + diameter2)

This is fine and gives me the following when calling quadraticModel:

Call:
lm(formula = whole ~ diameter + diameter2)

Coefficients:
(Intercept)     diameter    diameter2  
     0.3477      -3.3555      10.4968  

However, when I plot:

abline(quadraticModel)

I get the following warning:

Warning message:
In abline(quadraticModel) :
  only using the first two of 3 regression coefficients

which means that I am getting a straight line plot which isn't what I am aiming for. Can someone please explain to me why this is happening and possible ways around it? I am also having the same issue with cubic plots etc. (They always just plot the first two coefficients.)

2 Answers

You can not use abline to plot polynomial regression fitted. Try this:

x<-sort(diameter)
y<-quadraticModel$fitted.values[order(diameter)]
lines(x, y) 

I don't think you're producing a quadratic fit, rather a linear fit using diameter and the squared diameter. Try this instead:

library(stats)

df <- read.csv("abalone.data")
var_names <-
  c(
    "Sex",
    "Length",
    "Diameter",
    "Height",
    "Whole_weight",
    "Shucked_weight",
    "Viscera_weight",
    "Shell_weight",
    "Rings"
  )
colnames(df) <- var_names


fit <- lm(df$Whole_weight ~ poly(df$Diameter, 2))
summary(fit)

diameter <- df$Diameter
predicted_weight <- predict(fit, data.frame(x = diameter))

plot(diameter, predicted_weight)

enter image description here

> summary(fit)

Call:
lm(formula = df$Whole_weight ~ poly(df$Diameter, 2))

Residuals:
     Min       1Q   Median       3Q      Max 
-0.66800 -0.06579 -0.00611  0.04590  0.97396 

Coefficients:
                       Estimate Std. Error t value Pr(>|t|)    
(Intercept)            0.828818   0.002054  403.44   <2e-16 ***
poly(df$Diameter, 2)1 29.326043   0.132759  220.90   <2e-16 ***
poly(df$Diameter, 2)2  8.401508   0.132759   63.28   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1328 on 4173 degrees of freedom
Multiple R-squared:  0.9268,    Adjusted R-squared:  0.9267 
F-statistic: 2.64e+04 on 2 and 4173 DF,  p-value: < 2.2e-16
Related