Package 'ternvis'

Title: Visualisation, Verification and Calibration of Ternary Probabilistic Forecasts
Description: A suite of functions for visualising ternary probabilistic forecasts, as discussed in the paper by Jupp (2012) <doi:10.1098/rsta.2011.0350>.
Authors: Tim Jupp
Maintainer: Tim Jupp <[email protected]>
License: GPL-2
Version: 1.2
Built: 2024-12-07 06:29:52 UTC
Source: CRAN

Help Index


Visualisation, verification and calibration of ternary forecast data

Description

A suite of functions for visualising ternary probabilistic forecasts.

Details

Package: ternvis
Type: Package
Version: 1.0
Date: 2012-03-29
License: GPL-2

Colours can be assigned to ternary probabilistic forecasts using tcolour. These colours can be used to produce forecast maps as in the example function tmap. A set of ternary forecasts p can be compared with subsequent ternary observations o using the function tverify. plot.tverify then displays this information in a Ternary Reliability Diagram. Calibration is performed using tgetcal and tcalibrate.

Author(s)

Tim Jupp

Maintainer: Tim Jupp <[email protected]>

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

Some concepts adapted from those in package vcd.

Examples

data(foot)
# see the distribution of forecasts
tplot(foot$p,main="Bookmaker forecasts of \n football matches",
      dimnames=c("Home Win","Draw","Away Win"))
# see how well forecasts compare with results
# create object of class tverify
foot.verify <- tverify(p=foot$p,o=foot$o)
 
# plot ternary reliability diagram
dev.new()
plot(foot.verify, thresh=3)               

# get a (linear) calibration of these data
# create an object of class tverify
foot.calib <- tgetcal(foot.verify) 

# plot ternary reliability diagram of calibrated        
dev.new()
plot(foot.calib, thresh=3)                

data(rain)

tmap(rain,iyr=17,palette=TRUE,circles=FALSE,fac=10)

Ternary precipitation forecast data set

Description

An array of monthly seasonal precipitation forecasts and observations over South America, covering 25 years.

Usage

data(rain)

Format

A list containing 7 objects

rain$lats a vector of 35 latitudes

rain$lons a vector of 27 longitudes

rain$obs array of ternary observations

rain$preds array of ternary forecast probabilities

rain$rel array of reliabilities

rain$res array of resolutions

rain$q array of climatologies

Source

Data from Caio Coelho at http://eurobrisa.cptec.inpe.br.

Examples

data(rain)

ilon <- 5 # pick a longitude
ilat <- 6 # pick a latitude
iyr  <- 17 # pick a year (in the range 1:25)

rain$lons[ilon]                # the longitude
rain$lats[ilat]                # the latitude
rain$pred[ilon,ilat,iyr,] # the ternary forecast
rain$obs[ilon,ilat,iyr,]  # the ternary observation

Calibration of ternary forecasts

Description

Function to apply a calibration to a matrix of ternary forecasts.

Usage

tcalibrate(tv, p)

Arguments

tv

An object of class tverify, for example produced by tgetcal.

p

An n-by-3 matrix of ternary forecasts which will be calibrated according to the calibration function embedded in dat.

Value

An n-by-3 array of calibrated ternary forecasts.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tgetcal, tplot, tverify

Examples

# get football betting data
data(foot) 

# get verification info for these data                         
tv   <- tverify(p=foot$p,o=foot$o)  

# get a linear calibration based on verification info         
lc   <- tgetcal(tv)  

# consider an uncalibrated forecast p                 
p    <- cbind(0,0,1)    

# see where p is mapped by this calibration            
pcal <- tcalibrate(lc,cbind(0,0,1)) 

# plot the uncalibrated forecast
tplot(p) 
# plot the calibrated forecast                        
tplot(pcal,col="blue",newpage=FALSE,bg="transparent")

Assign a colour to a ternary forecast

Description

Function to assign a colour to a ternary forecast p, given a ternary climatology q.

Usage

tcolour(p = cbind(1, 1, 1)/3, q = cbind(1, 1, 1)/3, m = 0.7, 
        flip = FALSE, dich = "none", theta0 = 0)

Arguments

p

An n-by-3 matrix of ternary forecasts.

q

A 1-by-3 matrix containing the climatology.

m

Exponent controlling rate of colour saturation.

flip

Logical to transpose categories "B" and "A" in barycentric coordinates.

dich

Option to render colours as they would appear to a dichromat. Default choice dich = "none" leaves colours unchanged. Choices dich = "deutan" and dich = "protan" show colours as they would appear to those with deutanopia or protanopia (see dichromat).

theta0

Angle (degrees) through which to rotate colour palette around climatology q in barycentric coordinates.

Value

A colour, of the format produced by rgb.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

Examples

p <- cbind(0.7,0.2,0.1)   # define a forecast
q <- cbind(1,1,1)/3       # define a climatology
pcol <- tcolour(p=p,q=q)  # assign a colour
pcol                      # print the colour code to terminal
tplot(p,col=pcol)         # use this colour in a plot

Get a linear or quadratic calibration of ternary forecast / observation data

Description

Function to obtain a best-fitting calibration to a set of ternary forecasts p and corresponding observations o.

Usage

tgetcal(tv, quad = FALSE)

Arguments

tv

An object of the tverify class (as produced by tverify).

quad

A logical describing which functional form to use for calibration. Linear calibration (quad = FALSE) is faster. Quadratic calibration (quad = TRUE) is more sophisticated but can be slow for large data sets.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tverify, tcalibrate

Examples

data(foot)
# see the distribution of forecasts
tplot(foot$p,main="Bookmaker forecasts of \n football matches",
      dimnames=c("Home Win","Draw","Away Win"))
      
# see how well forecasts compare with results
# create object of class tverify
foot.verify <- tverify(p=foot$p,o=foot$o) 

# plot ternary reliability diagram
dev.new()
plot(foot.verify, thresh=3)    
           
# get a (linear) calibration of these data
# create an object of class tverify
foot.calib <- tgetcal(foot.verify) 

# plot ternary reliability diagram of calibrated forecasts       
dev.new()
plot(foot.calib, thresh=3)

Create a grid of points in barycentric coordinates

Description

Utility function to generate a grid of ternary forecasts equally spaced in baycentric coordinates.

Usage

tgrid(n)

Arguments

n

An integer describing the number of ‘bins’ along each side of the barycentric triangle.

Value

An n-by-3 matrix of ternary forecasts.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tplot

Examples

tg <- tgrid(6) # make a grid of forecasts in the triangle
tg             # print forecasts to the terminal
tplot(tg)      # plot forecasts

Demonstration of how to make coloured maps of ternary forecasts

Description

An example of how one might make forecast maps using tpalette.

Usage

tmap(dat, iyr, circles = FALSE, fac = 10, theta0 = 0, 
     dich = "none", m = 0.7, palette = TRUE, 
     flip = FALSE)

Arguments

dat

An array of forecasts in a suitable format. See Example code below

iyr

In this example, an integer in the range 1 to 25.

circles

circles = FALSE (the default) fills map with coloured polygons. circles = TRUE fills map with circles whose radii are dependent on verification information.

fac

Scaling factor for size of circles when circles = TRUE.

theta0

Angle to rotate colours on palette. See tcolour.

dich

Optionally render colours as they would appear to a dichromat. See tcolour.

m

Parameter controlling rate at which colours saturate in palette. See tcolour.

palette

Logical. If palette = TRUE a colour palette is included.

flip

Logical to choose whether to switch categories B and A. See tcolour.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

Examples

data(rain)

tmap(rain,iyr=17,palette=TRUE,circles=FALSE,fac=10)

Draw a palette of the colours assigned to ternary forecasts

Description

Function to fill barycentric coordinates with a colour palette.

Usage

tpalette(q = cbind(1, 1, 1)/3, bars = TRUE, m = 0.7, 
         flip = FALSE, lmain = FALSE, theta0 = 0, 
	 dich = "none", cex = 1, nhist = 12)

Arguments

q

A 1-by-3 matrix containing a climatology.

bars

Logical. If "FALSE" a simple colour palette is produced. If "TRUE" barplots representing the forecasts are overlain.

m

Parameter controlling the rate at which colours saturate towards the corners of the triangle.

flip

Logical to transpose categories "B" and "A" in barycentric coordinates.

lmain

Logical. If "TRUE" text is overlaid.

theta0

Angle (degrees) through which to rotate colour palette around climatology q in barycentric coordinates.

dich

Option to render colours as they would appear to a dichromat. Default choice dich = "none" leaves colours unchanged. Choices dich = "deutan" and dich = "protan" show colours as they would appear to those with deutanopia or protanopia (see dichromat).

cex

Factor for expanding the size of the dots when bars is "FALSE".

nhist

Number of barplots along each side of the triangle when bars is "TRUE".

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tmap

Examples

dev.new()
tpalette(bars=FALSE) # standard palette

dev.new()
tpalette(bars=TRUE) # palette with histograms overlaid

Plot ternary forecasts in barycentric coordinates.

Description

This function is almost exactly the same as the ternaryplot function in package vcd (on which it is based) except that the ordering of the vertices has been change to bottom left/top/bottom right, and the code has been extended work for an aribtrary scoring matrix L.

Usage

tplot(x = cbind(1, 1, 1)/3, L = diag(c(1, 1, 1))/sqrt(2), 
      scale = 1, dimnames = NULL, 
      dimnames_position = c("corner", "none"), 
      dimnames_color = "black", id = NULL, id_color = "black", 
      coordinates = FALSE, grid = TRUE, grid_color = "gray", 
      labels = c("inside", "outside", "none"), 
      labels_color = "darkgray", border = "grey", 
      bg = "white", pch = 19, cex = 1, prop_size = FALSE, 
      col = "red", main = "ternary plot", sub = NULL, 
      newpage = TRUE, pop = TRUE, col.main = "black", 
      col.sub = "black", ...)

Arguments

x

an n-by-3 matrix of ternary forecasts.

L

A matrix describing the quadratic score function. (Default corresponds to the Brier score).

scale

row sums scale to be used.

dimnames

dimension labels (defaults to the column names of x.

dimnames_position, dimnames_color

position and color of dimension labels.

id

optional labels to be plotted below the plot symbols. coordinates and id are mutually exclusive.

id_color

color of these labels.

coordinates

if TRUE, the coordinates of the points are plotted below them. coordinates and id are mutually exclusive.

grid

if TRUE, a grid is plotted. May optionally be a string indicating the line type (default: "dotted").

grid_color

grid colour.

labels, labels_color

position and colour of the grid labels.

border

colour of the triangle border.

bg

triangle background.

pch

plotting character. Defaults to filled dots.

cex

a numerical value giving the amount by which plotting text and symbols should be scaled relative to the default. Ignored for the symbol size if prop_size is not FALSE.

prop_size

if TRUE, the symbol size is plotted proportional to the row sum of the three variables, i.e., represents the weight of the observation.

col

plotting colour.

main

main title.

sub

a sub title.

newpage

if ‘TRUE’, the plot will appear on a new graphics page.

pop

logical; if ‘TRUE’, all newly generated viewports are popped after plotting.

col.main

colour for the title.

col.sub

colour for the sub title.

...

additional graphics parameters (see par).

Author(s)

Tim Jupp (using code for vcd:ternaryplot as source).

Examples

tg <- tgrid(6) # make a grid of forecasts in the triangle
tg             # print to terminal
tplot(tg)      # plot them

Rescale a 3-vector to yield a ternary forecast

Description

Utility function to rescale 3-vectors to be probability distributions.

Usage

tscale(p)

Arguments

p

a vector of length 3.

Details

A utility function.

Value

A vector of length 3 rescaled so that elements are all >= 0 and sum to unity.

Author(s)

Tim Jupp

Examples

tscale(cbind(1,2,3))  # rescale (1,2,3) to be a ternary forecast.

Calculate the (Brier) score associated with ternary forecasts and observations

Description

A utility function.

Usage

tscore(p, o, L = diag(c(1, 1, 1))/sqrt(2))

Arguments

p

An n-by-3 matrix of ternary forecasts.

o

An n-by-3 matrix of ternary observations.

L

A 3-by-3 matrix defining the quadratic score being used. (Default corresponds to Brier score.)

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

Examples

data(foot)
tscore(p=foot$p,o=foot$o)

Utility function to calculate geometry associated with a quadratic score matrix

Description

This is just a useful internal function.

Usage

tsetup(L = diag(c(1, 1, 1))/sqrt(2), q = cbind(1, 1, 1)/3)

Arguments

L

A 3-by-3 matrix defining the quadratic score being used. (Default corresponds to Brier score.)

q

A 1-by-3 matrix containing the climatology.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

Examples

tsetup() # returns a set of useful objects

Verification of ternary forecasts / observations

Description

Function to create verification information associated with a set of forecasts p and associated observations o.

Usage

tverify(p, o, q, forceq, ncirc, L)

## Default S3 method:
tverify(p = cbind(1, 1, 1)/3, o = cbind(0, 0, 1), 
        q = cbind(1, 1, 1)/3, forceq = FALSE, 
        ncirc = 11, L = diag(c(1, 1, 1))/sqrt(2))
## S3 method for class 'tverify'
plot(x, thresh = 0, lsharp = TRUE, 
     L = diag(c(1, 1, 1))/sqrt(2),...)

Arguments

p

An n-by-3 matrix of ternary forecasts.

o

An n-by-3 matrix of ternary observations.

q

A 1-by-3 matrix containing the climatology.

forceq

A logical. If forceq = TRUE then the input climatology q is used. If forceq = FALSE a climatology is calculated from o.

ncirc

Controls discretisation of barycentric coordinates. (The number of hexagons along each side of the triangle.)

x

An object of the tverify class (as produced by tverify or tgetcal.

thresh

A ‘threshold’ number of forecasts in a ‘bin’, below which the forecast will not be plotted. (Prevents diagram being cluttered by very rare forecasts).

lsharp

Logical controlling whether a ternary sharpness diagram is to be included.

L

A quadratic scoring matrix. The matrix for the Brier score is the default. Full functionality for other scores not yet implemented.

...

Not used.

Value

An object of class tverify, which is a list containing the following components:

pbin

array of bins in barycentric coordinates.

Nobs

number of forecasts in each bin.

obar

mean onservation associated with each forecast bin.

score

score associated with each forecast bin.

unc

uncertainty associated with each forecast bin.

rel

reliability associated with each forecast bin.

res

resolution associated with each forecast bin.

scorebar

overall score.

uncbar

overall uncertainty.

relbar

overall reliability.

resbar

overall resolution.

ncirc

number of bins along each side of triangle.

p

input forecast data.

o

input observation data.

assigned

index of bin assigned to each forecast.

L

scoring matrix used.

hexc

array of hexagon centres.

q

climatology.

pk

forecasts set to centre of each bin (tverify) or recalibrated (tgetcal)

ok

mean observations corresponding to forecasts pk

pars

parameters in calibration (tgetcal only).

opt

details of calibration (tgetcal only ).

f

calibration function (tgetcal only, for use with tcalibrate).

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

Examples

data(foot)                            # get football betting data
tv   <- tverify(p=foot$p,o=foot$o)    # get verification info
plot(tv)                              # plot verification info

x coordinate of a ternary forecast in barycentric coordinates

Description

This is a utility function to calculate the x-coordinate of a point in barycentric coordinates. It is unlikely that the user will want to use this function directly.

Usage

xf(p = cbind(1, 1, 1)/3, M = tsetup()$M32)

Arguments

p

An n-by-3 matrix of ternary forecasts.

M

A 2-by-3 matrix describing the transfromation for R^3 to R^2.

Value

An n-by-1 array of x-values for use in plotting.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tsetup, yf

Examples

xf(p=cbind(.1,.2,.7))

y coordinate of a ternary forecast in barycentric coordinates

Description

This is a utility function to calculate the y-coordinate of a point in barycentric coordinates. It is unlikely that the user will want to use this function directly.

Usage

yf(p = cbind(1, 1, 1)/3, M = tsetup()$M32)

Arguments

p

An n-by-3 matrix of ternary forecasts.

M

A 2-by-3 matrix describing the transfromation for R^3 to R^2.

Value

An n-by-1 array of y-values for use in plotting.

Author(s)

Tim Jupp

References

Jupp TE, Lowe R, Stephenson DB, Coelho CAS (2012) On the visualization, verification and recalibration of ternary probabilistic forecasts, Philosophical Transactions of the Royal Society, volume 370, pages 1100-1120.

http://rsta.royalsocietypublishing.org/content/370/1962/1100.full

http://arxiv.org/abs/1103.1303

See Also

tsetup, xf

Examples

yf(p=cbind(.1,.2,.7))