summaryrefslogtreecommitdiff
path: root/R/kinplot.R
blob: df21c13488beb8dbdfc1f7345c4c7cbeea86bf63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# $Id: kinplot.R 117 2011-06-14 08:52:14Z kati $

# Copyright (C) 2008-2013 Johannes Ranke
# Contact: mkin-devel@lists.berlios.de

# This file is part of the R package kinfit

# kinfit is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.

# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.

# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>

utils::globalVariables("x")
kinplot <- function(kinobject, 
	main = "",
	xlab = "Time [days]", ylab = "Parent [% of applied radioactivity]",
        ylim = c("auto", "auto"),
	lpos = "topright")
{
	kindata <- na.omit(kinobject$data)
	kinfits <- kinobject$fits
  if (ylim[1] == "auto") ylim[1] <- 0
  if (ylim[2] == "auto") ylim[2] <- max(kindata$parent)
  ylim <- as.numeric(ylim)
        
	plot(kindata$t, kindata$parent,
	  main = main,
	  xlab = xlab,
	  ylab = ylab,
	  ylim = ylim
  )
	n.m <- length(kinfits)
	colors <- ltys <- 1:n.m
	names(colors) <- names(ltys) <- names(kinfits)
  ltext <- paste(kinobject$parent, "measured")
	for (kinmodel in names(kinfits))
	{
		m = kinfits[[kinmodel]]
		if(class(m) == "nls") {
      if (!"parent.0" %in% names(coef(m))) {
        switch(kinmodel,
          SFO = lines(
              t <- seq(min(kindata$t), max(kindata$t), length.out=500),
              predict(m, 
              newdata = data.frame(t)),
              col = colors[[kinmodel]],
              lty = ltys[[kinmodel]]),
          FOMC = lines(
              t <- seq(min(kindata$t), max(kindata$t), length.out=500),
              predict(m, 
              newdata = data.frame(t)),
              col = colors[[kinmodel]],
              lty = ltys[[kinmodel]]), 
 	    HS = lines(
              t <- seq(min(kindata$t), max(kindata$t), length.out=500),
              predict(m, 
              newdata = data.frame(t)),
              col = colors[[kinmodel]],
              lty = ltys[[kinmodel]]), 
          DFOP = lines(
              t <- seq(min(kindata$t), max(kindata$t), length.out=500),
              predict(m, 
              newdata = data.frame(t)),
              col = colors[[kinmodel]],
              lty = ltys[[kinmodel]])
		)
        ltext <- c(ltext, paste("Fitted", kinmodel, "model"))
      } else {
        switch(kinmodel,
          SFO = curve(SFO(x, 
            coef(m)[["parent.0"]], 
            coef(m)[["k"]]),
            from = min(kindata$t), to = max(kindata$t), add=TRUE,
            col = colors[[kinmodel]],
            lty = ltys[[kinmodel]]),
          FOMC = curve(FOMC(x, 
            coef(m)[["parent.0"]],
            coef(m)[["alpha"]],
            coef(m)[["beta"]]),
            from = min(kindata$t), to = max(kindata$t), add=TRUE,
            col = colors[[kinmodel]],
            lty = ltys[[kinmodel]]),
          HS = curve(HS(x, 
            coef(m)[["parent.0"]], 
            coef(m)[["k1"]],
            coef(m)[["k2"]],
            coef(m)[["tb"]]),
            from = min(kindata$t), to = max(kindata$t), add=TRUE,
            col = colors[[kinmodel]],
            lty = ltys[[kinmodel]]),
          DFOP = curve(DFOP(x, 
            coef(m)[["parent.0"]], 
            coef(m)[["k1"]],
            coef(m)[["k2"]],
            coef(m)[["g"]]),
            from = min(kindata$t), to = max(kindata$t), add=TRUE,
            col = colors[[kinmodel]],
            lty = ltys[[kinmodel]]))
        ltext <- c(ltext, paste("Fitted", kinmodel, "model"))
      }
		} else {
        ltext <- c(ltext, paste(kinmodel, "model failed"))
            ltys[[kinmodel]] <- NA
		} 
	}
	legend(lpos, bty="n", inset = 0.05, 
		legend = ltext,
		pch = c(1, rep(NA, n.m)),
		lty = c(NA, ltys),
		col = c(1, colors))
}

Contact - Imprint