Example of a matlab ridge regression function:
function bks=ridge(Z,Y,kvalues)
% Ridge Function of Z (centered, explanatory)
% Y is the response,
% kvalues are the values where to compute
[n,p]=size(Z);
ZpY=Z'*Y;
ZpZ=Z'*Z;
m=length(kvalues);
bks=ones(p,m);
for k =1:m
bks(:,k)=(ZpZ+diag(kvalues(k)))\ZpY;
end
>> kvalues=(0:.05:.5)
kvalues =
Columns 1 through 7
0 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000
Columns 8 through 11
0.3500 0.4000 0.4500 0.5000
>> ridge(Z,Y,(0:.05:.5))
ans =
Columns 1 through 7
1.5511 1.5176 1.4882 1.4622 1.4390 1.4183 1.3996
0.5102 0.4775 0.4488 0.4234 0.4009 0.3806 0.3624
0.1019 0.0678 0.0378 0.0113 -0.0122 -0.0334 -0.0524
-0.1441 -0.1762 -0.2043 -0.2292 -0.2514 -0.2713 -0.2892
Columns 8 through 11
1.3827 1.3673 1.3532 1.3403
0.3459 0.3309 0.3172 0.3046
-0.0696 -0.0853 -0.0997 -0.1128
-0.3054 -0.3201 -0.3336 -0.3460
%Formula gives for choice of k:
>> norm(Yhat-Yc)^2
ans =
47.8636
>> 47.8636/(13-5)
ans =
5.9829 % estimates the variance sigma^2
>> bk0'*bk0
ans =
2.6973
>> k=(4*5.9829)/2.6973
k =
8.8724 % This is a suggested value for k