1 ## PsN R script for plotting results of llp
8 ## autogenerated code begins
9 ## we can set other things, like colours, line types, layout, etc here if we wish
11 ofv <- 3.843 # set this to the desired llp ofv tolerance
12 ci.lines <- TRUE # do we want grey broken lines for outer CI limits?
13 dofv.line <- TRUE # do we want grey broken line for final model parameter estimate?
14 hline <- TRUE # do we want grey broken line for desired llp ofv tolerance?
16 ## autogenerated code ends
19 llp.data <- read.csv("llplog1.csv")
21 ## get number of parameters
22 n <- (length(colnames(llp.data)) - 1) / 2
33 ## create data frame for each parameter
34 p1 <- c(llp.data[i],llp.data[i+1])
35 p1 <- as.data.frame(p1)
36 p1 <- p1[!(apply(is.na(p1),1,any)),]
38 parmlabel <- names(p1)[1]
40 names(p1) <- c("Parameter", "dOFV")
42 p1$Parameter <- as.numeric(as.vector(p1$Parameter))
43 p1$dOFV <- as.numeric(as.vector(p1$dOFV))
45 ## 3rd order polynomial
46 fit <- lm(p1$dOFV~p1$Parameter+I(p1$Parameter^2)+I(p1$Parameter^3))
47 ## 4th order polynomial
48 fit2 <- lm(p1$dOFV~p1$Parameter+I(p1$Parameter^2)+I(p1$Parameter^3)+I(p1$Parameter^4))
50 if (max(p1$Parameter,na.rm=TRUE) > 0) {
51 pmn <- min(p1$Parameter,na.rm=TRUE) - (max(p1$Parameter,na.rm=TRUE)/10)
52 pmx <- max(p1$Parameter,na.rm=TRUE) + (max(p1$Parameter,na.rm=TRUE)/10)
54 pmn <- min(p1$Parameter,na.rm=TRUE) + (max(p1$Parameter,na.rm=TRUE)/10)
55 pmx <- max(p1$Parameter,na.rm=TRUE) - (max(p1$Parameter,na.rm=TRUE)/10)
68 ip <- 0 ## inflections - down is bad
74 ## try third order first
76 pfit <- as.vector( fit$coefficients[1] +
77 (fit$coefficients[2] * pcur) +
78 (fit$coefficients[3] * (pcur^2)) +
79 (fit$coefficients[4] * (pcur^3))
81 addrow <- c(pcur, pfit)
82 mat.txt <- c(mat.txt, addrow)
91 if ((pfit <= ofv) && (pprev >= ofv)) {
92 if (abs(pfit-ofv) <= abs(pprev-ofv)) {
98 ## we have a number for int1
103 if ((pfit >= ofv) && (pprev <= ofv)) {
104 if (abs(pfit-ofv) <= abs(pprev-ofv)) {
110 ## we have a number for int2
114 ## have we reached the nadir of the parabola?
118 if ((ip == 1) && (pfit < pprev)) {
129 ## try fourth order fit
144 pfit <- as.vector( fit2$coefficients[1] +
145 (fit2$coefficients[2] * pcur) +
146 (fit2$coefficients[3] * (pcur^2)) +
147 (fit2$coefficients[4] * (pcur^3)) +
148 (fit2$coefficients[5] * (pcur^4))
150 addrow <- c(pcur, pfit)
151 mat.txt <- c(mat.txt, addrow)
160 if ((pfit <= ofv) && (pprev >= ofv)) {
161 if (abs(pfit-ofv) <= abs(pprev-ofv)) {
167 ## we have a number for int1
172 if ((pfit >= ofv) && (pprev <= ofv)) {
173 if (abs(pfit-ofv) <= abs(pprev-ofv)) {
179 ## we have a number for int2
183 ## have we reached the nadir of the parabola?
187 if ((ip == 1) && (pfit < pprev)) {
198 mdat <- matrix(mat.txt, nrow = pno, ncol=2, byrow=TRUE,
199 dimnames = list(c(), c("Parameter", "dOFV")))
201 llp.parabola <- as.data.frame(mdat)
203 postscript(file=paste("llp.", parmlabel, ".ps", sep=""), paper="a4",
204 title=paste("Log-likelihood profile - ", parmlabel, sep=""),
207 plot (llp.parabola$Parameter, llp.parabola$dOFV,
209 # ylim=c(0,max(data.main$DV,na.rm=TRUE)),
212 main=paste("Log-likelihood profile - ", parmlabel, sep="")
213 #sub="Binning tolerance: 0.25, occasion: 0"
216 points(p1$Parameter, p1$dOFV, col="red", pch=21)
217 abline(v=pint1, col="red", lty=2)
218 abline(v=pint2, col="red", lty=2)
219 text(pint1, ofv, labels=signif(pint1, digits = 3), cex = .8, adj = c(0,0), pos='4')
220 text(pint2, ofv, labels=signif(pint2, digits = 3), cex = .8, adj = c(0,0), pos='4')
221 text(p1[1,1], 0, labels=signif(p1[1,1], digits = 3), cex = .8, adj = c(0,0), pos='3')
224 abline(v=min(p1$Parameter,na.rm=TRUE), col="gray", lty=2)
225 abline(v=max(p1$Parameter,na.rm=TRUE), col="gray", lty=2)
229 abline(v=p1[1,1], col="gray", lty=2)
233 abline(h=ofv, col="gray", lty=2)