simul_data.sas 12.4 KB
Newer Older
Paul Brown's avatar
Paul Brown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/******************************************************************************
Author, date: Paul Brown, march2018
Macro name:   simul_data, 
              iterat_simul (run simul_data iteratively in order to
              obtain the desired correlations between the outcomes), 
              nearestcorr
Description:  simulate random samples from a multivariate normal distribution,
              transform variables to required types. Create a dataset containing 
              num random samples indicated by the variable samp=1,2,...num.  
Reference:    
Notes:        _co=control, _ac=active
Validation:   
******************************************************************************/


********************************************************************;
***               define macro parameters                       ***;
********************************************************************;

%macro simul_data(n=500,     /*total sample size ie n/2 in each grp*/
             num=1000,       /*no. of random samples*/
             numvars=5,      /*number of variables*/
             tte_fu=84,      /*follow-up time for censoring of TTE endpts, corresponds
                               to the response estimates assumed for TTE outcomes*/
             var1_tp=SURV,   /*variable type*/
             var1_co=0.91,   /*co=control grp, the base response*/
             var1_df=0.02,  /*df=treatment difference, defines response for 
                              ac=active grp*/
             var1_sd=,       /*if var1_tp=norm or lognorm (sd for log(post/base))*/
             var2_tp=SURV, var2_co=0.84, var2_df=0.02, var2_sd=, 
             var3_tp=NORM, var3_co=0,    var3_df=50,   var3_sd=80,
             var4_tp=NORM, var4_co=-10,  var4_df=6,    var4_sd=17,   
             var5_tp=LOGN, var5_co=0.7, var5_df=-0.2, var5_sd=0.7, 
             /*initial working correlations */   
             corr12=0, corr13=0, corr14=0, corr15=0, 
                       corr23=0, corr24=0, corr25=0,
                                 corr34=0, corr35=0,
                                           corr45=0); 

%let gtype1=%upcase(&var1_tp) ;
%let gtype2=%upcase(&var2_tp) ;
%let gtype3=%upcase(&var3_tp) ;
%let gtype4=%upcase(&var4_tp) ;
%let gtype5=%upcase(&var5_tp) ;

********************************************************************;
***               convert to normal variates           ***;
********************************************************************;

data _null_;
  %do i=1 %to &numvars;
    %if &&gtype&i=SURV %then %do;
      surv_co=&&var&i._co;
      surv_ac=&&var&i._co+&&var&i._df;
      *--hazards;
      survh_co=-log(surv_co)/&tte_fu;
      survh_ac=-log(surv_ac)/&tte_fu;
      *--log hazards;
      survz_co=log(survh_co);
      survz_ac=log(survh_ac);
      *--number of deaths;
      survd_co=(&n/2)*(1-surv_co);
      survd_ac=(&n/2)*(1-surv_ac);
      *--variance;
      survv_co=1/survd_co; 
      survv_ac=1/survd_ac;
      call symput("var&i.v_co",trim(left(put(survv_co,best.))));
      call symput("var&i.v_ac",trim(left(put(survv_ac,best.))));
      call symput("var&i.z_co",trim(left(put(survz_co,best.))));
      call symput("var&i.z_ac",trim(left(put(survz_ac,best.))));
    %end;
    %else %if &&gtype&i=BINO %then %do;
      bino_co=&&var&i._co;
      bino_ac=&&var&i._co+&&var&i._df;
      *--log odds;
      binoz_co=log(bino_co/(1-bino_co));
      binoz_ac=log(bino_ac/(1-bino_ac));
      *--variance of log odds;
      binov_co=1/((&n/2)*bino_co*(1-bino_co)); 
      binov_ac=1/((&n/2)*bino_ac*(1-bino_ac));
      call symput("var&i.v_co",trim(left(put(binov_co,best.))));
      call symput("var&i.v_ac",trim(left(put(binov_ac,best.))));
      call symput("var&i.z_co",trim(left(put(binoz_co,best.))));
      call symput("var&i.z_ac",trim(left(put(binoz_ac,best.))));
    %end;
    %else %if &&gtype&i=NORM %then %do;
      normv_co=&&var&i._sd**2;
      normv_ac=&&var&i._sd**2;
      normz_ac=&&var&i._co+&&var&i._df;
      normz_co=&&var&i._co;
      call symput("var&i.v_co",trim(left(put(normv_co,best.))));
      call symput("var&i.v_ac",trim(left(put(normv_ac,best.))));
      call symput("var&i.z_co",trim(left(put(normz_co,best.))));
      call symput("var&i.z_ac",trim(left(put(normz_ac,best.))));
    %end;
    %else %if &&gtype&i=LOGN %then %do;
      *--log ratio (post baseline/baseline);
      lognz_ac=log(&&var&i._co+&&var&i._df);
      lognz_co=log(&&var&i._co);
      *--variance of log ratio;
      lognv_co=&&var&i._sd**2;
      lognv_ac=&&var&i._sd**2;
      call symput("var&i.v_co",trim(left(put(lognv_co,best.))));
      call symput("var&i.v_ac",trim(left(put(lognv_ac,best.))));
      call symput("var&i.z_co",trim(left(put(lognz_co,best.))));
      call symput("var&i.z_ac",trim(left(put(lognz_ac,best.))));
    %end;
  %end;
run;


********************************************************************;
***          simulate data from multivariate normal         ***;
********************************************************************;

proc iml;
%nearestcorr(); 
  *specify parameters of multivariate normal dist;
  mean_co=1:&numvars; varn_co=1:&numvars;
  mean_ac=1:&numvars; varn_ac=1:&numvars;
  %do i=1 %to &numvars;
    mean_co[&i]=&&var&i.z_co; 
    varn_co[&i]=&&var&i.v_co; 
    mean_ac[&i]=&&var&i.z_ac; 
    varn_ac[&i]=&&var&i.v_ac;
  %end;
  corr_tmp_={1 &corr12 &corr13 &corr14 &corr15, 
        &corr12 1 &corr23 &corr24 &corr25, 
        &corr13 &corr23 1 &corr34 &corr35,
        &corr14 &corr24 &corr34 1 &corr45,
        &corr15 &corr25 &corr35 &corr45 1}; 
  corr_tmp=corr_tmp_[1:&numvars, 1:&numvars]; /*in case numvar < 5*/
  eigval=eigval(corr_tmp); 
  if all(eigval>0) then corr=corr_tmp; /*if positive definite*/
  else corr=NearestCorr(corr_tmp); /*otherwise find nearest pos def matrix*/
  print corr; 
  covr_co=corr#sqrt(varn_co`*varn_co); 
  covr_ac=corr#sqrt(varn_ac`*varn_ac); 
  print covr_co; print covr_ac; 
  *obtain random samples (seed=dec2014);
  call randseed(1214);           
  co=randnormal((&n/2)*&num,mean_co,covr_co); 
  ac=randnormal((&n/2)*&num,mean_ac,covr_ac);
  *create sas datasets;
  samp=colvec(repeat(T(1:&num),1,&n/2)); 
  z=samp||co;
  create rand_co from z[c={"samp" "x1" "x2" "x3" "x4" "x5"}];
    append from z;
  close rand_co;
  z=samp||ac;
  create rand_ac from z[c={"samp" "x1" "x2" "x3" "x4" "x5"}];
    append from z;
  close rand_ac;
quit;

*combine data for the 2 grps;
data randztmp;
  format trt $10.;
  set rand_co (in=c) rand_ac (in=a);
  if a then trt='Active';
  else if c then trt='Control';
run;

*sort data;
proc sort data=randztmp;
  by samp trt;
run;

*finalise data, create patient number;
data randz;
  retain subjno;
  set randztmp;
  by samp trt;
  if first.samp then subjno=1;
  else subjno=subjno+1;
run;

********************************************************************;
***               transform from normal variates                 ***;
********************************************************************;

data randsamp;
  set randz;
  %do i=1 %to &numvars;
    %if &&gtype&i=SURV %then %do;
      var&i=round(-log(ranuni(1412))/exp(x&i),1)+1;  
      var&i.2=var&i; var&i.c=0;
      if var&i gt &tte_fu then do;
        var&i.2=&tte_fu; 
        var&i.c=1;
      end;
      label var&i="Survival endpoint &i"
        var&i.2="Survival endpoint &i with censoring" 
        var&i.c="Survival endpoint &i censoring indicator";
    %end;
    %else %if &&gtype&i=BINO %then %do;
      if trt='Active' then do;
        x&i._=(x&i-&&var&i.z_ac)/sqrt(&&var&i.v_ac);
        x&i.__=tinv(1-(&&var&i._co+&&var&i._df),&n/2-1); 
        if x&i._ gt x&i.__ then var&i=1; 
        else var&i=0;
      end;
      else if trt='Control' then do;
        x&i._=(x&i-&&var&i.z_co)/sqrt(&&var&i.v_co);
        x&i.__=tinv(1-&&var&i._co,&n/2-1);
        if x&i._ gt x&i.__ then var&i=1;
        else var&i=0;
      end;
      drop x&i._ x&i.__;
      label var&i="Dichotomous endpoint &i";
    %end;
    %else %if &&gtype&i=NORM %then %do;
      var&i=x&i;
      label var&i="Continuous endpoint &i";
    %end;
    %else %if &&gtype&i=LOGN %then %do;
      var&i=100*(exp(x&i)-1); 
      label var&i="Pct change endpoint &i";
    %end;
  %end;
  drop x1-x&numvars ;
  label trt='Treatment' samp='Rand sample no.' subjno='Patient no.';
run;

proc sort data=randsamp;
  by samp trt;
run;

%mend simul_data;

********************************************************************;
***             iterations to obtain correlations                ***;
********************************************************************;

%macro iterat_simul(n_=500,num_=1000,numvars_=5,criterion=0.025,maxiter=50,
            var1_df_=0.02,var2_df_=0.02,var3_df_=50,var4_df_=6,var5_df_=-0.2,
                    aim12=0, aim13=0, aim14=0, aim15=0, 
                    aim23=0, aim24=0, aim25=0, 
                    aim34=0, aim35=0, aim45=0,
                    out=finalsamp, out_iter=finaliter); /*output datasets*/

%global gnumvars gnum gtype1 gtype2 gtype3 gtype4 gtype5 gprec ; 
  
%let gnumvars=&numvars_;
%let gnum=&num_;
%let t0 = %sysfunc(datetime()); 

data iterations;
  set _null_;
run;

%simul_data(n=&n_,num=&num_,numvars=&numvars_,
var1_df=&var1_df_,var2_df=&var2_df_,var3_df=&var3_df_,
var4_df=&var4_df_,var5_df=&var5_df_);

%let gprec=1;
%let iterat=0;
%do %while (&gprec gt &criterion and &iterat lt &maxiter); 
                   /*prec (precision) = max difference between actual
                                   correlation and the desired value*/

%let iterat=%eval(&iterat+1);

ods output pearsonCorr=corrns1 
           (keep=variable var:
            rename=(var1-var&numvars_ = varc1-varc&numvars_));
proc corr data=randsamp pearson; /*pearson used even for binary outcomes, 
                                   produces the same corr 
                                   as the biserial point correlation*/
  var var1-var&numvars_; /*careful to exclude var[]2 (survival vars)*/
run;

proc sort data=corrns1;
  by variable;
run;

data randzc;
  set randz;
  rename x1-x&numvars_ = var1-var&numvars_;
run;

ods output pearsonCorr=corrns2 
           (keep=variable var: 
            rename=(var1-var&numvars_ = varcz1-varcz&numvars_));
proc corr data=randzc pearson;
  var var:; /*across random samples*/
run;

proc sort data=corrns2;
  by variable;
run;

data corrns;
  merge corrns1 corrns2;
  by variable;
run;

data iterations;
  format precision 7.5 ;
  retain prectmp 0;
  set iterations corrns (in=a);
  conv=0.005; /*conv=convergence limit*/
  if a then do;
    iteration=&iterat;
    %do p=1 %to 5; 
      %do b=1 %to 5;
        if variable="var&p" then do;
          %if &b gt &p and &b le &numvars_ %then %do; /*corrns above diagonal*/
            diff&p&b=abs(varc&b-&&aim&p&b);
            prectmp=max(prectmp,diff&p&b);
            if diff&p&b lt conv then c&p&b=varcz&b; /*desired corrn attained*/
            else do; /*desired value not attained, thus adjust*/
              if &&aim&p&b gt 0 then c&p&b=min((&&aim&p&b/varc&b)*varcz&b,1); 
                                                             /*+ve corrns*/
              else c&p&b=max((&&aim&p&b/varc&b)*varcz&b,-1); /*-ve corrns*/
            end;
            call symput("c&p&b",trim(left(put(c&p&b,best.)))); 
                                                     /*corrn assigned its new value*/
          %end;
        end;
          %if &b gt &p and (&p gt &numvars_ or &b gt &numvars_) %then %do;
            call symput("c&p&b",trim(left(put(0,best.)))); /*superfluous correlations*/
          %end;
      %end;
    %end;
    if variable="var&numvars_" then call symput("gprec",trim(left(put(prectmp,7.5))));
    precision=prectmp; /*this precision will be compared against the criterion 
                         and the loop terminated if satisfied*/
  end;
run;

%simul_data(n=&n_,num=&num_,numvars=&numvars_,
var1_df=&var1_df_,var2_df=&var2_df_,var3_df=&var3_df_,
var4_df=&var4_df_,var5_df=&var5_df_,
             corr12=&c12, corr13=&c13, corr14=&c14, corr15=&c15, 
                          corr23=&c23, corr24=&c24, corr25=&c25,
                                       corr34=&c34, corr35=&c35,
                                                    corr45=&c45); 
%end;

%let speed=%sysfunc(datetime())-&t0;

data &out;
  set randsamp;
run;

data &out_iter;
  retain iteration variable varc1-varc&numvars_ precision speed ; /*put vars in order*/
  format varc1-varc&numvars_ 5.2;
  set iterations;
  speed=put(&speed,6.2)||' secs';
  keep iteration variable varc1-varc&numvars_ precision speed;
run;

proc sort data=&out_iter;
  by iteration variable;
run;

%mend iterat_simul;


*** end *****************************************;