The nhmgrid
R package provides an easy-to-use interface for estimation
and visualization of non-homogeneous Markov model transition
probabilities. The main features in the package are:
- Supports a variety of model types in transition probability estimation using marginaleffects.
- Is able to calculate state transition proportions without a model.
- Produces highly customizable plots using ggplot2.
You can install the most recent version of nhmgrid
by running the
following lines:
# install.packages("devtools")
devtools::install_github("mirojantti/nhmgrid")
The package contains a simulated panel data set health
. The data set
consists of 100 individuals whose health state has been measured every
year for 10 years.
library(nhmgrid)
head(health, 12)
#> id sex age state lagstate
#> 1 1 female 15 healthy <NA>
#> 2 1 female 16 sick healthy
#> 3 1 female 17 sick sick
#> 4 1 female 18 healthy sick
#> 5 1 female 19 healthy healthy
#> 6 1 female 20 healthy healthy
#> 7 1 female 21 sick healthy
#> 8 1 female 22 sick sick
#> 9 1 female 23 healthy sick
#> 10 1 female 24 sick healthy
#> 11 2 female 20 healthy <NA>
#> 12 2 female 21 healthy healthy
nrow(health)
#> [1] 1000
One can calculate the transition proportions between the health states
using stprops
and plot them. Because the plot is constructed using
ggplot2, it is highly customizable.
props <- stprops(health, x = "age", state = "state", id = "id")
plot(props) +
ggplot2::geom_smooth(se = FALSE)
The transition probabilities between the states can be estimated with a
Markov model using stprobs
. In this example we fit a multinomial
logistic regression model and estimate the probabilities separately for
men and women.
fit <- nnet::multinom(state ~ lagstate + age + sex, data = health)
probs <- stprobs(fit, x = "age", group = "sex")
plot(probs)
For more examples, see the package documentation.