library(seqHMM)
data("biofam3c")
# Building sequence objects
marr_seq <- seqdef(
biofam3c$married,
start = 15,
alphabet = c("single", "married", "divorced"),
cpal = c("violetred2", "darkgoldenrod2", "darkmagenta")
)
child_seq <- seqdef(
biofam3c$children,
start = 15,
alphabet = c("childless", "children"),
cpal = c("darkseagreen1", "coral3")
)
left_seq <- seqdef(
biofam3c$left,
start = 15,
alphabet = c("with parents", "left home"),
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
usethis::use_data(hmm_biofam)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.