c5734_a001

Embed Size (px)

Citation preview

  • 8/3/2019 c5734_a001

    1/14

    APPENDIX A

    Examples of R code

    In this Appendix we give examples of the R code used to perform theanalyses presented in the book. We do not provide complete coverage ofwhat we have done; only the case of a PoissonHMM is covered fully,but we also illustrate how that code can be modified to cover two other

    models. Users are encouraged to experiment with the code and to writetheir own functions for additional models, starting with simple models,such as the binomialHMM, and then progressing to more advancedmodels, e.g. multivariate models or HMMs with covariates. There is,however, other R software available which can implement many of ourmodels, e.g. the packages repeated (Lindsey, 2008), msm (Jackson et al.,2003), and HiddenMarkov (Harte, 2008).

    We are aware that the code presented in this appendix can be im-proved. We have sometimes sacrificed efficiency or elegance in the inter-

    est of transparency. Our aim has been to give code that is easy to modifyin order to deal with alternative models.

    The time series is assumed to be stored as a vector x of length n,rather than the T which was used in the body of the text. This is toavoid overwriting the standard R abbreviation T for TRUE.

    A.1 Fit a stationary PoissonHMM by direct numerical

    maximization

    Estimation by direct numerical maximization of the likelihood functionis illustrated here by code for a stationary PoissonHMM. The compu-tation uses four functions, which appear in A.1.1 A.1.4:

    pois.HMM.pn2pw transforms the natural parameters (the vector ofPoisson means and the t.p.m. ) of an m-state PoissonHMM intoa vector of (unconstrained) working parameters;

    pois.HMM.pw2pn performs the inverse transformation;

    pois.HMM.mllk computes minus the log-likelihood of a PoissonHMMfor a given set of working parameters; and

    pois.HMM.mle estimates the parameters of the model using numericalminimization of minus the log-likelihood.

    239

    2009 by Walter Zucchini and Iain MacDonald

  • 8/3/2019 c5734_a001

    2/14

    240 EXAMPLES OF R CODE

    A.1.1 Transform natural parameters to working

    1 p o is . H M M . p n 2 p w < - f u n c t i o n ( m , l a m bd a , g a m m a )2 {3 t l am b da < - l og ( l a m bd a )

    4 t gamma 1 )6 {7 fo o

  • 8/3/2019 c5734_a001

    3/14

    STATIONARY POISSONHMM, NUMERICAL MAXIMIZATION 241

    10 for ( i in 1: n)11 {12 f oo < - fo o %*% p n $g am ma * a ll pr ob s [i ,]13 s u mf o o < - s um ( f o o )14 l s ca l e < - l s ca l e + l og ( s u m fo o )15 foo

  • 8/3/2019 c5734_a001

    4/14

    242 EXAMPLES OF R CODE

    A.2 More on PoissonHMMs, including estimation by EM

    A.2.1 Generate a realization of a PoissonHMM

    1 p o is . H M M . g e n e r a t e_ s a m p l e < -2 f u n c t i o n ( n , m , l a m b d a , g a m m a , d e l t a = N U L L )

    3 {4 i f ( i s . n u l l ( d e l t a ) ) d e l t a < - s o l v e ( t ( d i a g ( m ) - g a m m a + 1 ) , r e p ( 1 , m ) )5 m ve ct < - 1 :m6 s t at e < - n u me r ic ( n )7 s t a te [ 1 ] < - s a m pl e ( m v e ct , 1 , p r ob = d e l ta )8 for ( i in 2: n)9 s t a t e [ i ] < - s a m p l e ( m v e c t , 1 , p r o b = g a m m a [ s t a t e [ i - 1 ] , ] )10 x < - r p oi s ( n , l a mb d a = l am b da [ s t a te ] )11 x12 }

    This function generates a realization of length n of an m-state HMM withparameters lambda and gamma. If delta is not supplied, the stationarydistribution is computed (line 4) and used as initial distribution. Ifdeltais supplied, it is used as initial distribution.

    A.2.2 Forward and backward probabilities

    1 p o i s . H M M . l a l p h a b e t a < - f u n c t i o n ( x , m , l a m b d a , g a m m a , d e l t a = N U L L )2 {3 i f ( i s . n u l l ( d e l t a ) ) d e l t a < - s o l v e ( t ( d i a g ( m ) - g a m m a + 1 ) , r e p ( 1 , m ) )

    4 n

  • 8/3/2019 c5734_a001

    5/14

    MORE ON POISSONHMMs, INCLUDING EM 243

    This function computes the logarithms of the forward and backwardprobabilities as defined by Equations (4.1) and (4.2) on p. 60, in theform of m n matrices. The initial distribution delta is handled as inA.2.1. To reduce the risk of underflow, scaling is applied, in the same

    way as in A.1.3.

    A.2.3 EM estimation of a PoissonHMM

    1 p o is . H M M . E M < - f u n c ti o n ( x , m , l a m bd a , g a mm a , d e lt a ,2 m a x i t e r = 1 0 0 0 , t o l = 1 e - 6 , . . . )3 {4 lamb da . ne xt

  • 8/3/2019 c5734_a001

    6/14

    244 EXAMPLES OF R CODE

    This function implements the EM algorithm as described in Sections4.2.2 and 4.2.3, with the initial values lambda, gamma and delta for thenatural parameters , and . In each iteration the logs of the forwardand backward probabilities are computed (lines 1012); the forward and

    backward probabilities themselves would tend to underflow. The log-likelihood l, which is needed in both E and M steps, is computed asfollows:

    l = log

    mi=1

    n(i)

    = c + log

    mi=1

    exp

    log(n(i)) c

    ,

    where c is chosen in such a way as to reduce the chances of underflowin the exponentiation. The convergence criterion crit used above is the

    sum of absolute values of the changes in the parameters in one iteration,and could be replaced by some other criterion chosen by the user.

    A.2.4 Viterbi algorithm

    1 p o i s . H M M . v i t e rb i < - f u n c t i o n ( x , m , l a m b d a , g a m m a , d e l t a = N U LL , . . . )2 {3 i f ( i s . n u l l ( d e l t a ) ) d e l t a < - s o l v e ( t ( d i a g ( m ) - g a m m a + 1 ) , r e p ( 1 , m ) )4 n

  • 8/3/2019 c5734_a001

    7/14

    MORE ON POISSONHMMs, INCLUDING EM 245

    6 fb

  • 8/3/2019 c5734_a001

    8/14

    246 EXAMPLES OF R CODE

    This function computes the probability Pr(Ct = i | X(n)) for t =

    n + 1, . . . ,n + H and i = 1, . . . ,m, by means of Equation (5.12) on p. 86.As in A.2.3, the logs of the forward probabilities are shifted before ex-ponentiation by a quantity c chosen to reduce the chances of underflow;

    see line 10.

    A.2.8 Forecast distributions

    1 p o is . H M M . f o r e ca s t < - f u n c ti o n ( x , m , l a m bd a , g a mm a ,2 d e l t a = N U L L , x r a n g e = N U L L , H = 1 , . . . )3 {4 i f ( i s . n u l l ( d e l t a ) )5 d e l t a < - s o l v e ( t ( d i a g ( m ) - g a m m a + 1 ) , r e p ( 1 , m ) )6 i f ( i s . n u l l ( x r a n g e ) )7 x r a n g e < - q p o i s ( 0 . 0 0 1 , m i n ( l a m b d a ) ) :8 q p o i s ( 0 . 9 9 9 , m a x ( l a m b d a ) )9 n

  • 8/3/2019 c5734_a001

    9/14

    MORE ON POISSONHMMs, INCLUDING EM 247

    4 i f ( i s . n u l l ( d e l t a ) )5 d el ta < - so lv e ( t ( d ia g ( m ) - ga mm a + 1 ) , r ep ( 1 , m ) )6 i f ( i s . n u l l ( x r a n g e ) )7 x r a ng e < - q p o is ( 0 .0 0 1 , m i n ( l a m bd a ) ) :8 q p o i s ( 0 . 9 9 9 , m a x ( l a m b d a ) )9 n

  • 8/3/2019 c5734_a001

    10/14

    248 EXAMPLES OF R CODE

    This function computes, for each t from 1 to n, the ordinary normalpseudo-residuals as described in Section 6.2.2. These are returned in ann 3 matrix in which the columns give (in order) the lower, mid- andupper pseudo-residuals.

    A.3 HMM with bivariate normal state-dependent

    distributions

    This section presents the code for fitting an m-state HMM with bivariatenormal state-dependent distributions, by means of the discrete likeli-hood. It is assumed that the observations are available as intervals: x1and x2 are n2 matrices of lower and upper bounds for the observations.

    (The values -inf and inf are permitted. Note that missing values arethereby allowed for.) The natural parameters are the means mu (an m 2matrix), the standard deviations sigma (an m 2 matrix), the vectorcorr ofm correlations, and the t.p.m. gamma.

    There are in all m2 + 4m parameters to be estimated, and each eval-uation of the likelihood requires mn bivariate-normal probabilities of arectangle: see line 19 in A.3.3. This code must therefore be expected tobe slow.

    An example of the use of this code appears in Section 10.4.

    A.3.1 Transform natural parameters to working

    1 b i v no r m . H M M . p n 2 p w < -2 f u n c t i o n ( m u , s i g m a , c o r r , g a m m a , m )3 {4 t s ig m a < - l og ( s i g ma )5 t c o r r < - l o g ( ( 1 + c o r r ) / ( 1 - c o r r ) )

    6 t ga mm a < - N UL L7 i f ( m > 1 )8 {9 foo

  • 8/3/2019 c5734_a001

    11/14

    BIVARIATE NORMAL STATE-DEPENDENT DISTRIBUTIONS 249

    A.3.2 Transform working parameters to natural

    1 b i v no r m . H M M . p w 2 p n < - f u n ct i o n ( p a r ve c t , m )2 {3 mu < - m at ri x ( pa rv ec t [ 1: (2 * m) ], m ,2)4

    s i g ma < - m a t ri x ( e x p ( p a r v e c t [ ( 2 * m + 1 ) : ( 4 * m ) ] ) , m , 2)5 t e m p < - e x p ( p a r v e c t [ ( 4 * m + 1 ) : ( 5 * m ) ] )6 c o rr < - ( t e m p - 1 ) / ( t e m p + 1 )7 g am ma < - d ia g ( m)8 i f ( m > 1 )9 {

    10 g a m m a [ ! g a m m a ] < - e x p ( p a r v e c t [ ( 5 * m + 1 ) : ( m * ( m + 4 ) ) ] )11 g a mm a < - g a m m a / a p p l y ( g a m ma , 1 , s u m )12 }13 d e l t a < - s o l v e ( t ( d i a g ( m ) - g a m m a + 1 ) , r e p ( 1 , m ) )14 l i s t ( m u = m u , s i g m a = s i g m a , c o r r = c o r r , g a m m a = g a m m a ,15 d e l t a = d e l t a )16 }

    A.3.3 Discrete log-likelihood

    1 b i v n o r m . H M M . m l l k < - f u n c t i o n ( p a r v e c t , x 1 , x 2 , m , . . . )2 {3 n

  • 8/3/2019 c5734_a001

    12/14

    250 EXAMPLES OF R CODE

    A.3.4 MLEs of the parameters

    1 b i v n o r m . H M M . m l e < -2 f u n c t i o n ( x 1 , x 2 , m , m u 0 , s i g m a 0 , c o r r 0 , g a m m a 0 , . . . )3 {

    4 n

  • 8/3/2019 c5734_a001

    13/14

    CATEGORICAL HMM, CONSTRAINED OPTIMIZATION 251

    that constrOptim can be very slow compared to transformation and un-constrained maximization by nlm. The R help for constrOptim statesthat it is likely to be slow if the optimum is on a boundary, and indeedthe optimum is on a boundary in the wind-direction application and

    many of our other models. The resulting parameter estimates differ verylittle from those supplied by nlm.Note that, if q = 2, the code presented here can be used to fit models

    to binary time series, although more efficient functions can be writtenfor that specific case.

    A.4.1 Log-likelihood

    1c at . H M M . n l l k < - f u n c ti o n ( p a r v ec t , x , m , q , . . .)

    2 {3 n

  • 8/3/2019 c5734_a001

    14/14

    252 EXAMPLES OF R CODE

    A.4.2 MLEs of the parameters

    1 c a t . H M M . m le < - f u n c ti o n ( x , m , g a m ma 0 , p r0 , . . .)2 {3 q