Search code examples
r3d2dleast-squareslms

Adding least squares and LMS lines to a plot


I have loaded the data set Animals2 from the package library (robustbase), and I am interested on working with the logarithm of these data.

library(robustbase)
x<-log(Animals2)
plot(x, main="Plot Animals2", col="darkgreen")

Now I need to add the least-squares line, the LMS line, and the line of Siegel, by using different colours. Finally, I also wanted to show two plots with the estimated 2-dimensional densities. The first plot should be a 3D visualization of the density (I was trying to use command persp) and the second plot with the command image (applied to the density estimate).

I am a bit stuck and would appreciate some help.


Solution

  • Assuming that mblm() does the Siegel model, you could use this:

      library(robustbase)
      library(mblm)
      data(Animals2)
      x <- log(Animals2)
      plot(x, main="Plot Animals2", col="darkgreen")
      abline(lm(brain ~ body, data=x), col="red")
      abline(MASS::lqs(brain ~ body, data=x, method="lms"), col="blue")
      abline(mblm(brain ~ body, data=x, repeated=TRUE), col="black")
      legend("topleft", c("LM", "LMS", "Siegel"), 
             col = c("red", "blue", "black"), 
             lty = c(1,1,1), 
             inset=.01)
    

    
    
    d2 <- MASS::kde2d(x$body, x$brain)
    persp(d2)
    

    image(d2, xlab="body", ylab="brain")
    

    Created on 2022-05-20 by the reprex package (v2.0.1)