MATH 376 -- Probability and Statistics 2 

April 12, 2010 

Multiple Regression and Least Squares -- Matrix Formulation. 

 

Example 1. 

Normal equations for fitting the linear model

Z = `+`(beta[0], Typesetting:-delayDotProduct(beta[1], x, true), Typesetting:-delayDotProduct(beta[2], y, true), epsilon)
 

 

to the data points [0,0,2], [1,0,1], [0,1,4], [1,1,5] 

> with(LinearAlgebra); -1; with(plots); -1
 

> X:=Matrix([[1.0,0,0],[1,1,0],[1,0,1],[1,1,1]]);
 

Matrix(%id = 157644504) (1)
 

> Z:=Matrix([[2],[1],[4],[5]]);
 

Matrix(%id = 157644568) (2)
 

> XtX:=Multiply(Transpose(X),X);
 

Matrix(%id = 157645016) (3)
 

> XtZ:=Multiply(Transpose(X),Z);
 

Matrix(%id = 157645528) (4)
 

> beta:=LinearSolve(XtX,XtZ);
 

Matrix(%id = 157646040) (5)
 

> PlP:=plot3d(beta[1,1]+beta[2,1]*x+beta[3,1]*y,x=-0.5..1.5,y=-0.5..1.5,axes=boxed):
 

> SP1:=plot3d([0.1*cos(theta)*sin(phi),0.1*sin(theta)*sin(phi),2+0.1*cos(phi)],theta=0..2*Pi,phi=0..Pi,color=blue):
 

> SP2:=plot3d([1+0.1*cos(theta)*sin(phi),0.1*sin(theta)*sin(phi),1+0.1*cos(phi)],theta=0..2*Pi,phi=0..Pi,color=blue):
 

> SP3:=plot3d([0.1*cos(theta)*sin(phi),1+0.1*sin(theta)*sin(phi),4+0.1*cos(phi)],theta=0..2*Pi,phi=0..Pi,color=blue):
 

> SP4:=plot3d([1+0.1*cos(theta)*sin(phi),1+0.1*sin(theta)*sin(phi),5+0.1*cos(phi)],theta=0..2*Pi,phi=0..Pi,color=blue):
 

> display3d(SP1,SP2,SP3,SP4,PlP,scaling=constrained);
 

Plot_2d
 

We see the plane in `*`(`^`(R, 3))  that comes closest to containing the 4 data points. 

 

Example 2. 

Normal equations for fitting the linear model  

 

 Y = `+`(beta[0], Typesetting:-delayDotProduct(beta[1], x, true), Typesetting:-delayDotProduct(beta[2], `*`(`^`(x, 2)), true), epsilon) 

 

to the data points  [0,1], [1,4], [2,7], [3,8]. 

We let X = Matrix(%id = 157650392)   and  Y = Vector[column](%id = 157650456) 

The normal equations are  

Typesetting:-delayDotProduct(Typesetting:-delayDotProduct(`^`(X, t), X, true), beta, true) = Typesetting:-delayDotProduct(`^`(X, t), Y, true) 

> X:=Matrix([[1.0,0,0],[1,1,1],[1,2,4],[1,3,9]]);
 

Matrix(%id = 157647512) (6)
 

> Y:=Matrix([[1],[4],[7],[8]]);
 

Matrix(%id = 157647576) (7)
 

> XtX:=Multiply(Transpose(X),X);
 

Matrix(%id = 157647832) (8)
 

> XtY:=Multiply(Transpose(X),Y);
 

Matrix(%id = 157648152) (9)
 

> beta:=LinearSolve(XtX,XtY);
 

Matrix(%id = 157648344) (10)
 

> Pts:=[[0,1],[1,4],[2,7],[3,8]];
 

[[0, 1], [1, 4], [2, 7], [3, 8]] (11)
 

> PP:=plot(Pts,style=point,symbol=circle,color=blue):
 

> RP:=plot(beta[1,1] + beta[2,1]*x + beta[3,1]*x^2,x=-1..4):
 

> display(PP,RP);
 

Plot_2d
 

>