Function Repository Resource:

# PlaneOfBestFit

Get the hyperplane that best fits a set of points

Contributed by: Jason Biggs
 ResourceFunction["PlaneOfBestFit"][{p1,p2,…}] returns an InfinitePlane that minimizes the sum of squares of orthogonal distances to the 3D points pi.

## Details

In two dimensions, ResourceFunction["PlaneOfBestFit"] returns an InfiniteLine.
In dimensions higher than three, ResourceFunction["PlaneOfBestFit"] returns a Hyperplane.

## Examples

### Basic Examples (3)

Generate a set of random points in space:

 In[1]:=
 Out[4]=

Find the plane of best fit:

 In[5]:=
 Out[5]=

Show the best­fit plane along with the points:

 In[6]:=
 Out[6]=

### Scope (2)

Create a best-fit line for points in 2D. First generate a set of random data:

 In[7]:=
 Out[10]=

Find the line of best fit:

 In[11]:=
 Out[11]=

Show the best­fit line along with the points:

 In[12]:=
 Out[12]=

Create points in 4D such that the spread along the last axis is much less than along the other three:

 In[13]:=

Create a best-fit 3D hyperplane to these points and obtain a normal vector close to {0,0,0,1}:

 In[14]:=
 Out[14]=