Description Usage Arguments Details Value Under Development References Examples
These functions create Markov modulated generalised linear model objects. These functions are in development and may change, see “Under Development” below.
1 2 3 4 5 6 | mmglm0(x, Pi, delta, family, link, beta, glmformula = formula(y~x1),
sigma = NA, nonstat = TRUE, msg = TRUE)
mmglm1(y, Pi, delta, glmfamily, beta, Xdesign,
sigma = NA, nonstat = TRUE, size = NA, msg = TRUE)
mmglmlong1(y, Pi, delta, glmfamily, beta, Xdesign, longitude,
sigma = NA, nonstat = TRUE, size = NA, msg = TRUE)
|
x |
a dataframe containing the observed variable (i.e. the response variable in the generalised linear model) and the covariate. The function |
y |
numeric vector, response variable. In the case of binomial, it is the number of successes (see argument |
Pi |
is the m*m transition probability matrix of the hidden Markov chain. |
delta |
is the marginal probability distribution of the m hidden states at the first time point. |
family |
character string, the GLM family, one of |
link |
character string, the link function. If |
glmfamily |
a |
Xdesign |
a (nN)*p design matrix, where p is the number of parameters in the linear predictor, N is the number of subjects (N=1 in |
beta |
a p*m matrix containing parameter values, used as initial values during estimation. In the case of the simple regression model of |
glmformula |
the only model formula for |
sigma |
if |
nonstat |
is logical, |
longitude |
a vector the same length as |
size |
is number of Bernoulli trials in each observation when the glm |
msg |
is logical, suppress messages about developmental status. |
This family of models is similar in nature to those of the class dthmm
, in that both classes have the distribution of the observed variable being “modulated” by the changing hidden Markov state. They differ slightly in the mechanism. This family assumes that the mean of the observation distribution can be expressed as a linear model of other known variables, but it is the parameters in the linear predictor that are being modulated by the hidden Markov process, thus causing the changes in the observed means. The linear model is assumed to be a generalised linear model as described by McCullagh & Nelder (1989).
The function mmglm0
is a very simple trivial case where the linear predictor is of the form beta0 + beta1*x1. The version mmglm1
does not have this limitation. The model formula for mmglm1
is defined implicitly through the structure of the specified design matrix. The model mmglmlong1
is similar to mmglm1
but can be applied to longitudinal observations. Models of the form given by mmglm1
are assumed to have one time series, and from a theoretical perspective, one would be interested in the asymptotic properties of the parameter estimates as the series length gets very large. In the longitudinal case (mmglmlong1
), the series of observations per individual is probably very small (<10), and hence interest is in the asymptotic properties as the number of individuals becomes large. Note that in the longitudinal case, the number of observations per individual is assumed to be the same. The responses are assumed to be conditionally independent given the value of the Markov chain and the explanatory variables in the linear predictor.
If family == "binomial"
then the response variable y
is interpreted as the number of successes. The dataframe x
must also contain a variable called size
being the number of Bernoulli trials. This is different to the format used by the function glm
where y
would be a matrix with two columns containing the number of successes and failures, respectively. The different format here allows one to specify the number of Bernoulli trials only so that the number of successes or failures can be simulated later.
When the density function of the response variable is from the exponential family (Charnes et al, 1976, Eq. 2.1), the likelihood function (Charnes et al, 1976, Eq. 2.4) can be maximised by using iterative weighted least squares (Charnes et al, 1976, Eq. 1.1 and 1.2). This is the method used by the R function glm
. In this Markov modulated version of the model, the third term of the complete data log-likelihood, as given in Harte (2006, Sec. 2.3), needs to be maximised. This is simply the sum of the individual log-likelihood contributions of the response variable weighted by the Markov state probabilities calculated in the E-step. This can also be maximised using iterative least squares by passing these additional weights (Markov state probabilities) into the glm
function.
A list
object with class "mmglm0"
, containing the above arguments as named components.
These functions are still being developed. In previous releases of the package (< 1.3), there was only one function called mmglm
. This has been renamed to mmglm0
. The most recent version is mmglm1
along with mmglmlong1
which has flexibility to include longitudinal data. Further development versions will be numbered sequentially. The name mmglm
has been reserved for the final stable version, at which point the numbered versions will become deprecated.
Cited references are listed on the HiddenMarkov manual page.
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | #--------------------------------------------------------
# Gaussian with identity link function
# using mmglm0
delta <- c(0,1)
Pi <- matrix(c(0.8, 0.2,
0.3, 0.7),
byrow=TRUE, nrow=2)
beta <- matrix(c(0.1, -0.1,
1.0, 5.0),
byrow=TRUE, nrow=2)
x <- mmglm0(NULL, Pi, delta, family="gaussian", link="identity",
beta=beta, sigma=c(1, 2))
n <- 1000
x <- simulate(x, nsim=n, seed=10)
# Increase maxiter below to achieve convergence
# Has been restricted to minimise time of package checks
y <- BaumWelch(x, bwcontrol(maxiter=2))
w <- hist(residuals(y))
z <- seq(-3, 3, 0.01)
points(z, dnorm(z)*n*(w$breaks[2]-w$breaks[1]), col="red", type="l")
box()
print(summary(y))
print(logLik(y))
#--------------------------------------------------------
# Gaussian with log link function
# using mmglm1
n <- 1000
# the range of x needs changing according to the glmfamily
x <- seq(-0.9, 1.5, length.out=n)
colour <- c("blue", "green", "red")
colnum <- rep(1:3, n/3+1)[1:n] - 1
data <- data.frame(x=x, colour=colour[colnum+1])
# will simulate response variable, not required in formula
# design matrix only depends on RHS of formula
glmformula <- formula( ~ x + I(x^2) + colour)
glmfamily <- gaussian(link="log")
Xdesign <- model.matrix(glmformula, data=data)
# --- Parameter Values and Simulation ---
Pi <- matrix(c(0.8, 0.2,
0.3, 0.7),
byrow=TRUE, nrow=2)
delta <- c(1, 0)
sd <- c(1.2, 1)
beta <- matrix(c(-1, -1.2,
-2, -1.8,
3, 2.8,
1, 0.8,
2, 2.2),
ncol=ncol(Pi), nrow=ncol(Xdesign), byrow=TRUE)
y <- mmglm1(NULL, Pi, delta, glmfamily, beta, Xdesign, sigma=sd)
y <- simulate(y, seed=5)
# --- Estimation ---
# Increase maxiter below to achieve convergence
# Has been restricted to minimise time of package checks
tmp <- BaumWelch(y, bwcontrol(posdiff=FALSE, maxiter=2))
print(summary(tmp))
#-------------------------------------------------
# Binomial with logit link function
# using mmglm1
# n = series length
n <- 1000
# the range of x need changing according to the glmfamily
x <- seq(-1, 1.5, length.out=n)
colour <- c("blue", "green", "red")
colnum <- rep(1:3, n/3+1)[1:n] - 1
data <- data.frame(x=x, colour=colour[colnum+1])
glmformula <- formula( ~ x + I(x^2) + colour)
glmfamily <- binomial(link="logit")
Xdesign <- model.matrix(glmformula, data=data)
# --- Parameter Values and Simulation ---
Pi <- matrix(c(0.8, 0.2,
0.3, 0.7),
byrow=TRUE, nrow=2)
delta <- c(1, 0)
beta <- matrix(c(-1, -1.2,
-2, -1.8,
3, 2.8,
1, 0.8,
2, 2.2),
ncol=ncol(Pi), nrow=ncol(Xdesign), byrow=TRUE)
y <- mmglm1(NULL, Pi, delta, glmfamily, beta, Xdesign, sigma=sd,
size=rep(100, n))
# each element of y$y is the number of successes in 100 Bernoulli trials
y <- simulate(y, seed=5)
# --- Estimation ---
# Increase maxiter below to achieve convergence
# Has been restricted to minimise time of package checks
tmp <- BaumWelch(y, bwcontrol(posdiff=FALSE, maxiter=2))
print(summary(tmp))
#-------------------------------------------------
# Gaussian with log link function, longitudinal data
# using mmglmlong1
# n = series length for each subject
# N = number of subjects
n <- 5
N <- 1000
# the range of x need changing according to the glmfamily
x <- seq(-0.9, 1.5, length.out=n)
colour <- c("blue", "green", "red")
colnum <- rep(1:3, n/3+1)[1:n] - 1
data <- data.frame(x=x, colour=colour[colnum+1])
# will simulate response variable, not required in formula
# design matrix only depends on RHS of formula
glmformula <- formula( ~ x + I(x^2) + colour)
glmfamily <- gaussian(link="log")
Xdesign0 <- model.matrix(glmformula, data=data)
# multiple subjects
Xdesign <- NULL
for (i in 1:N) Xdesign <- rbind(Xdesign, Xdesign0)
# --- Parameter Values and Simulation ---
Pi <- matrix(c(0.8, 0.2,
0.3, 0.7),
byrow=TRUE, nrow=2)
delta <- c(0.5, 0.5)
sd <- c(1.2, 1)
beta <- matrix(c(-1, -1.2,
-2, -1.8,
3, 2.8,
1, 0.8,
2, 2.2),
ncol=ncol(Pi), nrow=ncol(Xdesign), byrow=TRUE)
y <- mmglmlong1(NULL, Pi, delta, glmfamily, beta, Xdesign, sigma=sd,
longitude=rep(1:N, each=n))
y <- simulate(y, seed=5)
# --- Estimation ---
# Note: the "Not run" blocks below are not run during package checks
# as the makePSOCKcluster definition is specific to my network,
# modify accordingly if you want parallel processing.
cl <- NULL
## Not run:
if (require(parallel)){
cl <- makePSOCKcluster(c("localhost", "horoeka.localdomain",
"horoeka.localdomain", "localhost"))
}
## End(Not run)
# Increase maxiter below to achieve convergence
# Has been restricted to minimise time of package checks
tmp <- BaumWelch(y, bwcontrol(posdiff=FALSE, maxiter=2),
PSOCKcluster=cl)
## Not run:
if (!is.null(cl)){
stopCluster(cl)
rm(cl)
}
## End(Not run)
print(summary(tmp))
#-------------------------------------------------
# Binomial with logit link function, longitudinal data
# using mmglmlong1
# n = series length for each subject
# N = number of subjects
n <- 10
N <- 100
# the range of x need changing according to the glmfamily
x <- seq(-1, 1.5, length.out=n)
colour <- c("blue", "green", "red")
colnum <- rep(1:3, n/3+1)[1:n] - 1
data <- data.frame(x=x, colour=colour[colnum+1])
glmformula <- formula( ~ x + I(x^2) + colour)
glmfamily <- binomial(link="logit")
Xdesign0 <- model.matrix(glmformula, data=data)
# multiple subjects
Xdesign <- NULL
for (i in 1:N) Xdesign <- rbind(Xdesign, Xdesign0)
# --- Parameter Values and Simulation ---
Pi <- matrix(c(0.8, 0.2,
0.3, 0.7),
byrow=TRUE, nrow=2)
delta <- c(0.5, 0.5)
beta <- matrix(c(-1, -1.2,
-2, -1.8,
3, 2.8,
1, 0.8,
2, 2.2),
ncol=ncol(Pi), nrow=ncol(Xdesign), byrow=TRUE)
y <- mmglmlong1(NULL, Pi, delta, glmfamily, beta, Xdesign, sigma=sd,
longitude=rep(1:N, each=n), size=rep(200, N*n))
y <- simulate(y, seed=5)
# --- Estimation ---
# Increase maxiter below to achieve convergence
# Has been restricted to minimise time of package checks
tmp <- BaumWelch(y, bwcontrol(posdiff=FALSE, maxiter=1))
print(summary(tmp))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.