title: Linear predictor from predict.coxph from coefficients only
body: <p>I need to calculate the linear predictor of a Cox PH model manually.</p>
  
  <p>I can get continuous and binary variables to match the output of predict.coxph (specifying 'lp') but I can't seem to figure out how to calculate it for categorical variables with more than 2 levels.</p>
  
  <p>My aim is to assess calibration of a published model in my own data-I only have coefficients so need to be able to do this by hand.</p>
  
  <p>This post on stackoverflow describes how to calculate for continuous variables...</p>
  
  <p>(<a href="https://stackoverflow.com/questions/42843239/coxph-predictions-dont-match-the-coefficients">Coxph predictions don&#39;t match the coefficients</a>)</p>
  
  <p>Any advice would be appreciated! Thanks</p>
  
  <p>R example...</p>
  
  <pre><code>URL   &lt;- "http://socserv.mcmaster.ca/jfox/Books/Companion/data/Rossi.txt"
  Rossi &lt;- read.table(URL, header=TRUE)                  
  summary(Rossi[,c("week", "arrest", "fin")])
   #      week           arrest        fin     
   # Min.   : 1.00   Min.   :0.0000   no :216  
   # 1st Qu.:50.00   1st Qu.:0.0000   yes:216  
   # Median :52.00   Median :0.0000            
   # Mean   :45.85   Mean   :0.2639            
   # 3rd Qu.:52.00   3rd Qu.:1.0000            
   # Max.   :52.00   Max.   :1.0000 
  
  library(survival)     
  
  #for binary variable
  fitCPH &lt;- coxph(Surv(week, arrest) ~ fin, data=Rossi)    #Cox-PH model
  (coefCPH &lt;- coef(fitCPH))                               # estimated coefficients
  #   finyes 
  #-0.3690692 
  
  head(predict(fitCPH,type="lp"))
  #[1]  0.1845346  0.1845346  0.1845346 -0.1845346  0.1845346  0.1845346
  
  head(((as.numeric(Rossi$fin) - 1) - mean(as.numeric(Rossi$fin) - 1))    * coef(fitCPH))
  #[1]  0.1845346  0.1845346  0.1845346 -0.1845346  0.1845346  0.1845346
  
  #for categorical variable
  set.seed(170981)
  Rossi$categorical.example &lt;-    as.factor(sample(1:3,nrow(Rossi),replace = TRUE))
  summary(Rossi[,c("week", "arrest", "categorical.example")])
   #      week           arrest       categorical.example
   # Min.   : 1.00   Min.   :0.0000   1:156              
   # 1st Qu.:50.00   1st Qu.:0.0000   2:138              
   # Median :52.00   Median :0.0000   3:138              
   # Mean   :45.85   Mean   :0.2639                      
   # 3rd Qu.:52.00   3rd Qu.:1.0000                      
   # Max.   :52.00   Max.   :1.0000               
  
  fitCPH2 &lt;- coxph(Surv(week, arrest) ~ categorical.example, data=Rossi)    #Cox-PH model
  (coefCPH2 &lt;- coef(fitCPH2))  
  #categorical.example2 categorical.example3 
  #           -0.181790            -0.103019 
  
  head(predict(fitCPH2,type="lp"))
  #[1]  0.09098066 -0.01203832 -0.01203832  0.09098066 -0.09080938 -0.09080938
  
  #How to calculate manually??
  </code></pre>