'Trends, time series and forecasting Paul Fryers East Midlands KIT.

59
'Trends, time series and forecasting Paul Fryers East Midlands KIT

Transcript of 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Page 1: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

'Trends, time series and forecasting

Paul FryersEast Midlands KIT

Page 2: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Overview

Introduction

Understanding trends and time series – Seasonality– Transformations

Methods for analysing time series – Regression– Moving averages– Autocorrelation

Overview of forecasting

Forecasting methods – Extrapolation of regression – Holt’s method

Uses for forecasting – Setting and monitoring targets – Estimating current values

General methodological points

Page 3: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

What is a time series?

A set of well defined measures collected through time:– Mortality– Diagnoses– Temperature– Rainfall– Share price– Sunspots– Ice cream sales– Air passengers– Road accidents

Page 4: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

What is special about time series data?

There is an implicit order to the data with a first, second, third,..., nth value

Previous observations may be important determinants of later observations– this has implications for analysis

Trend and/or seasonal effects may be present– a trend is a tendency for observations to fall or rise over time– seasonal effects are regular repeating patterns of rises or falls

Different techniques are needed for analysis of historical data and for producing forecasts

Page 5: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Continuous time : electrocardiogram trace

Page 6: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Monthly emphysema deaths

Page 7: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Understanding trends and time series

First plot the data Is the time series consistent?

– Look for step changes in level or trend Is there any visual evidence of any pattern or trend? Is there evidence of a regular ‘seasonal’ pattern? If there is a trend, is it linear? (probably not!)

Page 8: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is the time series consistent? – change in trendIn

dica

tor V

alue

Time

Graph of an indicator over time demonstrating aclear change in trend

Page 9: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is the time series consistent? – step changes

Number of recorded 'violence against the person' crimes in England & Wales1980- 2006/07

0

200,000

400,000

600,000

800,000

1,000,000

1,200,00019

80

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1997

/8

1998

/9

1998

/9

1999

/00

2000

/01

2001

/02

2002

/03

2003

/04

2004

/05

2005

/06

2006

/07

Introduction of the National Crime Recording Standard (NCRS)

Change to the Home Office counting rules for recorded crime

Page 10: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Handling inconsistency

Usually, we will simply break the time series at the point where the trend changes, or the step change occurs

Analyse only the data since that point, or analyse the different parts of the time series separately

Or use a method/software that will do that automatically, eg by weighting more recent points more heavily

We may be able to adjust or transform the data prior to a step change but only if we understand the reason for the change and are confident that the adjustment makes the data consistent

– eg adjusting for a coding change (ICD coding, definition of unemployment, etc.)

But it’s not always clear cut...

Page 11: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is the time series consistent? – step changes?

Mortality rate per 100,000 population-years at risk

0

20

40

60

80

100

120

140

1990 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010

Year

Rate

Page 12: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is the time series consistent? – outlier

Mortality rate per 100,000 population-years at risk

0

20

40

60

80

100

120

140

1990 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010

Year

Rate

Page 13: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Handling outliers

Normally, we ignore outliers, ie exclude them from the analysis– this can be a nuisance for some analyses

But again, it’s not always clear cut:– we need to identify plausible reasons for the outlier/s (eg known issues

with data collection, or a specific factor that has influenced the outcome)

Page 14: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?In

dica

tor V

alue

Time

Graph of an indicator over time demonstrating asteady increasing trend

Page 15: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?In

dica

tor V

alue

Time

Graph of an indicator over time demonstrating anincreasing trend with greater variability

Page 16: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?In

dica

tor V

alue

Time

Graph of an indicator over time demonstrating ansteady decreasing trend

Page 17: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?In

dica

tor V

alue

Time

Graph of an indicator over time demonstratingno change over time

Page 18: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?In

dica

tor V

alue

Time

Graph of an indicator over time demonstrating aseasonal pattern

Page 19: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is there any visual evidence of any pattern or trend?

Mortality rate per 100,000 population-years at risk

0

20

40

60

80

100

120

140

Jun-90 Feb-93 Nov-95 Aug-98 May-01 Feb-04 Nov-06 Jul-09

Year

Rate

Graph of an indicator showing a seasonal pattern plus rising trend

Page 20: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Handling seasonality

Seasonality can be additive or multiplicative– ie each different period in the cycle has an extra factor added to (or

subtracted from) or multiplied by the overall average level We can adjust the data by applying the inverse factor to each period Easier to use an integrated method that adjusts for the seasonality within

the analysis

Page 21: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Is the trend linear?In

dica

tor V

alue

Time

Graph of an indicator over time demonstrating ansteady decreasing trend

Page 22: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Indi

cato

r Val

ue

Time

Graph of an indicator over time demonstrating ansteady decreasing trend

Example of a falling rate – straight line

Page 23: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Indi

cato

r Val

ue

Time

Graph of an indicator over time demonstrating ansteady decreasing trend

Example of a falling rate – exponential curve

Page 24: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – non-linear trends

In many cases, it is meaningless for the forecasts to fall below zero In public health we are most commonly dealing with counts, rates or

proportions We routinely transform the data in order to ‘make the data linear’ and

constrain them to be no less than zero By default, we should use a log-transformation for counts or rates, fitting

an exponential curve which assumes a constant rate of change, rather than a constant numerical increase or decrease

We should use a logit-transformation for proportions (or percentages), which constrains the variable to be between 0 and 1 (or 0% and 100%)

Page 25: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – falling exponential curve

A rapidly falling trend The indicator looks to be heading rapidly towards zero, but the log

transformation ensures that it stays positive: the rate or count is ‘tending towards’ zero but can never quite get there

It represents a constant rate of change (i.e. reducing by x% each year rather than reducing by a set amount each

year) This should be the

default option for analysis of counts or rates

0

50

100

150

200

250

Indi

cato

r Val

ue

Time

Graph of an indicator over time demonstrating aconstant rate of decrease (exponential curve)

Page 26: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – rising exponential curve

A rapidly increasing trend For a count or rate, mathematically it is preferable to use an exponential

curve, but need to beware of other practical constraints: there will usually be some practical limit to a count or rate

If the continued rise in the count or rate is implausible then it is better to use a linear model or logit...

0

5

10

15

20

25

30

Indi

cato

r Val

ue

Time

Graph of an indicator over time demonstrating aconstant rate of increase (exponential curve)

Page 27: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – log-transform counts and rates

Fitting an exponential curve:

Equation of curve: ln(y) = ln(a) + ln(b)t or y = a × bt

where y = value of variable being studieda = intercept on y-axis (nominal value of

indicator at time 0)t = time valueb = ‘gradient’ (amount y is multiplied by

for each increase of 1 in time)

ln(0) = –∞ln(∞) = ∞

Page 28: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – logistic curve

Proportions can not go below zero or above 1 The tails are equivalent: e.g. proportion surviving = 1 – proportion dying Particularly important for proportions that span a large range, from

under 0.5 to nearly 1, e.g. percentage achievement on QOF scores For proportions or

percentages close to zero, the logit is equivalent to the log

For proportions always close to 1 could subtract from 1 and use log

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Indi

cato

r Val

ue

Time

Graph of a proportion over time demonstrating alogistic curve

Page 29: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Transformations – logit-transform proportions

The logit function: logit(y) = ln(y/(1–y)) = ln(y) – ln(1–y)

logit(0) = –∞logit(½) = 0logit(1) = ∞

We transform proportions by applying the logit function, then fit a regression line to the transformed data

For rates or counts which have a practical limit, if we have a sound basis for estimating that realistic maximum then we could do so and treat the rate or count as a proportion of that upper limit

Page 30: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Methods for analysing time series

Regression– Most common method: simply fit a line or curve to the data, treating

‘time’ as any other explanatory variable– Gives equal weight to all points in the time series– Assumes points are independent, identically distributed, observations– Gradient has confidence intervals: if CIs don’t include zero, the gradient is

signicant Two other concepts that are used as the basis for analysing time series:– Moving average– Autocorrelation

Page 31: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Linear regression

Page 32: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Confidence intervals for the gradient

Page 33: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Moving average

Familiar as a method of presenting data– For annual data, rather than presenting data for 2004, 2005, 2006, 2007

and 2008, we may present three-year figures: 2004-06, 2005-07 and 2006-08

– Smoothes out fluctuations in the data, making trends easier to see Also called ‘rolling averages’ Moving averages of different periods can be used to highlight different

features of a time series (example follows) BUT!!!

Moving averages must not be used as the basis for regression, time series analysis or forecasting as they are not independent observations (they share their data with their neighbours)

[Note: time series methods such as Holt’s Method and Box-Jenkins (ARIMA) models use moving averages within the analysis, but the data from which the model is derived should not be moving averages]

Page 34: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Monthly emphysema deaths

Page 35: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

3-point moving average, highlighting seasonality

Page 36: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

13-point moving average, highlighting trend

Page 37: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Autocorrelation

In time series, observations can often be predicted by combinations of previous observations

If the observations are correlated with their immediate predecessors, we can calculate the Pearson correlation coefficient between them

This is called autocorrelation of lag 1 Observations can also be correlated with predecessors from further back

in the time series – autocorrelation of lag k (where k is number of observations back in the series)

– In time series, observations can be predicted by combinations of previous observations

– Smoothes out fluctuations in the data, making trends easier to see

Page 38: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Forecasting

Why do we need to forecast? Extrapolating Forecasting methods

– Examples– Holt’s Method– Interval forecasts– How far back and how far forward?

Using forecasts– to set and monitor progress against targets– to estimate current health outcomes/indicators

Page 39: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Why do we need to forecast?

To inform planning by estimating future needs– the health of the population tends to change slowly and react slowly

to public health interventions so we need to look ahead To anticipate future major events

– e.g. outbreaks To set and monitor progress against targets

– where are we likely to be on current progress?– are we on track to meet targets?

To estimate current health outcomes– our most recent data tend to be a year or more out of date so if we

want to know where we are now or even where we were last year we have to forecast

Page 40: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Forecasting from past trends

If we have time series for a health outcome, health service output indicator or risk factor, we can use this to forecast future values

eg:– mortality rates– teenage pregnancy rates– hospital activity rates– prevalence estimates

Assumes:– consistent definitions and measurement, past and future– either that nothing significant changes, or that changes/

improvements continue at the same rate

Page 41: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Extrapolating from regression lines

A common method is to fit a regression line (or curve) to the historic data and extrapolate it to the future

This is OK for a short time into the future as long as the historic data are stable, ie changing at a steady rate

But:The regression line is fitted across the whole of the historic data, and gives equal weight to all points: e.g. the value for last year is given the same weight as one from 20 years ago – it doesn’t give the best estimate of ‘current trends’

We cannot give realistic confidence intervals for future values (‘prediction intervals’ or ‘forecast intervals’)

Page 42: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Forecasting methods

There is a range of methods which are intended for forecasting, eg moving average methods, autocorrelation methods, Box-Jenkins methods

These methods take into account fluctuations from year to year, trends (ie gradual changes over time) and seasonal variations

They tend to give greater weight to more recent values, hence ‘start from where we are’

They give confidence intervals for forecasts, which tend to get wider as we move further into the future

The most useful methods for public health applications tend to be Holt’s Method (which includes a trend component) and Holt-Winters (which adds a seasonal component)

Note, as with regression analysis, the points in the time series must be independent of each other: rolling averages must never be used for forecasting

Page 43: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Teenage conceptions – England

0

10

20

30

40

50

60

70

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

Und

er 1

8 Co

ncep

tion

Rat

e pe

r 1,0

00 fe

mal

es a

ged

15-1

7

ENGLAND

Observed Rate Target Forecast Rate Forecast Confidence Interval

http://www.empho.org.uk/pages/viewResource.aspx?id=11285

Page 44: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Teenage conceptions – London GOR

0

10

20

30

40

50

60

70

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

Und

er 1

8 Co

ncep

tion

Rat

e pe

r 1,0

00 fe

mal

es a

ged

15-1

7

LONDON GOR

Observed Rate Target Forecast Rate Forecast Confidence Interval

http://www.empho.org.uk/pages/viewResource.aspx?id=11285

Page 45: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Teenage conceptions – Newham

0

10

20

30

40

50

60

70

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

Und

er 1

8 Co

ncep

tion

Rat

e pe

r 1,0

00 fe

mal

es a

ged

15-1

7

Newham LB

Observed Rate Target Forecast Rate Forecast Confidence Interval

http://www.empho.org.uk/pages/viewResource.aspx?id=11285

Page 46: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Teenage conceptions – Harrow

0

10

20

30

40

50

60

70

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

Und

er 1

8 Co

ncep

tion

Rat

e pe

r 1,0

00 fe

mal

es a

ged

15-1

7

Harrow LB

Observed Rate Target Forecast Rate Forecast Confidence Interval

http://www.empho.org.uk/pages/viewResource.aspx?id=11285

Page 47: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Alcohol-related admission rates – Bassetlaw PCT

0

200

400

600

800

1000

1200

1400

1600

180020

03

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

DS

R p

er 1

00,0

00 p

op

ula

tio

n

Year

Bassetlaw Alcohol Admission Rate

Observed Forecast 95% Confidence Interval

Data provided to Nottinghamshire and Bassetlaw PCTs for WCC trajectories

Page 48: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

0

100

200

300

400

500

600

DSR

per 1

00,0

00 p

opul

ation

Admission Rates for Fractured Neck of FemurNottinghamshire PCT

Actual

Fitted

LCI

UCI

Fractured neck of femur admission rates – Nottinghamshire PCT

Data provided to Nottinghamshire and Bassetlaw PCTs for WCC trajectories

Page 49: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Emergency admission rates for acute coronary syndrome – East Midlands – males

Report to East Midlands Cardiac & Stroke Network

Page 50: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Emergency admission rates for acute coronary syndrome – East Midlands – females

Report to East Midlands Cardiac & Stroke Network

Page 51: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Holt’s Method

Holt’s exponential smoothing (aka double exponential smoothing) is a moving average method

There are two equations involved in fitting the model:Lt = xt + (1–)(Lt–1 + Tt–1)

Tt = (Lt–Lt–1) + (1–)Tt–1

where xt is the observed value at time t

Lt is the forecast at time t (the ‘level’ parameter)

Tt is the estimated slope at time t (the ‘trend’ parameter)

is the first smoothing constant, used to smooth the level is the second smoothing constant, used to smooth the trend

The model is fitted iteratively from the start of the time series, usually setting L1 initially to x1 and T1 to x2 – x1

A software package optimises the constants and such that the squared differences between the observed values and the forecasts are minimised

Page 52: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Holt’s Method in practice

Several statistical packages will do this:– ForecastPro – not free but very easy to use– Stata – not free and needs code, but PHE has a corporate licence– R – open source software which requires code– Excel – you can put the equations into Excel but have to optimise the

parameters manually If you use Stata, R or Excel, you need to put some effort into optimising

the parameters, which requires some expertise and time ForecastPro has very clever optimisation routines, which always seem to

result in sensible forecasts and forecast intervals BUT!!!

Every forecast should be graphed and checked – even the most expert of automated ‘expert systems’ cannot and should not be totally relied on

Page 53: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Interval forecasts not point forecasts

When we forecast the future we give a single figure for each forecast that is our best estimate of the future value

0

10

20

30

40

50

60

70

80

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

Rate

per

100

,000

resi

dent

pop

ulati

on

Year

Teenage Conception Rates, 1992-2012East Midlands

Observed values Forecasts Target

Page 54: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Interval forecasts not point forecasts

When we forecast the future we give a single figure for each forecast that is our best estimate of the future value

However, of course there is uncertainty about that prediction Forecast intervals give an indication of the degree of uncertainty, and are

far more valuable than the actual point forecasts

These forecast intervals are calculated by the forecasting software

0

10

20

30

40

50

60

70

80

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

Rate

per

100

,000

resi

dent

pop

ulati

on

Year

Teenage Conception Rates, 1992-2012East Midlands

Observed values Forecasts 95% Forecast Interval Target

Page 55: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

How far back and how far forward?

As discussed earlier, if the graph shows a distinct change in trend or step change then we should ignore the data before the current trend

– If we use Holt’s Method or similar, it is less critical because the method tends to give more weight to recent data and largely ignores earlier points, but if it is clear from the graph, it is still wise to use only the data which exhibit the current trend

– If the change is very recent, then we probably don’t have a sound basis for forecasting – this would be reflected in the forecast intervals (covered later)

How far ahead can we forecast – the ‘forecast horizon’?– A rule of thumb is quoted, that you can forecast around half as far forwards

as you have data going back, however it depends on the stability of the series, and common sense should be applied

– The question is less critical if you present forecast intervals: these will become extremely wide as you get further into the future, demonstrating that the forecasts are meaningless

Page 56: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Using forecasting to estimate ‘current’ rates

By ‘current’ we normally mean ‘the average of the last three years for which data are available’

For deaths, the ‘current’ values, used for analysing our current mortality rates, for example, are based on 2010-2012 data, i.e. data from between 4½ and 1½ years ago: on average 3 years out of date

For small areas, even with 3 years’ data, we still have very few deaths or cases to work with and hence our baseline can be pretty arbitrary

We may be able to use forecasting methodology to help with both of these problems:If we forecast 2014 values based on a time series from 2000 to 2012 then we have a) a more robust baseline, based on 13 years’ data not 3

b) a baseline which reflects ‘now’ rather than 3 years ago Forecasts of ‘current’ periods can give us robust ‘underlying’ values or

rates

Page 57: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

0

20

40

60

80

100

120

140

160

180

200

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011

Rate

per

100

,000

resi

dent

pop

ulati

on

Year

Circulatory Disease Mortality Rates, 1993-2010Doncaster and England

England Rate Doncaster Rate Forecast95% Forecast Interval Upper Limit England Forecast'Current' Values

Example – rapidly changing rates

Circulatory disease death rates are falling dramatically 2004-06 average rate was 91 deaths per 100,000 population-years 2008 forecast was 74 In 2008, by taking the average of 2004-06 as our ‘current’ rate we were

potentially overestimating the impact of interventions by 23%

Page 58: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Summary – key points

Look at a graph of the data, and think about the data you are working with, considering whether there are reasons why past trends may not be a sound basis for future changes

Decide how far back you should start Transform data to ensure that the shape of the graph and any logical

limits on variability (e.g. >0, <100%) are reflected in the mathematical assumptions

Use regression to analyse past changes Use forecasting methods such as Holt’s Method (or Holt-Winters for

seasonal data) to make predictions of future rates with realistic forecast intervals

Ensure that data are independent of one another: no rolling averages Always graph the results, to ensure that the maths hasn’t had an off day

Page 59: 'Trends, time series and forecasting Paul Fryers East Midlands KIT.

Contact

Paul [email protected]