A five-state hidden Markov model (HMM) fitted for the biofam
data.
A hidden Markov model of class hmm
;
a left-to-right model with four hidden states.
The model is loaded by calling data(hmm_biofam)
. It was created with the
following code:
data("biofam3c")# Building sequence objects marr_seq <- seqdef(biofam3c$married, start = 15, alphabet = c("single", "married", "divorced")) child_seq <- seqdef(biofam3c$children, start = 15, alphabet = c("childless", "children")) left_seq <- seqdef(biofam3c$left, start = 15, alphabet = c("with parents", "left home"))
## Choosing colors attr(marr_seq, "cpal") <- c("violetred2", "darkgoldenrod2", "darkmagenta") attr(child_seq, "cpal") <- c("darkseagreen1", "coral3") attr(left_seq, "cpal") <- c("lightblue", "red3")
init <- c(0.9, 0.05, 0.02, 0.02, 0.01)
# Starting values for transition matrix trans <- matrix( c(0.8, 0.10, 0.05, 0.03, 0.02, 0, 0.9, 0.05, 0.03, 0.02, 0, 0, 0.9, 0.07, 0.03, 0, 0, 0, 0.9, 0.1, 0, 0, 0, 0, 1), nrow = 5, ncol = 5, byrow = TRUE)
# Starting values for emission matrices emiss_marr <- matrix( c(0.9, 0.05, 0.05, # High probability for single 0.9, 0.05, 0.05, 0.05, 0.9, 0.05, # High probability for married 0.05, 0.9, 0.05, 0.3, 0.3, 0.4), # mixed group nrow = 5, ncol = 3, byrow = TRUE)
emiss_child <- matrix( c(0.9, 0.1, # High probability for childless 0.9, 0.1, 0.1, 0.9, 0.1, 0.9, 0.5, 0.5), nrow = 5, ncol = 2, byrow = TRUE)
emiss_left <- matrix( c(0.9, 0.1, # High probability for living with parents 0.1, 0.9, 0.1, 0.9, 0.1, 0.9, 0.5, 0.5), nrow = 5, ncol = 2, byrow = TRUE)
initmod <- build_hmm( observations = list(marr_seq, child_seq, left_seq), initial_probs = init, transition_probs = trans, emission_probs = list(emiss_marr, emiss_child, emiss_left), channel_names = c("Marriage", "Parenthood", "Residence"))
fit_biofam <- fit_model(initmod, em = FALSE, local = TRUE) hmm_biofam <- fit_biofam$model
Examples of building and fitting HMMs in build_hmm
and
fit_model
; and biofam
for the original data and
biofam3c
for the three-channel version used in this model.
# NOT RUN {
# Plotting the model
plot(hmm_biofam)
# }
Run the code above in your browser using DataLab