data(mvad)
## Defining a state sequence object
mvad.seq <- seqdef(mvad[, 17:86])
## Computing dissimilarities (any dissimilarity measure can be used)
mvad.ham <- seqdist(mvad.seq, method="HAM")
## Grow the tree using a low R value for illustration.
## For R=10, pval cannot be lower than 0.1
dt <- disstree(mvad.ham~ male + Grammar + funemp + gcse5eq + fmpr + livboth,
data=mvad, R = 10, pval = 0.1)
print(dt)
## Will only work if GraphViz is properly installed
## See seqtree for a simpler way to plot a sequence tree.
if (FALSE) {
disstreedisplay(dt, image.fun = seqdplot, image.data = mvad.seq,
## Additional parameters passed to seqdplot
with.legend = FALSE, xaxis = FALSE, ylab = "", border=NA)
}
## Second method, using a specific function
myplotfunction <- function(individuals, seqs, ...) {
par(font.sub=2, mar=c(3,0,6,0), mgp=c(0,0,0))
## using mds to order sequence in seqIplot
mds <- suppressMessages(cmdscale(seqdist(seqs[individuals,], method="HAM"),k=1))
seqIplot(seqs[individuals,], sortv=mds,...)
}
## If image.data is not set, indexes of individuals are sent to image.fun
if (FALSE) {
disstreedisplay(dt, image.fun = myplotfunction, cex.main = 3,
## additional parameters passed to myplotfunction
seqs = mvad.seq,
## additional parameters passed to seqIplot (through myplotfunction)
with.legend = FALSE, xaxis = FALSE, ylab = "")
}
## Retrieving terminal node membership
term.leaf <- disstreeleaf(dt)
table(term.leaf)
## Retrieving classification rules
rules <- disstree.get.rules(dt)
## Index of rule (terminal leaf) that applies to a specified profile
## covariates are: male, Grammar, funemp, gcse5eq, fmpr, livboth
profile <- data.frame(male="no", Grammar="yes", funemp="no", gcse5eq="yes", fmpr="no", livboth="no")
rules[disstree.assign(rules, profile=profile)]
Run the code above in your browser using DataLab