title: Maximizing Partial Likelihood using R code
body: <p>Suppose I have a lifetime data set like kidney data. There are some covariates age, sex, disease. Sex have 2 categories (F,M) and disease have 4 categories (Other, GN, AN, PKD). I want to estimate the parameters of Cox proportional hazard model. There are 5 parameters for age, sex, GN, AN, PKD. I am trying like this. How can I maximize this function?</p>
  
  <pre><code>library(survival)
  dat &lt;- kidney
  head(kidney)
  #      id time status age sex disease frail
  #    1  1    8      1  28   1   Other   2.3
  #    2  1   16      1  28   1   Other   2.3
  #    3  2   23      1  48   2      GN   1.9
  #    4  2   13      0  48   2      GN   1.9
  #    5  3   22      1  32   1   Other   1.2
  #    6  3   28      1  32   1   Other   1.2  
      ......................................
  </code></pre>
  
  <h1>Partial likelihood function</h1>
  
  <pre><code>logPL &lt;- function( par, time, status, cov ){
    data &lt;- data.frame( time, status, cov)
    data &lt;- data[order(data$time, decreasing=T),]
    risk.score &lt;- data.matrix(data[,-c(1:2)]) %*% par
    PL1 &lt;- risk.set
    PL2 &lt;- log(cumsum(exp(risk.set)))
    PL &lt;- sum(data$status*(PL1-PL2))
    return(-PL)}
  </code></pre>