Guest User

Untitled

a guest
Mar 27th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #---------------------------------
  2. #Run this part to generate the data frame to be processed by the function
  3. dosetimes <- c(0,12)
  4. sampletimes <- sort(unique(c(seq(from=0,to=100,by=1))))
  5. #number of subjects
  6. ID <- 1:10
  7. #Make dataframe
  8. df <- expand.grid("ID"=ID,"TIME"=sampletimes, "AMT"=0)
  9. doserows <- subset(df, TIME%in%dosetimes)
  10. doserows$AMT <- 100
  11. #Add back dose information
  12. df <- rbind(df,doserows)
  13. df <- df[order(df$ID,df$TIME),]
  14. df <- subset(df, (TIME==0 & AMT==0)==F)
  15. df$CL[df$ID<=1] <- 2
  16. df$CL[df$ID>=2] <- 2.5
  17. df$V2[df$ID<=1] <- 10
  18. df$V2[df$ID>=2] <- 15
  19. df$Q [df$ID<=1] <- 20
  20. df$Q [df$ID>=2] <- 20
  21. df$V3[df$ID<=1] <- 30
  22. df$V3[df$ID>=2] <- 30
  23. df$KA[df$ID<=1] <- 0.2
  24. df$KA[df$ID>=2] <- 0.25
  25. df$F1[df$ID<=1] <- 1
  26. df$F1[df$ID>=2] <- 0.5
  27.  
  28. #calculate micro-parameters
  29. df$k20 <- df$CL/df$V2
  30. df$k23 <- df$Q/df$V2
  31. df$k32 <- df$Q/df$V3
  32.  
  33. #-----------------------------------
  34. # This is the function to provess the data frame
  35. TwoCompOral <- function(d){
  36. #Accepts a NONMEM style data frame for 1 subject with columns for TIME, AMT,MDV,DV, CL, V2, Q, V3, KA & F1
  37. #Returns a dataframe with populated columns for A1, A2, A3 and DV
  38.  
  39. #set initial values in the compartments
  40. d$A1[d$TIME==0] <- d$AMT[d$TIME==0]*d$F1[1] # Amount in the absorption compartment at time zero.
  41. d$A2[d$TIME==0] <- 0 # Amount in the central compartment at time zero.
  42. d$A3[d$TIME==0] <- 0 # Amount in the peripheral compartment at time zero.
  43.  
  44. #This loop calculates micro-rate constants based on individual's PK parameter values.
  45. #It uses these values to calculate macro-rate constants (Lambda1/lambda2).
  46. #Rate constants(micro- and macro), along other parameters, are used in the equations to calculate drug amounts in each compartment.
  47. #The loop advances the solution from one time interval to the next.
  48. k20 <- d$k20[1]
  49. k23 <- d$k23[1]
  50. k32 <- d$k32[1]
  51. KA <- d$KA[1]
  52. k30 <- 0
  53. E2 <- k20+k23
  54. E3 <- k32+k30
  55.  
  56. #calculate hybrid rate constants
  57. lambda1 = 0.5*((E2+E3)+sqrt((E2+E3)^2-4*(E2*E3-k23*k32)))
  58. lambda2 = 0.5*((E2+E3)-sqrt((E2+E3)^2-4*(E2*E3-k23*k32)))
  59.  
  60.  
  61. for(i in 2:nrow(d))
  62. {
  63.  
  64. t <- d$TIME[i]-d$TIME[i-1]
  65. A2last <- d$A2[i-1]
  66. A3last <- d$A3[i-1]
  67. A1last <- d$A1[i-1]
  68.  
  69. A2term1 = (((A2last*E3+A3last*k32)-A2last*lambda1)*exp(-t*lambda1)-((A2last*E3+A3last*k32)-A2last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
  70. A2term2 = A1last*KA*(exp(-t*KA)*(E3-KA)/((lambda1-KA)*(lambda2-KA))+exp(-t*lambda1)*(E3-lambda1)/((lambda2-lambda1)*(KA-lambda1))+exp(-t*lambda2)*(E3-lambda2)/((lambda1-lambda2)*(KA-lambda2)))
  71. d$A2[i] = A2term1+A2term2 #Amount in the central compartment
  72.  
  73. A3term1 = (((A3last*E2+A2last*k23)-A3last*lambda1)*exp(-t*lambda1)-((A3last*E2+A2last*k23)-A3last*lambda2)*exp(-t*lambda2))/(lambda2-lambda1)
  74. A3term2 = A1last*KA*k23*(exp(-t*KA)/((lambda1-KA)*(lambda2-KA))+exp(-t*lambda1)/((lambda2-lambda1)*(KA-lambda1))+exp(-t*lambda2)/((lambda1-lambda2)*(KA-lambda2)))
  75. d$A3[i] = A3term1+A3term2 #Amount in the peripheral compartment
  76.  
  77. A1last = A1last*exp(-t*KA)
  78. d$A1[i] = A1last + d$AMT[i]*d$F1[1] #Amount in the absorption compartment
  79.  
  80. d$DV[i] <- d$A2[i]/d$V2[i] #Concentration in the central compartment
  81.  
  82. }
  83. d
  84. }
  85.  
  86. #Apply the function to each ID
  87. system.time(simdf <- ddply(df, .(ID), TwoCompOral))
Add Comment
Please, Sign In to add comment