fasthsmmfit: Fast gradient descent / stochastic gradient descent algorithm...

Description Usage Arguments Value References Examples

View source: R/fasthsmmfit.R

Description

Fast gradient descent / stochastic gradient descent algorithm to learn the parameters in a specialized zero-inflated hidden semi-Markov model, where zero-inflation only happens in State 1. And if there were covariates, they could only be the same ones for the state-dependent log Poisson means and the logit structural zero proportion. In addition, the dwell time distributions are nonparametric for all hidden states.

Usage

1
2
3
4
fasthsmmfit(y, x = NULL, ntimes = NULL, M, trunc, dt_init, prior_init,
  tpm_init, emit_init, zero_init, yceil = NULL, stochastic = FALSE,
  nmin = 1000, nupdate = 100, power = 0.7, rate = c(1, 0.05),
  method = "Nelder-Mead", hessian = FALSE, ...)

Arguments

y

observed time series values

x

matrix of covariates for the log poisson means and logit zero proportion. Default to NULL.

ntimes

a vector specifying the lengths of individual, i.e. independent, time series. If not specified, the responses are assumed to form a single time series, i.e. ntimes=length(y).

M

number of latent states

trunc

a vector specifying truncation at the maximum number of dwelling time in each state.

dt_init

a matrix whose i,j th element is the probability of staying in state i for duration j, which is the nonparametric state duration distributions.

prior_init

a vector of initial values for prior probability for each state

tpm_init

a matrix of initial values for transition probability matrix

emit_init

a vector of initial values for the means for each poisson distribution

zero_init

a scalar initial value for the structural zero proportion

yceil

a scalar defining the ceiling of y, above which the values will be truncated. Default to NULL.

stochastic

Logical. Should the stochastic gradient descent methods be used.

nmin

a scalar for the minimum number of observations before the first iteration of stochastic gradient descent. Default to 1000.

nupdate

a scalar specifying the total number of updates for stochastic gradient descent. Default to 100.

power

a scalar representing the power of the learning rate, which should lie between (0.5,1]. Default to 0.7

rate

a vector of learning rate in stochastic gradient descent for the logit parameters and log parameters. Default to c(1,0.05).

method

method to be used for direct numeric optimization. See details in the help page for optim() function. Default to Nelder-Mead.

hessian

Logical. Should a numerically differentiated Hessian matrix be returned? Note that the hessian is for the working parameters, which are the generalized logit of prior probabilities (except for state 1), the generalized logit of the transition probability matrix(except 1st column), the logit of non-zero zero proportions, and the log of each state-dependent poisson means

...

Further arguments passed on to the optimization methods

Value

the maximum likelihood estimates of the zero-inflated hidden Markov model

References

Walter Zucchini, Iain L. MacDonald, Roland Langrock. Hidden Markov Models for Time Series: An Introduction Using R, Second Edition. Chapman & Hall/CRC

Examples

  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
#1. no covariates
set.seed(123)
prior_init <- c(0.5,0.2,0.3)
dt_init <- matrix(c(0.4,0.3,0.2,0.1,0.5,0.2,0.2,0.1,
                   0.25,0.25,0.25,0.25),3,4,byrow=TRUE)
emit_init <- c(10,50,100)
zeroprop <- c(0.6,0,0)
omega <- matrix(c(0,0.3,0.7,0.4,0,0.6,0.5,0.5,0),3,3,byrow=TRUE)
sim1 <- hsmmsim(n=1000,M=3,prior=prior_init,dt_dist="nonparametric",
               dt_parm=dt_init, tpm_parm=omega,
               emit_parm=emit_init,zeroprop=zeroprop)
str(sim1)
y <- sim1$series


fitnost <- fasthsmmfit(y,NULL,NULL,3,trunc=c(4,4,4),
           dt_init=matrix(c(0.3,0.3,0.2,0.2,
           0.4,0.2,0.2,0.2,0.25,0.25,0.25,0.25),3,4,byrow=TRUE),
           prior_init=c(0.3,0.3,0.4),
           tpm_init=matrix(c(0,0.7,0.3,0.5,0,0.5,0.3,0.7,0),3,3,byrow=TRUE), 
           emit_init=c(8,40,80),zero_init=0.4,method="BFGS",control=list(trace=1))
           
decode1 <- hsmmviterbi(y, ntimes=NULL, 3, trunc=c(4,4,4), fitnost$prior, 
                      dt_dist="nonparametric", 
                      fitnost$dt, fitnost$tpm, 
                      fitnost$emit, c(fitnost$zeroprop,0,0))

#2. with covariates
dtparm <-  matrix(c(0.4,0.3,0.2,0.1,0.7,0.2,0.1,0,0.25,0.25,0.25,0.25),3,4,byrow=TRUE)
priorparm <- c(0,0)
zeroindex <- c(1,0,0)
zeroparm <- c(0,-1,1)
emitparm <- c(2,0.5,-0.5,3,0.3,-0.2,4,0.4,-0.4)
tpmparm <- c(-1,0,1)
workparm <- c(priorparm,zeroparm,emitparm,tpmparm) 
trunc <- c(4,3,4)

designx <- matrix(rnorm(4000),nrow=2000,ncol=2)
result <- hsmmsim2(workparm,3,2000,zeroindex,"nonparametric",
                  emit_x=designx,zeroinfl_x=designx,dt_x=dtparm)
y <- result$series

fitnost <- fasthsmmfit(y,designx,NULL,3,trunc=c(4,3,4),
           dt_init=matrix(c(0.3,0.3,0.2,0.2,0.4,0.2,0.2,0.2,
           0.25,0.25,0.25,0.25),3,4,byrow=TRUE),
           prior_init=c(0.3,0.3,0.4),
           tpm_init=matrix(c(0,0.8,0.2,0.4,0,0.6,0.2,0.8,0),3,3,byrow=TRUE), 
           emit_init=c(8,40,80),zero_init=0.4,method="BFGS",control=list(trace=1))

decode2 <- hsmmviterbi2(y,NULL,3,trunc=c(4,3,4),
                       fitnost$working_parm[-(1:8)],dt_x=fitnost$dt,
                       dt_dist="nonparametric", zero_init=c(1,0,0),
                       emit_x=designx,zeroinfl_x=designx)

#3. stochastic gradient descent without covariates
prior_init <- c(0.5,0.2,0.3)
dt_init <- matrix(c(0.4,0.3,0.2,0.1,0.5,0.2,0.2,0.1,
                   0.25,0.25,0.25,0.25),3,4,byrow=TRUE)
emit_init <- c(10,50,100)
zeroprop <- c(0.6,0,0)
omega <- matrix(c(0,0.3,0.7,0.4,0,0.6,0.5,0.5,0),3,3,byrow=TRUE)
sim1 <- hsmmsim(n=50000,M=3,prior=prior_init,dt_dist="nonparametric",
               dt_parm=dt_init, tpm_parm=omega,
               emit_parm=emit_init,zeroprop=zeroprop)
y <- sim1$series

fitst <- fasthsmmfit(y,NULL,NULL,3,trunc=c(4,4,4),
         dt_init=matrix(c(0.4,0.3,0.2,0.1,0.4,0.2,0.2,0.2,
         0.25,0.25,0.25,0.25),3,4,byrow=TRUE),
         prior_init=c(0.3,0.3,0.4),
         tpm_init=matrix(c(0,0.8,0.2,0.5,0,0.5,0.2,0.8,0),3,3,byrow=TRUE), 
         emit_init=c(15,40,90),zero_init=0.4,stochastic=TRUE,
         nmin=500,nupdate=500,power=0.6,rate=c(0.5,0.08))
str(fitst)

#4. stochastic descent without covariates
dtparm <-  matrix(c(0.4,0.3,0.2,0.1,0.7,0.2,0.1,0,
           0.25,0.25,0.25,0.25),3,4,byrow=TRUE)
priorparm <- c(0,0)
zeroindex <- c(1,0,0)
zeroparm <- c(0,-0.5,0.5)
emitparm <- c(2,0.1,-0.1,3,0.3,-0.2,4,0.4,-0.4)
tpmparm <- c(-1,0,1)
workparm <- c(priorparm,zeroparm,emitparm,tpmparm) 
trunc <- c(4,3,4)

designx <- matrix(rnorm(100000),nrow=50000,ncol=2)
result <- hsmmsim2(workparm,3,50000,zeroindex,"nonparametric",
                  emit_x=designx,zeroinfl_x=designx,dt_x=dtparm)

y <- result$series

fitst <- fasthsmmfit(y,designx,NULL,3,trunc=c(4,3,4),
         dt_init=matrix(c(0.4,0.3,0.2,0.1,0.6,0.3,0.1,0,
         0.25,0.25,0.25,0.25),3,4,byrow=TRUE),
         prior_init=c(0.3,0.3,0.4),
         tpm_init=matrix(c(0,0.8,0.2,0.5,0,0.5,0.2,0.8,0),3,3,byrow=TRUE), 
         emit_init=c(15,40,90),zero_init=0.6,stochastic=TRUE,
         nmin=500,nupdate=500,power=0.6,rate=c(0.3,0.05))
str(fitst)

ziphsmm documentation built on May 2, 2019, 6:10 a.m.

Related to fasthsmmfit in ziphsmm...