3

My question is naive but I try to understand something : I have some 3D points and I want to compute the plane which fit to my data 3D points with R.

library(scatterplot3d)
x<-sample(1:100, 100)
y<-sample(1:100, 100)
z<-sample(1:100, 100)
xyz <- cbind(x,y,z)
s3d <- scatterplot3d(xyz, type="p", highlight.3d=TRUE, angle=55, scale.y=0.7, pch=16, main="test xyz")
# regression plane
reg <- lm(x ~ y + z)
summary(my.lm)

it returns :

Call:
  lm(formula = x ~ y + z)
Residuals:
   Min      1Q    Median    3Q     Max 
 -51.085 -22.956  -0.801  23.806  51.610 
Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
   (Intercept) 47.34428    8.28049   5.718 1.19e-07 ***
      y         0.11647    0.10163   1.146    0.255    
      z        -0.05398    0.10163  -0.531    0.597    
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 29.04 on 97 degrees of freedom
Multiple R-squared:  0.01826,  Adjusted R-squared:  -0.001985 
F-statistic: 0.9019 on 2 and 97 DF,  p-value: 0.4092

Finally I plot the plane :

s3d$plane3d(47.34428, 0.11647, -0.05398, lty.box = "solid") #or s3d$plane3d(reg, lty.box = "solid")

3D-points and plane

But I don't know/understand how plane3d can generate the equation of the plane from these 3 values... I tried with the rgl.planes3d() (parameters are a, b, c and d, the value of the equation ax+by+cz+d=0) and the plane is not what I expected... Could someone help me ?

EaudeRoche
  • 207
  • 4
  • 19

2 Answers2

2

This is a follow-up on the detailed and valid answer of @coffeinjunky.

The OP is looking for a representation of the plane surface in the form of an equation of the type

a*x + b*y + c*z + d = 0

First of all it should be noted that the coefficients a, b, c, and d are not unique for a given plane surface in 3D, as we can multiply this equation with any real constant that is not equal to zero and obtain an equivalent result.

We are thus free to choose c=1 (provided that we are not dealing with the notorious case of a surface that is perpendicular to the xy plane).

Now the output of scatterplot3D provides, among other things, the intercept, which is the value of z at (x=0,y=0). If i0 is the intercept, we immediately obtain

d = -i0

The other coefficients are easily determined by rearranging the OP's equation into

z = -d - a*x - b*y

The output of scatterplot3d provides the the slope (of z(x,y)) in the x direction (sl_x) and the slope in y direction (sl_y). With

dz/dx = sl_x 

we obtain sl_x= -a and, analogously, sl_y = -b

In summary, one of the equations of the type a*x + b*y +c*z +d = 0 describing the plane in 3D space is given by the coefficients:

a = -sl_x
b = -sl_y
c = 1
d = -i0

Hope this helps.

halfer
  • 19,824
  • 17
  • 99
  • 186
RHertel
  • 23,412
  • 5
  • 38
  • 64
1

Looking at the helpfile of scatterplot3d, see ?scatterplot3d, it says that

plane3d 
function which draws a plane into the existing plot: 
  plane3d(Intercept, x.coef = NULL, y.coef = NULL, lty = "dashed", 
  lty.box = NULL, ...). [...]

So, the first argument is the intercept, the second the coefficient (slope) along the x dimension, and the third the coefficient (slope) along the y dimension. That is also pretty much how your graph looks like. Looking at your inputs: 47.34428 is the intercept. 0.11647 is the slope along the x axis, and -0.05398 is the slope along the y axis.

For instance, inspecting your graph visually, you see that at x=0, the plane is at about 45, which corresponds well with the supplied intercept of 47. x goes from 0 to 100, and you see that at x=100, the value of the plane is 47.34428 + 100*0.11647 = 58.99128, and visually you see it is roughly at 60 if you look at the z axis. The difference along the y axis is difficult to discern since the slope is almost zero, but I think you got the point.

coffeinjunky
  • 11,254
  • 39
  • 57
  • Ok, I see what you mean. I suppose that the reference you use is the axis x because I use lm(x ~ y + z) and not lm(y~x+z)... But for me a plane is define by ax+by+cz+d=0 or by the origin and a vector normal to the plane and these notions of intercept and something seem strange. In the documentation of rgl.planes3d() they use a b c and d extracted with coef() from the lm() output (but the z is used as dependant variable), so I think I'm not familiar enough with lm() and how it works... – EaudeRoche Mar 02 '16 at 13:17