File: plot-qqPlot.R

package info (click to toggle)
fbasics 4052.98-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,020 kB
  • sloc: ansic: 738; makefile: 14
file content (397 lines) | stat: -rw-r--r-- 10,223 bytes parent folder | download | duplicates (9)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


################################################################################
# FUNCTION:                DESCRIPTION:
#  qqnormPlot               Returns a tailored normal quantile-quantile plot
#  qqnigPlot                Returns a tailored NIG quantile-quantile plot
#  qqghtPlot                Returns a tailored GHT quantile-quantile plot
#  qqgldPlot                Returns a tailored LD quantile-quantile plot
################################################################################


qqnormPlot <-
function(x, labels = TRUE, col = "steelblue", pch = 19,
    title = TRUE, mtext = TRUE, grid = FALSE, rug = TRUE, scale = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Example of a Normal quantile plot of data x to provide a visual
    #   assessment of its conformity with a normal (data is standardised
    #   first).

    # Arguments:
    #   x - an univariate return series of class 'timeSeries'
    #       or any other object which can be transformed by the function
    #       'as.timeSeries()' into an object of class 'timeSeries'.

    # Details:
    #   The ordered data values are posterior point estimates of the
    #   underlying quantile function. So, if you plot the ordered data
    #   values (y-axis) against the exact theoretical quantiles (x-axis),
    #   you get a scatter that should be close to a straight line if the
    #   data look like a random sample from the theoretical distribution.
    #   This function chooses the normal as the theory, to provide a
    #   graphical/visual assessment of how normal the data appear.
    #   To help with assessing the relevance of sampling variability on
    #   just "how close" to the normal the data appears, we add (very)
    #   approximate posterior 95% intervals for the uncertain quantile
    #   function at each point (Based on approximate theory) .

    # Author:
    #   Based on code written by Mike West, mw@stat.duke.edu

    # Note:
    #   Source from
    #   http://www.stat.duke.edu/courses/Fall99/sta290/Notes/

    # Example:
    #   x = rnorm(100); qqnormPlot(x); qqnormPlot(x, labels = FALSE)

    # FUNCTION:

    # Settings:
    if (!is.timeSeries(x)) x = as.timeSeries(x)
    Units = x@units
    x = as.vector(x)
    n = length(x)

    # Fit:
    p = (1:n)/(n+1)
    if (scale) x = (x-mean(x))/sqrt(var(x))
    par = c(mean = mean(x), var = var(x))

    # Quantiles:
    x = sort(x)
    p = ppoints(x)
    if (scale) z = qnorm(p) else z = qnorm(p, mean(x), sd(x))

    # Plot:
    if (labels) {
        xlab = "Normal Quantiles"
        ylab = paste(Units, "Ordered Data")
        plot(z, x, xlab = xlab, ylab = ylab,
            col = col, pch = 19, ...)
    } else {
        plot(z, x, ...)
    }

    # Title:
    if(title) {
        title(main = "NORM QQ PLOT")
    }

    # Margin Text:
    if (mtext) {
        Text = "Confidence Intervals: 95%"
        mtext(Text, side = 4, adj = 0, col = "darkgrey", cex = 0.7)
    }

    # Grid:
    if (grid) {
        grid()
    }

    # Add Diagonal Line:
    abline(0, 1, col = "grey")

    # Add Rugs:
    if(rug) {
        rug(z, ticksize = 0.01, side = 1, quiet = TRUE)
        rug(x, ticksize = 0.01, side = 2, quiet = TRUE)
    }

    # 95% Confidence Intervals:
    s = 1.96*sqrt(p*(1-p)/n)
    pl = p-s
    i = pl<1 & pl>0
    lower = quantile(x, probs = pl[i])
    lines(z[i], lower, col = "brown")
    pl = p+s
    i = pl < 1 & pl > 0
    upper = quantile(x, probs = pl[i])
    lines(z[i], upper, col = "brown")
    abline(h = mean(x), col = "grey")
    abline(v = mean(x), col = "grey")

    # Result:
    ans = list(x = z, y = x)
    attr(ans, "control")<-par

    # Return Value:
    invisible(ans)
}


# ------------------------------------------------------------------------------


qqnigPlot <-
function(x, labels = TRUE, col = "steelblue", pch = 19,
    title = TRUE, mtext = TRUE, grid = FALSE, rug = TRUE, scale = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Displays a NIG quantile-quantile Plot

    # Arguments:
    #   x - an univariate return series of class 'timeSeries'
    #       or any other object which can be transformed by the function
    #       'as.timeSeries()' into an object of class 'timeSeries'.

    # Example:
    #   qqnigPlot(rnig(100))

    # FUNCTION:

    # Settings:
    if (!is.timeSeries(x)) x = as.timeSeries(x)
    stopifnot(isUnivariate(x))
    Units = x@units
    x = as.vector(x)
    n = length(x)

    ## YC: no scaling
    ## FIXME: should take care of too small time series

    # Fit:
    fit = nigFit(x, doplot = FALSE, trace = FALSE)
    par = fit@fit$estimate
    names(par) = c("alpha", "beta", "delta", "mu")

    # Quantiles:
    x = sort(x)
    p = ppoints(x)

    z = qnig(p, par[1], par[2], par[3], par[4])

    # Plot:
    if (labels) {
        xlab = "Theoretical Quantiles"
        ylab = "Sample Quantiles"
        plot(z, x, xlab = xlab, ylab = ylab, col = col, pch = pch, ...)
    } else {
        plot(z, x, ...)
    }

    # Title:
    if (title) {
        title(main = "NIG QQ Plot")
    }

    # Margin Text:
    rpar = signif(par, 3)
    text = paste(
        "alpha =", rpar[1],
        "| beta =", rpar[2],
        "| delta =", rpar[3],
        "| mu =", rpar[4])
    mtext(text, side = 4, adj = 0, col = "grey", cex = 0.7)

    # Grid:
    if (grid) {
        grid()
    }

    # Add Fit:
    abline(lsfit(z, x))

    # Add Rugs:
    if(rug) {
        rug(z, ticksize = 0.01, side = 3, quiet = TRUE)
        rug(x, ticksize = 0.01, side = 4, quiet = TRUE)
    }

    # Result:
    ans = list(x = z, y = x)
    attr(ans, "control") <- par

    # Return Value:
    invisible(ans)
}


# ------------------------------------------------------------------------------


qqghtPlot <-
function(x, labels = TRUE, col = "steelblue", pch = 19,
    title = TRUE, mtext = TRUE, grid = FALSE, rug = TRUE, scale = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Displays a GHT quantile-quantile Plot

    # Arguments:
    #   x - an univariate return series of class 'timeSeries'
    #       or any other object which can be transformed by the function
    #       'as.timeSeries()' into an object of class 'timeSeries'.

    # Example:
    #   qqnigPlot(rgh(100))

    # FUNCTION:

    # Settings:
    if (!is.timeSeries(x)) x = as.timeSeries(x)
    stopifnot(isUnivariate(x))
    Units = x@units
    x = as.vector(x)
    n = length(x)

    # Fit:
    fit = ghtFit(x, doplot = FALSE, trace = FALSE)
    par = fit@fit$estimate
    names(par) = c("beta", "delta", "mu", "nu")

    # Plot:
    x <- sort(x)
    p <- ppoints(x)
    z <- qght(p, par[1], par[2], par[3], par[4])


    if (labels) {
        plot(z, x, col = col, ann = FALSE, ...)
    } else {
        plot(z, x, ...)
    }

    # Add Grid:
    if (grid) {
        grid()
    }

    # Add title:
    if (title) {
        title(
            main = "GHT QQ Plot",
            xlab = "Theoretical Quantiles",
            ylab = "Sample Quantiles")
    }


    # Add Fit:
    abline(lsfit(z, x))

    # Add Rugs:
    if(rug) {
        rug(z, ticksize = 0.01, side = 3, quiet = TRUE)
        rug(x, ticksize = 0.01, side = 4, quiet = TRUE)
    }

    # Result:
    ans = list(x = z, y = x)
    attr(ans, "control")<-par

    # Return Value:
    invisible(ans)
}


################################################################################


qqgldPlot <-
function(x, labels = TRUE, col = "steelblue", pch = 19,
    title = TRUE, mtext = TRUE, grid = FALSE, rug = TRUE, scale = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Displays a Generalized lambda Distribution quantile-quantile Plot

    # Arguments:
    #   x - an univariate return series of class 'timeSeries'
    #       or any other object which can be transformed by the function
    #       'as.timeSeries()' into an object of class 'timeSeries'.

    # Example:
    #   qqgldPlot(rgld(100))

    # FUNCTION:

    # Settings:
    if (!is.timeSeries(x)) x = as.timeSeries(x)
    stopifnot(isUnivariate(x))
    Units = x@units
    x = as.vector(x)
    n = length(x)

    ## YC: no scaling
    ## FIXME: should take care of too small time series

    # Fit:
    fit = gldFit(x, doplot = FALSE, trace = FALSE)
    par = fit@fit$estimate
    names(par) = c("lambda1", "lambda2", "lambda3", "lambda4")

    # Quantiles:
    x = sort(x)
    p = ppoints(x)

    z = qgld(p, par[1], par[2], par[3], par[4])

    # Plot:
    if (labels) {
        xlab = "Theoretical Quantiles"
        ylab = "Sample Quantiles"
        plot(z, x, xlab = xlab, ylab = ylab, col = col, pch = pch, ...)
    } else {
        plot(z, x, ...)
    }

    # Title:
    if (title) {
        title(main = "NIG QQ Plot")
    }

    # Margin Text:
    rpar = signif(par, 3)
    text = paste(
        "lambda1 =", rpar[1],
        "| lambda2 =", rpar[2],
        "| lambda3 =", rpar[3],
        "| lambda4 =", rpar[4])
    mtext(text, side = 4, adj = 0, col = "grey", cex = 0.7)

    # Grid:
    if (grid) {
        grid()
    }

    # Add Fit:
    abline(lsfit(z, x))

    # Add Rugs:
    if(rug) {
        rug(z, ticksize = 0.01, side = 3, quiet = TRUE)
        rug(x, ticksize = 0.01, side = 4, quiet = TRUE)
    }

    # Result:
    ans = list(x = z, y = x)
    attr(ans, "control") <- par

    # Return Value:
    invisible(ans)
}


################################################################################