(1)    %  rdgpred Efficient prediction using ridge regression. (2)   % rdgpred(Y,A,A21,L) returns ridge regression predictions in test set. (3)   % Vector Y contains outcome training set, matrix A similarity measures (4)   % in training set (e.g., A=XX′  for N-by-P input matrix X), matrix A21 (5)   % similarity individuals in test set (rows) and training set (columns), (6)   % and vector L the penalty values to consider. (7)   % (8)   % rdgpred(Y,A,A21,L,Z,Z2) first corrects A and A21 for confounders. (9)   % Matrix Z contains confounders in traing set and Z2 those in test set. (10) % (11)  %  Author    : R de Vlaming and PJF Groenen (12) % Institute: Erasmus School of Economics  Date: November 25, 2014 (13) function Y2 = rdgpred(Y,A,A21,L,Z,Z2) (14) (15) P   = numel(L);      % find size of set of penalties (16) N   = numel(Y);      % find size of training set (17) N2 = size(A21,1); % find size of test set (18) (19) if nargin > 5      % correct similarities if confounders present (20)  M  = eye(N) − Zinv(Z′Z)Z′;    % anti projection matrix of Z (21)   M2  = eye(N2)  -  Z2inv(Z2′Z2)Z2′;  % anti projection matrix of Z2 (22)  A  = MAM;              % adjust similarties A (23)  A21 = M2A21M;            % adjust similarties A21 (24) end (25) (26) [Q,D] = eig(A); D = diag(D); % obtain eigenvecs Q and eigenvalues D of A (27) (28) % for each eigenvalue E (rows) and lambda S (cols) find 1/(E+S) (29) D = 1./(repmat(D,1,P) + repmat(L(:)′,N,1)); (30) (31) % predict for observations in test set (rows) for each lambda (cols) (32) QTY = repmat((Y′Q)′,1,P); (33) Y2  = A21(Q(D.QTY)); (34) Y2  = real(Y2); % remove imaginary part due to numerical imprecession (35) (36) end
Algorithm 1: MATLAB code for efficient ridge regression prediction: rdgpred.m.