0

I'm using the package coxme and I want to extract the AIC from models, in order to select the best one.

1) I didn't found how to do this directly and I think this is not possible without changing the function coxme()'s code but I would be very happy to be wrong, let me know if I am !

2) I looked the function's code with the command :

coxme:::print.coxme

in order to add a variable to stock AIC and saw the code but if I call it 'coxme2' for example (I just add coxme2<- at the begining) and try to use it (without any other add), I get an error :

Error in colnames<-(*tmp*, value = c("NULL", "Integrated", "Fitted" : length of 'dimnames' [2] not equal to array extent

To sum it up, the coxme function works well but if I juste copy and paste its code, it doesn't. How can I fix this issue ?

Vincent
  • 955
  • 2
  • 15
  • 32

2 Answers2

2

The AIC can be extracted through

extractAIC.coxme <- function(x){
  loglik <- x$loglik + c(0, 0, x$penalty)
  chi1 <- 2*diff(loglik[1:2]) 
  chi2 <- 2*diff(loglik[c(1,3)])
  c(chi1 - 2*x$df[1], chi2 - 2*x$df[2])
}

fit <- coxme(Surv(time, status) ~ age + sex + (1|ph.ecog), lung)
extractAIC(fit)

See the print.coxme function https://github.com/cran/coxme/blob/master/R/print.coxme.R

junkka
  • 543
  • 7
  • 11
1

have you tried getAnywhere(print.coxme)? It gives you all the code necessary to copy and modify the function in R.

Edit

Just write

copyOfAIC <<- temp

below of

dimnames(temp) <- list(c("Integrated loglik", " Penalized loglik"), 
                           c("Chisq", "df", "p", "AIC", "BIC"))

And you will get a copy of the values that you are looking for

eyanquenb
  • 193
  • 7
  • Thx for your answer but I think this code is less relevant that the one I had with print.coxme. Or maybe I don't understand it well but I don't see how to fix my problem with it... – Vincent Jun 25 '13 at 07:35
  • in fact, this is the complete code which allows you to modify the function. With it you can make a new function `coxme2`, but with this one you won't have any errors because it does not use hidden functions and objects from the coxme package – eyanquenb Jun 25 '13 at 07:46
  • sorry, I edited my answer in order to point the function you where looking for. But in fact the principle is the same – eyanquenb Jun 25 '13 at 07:56
  • Thx again but there's still an issue. ^^' In fact, It's the same that with print.coxme : The use of the function gives the same error... – Vincent Jun 25 '13 at 09:31
  • I have run the example and I have applied the new function and it works for me. try [this](http://pastebin.com/uzzTnfLY) – eyanquenb Jun 25 '13 at 10:20
  • That's strange... Well thank you for your help ! I would vote you up but I'm just a 6-reputation newbie that is not allowed to judge about answers' usefuless ^^' – Vincent Jun 25 '13 at 11:05
  • Could you please try to copy and run the output of coxme:::print.coxme to see if it works with you ? – Vincent Jun 25 '13 at 11:15
  • I just added "stack<-" before your pastebin and then ran stack(...), it does'nt work for me. – Vincent Jun 25 '13 at 11:18