www.gusucode.com > stats 源码程序 matlab案例代码 > stats/RidgeRegressionExample.m

    %% Ridge Regression
% Load the sample data.

% Copyright 2015 The MathWorks, Inc.

load acetylene
%%
% |acetylene| has observations for the predictor variables |x1| , |x2| ,
% |x3| , and the response variable |y| .
%%
% Plot the predictor variables against each other.
subplot(1,3,1)
plot(x1,x2,'.')
xlabel('x1'); ylabel('x2'); grid on; axis square

subplot(1,3,2)
plot(x1,x3,'.')
xlabel('x1'); ylabel('x3'); grid on; axis square

subplot(1,3,3)
plot(x2,x3,'.')
xlabel('x2'); ylabel('x3'); grid on; axis square

%%
% Note the correlation between |x1| and the other two predictor variables.
%%
% Compute coefficient estimates for a multilinear model with interaction
% terms, for a range of ridge parameters using |ridge| and |x2fx| .
X = [x1 x2 x3];
D = x2fx(X,'interaction');
D(:,1) = []; % No constant term
k = 0:1e-5:5e-3;
b = ridge(y,D,k);
%%
% Plot the ridge trace.
figure
plot(k,b,'LineWidth',2)
ylim([-100 100])
grid on 
xlabel('Ridge Parameter') 
ylabel('Standardized Coefficient') 
title('{\bf Ridge Trace}') 
legend('x1','x2','x3','x1x2','x1x3','x2x3')
%%
% The estimates stabilize to the right of the plot. Note that the
% coefficient of the |x2x3| interaction term changes sign
% at a value of the ridge parameter $\approx 5*10^{-4}$ .