residuals            package:HiddenMarkov            R Documentation

_R_e_s_i_d_u_a_l_s _o_f _H_i_d_d_e_n _M_a_r_k_o_v _M_o_d_e_l_s

_D_e_s_c_r_i_p_t_i_o_n:

     Provides methods for the generic function 'residuals'. There is
     currently no method for objects of class '"mmpp"'.

_U_s_a_g_e:

     ## S3 method for class 'dthmm':
     residuals(object, ...)
     ## S3 method for class 'mmglm':
     residuals(object, ...)

_A_r_g_u_m_e_n_t_s:

  object: an object with class 'dthmm' or 'mmglm'. There is not yet a
          method for 'mmpp'.

     ...: other arguments.

_D_e_t_a_i_l_s:

     For objects of class '"dthmm"' or '"mmglm"' the calculated
     residuals are pseudo residuals. Under satisfactory conditions they
     have an approximate standard normal distribution.

     Initially the function 'probhmm' is called. If the model fits
     satisfactorily, the returned values should be approximately
     uniformly distributed. Hence by applying the function 'qnorm', the
     resultant "residuals" should have an approximate standard normal
     distribution.

     When the distribution (observed) is discrete an adjustment is
     made. However, if relatively few of the possible outcomes are
     observed, the pseudo residuals will be rather poorly described by
     the standard normal distribution; see the Poisson example below.

     The code for the methods '"dthmm"' and '"mmglm"' can be viewed by
     typing 'residuals.dthmm' or 'residuals.mmglm', respectively, on
     the R command line.

_V_a_l_u_e:

     A vector containing the pseudo residuals.

_E_x_a_m_p_l_e_s:

     #   Example Using Beta Distribution

     Pi <- matrix(c(0.8, 0.2,
                    0.3, 0.7),
                  byrow=TRUE, nrow=2)

     n <- 2000

     x <- dthmm(NULL, Pi, c(0,1), "beta",
                list(shape1=c(2, 6), shape2=c(6, 2)))

     x <- simulate(x, nsim=n, seed=5)

     y <- residuals(x) 

     w <- hist(y, main="Beta HMM: Pseudo Residuals")
     z <- seq(-3, 3, 0.01)
     points(z, dnorm(z)*n*(w$breaks[2]-w$breaks[1]), col="red", type="l")
     box()

     qqnorm(y, main="Beta HMM: Q-Q Plot of Pseudo Residuals")
     abline(a=0, b=1, lty=3)
     abline(h=seq(-2, 2, 1), lty=3)
     abline(v=seq(-2, 2, 1), lty=3)

     #-----------------------------------------------
     #   Example Using Gaussian Distribution

     Pi <- matrix(c(1/2, 1/2,   0,   0,   0,
                    1/3, 1/3, 1/3,   0,   0,
                      0, 1/3, 1/3, 1/3,   0,
                      0,   0, 1/3, 1/3, 1/3,
                      0,   0,   0, 1/2, 1/2),
                  byrow=TRUE, nrow=5)

     x <- dthmm(NULL, Pi, c(0, 1, 0, 0, 0), "norm",
                list(mean=c(1, 4, 2, 5, 3), sd=c(0.5, 1, 1, 0.5, 0.1)))

     n <- 2000
     x <- simulate(x, nsim=n, seed=5)

     y <- residuals(x) 

     w <- hist(y, main="Gaussian HMM: Pseudo Residuals")
     z <- seq(-3, 3, 0.01)
     points(z, dnorm(z)*n*(w$breaks[2]-w$breaks[1]), col="red", type="l")
     box()

     qqnorm(y, main="Gaussian HMM: Q-Q Plot of Pseudo Residuals")
     abline(a=0, b=1, lty=3)
     abline(h=seq(-2, 2, 1), lty=3)
     abline(v=seq(-2, 2, 1), lty=3)

     #-----------------------------------------------
     #   Example Using Poisson Distribution  

     Pi <- matrix(c(0.8, 0.2,
                    0.3, 0.7),
                  byrow=TRUE, nrow=2)

     x <- dthmm(NULL, Pi, c(0, 1), "pois",
                list(lambda=c(1, 5)), discrete=TRUE)

     n <- 2000
     x <- simulate(x, nsim=n, seed=5)

     y <- residuals(x) 

     w <- hist(y, main="Poisson HMM: Pseudo Residuals")
     z <- seq(-3, 3, 0.01)
     points(z, dnorm(z)*n*(w$breaks[2]-w$breaks[1]), col="red", type="l")
     box()

     qqnorm(y, main="Poisson HMM: Q-Q Plot of Pseudo Residuals")
     abline(a=0, b=1, lty=3)
     abline(h=seq(-2, 2, 1), lty=3)
     abline(v=seq(-2, 2, 1), lty=3)

