How do I plot the spline effect in PHREG?

Reply
N/A
Posts: 1

How do I plot the spline effect in PHREG?

Hi,

I am new to SAS and am having trouble graphing the SPLINE constructed effect in PHREG.

My question is, what is the best way to draw a spline using the output from the SPLINE effect in PHREG? This is a survival analysis so it seems to me that using TRANSREG would not work as well.

/* Here is the PHREG statement I am using:    */


PROC PHREG DATA=WORK.TMP0TempTableInput
;
CLASS smoking(ref = 'N');
CLASS AgeCategory(ref = '1');
EFFECT spl = spline(BMI / degree=3 knotmethod=rangefractions(.05 .25 .75 .95) Details);
MODEL MonthsToDeathEnd * CensoredDeath (1) = spl Female Smoking AgeCategory HxCAD HxCHF HxCVA HxPVD HxDiabetes HxCOPD Charlson weightedMPR weightedtownsend /
  TIES=BRESLOW
  RISKLIMITS ALPHA=0.05
  SELECTION=NONE
;

RUN;TITLE;

/* Here is an example of the type of output I am trying to generate: */

spline example.jpg

Thanks!

N/A
Posts: 1

Re: How do I plot the spline effect in PHREG?

Hi,

Just wondering whether you know by now how to generate the plot you showed? I am aiming for a similar plot after a similar PHREG statement.

Many thanks in advance!

Super Contributor
Posts: 280

Re: How do I plot the spline effect in PHREG?

I sometime also deals with this problem. I have not found a way to plot the spline-curve created by the effect-estimates. I therefore only use the spline-feature in the effect-statement when I want to adjust for some covariate which own effect is not of interest.

If I want to plot the spline, then I do all the coding wihout use of the effectstatement. That is (1) create the coefficients which will be used as regressors in phreg (or some other regression procedure), (2) from the estimates a plot dataset can be created and at last (3) plot the curves with proc gplot. It can be it can be done more easily, so I encourage others to come with better solutions. With some small modifications, it is also possible to use the code below to plot a time-dependent effect in a cox-regression.

There are some matrix-calculations, and it is thefore neccesary to have run the matrix-package first. The matrixalgebra file which are included is the one that is attached this post:

Here is an example:

*k is is the number of knots;

%let k=9;

%include "" ; /*insert path to the matrix package */

option cmplib=work.func; /*this should be the same location as the one used in the matrix-package*/

*for this example I also simulate data;

data simulation;

  array spl{%eval(&k.-1)};

  array knots{&k.} _temporary_  (-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4)  ; /*this is my knot-points*/

  do j=1 to 10000;

   * a is my covariate, which is here some number between zero and one.;

    a=ranuni(-1)-0.5;

*t is my survival time;

t=-log(ranuni(-1))/(exp(-(a)**2));

  *then create the spline-coefficients;

ref=0; *0 is here my reference value;

spl[1]=a-ref;

do i=1 to &k.-2;

  spl[i+1]=max(a-knots,0)**3

     -((knots[&k]-knots)/(knots[&k]-knots[&k-1]))*max(a-knots[&k.-1],0)**3

    +((knots[&k-1]-knots)/(knots[&k]-knots[&k-1]))*max(a-knots[&k.],0)**3

-(max(ref-knots,0)**3

   -((knots[&k]-knots)/(knots[&k]-knots[&k-1]))*max(ref-knots[&k.-1],0)**3

   +((knots[&k-1]-knots)/(knots[&k]-knots[&k-1]))*max(ref-knots[&k.],0)**3);

end;

    output;

  end;

run;

*because I also wants pointwise confidence-limits I put the covariance-matrix into a dataset "outest";

proc phreg data=simulation covout outest=outest;

  model t=spl1-spl%eval(&k.-1);

run;

data plot;

  array knots{&k.} _temporary_ (-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4) ;

  array spl{%eval(&k.-1)};

  array variance{%eval(&k.-1),%eval(&k.-1)} _temporary_;

  array beta{%eval(&k.-1),1} _temporary_;

  array design{1,%eval(&k.-1)} _temporary_;

  array designt{%eval(&k.-1),1} _temporary_;

  array logest{1,1} _temporary_;

  array var{1,1} _temporary_;

*read in the estimates;

  do until (eof);

    set outest end=eof;

    do i=1 to &k-1.;

      if _type_='PARMS' then beta[i,1]=spl;

      else variance[i,input(substr(_name_,4,length(_name_)-3),best.)]=spl;

    end;

  end;

  call show(variance);

*create the curve and confidencelimits;

ref=0;

do a=-0.5 to 0.5 by 0.001;

design[1,1]=a-ref;   

do i=1 to &k.-2;

design[1,i+1]=max(a-knots,0)**3

-((knots[&k]-knots)/(knots[&k]-knots[&k-1]))*max(a-knots[&k.-1],0)**3

+((knots[&k-1]-knots)/(knots[&k]-knots[&k-1]))*max(a-knots[&k.],0)**3

-(max(ref-knots,0)**3

-((knots[&k]-knots)/(knots[&k]-knots[&k-1]))*max(ref-knots[&k.-1],0)**3

+((knots[&k-1]-knots)/(knots[&k]-knots[&k-1]))*max(ref-knots[&k.],0)**3);

end;

    call trans(design,designt);

    call multiplicer(design,beta,logest);

    call multiplicer3(design,variance,designt,var);

    rr=exp(logest[1,1]);

    lower=exp(logest[1,1]-1.96*sqrt(var[1,1]));

    upper=exp(logest[1,1]+1.96*sqrt(var[1,1]));

    output;

  end;

  keep a rr lower upper;

run;

symbol1 i=join v=none l=1 c=black;

symbol2 i=join v=none l=2 c=black;

symbol3 i=join v=none l=2 c=black;

proc gplot data=plot;

  plot (rr lower upper)*a/overlay;

run;

New Contributor
Posts: 2

Re: How do I plot the spline effect in PHREG?

Hi,

I want to do the same approximately.

Current code:

ods graphics on;

proc phreg data=splinedata ;

     effect lala=spline(log_lab/knotmethod=list(1 3 5));

     model time*event(0)=lala;

run;

Anyone knows how to plot this? I want to plot on the y-axis HR, and a continuous variable on the x-axis.

I'm using SAS 9.3.

Anyone familiar with the %rcs_reg macro by Desquibet (Dose-response analyses using restricted cubic spline functions in public health research - Desquilbe...)?

Thanks!

Tessa

N/A
Posts: 1

Re: How do I plot the spline effect in PHREG?

Hi,

I think something like this should do it:

proc phreg data=temp;

    effect bmiS = spline(bmi / basis=tpf(noint) NATURALCUBIC details knotmethod=list(18.0175 20.5289 22.4059 27.4693) );

    model time*event(0)=bmiS  / rl=wald ties=EFRON ;

    store sasuser.coxr;

run;

%macro est(ref=20, start=15, end=35, by=0.1);

%Do i = 1 %To %eval(%SysFunc( Ceil( %SysEvalF( ( &End - &Start ) / &By ) ) ) +1) ;

   %Let value=%SysEvalF( ( &Start - &By ) + ( &By * &I ) ) ;

    estimate "&value." bmis [-1, &ref] [1, &value] / exp cl;

    %end;

%mend est;

ods html select none;

ods rtf select none;

ods dataset Estimates=Estimates;

proc plm restore=sasuser.coxr;

    %est(ref=20, start=16, end=35, by=0.01);

run;

ods html select all;

ods rtf select all;

data estimates;

    set estimates;

    BMI=label*1;

run;

proc sgplot data=estimates NOAUTOLEGEND Noborder;

    Series y=ExpEstimate x=BMI / ;

    Series y=LowerExp x=BMI / LINEATTRS=(pattern=ShortDash color=Black THICKNESS=1);

    Series y=UpperExp x=BMI / LINEATTRS=(pattern=ShortDash color=Black THICKNESS=1);

    REFLINE 1 / axis=y;

    REFLINE 20 / Axis=X LINEATTRS=(pattern=ThinDot color=Black THICKNESS=1);;

    yaxis Values=(0.8 1 1.5 2 3 4 6 8 10) Label="Hazard ratio" Type=LOG LABELATTRS=(weight=BOLD);

    xaxis min=16 VALUES=(16 to 35 by 1) LABELATTRS=(weight=BOLD);

run;

N/A
Posts: 1

Re: How do I plot the spline effect in PHREG?

Hi LinusS.,

Thank you! This works well for PHREG. I am also looking for a way to draw a spline based on the survey data. I think the above code would not work well for the PROC SURVEYPHREG, because there is not the effect statement in SURVEYPHREG. Is there a best way to draw a spline after SURVEYPHREG?

Thanks a lot!

Xi

Ask a Question
Discussion stats
  • 5 replies
  • 8203 views
  • 3 likes
  • 6 in conversation