FelicienLL / mapbayr

Easy Maximum A Posteriori Bayesian Estimation of PK parameters in R.
19 stars 2 forks source link

Plot Method GOF #21

Open FelicienLL opened 3 years ago

FelicienLL commented 3 years ago

A method for plotting goodness of fit should be implemented, including :

AlexandreLeM commented 2 years ago

Hello

I have written a function that allows you to make different types of plots:

plot_GOF <- function(my_est, x = "DV", y = "IPRED", log = FALSE){

  #'x =' for the value on x-axis. You can choose between "IPRED", "DV", "RES", "IRES" and "PRED"
  #'y =' for the value on y-axis You can choose between "time", "IPRED","DV","RES","IRES","PRED"
  #'log =' for a log transformation of the data (TRUE/FALSE) 

  if (log == FALSE){
    x_legend = x
    y_legend = y
  }else{
    x_legend = paste0("log(",x,")")
    y_legend = paste0("log(",y,")")}

  if( !x %in% c("time", "IPRED","DV","RES","IRES","PRED")) stop("output type must be 'time', 'DV', 'IPRED', 'PRED', 'IRES' or 'RES'", call. = FALSE)
  if( !y %in% c("IPRED","DV","RES","IRES",'PRED')) stop("output type must be 'DV', 'IPRED', 'PRED', 'IRES' or 'RES'", call. = FALSE)

  if (x == "IPRED") {x1 = (my_est$mapbay_tab$IPRED[my_est$mapbay_tab$mdv == 0])}
  if (y == "IPRED") {y1 <- (my_est$mapbay_tab$IPRED[my_est$mapbay_tab$mdv == 0])}
  if (x == "DV") {x1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])}
  if (y == "DV") {y1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])}
  if (x == "PRED") {x1 <- (my_est$mapbay_tab$PRED[my_est$mapbay_tab$mdv == 0])}
  if (y == "PRED") {y1 <- (my_est$mapbay_tab$PRED[my_est$mapbay_tab$mdv == 0])}
  if (x == "RES") {x1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])-(my_est$mapbay_tab$PRED[my_est$mapbay_tab$mdv == 0])}
  if (y == "RES") {y1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])-(my_est$mapbay_tab$PRED[my_est$mapbay_tab$mdv == 0])}
  if (x == "IRES") {X1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])-(my_est$mapbay_tab$IPRED[my_est$mapbay_tab$mdv == 0])}
  if (y == "IRES") {y1 <- (my_est$mapbay_tab$DV[my_est$mapbay_tab$mdv == 0])-(my_est$mapbay_tab$IPRED[my_est$mapbay_tab$mdv == 0])}
  if (x == "time") {x1 <- (my_est$mapbay_tab$time[my_est$mapbay_tab$mdv == 0])}

  if (log == TRUE){(x1<-log10(x1)) & (y1<-log10(y1))}

ID <- my_est$mapbay_tab$ID[my_est$mapbay_tab$mdv == 0]

data_plot <-data.frame(x1,y1,ID)

plot <- ggplot(data_plot, aes(x1,y1)) +
  theme_bw() +  
  xlab(x_legend) + ylab(y_legend) 

  if (x != "time") {plot <- plot + geom_abline(intercept = 0, slope = 1)} else {plot <- plot + geom_hline(yintercept=0)}
  if (x != "time") {plot <- plot + geom_smooth(aes(x1,y1), se = FALSE, lty = 2,col = "red",lwd = 1)} else {plot <- plot + geom_smooth(aes(x1,y1), se = FALSE, lty = 2,col = "red",lwd = 1)}
  if (x == "time") {plot <- plot + ylim(-(max(abs(y1))),max(abs(y1)))}
  if (log == TRUE) {plot <- plot + ylim(min(abs(c(y1,x1))),max(abs(c(y1,x1)))) + xlim(min(abs(c(y1,x1))),max(abs(c(y1,x1))))}

plot <- plot + geom_point(lwd=3,col = ID) 

print(plot)
}

Perhaps you could review and optimise it for implementation :)

Alexandre


For example:

library(mapbayr)
library(ggplot2)

my_est <- est001 

DV versus IPRED

plot_GOF(my_est, x = "IPRED", y="DV", log = FALSE)

Rplot

DV versus IPRED with log scale

plot_GOF(my_est, x = "IPRED", y="DV", log = TRUE)

Rplot02