title: (all the) directions perpendicular to hyperplane through p data points
body: <p>I have a simple question: 
  given p points (non-collinear) in R^p i find the hyperplane passing by these points (to help clarify i type everything in R):</p>
  
  <pre><code>p&lt;-2
  x&lt;-matrix(rnorm(p^2),p,p)
  b&lt;-solve(crossprod(cbind(1,x[,-2])))%*%crossprod(cbind(1,x[,-2]),x[,2])
  </code></pre>
  
  <p>then, given a p+1^th points not collinear with first p points, i find the direction perpendicular to b:</p>
  
  <pre><code>x2&lt;-matrix(rnorm(p),p,1)
  b2&lt;-solve(c(-b[-1],1)%*%t(c(-b[-1],1))+x2%*%t(x2))%*%x2
  </code></pre>
  
  <p>That is, b2 defines a p dimensional hyperplane perpendicular to b and passing by x2.
  Now, my questions are:</p>
  
  <p>The formula comes from <a href="http://en.wikipedia.org/wiki/Surface_normal" rel="nofollow noreferrer">my interpretation of this wikipedia entry</a> ("solve(A)" is the R command for A^-1). Why this doesn't work for p>2 ? What am i doing wrong ?</p>
  
  <p>PS: I have seen this post (on stakeoverflow edit:sorry cannot post more than one link) but somehow it doesn't help me.</p>
  
  <p>Thanks in advance,</p>
  
  <p>i have a problem implementation/understanding of Liu's solution when p>2:</p>
  
  <p>shouldn't the dot product between the qr decomposition of the sweeped matrix and the direction of the hyperplane be 0 ? (i.e. if the qr vectors are perpendicular to the hyperplane)</p>
  
  <p>i.e, when p=2 this </p>
  
  <pre><code>c(-b[2:p],1)%*%c(a1)
  </code></pre>
  
  <p>gives 0. When p>2 it does not. </p>
  
  <hr>
  
  <p>Here is my attempt to implement Victor Liu's solution.</p>
  
  <p>a) given p linearly independent observations in R^p:</p>
  
  <pre><code>p&lt;-2;x&lt;-matrix(rnorm(p^2),p,p);x
        [,1]       [,2]
  [1,] -0.4634923 -0.2978151
  [2,]  1.0284040 -0.3165424
  </code></pre>
  
  <p>b) stake them in a matrix and subtract the first row:</p>
  
  <pre><code>a0&lt;-sweep(x,2,x[1,],FUN="-");a0
          [,1]        [,2]
  [1,] 0.000000  0.00000000
  [2,] 1.491896 -0.01872726
  </code></pre>
  
  <p>c) perform a QR decomposition of the matrix a0. The vector in the nullspace is the direction im looking for:</p>
  
  <pre><code>qr(a0)
            [,1]       [,2]
  [1,] -1.491896 0.01872726
  [2,]  1.000000 0.00000000
  </code></pre>
  
  <p>Indeed; this direction is the same as the one given by application of the formula from wikipedia (using x2=(0.4965321,0.6373157)): </p>
  
  <pre><code>       [,1]
  [1,]  2.04694853
  [2,] -0.02569464
  </code></pre>
  
  <p>...with the advantage that it works in higher dimensions.</p>
  
  <p>I have one last question: what is the meaning of the other p-1 (i.e. (1,0) here) QR vector when p>2 ?
  -thanks in advance, </p>