# [R] Smoothed HR for interaction term in coxph model

Therneau, Terry M., Ph.D. therneau at mayo.edu
Mon Jun 2 15:59:46 CEST 2014

```
On 05/30/2014 05:00 AM, r-help-request at r-project.org wrote:
>
>
> I have a dataset with 2 treatments and want to assess the effect of a
> continous covariate on the Hazard ratio between treatment A and B.  I want a
> smoothed interaction term which I have modelled below with the following
> code:
>
>
>
> surv.fit <- coxph(my.surv ~ pspline(CONTINUOUS, df=0) + factor(DICHOTOMOUS)
> +  pspline(CONTINUOUS, df=0)*factor(DICHOTOMOUS), data = datanew2)
>
>
>
> and consequently I would like to obtain a smoothed plot of the hazard ratio
> between treatment A and B on the y-axis with the continuous covariate on the
> x-axis.  As termplot ignores interaction terms, I was wondering if anyone
> has seen anything like this before and can advise on the best way to do it.
>
>

You have 2 problems.
1. The pspline code's maximization routine simply can't cope with two terms that both
have "df=0", i.e., asking it to find the best degrees of freedom.  You have to choose df
yourself.  (Making the code smarter has been on my TODO list for years, and will likely
remain there a while longer.)

2. What you want to do is harder than you think.  For definiteness assume that we have
CONTINUOUS= age and DICHO= sex.  Then one wants a smooth curve of risk vs age for the
males, and a separate one for the females.
For a smoothing spline, that means two penalties, one attached to each term.
People get sloppy about the term "interaction".  For two categorical variables what
needs to be done is clear, namely to have one coefficient for each unique combination.
Software will add the batch of coefficients for us automatically when a "*" is placed in
the formula.  For continuous variables the use of "*" in a formula adds the product of the
two terms, which is not an interaction except in very special circumstances.

Between the two of these, an interaction times a pspline term is doomed to fail.
(Another update for the package -- I need to print an error message in this case).

Products of coefficients will work for ns() terms and an interaction.

Terry T.

```