lmestMc {LMest} | R Documentation |
Estimate Markov Chain models
Description
Main function for estimating Markov Chain (MC) models for categorical responses with or without covariates.
Usage
lmestMc(responsesFormula = NULL,
data, index, start = 0,
modBasic = 0, weights = NULL,
tol = 10^-8, maxit = 1000,
out_se = FALSE, output = FALSE, fort = TRUE, seed = NULL)
Arguments
responsesFormula |
a symbolic description of the model to fit. A detailed description is given in the ‘Details’ section |
data |
a |
index |
a character vector with two elements, the first indicating the name of the unit identifier, and the second the time occasions |
start |
type of starting values (0 = deterministic, 1 = random, 2 = initial values in input) |
modBasic |
model on the transition probabilities (0 for time-heter., 1 for time-homog., from 2 to (TT-1) partial homog. of that order) |
weights |
an optional vector of weights for the available responses |
tol |
tolerance level for convergence |
maxit |
maximum number of iterations of the algorithm |
out_se |
to compute the information matrix and standard errors (FALSE is the default option) |
output |
to return additional output ( |
fort |
to use fortran routines when possible (By default is set to TRUE) |
seed |
An integer value with the random number generator state. |
Details
The function lmestMc
estimates the basic MC model and the MC model with covariates for categorical responses. The function requires data in long format and two additional column indicating the unit identifier and the time occasions.
responsesFormula
is used to specify the basic MC models and the model with covariates:
responsesFormula = y1 + y2 ~ NULL
the MC model without covariates and two responses (y1
andy2
) is specified;responsesFormula = NULL
all the columns in the data except the"id"
and"time"
columns are used to estimate MC without covariates;responsesFormula = y1 ~ x1 + x2 | x3 + x4
the MC model with one response (y1
), two covariates affecting the initial probabilities (x1
andx2
) and other two different covariates affecting the transition probabilities (x3
andx4
) is specified;responsesFormula = y1 ~ x1 + x2
the MC model with one response (y1
) and two covariates (x1
andx2
) affecting both the initial and transition probabilities is specified.
Missing responses are not allowed.
Value
Returns an object of class 'MCbasic'
for the basic model without covariates (see MCbasic-class
), or an object of class 'MCcov'
for the model with covariates (see MCcov-class
).
Author(s)
Francesco Bartolucci, Silvia Pandolfi, Fulvia Pennoni, Alessio Farcomeni, Alessio Serafini
References
Bartolucci F., Pandolfi S., Pennoni F. (2017) LMest: An R Package for Latent Markov Models for Longitudinal Categorical Data, Journal of Statistical Software, 81(4), 1-38.
Bartolucci, F., Farcomeni, A. and Pennoni, F. (2013) Latent Markov Models for Longitudinal Data, Chapman and Hall/CRC press.
Examples
## Not run:
# Basic Markov Chain model
data("RLMSlong")
# Categories rescaled from 1 “absolutely unsatisfied” to 5 “absolutely satisfied”
RLMSlong$value <- 5 - RLMSlong$value
out <- lmestMc(responsesFormula = value ~ NULL,
index = c("id","time"),
modBasic = 1,
data = RLMSlong)
out
summary(out)
# Example of drug consumption data
data("data_drug")
long <- data_drug[,-6]
long <- data.frame(id = 1:nrow(long),long)
long <- reshape(long,direction = "long",
idvar = "id",
varying = list(2:ncol(long)))
out1 <- lmestMc(index = c("id","time"), data = long,
weights = data_drug[,6], modBasic = 1, out_se = TRUE)
out1
### MC model with covariates
### Covariates: gender, race, educational level (2 columns), age and age^2
data("data_SRHS_long")
SRHS <- data_SRHS_long[1:2400,]
# Categories of the responses rescaled from 1 “poor” to 5 “excellent”
SRHS$srhs <- 5 - SRHS$srhs
out2 <- lmestMc(responsesFormula = srhs ~
I( 0 + (race==2) + (race == 3)) +
I(0 + (education == 4)) +
I(0 + (education == 5)) +
I(age - 50) +
I((age-50)^2/100),
index = c("id","t"),
data = SRHS)
out2
summary(out2)
# Criminal data
data(data_criminal_sim)
data_criminal_sim = data.frame(data_criminal_sim)
out3 <- lmestMc(responsesFormula = y5~sex,
index = c("id","time"),
data = data_criminal_sim,
output = TRUE)
out3
## End(Not run)