简体   繁体   English

将最小二乘法和 LMS 线添加到绘图中

[英]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)加载了数据集Animals2 ,我对使用这些数据的对数很感兴趣。

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.现在我需要使用不同的颜色添加最小二乘线、LMS 线和 Siegel 线。 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).第一个图应该是密度的 3D 可视化(我试图使用命令persp )和带有命令image的第二个图(应用于密度估计)。

I am a bit stuck and would appreciate some help.我有点卡住了,希望能得到一些帮助。

Assuming that mblm() does the Siegel model, you could use this:假设mblm()使用 Siegel 模型,您可以使用它:

  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)reprex 包于 2022-05-20 创建 (v2.0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM