Thanks to Danny for this wonderful resource on how to score a fixed effects logistic Bayesian model: https://communities.sas.com/t5/SAS-Communities-Library/Bayesian-Analysis-and-PROC-IML-What-s-the-Score/ta-p/923044.
I need to take this one step further and add random intercepts to the process. My IML skills need serious work so I had to take the output of the fixed effect scoring out of PROC IML, transpose them to create one tall file and then merge it with the Iteration by RandomIntercept matrix in the OutPost file from MCMC/BGLIMMIX. I use vValueX() function to match each Level 2 ID to the corresponding column in the Random Intercept dataset.
This workflow is straightforward with relatively light code. Maybe someone can use it as is. For my purposes it's too SLOW! I am working with over 1,000 Level 2 units containing over 500 Level 1 Units each and 5,000 iterations seems like a minimum for Bayesian output. Marginal Linear Predictor (mLP) takes seconds to compute even for this large dataset in PROC IML. Adding Random intercepts to each mLP estimate to obtain Conditional Linear Predictors takes hours. I am in an evironment where I need to do this on a regular basis for dozens of models so the time adds up very quickly. I am hoping some good soul can translate what I have done with transpose/merge into PROC IML and that matrix addition can shave serious time off the process. Thanks in advance.
Simulated source data files and the workflow to add ConditionalLP = mLP + RI_L2 are in the code below. L12 dataset is what comes out from the scoring effort in the blog reference above (minus the L2 unit IDs).
%let nL2 = 10; * Level 2 units ;
%let nL1 = 20; * L1 units per L2 unit on average ;
%let nIter = 50; * MCMC iterations ;
data L12;
do L2ID=1 to &nL2;
do L1ID=1 to ceil(RanUni(1)*&nL1*2); * simulate variable number of L1 units per L2 unit ;
array mLR_Iter[&nIter]; do i=1 to &nIter; mLR_Iter[i] = RanNor(1); end;
output;
end;
end;
drop i;
data OutPost; * Random Intercepts: one row per MCMC Iteration, one column per L2 unit ;
do Iteration=1 to &nIter;
array RI_L2[&nL2]; do i=1 to &nL2; RI_L2[i] = RanNor(1);end;
output;
end;
drop i;
run;
*** 2. TRANSPOSE PT DATA to TALL AND MERGE WITH RANDOM INTERCEPTS ;
proc transpose data=L12 out=L12_Tall;
by L2ID L1ID;
var mLR_Iter:;
proc sql;
create table L12_Tall as
select input(substr(_Name_,9),best.) as Iteration, /* extract Iteration ID that starts with character 9 */
L2ID, L1ID, Col1 as mLP /* marginal linear predictor */
from L12_Tall
order by Iteration, L2ID, L1ID
;
quit;
data WantM;
merge L12_Tall
OutPost;
by Iteration;
ConditionalLP = mLP + vValueX(cats('RI_L2',L2ID));
drop RI_L2:;
run;
... View more