简体   繁体   English

如何通过 $plane3d 函数使用 scatterplot3d 绘制 R 线性模型 lm()

[英]How to plot R linear model lm() using scatterplot3d via $plane3d function

Stackoverflow community, Stackoverflow 社区,

I am looking to plot the results of R's lm() as a plane in a 3d graph made with the scatterplot3d() command from the R package scatterplot3d.我希望将 R 的lm()的结果绘制为使用 R 包 scatterplot3d 中的scatterplot3d()命令制作的 3d 图中的平面。 I keep getting multiple errors, depending on my method of trying to graph via the $plane3d() function.我不断收到多个错误,这取决于我尝试通过$plane3d()函数绘制图形的方法。

First, some reproducible data - Step 1: making the data-frame首先,一些可重复的数据 - 第 1 步:制作数据框

elem <- data.frame(pH = c(8.12, 8.19, 6.09, 5.99, 5.18, 6.09, 5.40, 5.50, 4.93, 5.16, 7.57, 7.21, 5.13, 6.23, 5.72), 
              water_Loss = c(0.010, 0.005, 0.065, 0.120, 0.250, 0.305, 0.100, 0.020, 0.430, 0.060, 0.065, 0.050, 0.025, 0.050, 0.020), 
              elev = c(2397, 2393, 2593, 2599, 2741, 2774, 2979, 2787, 3173, 3370, 2147, 2130, 2374, 2359, 2643), 
              co2 = c(1.8410, 1.9810, 2.0110, 1.8960, 1.3060, 2.0160, 1.7360, 1.5860, 1.6360, 1.9665, 1.6360, 1.7660, 1.9760, 2.7510, 1.3310))

Step 2 - fitting the linear model第 2 步 - 拟合线性模型

lms <- lm(elem$co2 ~ elem$pH + elem$water_Loss + elem$elev + I(elem$pH * elem$water_Loss * elem$elev))
summary(lms)

To note: there aren't lms$model$x and lms$model$y parameters on the results of this linear model注意:这个线性模型的结果没有 lms$model$x 和 lms$model$y 参数

Step 3 - making the 3d graph第 3 步 - 制作 3d 图形

library(scatterplot3d)

s3d <- scatterplot3d(elem[, -4], pch = 19, type = "p", grid = TRUE, box = FALSE, angle = 55)

To graph the scatterplot graph + lm() result, the s3d$plane3d() is run directly after plotting the graph first.要绘制散点图 + lm()结果,首先在绘制图形后直接运行s3d$plane3d()

Like this:像这样:

s3d <- scatterplot3d(elem[, -4], pch = 19, type = "p", grid = TRUE, box = FALSE, angle = 55) s3d$plane3d() s3d <- scatterplot3d(elem[, -4], pch = 19, type = "p", grid = TRUE, box = FALSE, angle = 55) s3d$plane3d()

However, moving forward, I will only indicate the s3d$plane3d() portion.但是,继续前进,我只会指出s3d$plane3d()部分。

This is where the issue comes in. I will highlight 3 different ways I have tried to get the linear model to display on this graph这就是问题所在。我将强调 3 种不同的方法,我试图让线性模型显示在此图上

Attempt 1: Plotting the results of lms directly尝试 1:直接绘制 lms 的结果

s3d$plane3d(lms, draw_polygon = TRUE, draw_lines = TRUE)

Which produces the following error:这会产生以下错误:

Error in xy.coords(x, y, setLab = FALSE) : 'x' and 'y' lengths differ xy.coords(x, y, setLab = FALSE) 中的错误:“x”和“y”长度不同

Looking into fixing this, I went here: 'x' and 'y' lengths differ ERROR when plotting为了解决这个问题,我去了这里: 绘图时'x'和'y'长度不同错误

Like the post suggested, I used the flag lm(x = TRUE, y = TRUE) but I still had no such parameters in the lm() results to be able to check the length() and this did not work.就像帖子建议的那样,我使用了标志lm(x = TRUE, y = TRUE)但我在lm()结果中仍然没有这样的参数能够检查length()并且这不起作用。

Attempt 2: Specifying the x,y,z coordinates with intercept in the scatterplot尝试 2:在散点图中用截距指定 x,y,z 坐标

I was following this suggestion to do so: R - Extending Linear Model beyond scatterplot3d我是按照这个建议这样做的: R - 将线性模型扩展到 scatterplot3d

For the intercept flag, I used the following code: lms$coefficients and took the value under (Intercept).对于拦截标志,我使用了以下代码: lms$coefficients并在 (Intercept) 下取值。

s3d$plane3d(xyz.coords(x = as.numeric(lms$model$`elem$pH`), y = as.numeric(lms$model$`elem$water_Loss`), z = as.numeric(lms$model$`elem$elev`)), Intercept = 3.010033e+00  )

Which produces the following error:这会产生以下错误:

Error in x.coef * x.scal : non-numeric argument to binary operator x.coef * x.scal 中的错误:二元运算符的非数字参数

Attempt 3: Plotting individual coefficients & attempting to draw polygons and lines尝试 3:绘制单个系数并尝试绘制多边形和线条

I tried it this way after reading the documentation in R for the planes3d() command from {rgl}在阅读了 R 中的文档后,我以这种方式尝试了 {rgl} 中的planes3d()命令

Getting the coefficients:获取系数:

coefs <- coef(lms) ; coefs
s3d$plane3d(a = -5.901006e-02 , b = -1.546285e+01, c = -2.946729e-04, Intercept = 3.010033e+00)

Which produces the following error:这会产生以下错误:

Error in x.coef * x.scal : non-numeric argument to binary operator x.coef * x.scal 中的错误:二元运算符的非数字参数

I also tried to add the flags draw_polygon = TRUE, draw_lines = TRUE to the above command, which only gave yet another error - bottom line - did not work.我还尝试将标志draw_polygon = TRUE, draw_lines = TRUE到上述命令中,这仅给出了另一个错误 - 底线 - 不起作用。

At this point, I am at a complete loss (I've tried many other methods - which I can't post them all).在这一点上,我完全不知所措(我尝试了许多其他方法 - 我无法全部发布)。 I would like to ask help in trying to pin-point what exactly I am missing to plot this plane on this graph.我想寻求帮助,试图找出在这张图上绘制这个平面时我到底缺少什么。 Any and all help will be very much appreciated.任何和所有的帮助将不胜感激。

Thank you.谢谢你。

scatterplot3d() will not be able to plot models with larger dimensionality (than 2 input dimensions and 1 output dimension) in 3D. scatterplot3d()将无法在 3D 中绘制具有更大维度(超过 2 个输入维度和 1 个输出维度)的模型。 In fact, such a plot would not be valid since the values in the additional dimensions will presumably be different for the different observations.事实上,这样的图是无效的,因为对于不同的观察,附加维度中的值可能会有所不同。 They would therefore influence how closely the model fits and a plot that neglects these would be misleading.因此,它们会影响模型拟合的紧密程度,而忽略这些的绘图会产生误导。

That said, s3d$plane3d does not handle malformed input very well.也就是说, s3d$plane3d不能很好地处理格式错误的输入。 For instance, if the dimensionality of the model is not as expected, it will return confusing error messages (as you have seen).例如,如果模型的维度不符合预期,它将返回令人困惑的错误消息(如您​​所见)。 There is also no help for this function and in fact the function is nested in another function in the package and has no comments.此函数也没有帮助,实际上该函数嵌套在包中的另一个函数中并且没有注释。 As a result this will all be fairly difficult to understand, but if you want to go deeper you have to read the code of the package, which you can find here .因此,这一切都相当难以理解,但如果您想深入了解,则必须阅读包的代码,您可以在此处找到。

You can absolutely have your plot show a partial regression surface, but you have to tell plot3d, which dimensions you want.您绝对可以让您的绘图显示部分回归曲面,但您必须告诉 plot3d,您想要哪些尺寸。 Essentially you'd be plotting a plane in 3d space where you should have a hyperplane in higher dimensional space.本质上,您将在 3d 空间中绘制一个平面,您应该在更高维空间中有一个超平面。

Your attempt 2 was on the right track.您的尝试 2 走在正确的轨道上。 But you do not hand over the right argument.但是你没有交出正确的论点。 The function wants x.coef and y.coef etc. but not xyz.coords and therefore it apparently tries to interpret the vectors you hand over as coefficient and fails.该函数需要x.coefy.coef等,但不是xyz.coords ,因此它显然试图将您移交的向量解释为系数并失败。 You could do this instead:你可以这样做:

s3d$plane3d(Intercept=lms$coefficients["(Intercept)"][[1]],
            x.coef=lms$coefficients["elem$pH"][[1]],
            y.coef=lms$coefficients["elem$water_Loss"][[1]],
            draw_polygon = TRUE, 
            draw_lines = TRUE, 
            polygon_args = list(col = rgb(0.8, 0.8, 0.8, 0.8)))

However, it is unlikely, however, that you will even see the regression surface in your plot, because the influence of the dimensions you do not plot will shift it out of the visible area of your figure.但是,您甚至不太可能在绘图中看到回归曲面,因为您未绘制的维度的影响会将其移出图形的可见区域。 If you want to pull it back by force, you have to modify your intercept:如果你想强行拉回来,你必须修改你的拦截:

average_intercept <- lms$coefficients["(Intercept)"][[1]] + lms$coefficients["elem$elev"][[1]] * mean(elem$elev)
s3d$plane3d(Intercept=average_intercept,
            x.coef=lms$coefficients["elem$pH"][[1]],
            y.coef=lms$coefficients["elem$water_Loss"][[1]],
            draw_polygon = TRUE, 
            draw_lines = TRUE, 
            polygon_args = list(col = rgb(0.8, 0.8, 0.8, 0.8)))

But the plane you see is actually only a 2d-slice through the 3d-surface that is your regression and accurately only represents the observations you that happen to have exactly the average value in that third dimension ( elev in your case).但是您看到的平面实际上只是通过 3d 表面的 2d 切片,这是您的回归,并且准确地仅表示您恰好在第三维(在您的情况下为elev中恰好具有平均值的观察结果。

In fact, this is exactly what you would get if you ran the regression without the additional dimension(s);事实上,这正是你在没有额外维度的情况下运行回归时得到的结果; so you might as well do and plot that.所以你也可以这样做并绘制它。

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

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