-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpdfplot.R
executable file
·209 lines (208 loc) · 8.96 KB
/
pdfplot.R
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#------------------------------------------------------------------------------------------
# pdf.plot
# Kalliope Akantziliotou, Mikis Stasinopoulos
# Thursday, February 6, 2003 at 12:04
# last May 2019 Mikis and Fernanda
#------------------------------------------------------------------------------------------
#------------------------------------------------------
pdf.plot<- function (obj = NULL,
obs = c(1),
family = NO(),
mu = NULL,
sigma = NULL,
nu = NULL,
tau = NULL,
from = 0,
to = 10,
min = NULL,
max = NULL,
no.points = 201,
no.title = FALSE,
col = gray(.4),
y.axis.lim = 1.1,
frame.plot = TRUE,
...)
{
if (is.null(min)) {min <- from}
if (is.null(max)) {max <- to}
gamlss.bi.list <- .binom
if (!is.null(obj)) family <- obj[["family"]][1]
fname <- if (is.name(family)) as.character(family)
else if (is.character(family)) family
else if (is.call(family)) as.character(family[[1]])
else if (is.function(family)) deparse(substitute(family))
else if (is(family, "gamlss.family")) family$family[1]
else stop("the family must be a character or a gamlss.family name")
fam1 <- eval(parse(text=fname)) # the family to output
fam <- as.gamlss.family(family) # this is created so I can get things
dorfun <- paste("d",fname,sep="") # say dNO
nopar <- fam$nopar # or fam1$nopar
type <- fam$type
#---------------------------------------------------------
y.var <- if(type=="Discrete") seq(min,max,by=1)
else seq(min,max,length=no.points)
if(!is.null(obj))
{
if (!is.gamlss(obj)) stop(paste("This is not an gamlss object", "\n", ""))
lobs <- length(obs)
if (lobs > 9) stop(paste("Use up to eight observations for ploting"))
plots <- list(c(1,1),c(2,1),c(3,1),c(2,2),c(3,2),c(3,2),c(4,2),c(4,2))
vvv <- unlist(plots[lobs])
if (type=="Discrete") {typelh <- "h" } else {typelh <- "l" }
linter <- length(y.var)
if (any(fname%in%.gamlss.bi.list))
{
stop(paste("This function is not working for binomial type distibutions", "\n", ""))
}
if ("mu"%in%names(fam$parameters))
{
mu.var <- fitted(obj)[obs]
if (!fam$mu.valid(mu.var)) stop( "`mu' parameter out of range")
}
if ("sigma"%in%names(fam$parameters))
{
sigma.var <- fitted(obj,"sigma")[obs]
if (!fam$sigma.valid(sigma.var)) stop( "`sigma' parameter out of range")
}
if ("nu"%in%names(fam$parameters))
{
nu.var <- fitted(obj,"nu")[obs]
if (!fam$nu.valid(nu.var)) stop( "`nu' parameter out of range")
}
if ("tau"%in%names(fam$parameters))
{
tau.var <- fitted(obj,"tau")[obs]
if (!fam$tau.valid(tau.var)) stop( "`tau' parameter out of range")
}
pdfArr <- array(0:0, c(linter,lobs))
title.label <- rep(NA, lobs)
op <- par(mfrow=vvv, mar=par("mar")+c(0,0,0,0),
col.axis=gray(.2),col.main=gray(.2),
col.lab=gray(.2), cex=0.6, font=2)
# loop
for (j in 1:lobs)
{
pdf11 <- switch (nopar,
paste0("d",fname,"(y.var,mu.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j], nu.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j], nu.var[j],
tau.var[j])")
)
fy11 <- eval(parse(text=pdf11))
pdfArr[,j] <- fy11
if (!is.null(fam$parameters$mu))
{
m.title <- bquote(paste(.(fname),"(",paste(mu," = ",.(sprintf("%.2f",mu.var[j])),")")))
}
#paste0(" mu = ", sprintf("%.2f",mu.var[j]), ifelse(!is.null(fam$parameters$sigma),",", " "))
if (!is.null(fam$parameters$sigma))
{
m.title <- bquote(paste(.(fname),"(",paste(mu," = ",
.(sprintf("%.2f", mu.var[j])),","),
paste(sigma," = ",.(sprintf("%.2f",sigma.var[j])),")")))
} # paste(" sigma = ",sprintf("%.2f",sigma.var[j]), ifelse(!is.null(fam$parameters$nu),",", " "), sep="")
if (!is.null(fam$parameters$nu))
{
m.title <- bquote(paste(.(fname),"(",paste(mu," = ", .(sprintf("%.2f", mu.var[j])),","),
paste(sigma," = ",.(sprintf("%.2f",sigma.var[j])),","),
paste(nu," = ", .(sprintf("%.2f", nu.var[j])),")"),))
}
if (!is.null(fam$parameters$tau))
{
m.title <- bquote(paste(.(fname),"(",paste(mu," = ", .(sprintf("%.2f", mu.var[j])),","),
paste(sigma," = ",.(sprintf("%.2f",sigma.var[j])),","),
paste(nu, " = ", .(sprintf("%.2f", nu.var[j])),","),
paste(tau," = ", .(sprintf("%.2f", tau.var[j])),")")))
}
title.label[j] <- if (!no.title) as.expression(m.title) else ""
if(obj$type=="Discrete") y.title <- "P(Y=y)" else y.title <- "f(y)"
plot(y.var , pdfArr[,j], xlab="y",
ylab=y.title,
main=title.label[j],
col=gray(.4),
frame.plot = frame.plot,
type=typelh, lty=1, lwd=1, ...)
}
par(op)
}###################################################################
else # if only family (NOT A MODEL) is used
{
lobs <- max(c(length(mu),length(sigma),length(nu),length(tau)))
if (lobs >= 9) stop(paste("Use up to eight different combinations of parameters for plotting"))
plots <- list(c(1,1),c(2,1),c(3,1),c(2,2),c(3,2),c(3,2),c(4,2),c(4,2))
vvv <- unlist(plots[lobs])
if(any(fname%in%.gamlss.bi.list)) bd <- max
pdfunction <- rep(0, length(y.var))
if(type=="Discrete") {typelh <- "h" } else {typelh <- "l" }
if ("mu"%in%names(fam$parameters))
{ if (is.null(mu)) stop("At least one value of mu has to be set")
mu.var <- rep(mu, length = lobs)
if (!fam$mu.valid(mu.var)) stop( "`mu' parameter out of range")
}
if ("sigma"%in%names(fam$parameters))
{ if (is.null(sigma)) stop("At least one value of sigma has to be set")
sigma.var <- rep(sigma, length = lobs)
if (!fam$sigma.valid(sigma.var)) stop( "`sigma' parameter out of range")
}
if ("nu"%in%names(fam$parameters))
{
if (is.null(nu)) stop("At least one value of nu has to be set")
nu.var <- rep(nu, length = lobs)
if (!fam$nu.valid(nu.var)) stop( "`nu' parameter out of range")
}
if ("tau"%in%names(fam$parameters))
{ if (is.null(tau)) stop("At least one value of tau has to be set")
tau.var <- rep(tau, length = lobs)
if (!fam$tau.valid(tau.var)) stop( "`tau' parameter out of range")
}
if (!fam$y.valid(y.var)) stop( "response variable out of range")
pdfArr <- array(0:0, c(length(y.var),lobs))
title.label <- rep(NA, lobs)
op <- par(mfrow=vvv, mar=par("mar")+c(0,0,0,0),
col.axis=gray(.2),
col.main=gray(.2),
col.lab=gray(.2),
cex=0.6, font=2)
for (j in 1:lobs)
{
pdf11 <- switch (nopar,
paste0("d",fname,"(y.var,mu.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j], nu.var[j])"),
paste0("d",fname,"(y.var,mu.var[j],sigma.var[j], nu.var[j], tau.var[j])")
)
fy11 <- eval(parse(text=pdf11))
pdfArr[,j] <- fy11
if (!is.null(fam$parameters$mu)) {
#bquote(paste(.(fname),"(",paste(mu," = ",.(2),")")))
m.title <- bquote(paste(.(fname),"(",paste(mu," = ",.(2),")")))}
if (!is.null(fam$parameters$sigma)) {
m.title <- bquote(paste(.(fname),"(",paste(mu," = ", .(mu.var[j]),
sep=","),
paste(sigma," = ",.(sigma.var[j]) ),")"))}
if (!is.null(fam$parameters$nu)) {
m.title <- bquote(paste(.(fname),"(",paste(mu," = ", .(mu.var[j]),
sep=","),
paste(sigma," = ",.(sigma.var[j]), sep=","),
paste( nu," = ",.( nu.var[j])) ,")"))}
if (!is.null(fam$parameters$tau)){
m.title <- bquote(paste(.(fname),"(",paste(mu," = ", .(mu.var[j]),
sep=","),
paste(sigma," = ",.(sigma.var[j]), sep=","),
paste( nu," = ",.( nu.var[j]), sep=","),
paste( tau," = ",.( tau.var[j])) ,')'))}
title.label[j] <- if (!no.title) as.expression(m.title) else ""
y.title <- if(type=="Discrete") "P(Y=y)" else "f(y)"
plot(y.var , pdfArr[,j], xlab="y",
ylab=y.title,
main= title.label[j],
col = col,
frame.plot = frame.plot,
type = typelh,
...)
}
par(op)
}
par(op)
}