R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("VLMC-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('VLMC') Loading required package: MASS > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "OZrain" > > ### * OZrain > > flush(stderr()); flush(stdout()) > > ### Name: OZrain > ### Title: Daily Rainfall in Melbourne, Australia, 1981-1990 > ### Aliases: OZrain > ### Keywords: datasets > > ### ** Examples > > data(OZrain) > (n <- length(OZrain)) ## should be 1 more than [1] 3653 > ISOdate(1990,12,31) - ISOdate(1981, 1,1)## but it's 2 .. Time difference of 3651 days > > has.rain <- OZrain > 0 > > summary(OZrain[has.rain])# Median = 18, Q3 = 50 Min. 1st Qu. Median Mean 3rd Qu. Max. 2.00 6.00 18.00 43.63 50.00 692.00 > table(rain01 <- as.integer(has.rain)) 0 1 2237 1416 > table(rain4c <- cut(OZrain, c(-.1, 0.5, 18.5, 50.1, 1000))) (-0.1,0.5] (0.5,18.5] (18.5,50.1] (50.1,1e+03] 2237 713 356 347 > > ## Don't show: > table(rain4c <- as.integer(rain4c) - 1:1) 0 1 2 3 2237 713 356 347 > draw(v4 <- vlmc(rain4c)) [x]-( 2236 713 356 347| 3652)-F +--[0]-( 1613 316 148 159| 2236) <59.03> | `--[0]-( 1185 210 99 118| 1612) <1.26> | +--[0]-( 876 149 71 88| 1184) <0.14> | | +--[0]-( 654 101 59 62| 876) <0.88> | | | `--[0]-( 495 70 48 41| 654) <0.73> | | | `--[2]-( 17 9 2 1| 29) <4.51>-T | | +--[1]-( 147 27 8 15| 197) <0.80> | | | `--[0]-( 66 8 3 7| 84) <0.74> | | | `--[0]-( 43 4 3 4| 54) <0.42> | | | `--[3]-( 0 2 1 0| 3) <6.19>-T | | `--[2]-( 43 13 0 7| 63) <5.78>-T | `--[3]-( 48 9 4 3| 64) <0.38> | `--[0]-( 24 3 4 2| 33) <1.12> | `--[1]-( 1 3 1 1| 6) <4.97>-T +--[1]-( 375 164 99 75| 713) <12.55> | +--[0]-( 168 66 43 39| 316) <0.81> | | `--[0]-( 107 41 29 33| 210) <1.09> | | `--[0]-( 76 28 20 25| 149) <0.08> | | `--[0]-( 59 18 9 15| 101) <1.53> | | `--[0]-( 39 12 8 11| 70) <0.30> | | `--[0]-( 28 10 4 7| 49) <0.44> | | `--[0]-( 23 9 3 4| 39) <0.33> | | `--[2]-( 0 0 0 2| 2) <4.55>-T | `--[1]-( 87 42 22 13| 164) <0.82> | `--[0]-( 34 16 8 8| 66) <0.71> | `--[0]-( 18 9 7 7| 41) <1.02> | `--[0]-( 13 6 3 6| 28) <0.55> | `--[2]-( 0 3 0 1| 4) <3.91>-T +--[2]-( 146 113 48 49| 356) <30.14> | +--[0]-( 54 39 22 33| 148) <4.49>-T | +--[1]-( 43 36 13 7| 99) <2.36> | | `--[1]-( 10 8 0 4| 22) <4.23>-T | `--[3]-( 27 21 7 6| 61) <0.62> | `--[3]-( 0 3 3 0| 6) <5.54>-T `--[3]-( 102 120 61 64| 347) <72.18> +--[0]-( 55 48 27 29| 159) <1.17> | `--[0]-( 41 36 20 21| 118) <0.01> | `--[1]-( 2 3 6 6| 17) <4.70>-T `--[2]-( 11 19 16 3| 49) <5.81>-T > v4 # cutoff 3.907; MC = 9, context = 39 `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 3653. Call: vlmc(dts = rain4c); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 9 39 11 322 AIC = 7485 > vlmc(rain4c, cut = 0 )$size # MC = 25; context = 2355, #{leaves}= 1062 ord.MC context nr.leaves total 25 2355 1062 20018 > vlmc(rain4c, cut = 0.1)$size # MC = 25; context = 2344 ord.MC context nr.leaves total 25 2344 1054 19930 > vlmc(rain4c, cut = 0.3)$size # MC = 25; context = 1990, #{leaves}= 967 ord.MC context nr.leaves total 25 1990 967 17074 > vlmc(rain4c, cut = 6.0)$size # MC = 7; context = 10, #{leaves}= 4 ord.MC context nr.leaves total 7 10 4 90 > lLv <- function(co) { + l <- logLik(vlmc(rain4c, cut = co)); c(Ll = l, df = attr(l,"df")) + } > cuts4 <- c(0,0.1,0.3,0.5,0.7, seq(1,7, by = .25), 9, 12,13) > ll4 <- sapply(cuts4, lLv) > par(mfcol = c(2,2)) > plot(cuts4, AIC4 <- c(c(-2,2) %*% ll4), type = 'b') > plot(cuts4, BIC4 <- c(c(-2,log(n)) %*% ll4), type = 'b') > ii <- cuts4 > 2. > plot(cuts4[ii], AIC4[ii], type = 'b') > plot(cuts4[ii], BIC4[ii], type = 'b') > ## End Don't show > > AIC(v1 <- vlmc(rain01))# cutoff = 1.92 [1] 4547.764 > AIC(v00 <- vlmc(rain01, cut = 1.4)) [1] 4560.286 > AIC(v0 <- vlmc(rain01, cut = 1.5)) [1] 4554.815 > > ## Don't show: > lLv <- function(co) { + cat(co,"") + l <- logLik(vlmc(rain01, cut = co)) + c(Ll = l, df = attr(l,"df")) + } > cc <- seq(1., 3, len = 401) > system.time(vll <- sapply(cc, lLv))# 21 & 49 sec. (on P III 900 MHz) 1 1.005 1.01 1.015 1.02 1.025 1.03 1.035 1.04 1.045 1.05 1.055 1.06 1.065 1.07 1.075 1.08 1.085 1.09 1.095 1.1 1.105 1.11 1.115 1.12 1.125 1.13 1.135 1.14 1.145 1.15 1.155 1.16 1.165 1.17 1.175 1.18 1.185 1.19 1.195 1.2 1.205 1.21 1.215 1.22 1.225 1.23 1.235 1.24 1.245 1.25 1.255 1.26 1.265 1.27 1.275 1.28 1.285 1.29 1.295 1.3 1.305 1.31 1.315 1.32 1.325 1.33 1.335 1.34 1.345 1.35 1.355 1.36 1.365 1.37 1.375 1.38 1.385 1.39 1.395 1.4 1.405 1.41 1.415 1.42 1.425 1.43 1.435 1.44 1.445 1.45 1.455 1.46 1.465 1.47 1.475 1.48 1.485 1.49 1.495 1.5 1.505 1.51 1.515 1.52 1.525 1.53 1.535 1.54 1.545 1.55 1.555 1.56 1.565 1.57 1.575 1.58 1.585 1.59 1.595 1.6 1.605 1.61 1.615 1.62 1.625 1.63 1.635 1.64 1.645 1.65 1.655 1.66 1.665 1.67 1.675 1.68 1.685 1.69 1.695 1.7 1.705 1.71 1.715 1.72 1.725 1.73 1.735 1.74 1.745 1.75 1.755 1.76 1.765 1.77 1.775 1.78 1.785 1.79 1.795 1.8 1.805 1.81 1.815 1.82 1.825 1.83 1.835 1.84 1.845 1.85 1.855 1.86 1.865 1.87 1.875 1.88 1.885 1.89 1.895 1.9 1.905 1.91 1.915 1.92 1.925 1.93 1.935 1.94 1.945 1.95 1.955 1.96 1.965 1.97 1.975 1.98 1.985 1.99 1.995 2 2.005 2.01 2.015 2.02 2.025 2.03 2.035 2.04 2.045 2.05 2.055 2.06 2.065 2.07 2.075 2.08 2.085 2.09 2.095 2.1 2.105 2.11 2.115 2.12 2.125 2.13 2.135 2.14 2.145 2.15 2.155 2.16 2.165 2.17 2.175 2.18 2.185 2.19 2.195 2.2 2.205 2.21 2.215 2.22 2.225 2.23 2.235 2.24 2.245 2.25 2.255 2.26 2.265 2.27 2.275 2.28 2.285 2.29 2.295 2.3 2.305 2.31 2.315 2.32 2.325 2.33 2.335 2.34 2.345 2.35 2.355 2.36 2.365 2.37 2.375 2.38 2.385 2.39 2.395 2.4 2.405 2.41 2.415 2.42 2.425 2.43 2.435 2.44 2.445 2.45 2.455 2.46 2.465 2.47 2.475 2.48 2.485 2.49 2.495 2.5 2.505 2.51 2.515 2.52 2.525 2.53 2.535 2.54 2.545 2.55 2.555 2.56 2.565 2.57 2.575 2.58 2.585 2.59 2.595 2.6 2.605 2.61 2.615 2.62 2.625 2.63 2.635 2.64 2.645 2.65 2.655 2.66 2.665 2.67 2.675 2.68 2.685 2.69 2.695 2.7 2.705 2.71 2.715 2.72 2.725 2.73 2.735 2.74 2.745 2.75 2.755 2.76 2.765 2.77 2.775 2.78 2.785 2.79 2.795 2.8 2.805 2.81 2.815 2.82 2.825 2.83 2.835 2.84 2.845 2.85 2.855 2.86 2.865 2.87 2.875 2.88 2.885 2.89 2.895 2.9 2.905 2.91 2.915 2.92 2.925 2.93 2.935 2.94 2.945 2.95 2.955 2.96 2.965 2.97 2.975 2.98 2.985 2.99 2.995 3 [1] 9.83 0.56 10.48 0.00 0.00 > llv <- vll["Ll",] ; dfv <- vll["df",] > par(mfrow=c(2,2)) > ## very interesting: quite a few local minima!! > plot (cc, -2*llv + 2*dfv, type='l', main="AIC(vlmc(rain01, cutoff = cc))") > plot (cc, dfv, type='l', main="Complexity(vlmc(*, cutoff = cc))") > plot (cc, llv, type='l', main="Log.Likelihood(vlmc(*, cutoff = cc))") > > ## BIC is all falling: > ## par(new=T) > plot (cc, -2*llv + log(3653)*dfv, type='l', main="BIC(vlmc(*, cutoff = cc))") > ## End Don't show > > hist(OZrain) > hist(OZrain, breaks = c(0,1,5,10,50,1000), xlim = c(0,100)) > > plot(OZrain, main = "Rainfall 1981-1990 in Melbourne") > ## work around bug in plot.ts() for R <= 1.3.1 : > newer1.4 <- {v <- R.version; v$major > 1 || as.numeric(v$minor) >= 4.2} > if(newer1.4) { + plot(OZrain, log="y", main = "Non-0 Rainfall [LOG scale]") + } else + plot.ts(time(OZrain)[has.rain], + OZrain[has.rain], log="y", main = "Non-0 Rainfall [LOG scale]") Warning in xy.coords(x, NULL, log = log) : 2237 y values <= 0 omitted from logarithmic plot > lOZ <- lowess(log10(OZrain[has.rain]), f= .05) > lines(time(OZrain)[has.rain], 10^lOZ$y, col = 2, lwd = 2) > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "RCplot" > > ### * RCplot > > flush(stderr()); flush(stdout()) > > ### Encoding: latin1 > > ### Name: RCplot > ### Title: Residuals vs Context plot > ### Aliases: RCplot > ### Keywords: hplot utilities > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > RCplot(vlmc.pres) > RCplot(vlmc.dt1c01)## << almost perfect fit (0 resid.) > > > > cleanEx(); ..nameEx <- "alpha2int" > > ### * alpha2int > > flush(stderr()); flush(stdout()) > > ### Name: alpha2int > ### Title: `Single Character' <-> Integer Conversion for Discrete Data > ### Aliases: alpha2int int2alpha > ### Keywords: character utilities > > ### ** Examples > > alphabet <- "abcdefghijk" > (ch <- sample(letters[1:10], 30, replace = TRUE)) [1] "c" "d" "f" "j" "c" "i" "j" "g" "g" "a" "c" "b" "g" "d" "h" "e" "h" "j" "d" [20] "h" "j" "c" "g" "b" "c" "d" "a" "d" "i" "d" > (ic <- alpha2int(ch, alphabet)) [1] 2 3 5 9 2 8 9 6 6 0 2 1 6 3 7 4 7 9 3 7 9 2 6 1 2 3 0 3 8 3 > stopifnot(int2alpha(ic, alphabet) == ch) > > > > cleanEx(); ..nameEx <- "alphabet" > > ### * alphabet > > flush(stderr()); flush(stdout()) > > ### Name: alphabet > ### Title: The Alphabet in Use > ### Aliases: alphabet alphabet.vlmc > ### Keywords: character utilities > > ### ** Examples > > data(bnrf1) > vb <- vlmc(bnrf1EB, cutoff = 5) > alphabet(vb) # |--> "a" "c" "g" "t" [1] "a" "c" "g" "t" > > > > cleanEx(); ..nameEx <- "as.dendrogram.vlmc" > > ### * as.dendrogram.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: as.dendrogram.vlmc > ### Title: Dendrogram Construction from VLMCs > ### Aliases: as.dendrogram.vlmc > ### Keywords: graphs iplot > > ### ** Examples > > data(presidents) > dpr <- factor(cut(presidents, c(0,45,70,100)), exclude=NULL)# NA = 4th level > (vlmc.pres <- vlmc(dpr)) Warning in vlmc(dpr) : alphabet with >1-letter strings; trying to abbreviate `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpr); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 > draw(vlmc.pres) [x]-( 28 60 26 5| 119) +--[0]-( 18 7 0 2| 27) <15.22>-T +--[1]-( 9 39 11 1| 60) <3.00> | `--[2]-( 0 6 6 0| 12) <4.45>-T `--[2]-( 0 12 14 0| 26) <11.57>-T > (dv.dpr <- as.dendrogram(vlmc.pres)) 'dendrogram' with 3 branches and 3 members total, at height 2 > str(dv.dpr) --[dendrogram w/ 3 branches and 3 members at h = 2] |--leaf 27 (h= 1 count = c("18", " 7", " 0", " 2"), edgetext = 0 ) |--[dendrogram w/ 1 branches and 1 members at h = 1] | `--leaf 12 ( count = c("0", "6", "6", "0"), edgetext = 2 ) `--leaf 26 (h= 1 count = c(" 0", "12", "14", " 0"), edgetext = 2 ) > str(unclass(dv.dpr)) List of 3 $ 0: atomic [1:1] 27 ..- attr(*, "members")= int 1 ..- attr(*, "leaf")= logi TRUE ..- attr(*, "height")= int 1 ..- attr(*, "count")= int [1:4] 18 7 0 2 ..- attr(*, "edgetext")= chr "0" $ 1:List of 1 ..$ 2: atomic [1:1] 12 .. ..- attr(*, "members")= int 1 .. ..- attr(*, "leaf")= logi TRUE .. ..- attr(*, "height")= int 0 .. ..- attr(*, "count")= int [1:4] 0 6 6 0 .. ..- attr(*, "edgetext")= chr "2" ..- attr(*, "0-kids")= int [1:3] 0 1 3 ..- attr(*, "members")= int 1 ..- attr(*, "height")= int 1 ..- attr(*, "count")= int [1:4] 9 33 5 1 ..- attr(*, "edgetext")= chr "1" $ 2: atomic [1:1] 26 ..- attr(*, "members")= int 1 ..- attr(*, "leaf")= logi TRUE ..- attr(*, "height")= int 1 ..- attr(*, "count")= int [1:4] 0 12 14 0 ..- attr(*, "edgetext")= chr "2" - attr(*, "0-kids")= int 3 - attr(*, "members")= int 3 - attr(*, "height")= int 2 - attr(*, "count")= int [1:4] 1 2 1 2 > > plot(dv.dpr, type ="tr", nodePar = list(pch=c(1,16), cex = 1.5)) > > ## Artificial example > f1 <- c(1,0,0,0) ; f2 <- rep(1:0, 2) > (dt1 <- c(f1,f1,f2,f1,f2,f2,f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 > (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 > (dvlmc <- as.dendrogram(vlmc.dt1c01)) 'dendrogram' with 2 branches and 6 members total, at height 7 > str(dvlmc) --[dendrogram w/ 2 branches and 6 members at h = 7] |--[dendrogram w/ 2 branches and 5 members at h = 6] | |--[dendrogram w/ 2 branches and 2 members at h = 5] | | |--leaf 3 (h= 4 count = c("0", "3"), edgetext = 0 ) | | `--leaf 4 (h= 4 count = c("4", "0"), edgetext = 1 ) | `--[dendrogram w/ 1 branches and 3 members at h = 5] | `--[dendrogram w/ 2 branches and 3 members at h = 4] | |--[dendrogram w/ 1 branches and 1 members at h = 3] | | `--[dendrogram w/ 1 branches and 1 members at h = 2] | | `--[dendrogram w/ 1 branches and 1 members at h = 1] | | `--leaf 2 ( count = c("0", "2"), edgetext = 0 ) | `--[dendrogram w/ 1 branches and 2 members at h = 3] | `--[dendrogram w/ 2 branches and 2 members at h = 2] | |--leaf 2 (h= 1 count = c("0", "2"), edgetext = 0 ) | `--leaf 4 (h= 1 count = c("2", "2"), edgetext = 1 ) `--leaf 10 (h= 6 count = c("10", " 0"), edgetext = 1 ) > ## not so useful: > plot(dvlmc, nodePar= list(pch=c(1,16))) > ## complete disaster: > plot(dvlmc, type ="tr", nodePar= list(pch=c(1,16))) > > ## but this is not (yet) so much better (want the same angles to left > ## and right!! > plot(dvlmc, type ="tr", nodePar = list(pch=c(1,16)), center=TRUE, + main = format(vlmc.dt1c01$call)) > mtext(paste("dt1 =", gsub(" ","",deparse(dt1,width=100)))) > > > > cleanEx(); ..nameEx <- "bnrf1" > > ### * bnrf1 > > flush(stderr()); flush(stdout()) > > ### Name: bnrf1 > ### Title: BNRF1 Gene DNA sequences: Epstein-Barr and Herpes > ### Aliases: bnrf1EB bnrf1HV > ### Keywords: datasets > > ### ** Examples > > data(bnrf1) > bnrf1EB[1:500] [1] a t g g a a g a g a g g g g c a g g g a a a c g c a a a t g c c g g t t g [38] c c c g g t a t g g g g g c c c g t t t a t t a t g g t a a g g c t c t t [75] c g g g c a a g a t g g a g a g g c a a a c a t a c a g g a g g a a a g g [112] c t a t a t g a g c t a c t c t c t g a c c c a c g c t c c g c g c t c g [149] g c c t a g a c c c g g g g c c c c t g a t t g c t g a g a a c c t g c t [186] g c t a g t g g c g c t g c g t g g c a c c a a c a a c g a t c c c a g g [223] c c t c a g c g t c a g g a g a g g g c c a g a g a a c t g g c c c t c g [260] t t g g c a t t c t a c t a g g a a a c g g c g a g c a g g g t g a a c a [297] c t t g g g c a c g g a g a g t g c c c t g g a g g c c t c a g g c a a c [334] a a c t a t g t g t a t g c c t a c g g a c c a g a c t g g a t g g c a a [371] g g c c t t c c a c a t g g t c c g c g g a a a t c c a g c a a t t c c t [408] g c g a c t c c t g g g c g c c a c g t a c g t g c t t c g c g t g g a g [445] a t g g g c a g g c a g t t t g g c t t c g a g g t g c a t a g a a g c c [482] g g c c c t c c t t c c g t c a g t t Levels: a c g t > table(bnrf1EB) bnrf1EB a c g t 744 1195 1232 783 > table(bnrf1HV) bnrf1HV a c g t 1172 717 680 1172 > n <- length(bnrf1HV) > table(t = bnrf1HV[-1], "t-1" = bnrf1HV[-n]) t-1 t a c g t a 389 280 200 302 c 227 134 150 206 g 256 39 120 265 t 299 264 210 399 > > plot(as.integer(bnrf1EB[1:500]), type = "b") > ## Don't show: > ftable(table( t = bnrf1HV[-(1:2)], + "t-1" = bnrf1HV[-c(1,n)], + "t-2" = bnrf1HV[-c(n-1,n)])) t-2 a c g t t t-1 a a 146 99 71 73 c 97 47 56 80 g 88 11 43 58 t 76 73 49 104 c a 73 49 47 58 c 37 34 24 39 g 53 7 34 56 t 47 48 38 73 g a 94 57 36 69 c 15 6 10 8 g 33 4 19 64 t 93 38 46 88 t a 75 75 46 102 c 78 47 60 79 g 82 17 24 87 t 83 105 77 134 > lag.plot(jitter(as.ts(bnrf1HV)),lag = 4, pch = ".") > ## End Don't show > > ## Simplistic gene matching: > percent.eq <- sapply(0:200, + function(i) 100 * sum(bnrf1EB[(1+i):(n+i)] == bnrf1HV))/n > plot.ts(percent.eq) > > > > cleanEx(); ..nameEx <- "deviance.vlmc" > > ### * deviance.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: deviance.vlmc > ### Title: Compute the Deviance of a Fitted VLMC Object > ### Aliases: deviance.vlmc > ### Keywords: ts models > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > deviance(vlmc.pres) [1] 197.6090 > > devianceR <- function(object) + { + dn <- dimnames(pr <- predict(object)) + -2 * sum(log(pr[cbind(2:nrow(pr), match(dn[[1]][-1], dn[[2]]))])) + } > all.equal(deviance(vlmc.pres), devianceR(vlmc.pres), tol = 1e-14) [1] TRUE > > > > cleanEx(); ..nameEx <- "draw.vlmc" > > ### * draw.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: draw.vlmc > ### Title: Draw a "VLMC" Object (in ASCII) as tree. > ### Aliases: draw.vlmc draw > ### Keywords: ts models > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > draw(vlmc.dt1c01) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38>-F | +--[0]-( 4 3| 7) <0.14>-F | | +--[0]-( 0 3| 3) <2.54>-T | | `--[1]-( 4 0| 4) <2.24>-T | `--[1]-( 4 6| 10) <0.10> | `--[0]-( 3 6| 9) <0.09>-F | +--[0]-( 1 2| 3) <0.00> | | `--[0]-( 1 2| 3) <0.00> | | `--[1]-( 1 2| 3) <0.00> | | `--[0]-( 0 2| 2) <0.81>-T | `--[1]-( 2 4| 6) <0.00> | `--[0]-( 2 4| 6) <0.00>-F | +--[0]-( 0 2| 2) <0.81>-T | `--[1]-( 2 2| 4) <0.24>-T `--[1]-( 10 0| 10) <4.05>-T > draw(vlmc.dt1c01, flag = FALSE) -4.000000 > draw(vlmc.dt1c01, kind = 1) [x]-( 18 9| 27) +--[0]-( 8 9| 17) <1.38> +---+--[0]-( 4 3| 7) <0.14> +---+---+--[0]-( 0 3| 3) <2.54> +---+---+--[1]-( 4 0| 4) <2.24> +---+--[1]-( 4 6| 10) <0.10> +---+---+--[0]-( 3 6| 9) <0.09> +---+---+---+--[0]-( 1 2| 3) <0.00> +---+---+---+---+--[0]-( 1 2| 3) <0.00> +---+---+---+---+---+--[1]-( 1 2| 3) <0.00> +---+---+---+---+---+---+--[0]-( 0 2| 2) <0.81> +---+---+---+--[1]-( 2 4| 6) <0.00> +---+---+---+---+--[0]-( 2 4| 6) <0.00> +---+---+---+---+---+--[0]-( 0 2| 2) <0.81> +---+---+---+---+---+--[1]-( 2 2| 4) <0.24> +--[1]-( 10 0| 10) <4.05> > draw(vlmc.dt1) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38> | `--[0]-( 4 3| 7) <0.14>-F | +--[0]-( 0 3| 3) <2.54>-T | `--[1]-( 4 0| 4) <2.24>-T `--[1]-( 10 0| 10) <4.05>-T > draw(vlmc.dt1, show = 3) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38>-. | +--[0]-( 4 3| 7) <0.14>-F | | +--[0]-( 0 3| 3) <2.54>-T | | `--[1]-( 4 0| 4) <2.24>-T | `--[1]-(___) `--[1]-( 10 0| 10) <4.05>-T > draw(vlmc.dt1, cumulative = FALSE) [x]-( 0 0| 0)-F +--[0]-( 4 6| 10) | `--[0]-( 0 0| 0)-F | +--[0]-( 0 3| 3)-T | `--[1]-( 4 0| 4)-T `--[1]-( 10 0| 10)-T > > > > cleanEx(); ..nameEx <- "id2ctxt" > > ### * id2ctxt > > flush(stderr()); flush(stdout()) > > ### Name: id2ctxt > ### Title: VLMC Context ID Conversion > ### Aliases: id2ctxt > ### Keywords: utilities > > ### ** Examples > > id2ctxt(c(2,3,5,9), alpha = "Ab") [1] "A" "b" "Ab" "AAb" > str(id2ctxt(c(2,3,5,9), 2)) List of 4 $ : int 0 $ : int 1 $ : int [1:2] 0 1 $ : int [1:3] 0 0 1 > > > > cleanEx(); ..nameEx <- "int2char" > > ### * int2char > > flush(stderr()); flush(stdout()) > > ### Name: int2char > ### Title: Character - Integer Conversion > ### Aliases: int2char char2int > ### Keywords: character utilities > > ### ** Examples > > char2int("vlmc", paste(letters, collapse="")) [1] 21 11 12 2 > > int2char(c(0:3, 3:1), "abcd") [1] "abcddcb" > int2char(c(1:0,3,3), "abc") # to eat ;-) [1] "baNANA" > > > > cleanEx(); ..nameEx <- "logLik" > > ### * logLik > > flush(stderr()); flush(stdout()) > > ### Name: logLik > ### Title: Log Likelihood of and between VLMC objects > ### Aliases: logLik.vlmc entropy entropy2 > ### Keywords: ts models > > ### ** Examples > > dd <- cumsum(rpois(999, 1.5)) %% 10 > (vd <- vlmc(dd)) `vlmc', a Variable Length Markov Chain; alphabet '0123456789', |alphabet| = 10, n = 999. Call: vlmc(dts = dd); default cutoff = 8.46 -> extensions (= $size ) : ord.MC context nr.leaves total 1 10 10 222 AIC = 3202 > logLik(vd) 'log Lik.' -1511.208 (df=90) > > ## AIC model selection: > f1 <- c(1,0,0,0) # as in example(vlmc) > f2 <- rep(1:0,2) > (dt1 <- c(f1,f1,f2,f1,f2,f2,f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 > AIC(print(vlmc(dt1))) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 [1] 21.46023 > AIC(print(vlmc(dt1, cutoff = 2.6))) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 2.6) -> extensions (= $size ) : ord.MC context nr.leaves total 1 2 1 10 AIC = 27.51 [1] 27.50815 > AIC(print(vlmc(dt1, cutoff = 0.4)))# these two differ ``not really'' `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.4) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 5 62 AIC = 27.55 [1] 27.54518 > AIC(print(vlmc(dt1, cutoff = 0.1))) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 [1] 27.54518 > > ## Show how to compute it from the fitted conditional probabilities : > logLikR <- function(x) { + dn <- dimnames(pr <- predict(x)) + sum(log(pr[cbind(2:nrow(pr), match(dn[[1]][-1], dn[[2]]))])) + } > > all.equal( logLikR(vd), + c(logLik (vd)), tol=1e-10) # TRUE, they do the same [1] TRUE > > ## Compare different ones: [cheap example]: > example(draw) draw> example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" draw> draw(vlmc.dt1c01) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38>-F | +--[0]-( 4 3| 7) <0.14>-F | | +--[0]-( 0 3| 3) <2.54>-T | | `--[1]-( 4 0| 4) <2.24>-T | `--[1]-( 4 6| 10) <0.10> | `--[0]-( 3 6| 9) <0.09>-F | +--[0]-( 1 2| 3) <0.00> | | `--[0]-( 1 2| 3) <0.00> | | `--[1]-( 1 2| 3) <0.00> | | `--[0]-( 0 2| 2) <0.81>-T | `--[1]-( 2 4| 6) <0.00> | `--[0]-( 2 4| 6) <0.00>-F | +--[0]-( 0 2| 2) <0.81>-T | `--[1]-( 2 2| 4) <0.24>-T `--[1]-( 10 0| 10) <4.05>-T draw> draw(vlmc.dt1c01, flag = FALSE) -4.000000 draw> draw(vlmc.dt1c01, kind = 1) [x]-( 18 9| 27) +--[0]-( 8 9| 17) <1.38> +---+--[0]-( 4 3| 7) <0.14> +---+---+--[0]-( 0 3| 3) <2.54> +---+---+--[1]-( 4 0| 4) <2.24> +---+--[1]-( 4 6| 10) <0.10> +---+---+--[0]-( 3 6| 9) <0.09> +---+---+---+--[0]-( 1 2| 3) <0.00> +---+---+---+---+--[0]-( 1 2| 3) <0.00> +---+---+---+---+---+--[1]-( 1 2| 3) <0.00> +---+---+---+---+---+---+--[0]-( 0 2| 2) <0.81> +---+---+---+--[1]-( 2 4| 6) <0.00> +---+---+---+---+--[0]-( 2 4| 6) <0.00> +---+---+---+---+---+--[0]-( 0 2| 2) <0.81> +---+---+---+---+---+--[1]-( 2 2| 4) <0.24> +--[1]-( 10 0| 10) <4.05> draw> draw(vlmc.dt1) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38> | `--[0]-( 4 3| 7) <0.14>-F | +--[0]-( 0 3| 3) <2.54>-T | `--[1]-( 4 0| 4) <2.24>-T `--[1]-( 10 0| 10) <4.05>-T draw> draw(vlmc.dt1, show = 3) [x]-( 18 9| 27)-F +--[0]-( 8 9| 17) <1.38>-. | +--[0]-( 4 3| 7) <0.14>-F | | +--[0]-( 0 3| 3) <2.54>-T | | `--[1]-( 4 0| 4) <2.24>-T | `--[1]-(___) `--[1]-( 10 0| 10) <4.05>-T draw> draw(vlmc.dt1, cumulative = FALSE) [x]-( 0 0| 0)-F +--[0]-( 4 6| 10) | `--[0]-( 0 0| 0)-F | +--[0]-( 0 3| 3)-T | `--[1]-( 4 0| 4)-T `--[1]-( 10 0| 10)-T > for(n in ls()) + if(is.vlmc(get(n))) { + vv <- get(n) + cat(n,":",formatC(logLik(vv) / log(vv$alpha.len), + format= "f", wid=10),"\n") + } vd : -656.3095 vlmc.dt1 : -9.7095 vlmc.dt1c01 : -4.0000 vlmc.pres : -71.2724 > > > > cleanEx(); ..nameEx <- "predict.vlmc" > > ### * predict.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: predict.vlmc > ### Title: Prediction of VLMC for (new) Series > ### Aliases: print.predict.vlmc predict.vlmc fitted.vlmc > ### Keywords: ts models > > ### ** Examples > > f1 <- c(1,0,0,0) > f2 <- rep(1:0,2) > (dt2 <- rep(c(f1,f1,f2,f1,f2,f2,f1),2)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 [39] 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 > > (vlmc.dt2c15 <- vlmc(dt2, cutoff = 1.5)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 56. Call: vlmc(dts = dt2, cutoff.prune = 1.5) -> extensions (= $size ) : ord.MC context nr.leaves total 6 8 4 46 AIC = 36.65 > draw(vlmc.dt2c15) [x]-( 36 19| 55)-F +--[0]-( 16 19| 35) <2.84>-F | +--[0]-( 8 7| 15) <0.17>-F | | +--[0]-( 0 7| 7) <5.33>-T | | `--[1]-( 8 0| 8) <5.03>-T | `--[1]-( 8 12| 20) <0.13> | `--[0]-( 7 12| 19) <0.04> | `--[1]-( 4 8| 12) <0.03> | `--[0]-( 4 8| 12) <0.00> | `--[0]-( 0 4| 4) <1.62>-T `--[1]-( 20 0| 20) <8.48>-T > > ## Fitted Values: > all.equal(predict(vlmc.dt2c15, dt2), predict(vlmc.dt2c15)) [1] TRUE > (pa2c15 <- predict(vlmc.dt2c15, type = "ALL")) fit Pr[X= 0 ] Pr[X= 1 ] id flags ctxt 1 0 NA 0 0 1 0 3 55 1 0 0 1 0 5 55 01 0 0 1 0 9 55 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 1 1 3/7 4/7 10 0 010 0 0 1 0 3 0 1 1 1 0 1 84 0 010100 0 0 1 0 3 0 1 0 1 1/2 1/2 42 0 01010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 1 1 3/7 4/7 10 0 010 0 0 1 0 3 0 1 1 1 0 1 84 0 010100 0 0 1 0 3 0 1 1 1 1/2 1/2 42 0 01010 0 0 1 0 3 0 1 1 0 1/2 1/2 42 0 01010 0 0 1 0 3 0 1 0 0 1/2 1/2 42 0 01010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 1 1 3/7 4/7 10 0 010 0 0 1 0 3 0 1 1 1 0 1 84 0 010100 0 0 1 0 3 0 1 0 1 1/2 1/2 42 0 01010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 1 1 3/7 4/7 10 0 010 0 0 1 0 3 0 1 1 1 0 1 84 0 010100 0 0 1 0 3 0 1 1 0 1/2 1/2 42 0 01010 0 0 1 0 3 0 1 1 0 1/2 1/2 42 0 01010 0 0 1 0 3 0 1 0 0 1/2 1/2 42 0 01010 0 0 1 0 9 0 001 > > ## Depth = context length ([1] : NA) : > stopifnot(nchar(pa2c15 $ ctxt)[-1] == + predict(vlmc.dt2c15, type = "depth")[-1]) > > same <- (ff1 <- pa2c15 $ fitted) == + (ff2 <- int2alpha(predict(vlmc.dt2c15, type ="response"), alpha="01")) > which(!same) #-> some are different, since max.col() breaks ties at random! [1] 15 23 43 > > ndt2 <- c(rep(0,6),f1,f1,f2) > predict(vlmc.dt2c15, ndt2, "ALL") fit Pr[X= 0 ] Pr[X= 1 ] id flags ctxt 0 0 NA 0 1 16/35 19/35 2 65 0 0 0 8/15 7/15 4 65 00 0 1 0 1 8 55 000 0 1 0 1 8 0 000 0 1 0 1 8 0 000 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 1 1 3/7 4/7 10 0 010 0 0 1 0 3 0 1 > > (newdt2 <- sample(dt2, 17)) [1] 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 > pm <- predict(vlmc.dt2c15, newdt2, allow.subset = TRUE) > summary(apply(pm, 1, sum))# all 1 Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 1 1 1 1 1 1 1 > > predict(vlmc.dt2c15, newdt2, type = "ALL") fit Pr[X= 0 ] Pr[X= 1 ] id flags ctxt 0 0 NA 0 1 16/35 19/35 2 65 0 0 0 8/15 7/15 4 65 00 0 1 0 1 8 55 000 0 1 0 1 8 0 000 0 1 0 1 8 0 000 1 1 0 1 8 0 000 1 0 1 0 3 0 1 0 0 1 0 3 0 1 1 0 1 0 5 0 01 0 0 1 0 3 0 1 0 1 1/3 2/3 21 10 0101 0 0 1 0 9 0 001 1 1 0 1 8 0 000 0 0 1 0 3 0 1 0 1 3/7 4/7 10 0 010 1 0 1 0 9 0 001 > > data(bnrf1) > (vbnrf <- vlmc(bnrf1EB)) `vlmc', a Variable Length Markov Chain; alphabet 'acgt', |alphabet| = 4, n = 3954. Call: vlmc(dts = bnrf1EB); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 6 73 28 618 AIC = 10555 > (pA <- predict(vbnrf, bnrf1EB[1:24], type = "ALL")) fit Pr[X= a ] Pr[X= c ] Pr[X= g ] Pr[X= t ] id flags ctxt a 0 NA t t 0 0 0 1 4 55 a g g 4/51 14/51 26/51 7/51 28 55 ta g g 3/14 13/56 19/56 3/14 108 55 gta a c 17/72 3/8 19/72 1/8 107 0 ggt a g 32/189 7/27 73/189 5/27 18 0 ag g g 13/76 5/19 6/19 1/4 16 0 aa a c 3/10 3/10 1/5 1/5 96 0 gaa g g 32/189 7/27 73/189 5/27 18 0 ag a g 52/203 54/203 10/29 27/203 24 0 ga g g 32/189 7/27 73/189 5/27 18 0 ag g g 52/203 54/203 10/29 27/203 24 0 ga g c 19/100 53/150 3/10 47/300 26 0 gg g c 19/100 53/150 3/10 47/300 26 0 gg c c 19/100 53/150 3/10 47/300 26 0 gg a c 1/12 3/4 0 1/6 1450 0 cgggg g g 43/197 47/197 70/197 37/197 17 0 ac g g 52/203 54/203 10/29 27/203 24 0 ga g c 19/100 53/150 3/10 47/300 26 0 gg a c 19/100 53/150 3/10 47/300 26 0 gg a g 32/189 7/27 73/189 5/27 18 0 ag a g 13/76 5/19 6/19 1/4 16 0 aa c g 13/76 5/19 6/19 1/4 16 0 aa g g 49/219 65/219 67/219 38/219 20 0 ca > pc <- predict(vbnrf, bnrf1EB[1:24], type = "class") > pr <- predict(vbnrf, bnrf1EB[1:24], type = "resp") > stopifnot(as.integer (pc[-1]) == 1 + pr[-1], + as.character(pc[-1]) == strsplit(vbnrf$alpha,NULL)[[1]][1 + pr[-1]]) > > ##-- Example of a "perfect" fit -- just for illustration: > ## the default, thresh = 2 doesn't fit perfectly(i=38) > (vlmc.dt2c0th1 <- vlmc(dt2, cutoff = 0, thresh = 1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 56. Call: vlmc(dts = dt2, cutoff.prune = 0, threshold.gen = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 12 28 12 158 AIC = 56 > > ## "Fitted" = "Data" (but the first which can't be predicted): > stopifnot(dt2[-1] == predict(vlmc.dt2c0th1,type = "response")[-1]) > > > > cleanEx(); ..nameEx <- "prt.vvec" > > ### * prt.vvec > > flush(stderr()); flush(stdout()) > > ### Name: prt.vvec > ### Title: Recursively Print the VLMC Result Vector > ### Aliases: prt.vvec > ### Keywords: utilities > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > str(vv <- vlmc.dt1$vlmc) int [1:26] 2 0 0 0 1 4 6 2 0 0 ... > prt.vvec(vv[-1], n = 2) {0} [0, 0] {1} [4, 6] {2} [0, 0] {3} [0, 3] - - {3} [4, 0] - - - {1} [10, 0] - - NULL > prt.vvec(vv[-1], n = 2, pad = " | ") {0} [0, 0] | | {1} [4, 6] | | | | | {2} [0, 0] | | | | | | | | {3} [0, 3] - - | | | | | | | | {3} [4, 0] - - - | | {1} [10, 0] - - NULL > > > > cleanEx(); ..nameEx <- "residuals.vlmc" > > ### * residuals.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: residuals.vlmc > ### Title: Compute Residuals of a Fitted VLMC Object > ### Aliases: residuals.vlmc > ### Keywords: ts models > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > rp <- residuals(vlmc.pres) > stopifnot(all(abs(apply(rp[-1,],1,sum)) < 1e-15)) > matplot(seq(presidents), rp, ylab = "residuals", type="l") > ## ``Tukey-Anscombe'' (the following is first stab at plot method): > matplot(fitted(vlmc.pres), rp, ylab = "residuals", xaxt = "n", + type="b", pch=vlmc.pres$alpha) > axis(1, at = 0:(vlmc.pres$alpha.len-1), + labels = strsplit(vlmc.pres$alpha,"")[[1]]) > > summary(rd <- residuals(vlmc.pres, type = "dev")) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's -2.2820 -1.2440 -1.1130 -1.1220 -0.8657 2.7830 1.0000 > rd[1:7] [1] NA -1.893018 -1.112690 -1.112690 -1.243535 -1.177410 -1.829741 > ## sum of squared dev.residuals === deviance : > all.equal(sum(rd[-1] ^ 2), + deviance(vlmc.pres)) [1] TRUE > ## Don't show: > stopifnot(all.equal(sum(rd[-1] ^ 2), + deviance(vlmc.pres),tol=1e-12)) > ## End Don't show > > > > cleanEx(); ..nameEx <- "simulate.vlmc" > > ### * simulate.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: simulate.vlmc > ### Title: Simulate a Discrete Time Series from fitted VLMC model > ### Aliases: simulate.vlmc > ### Keywords: ts models > > ### ** Examples > > example(vlmc) vlmc> f1 <- c(1, 0, 0, 0) vlmc> f2 <- rep(1:0, 2) vlmc> (dt1 <- c(f1, f1, f2, f1, f2, f2, f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 vlmc> (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> vlmc(dt1, dump = 1, ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 vlmc> (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 vlmc> data(presidents) vlmc> dpres <- cut(presidents, c(0, 45, 70, 100)) vlmc> table(dpres <- factor(dpres, exclude = NULL)) (0,45] (45,70] (70,100] 28 60 26 6 vlmc> vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 vlmc> vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 vlmc> vlmc.pres$alpha [1] "0123" vlmc> stopifnot(length(print(strsplit(vlmc.pres$alpha, NULL)[[1]])) == vlmc.pres$alpha.len) [1] "0" "1" "2" "3" > > simulate.vlmc(vlmc.dt1, 100) [1] "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "0" [19] "0" "1" "0" "0" "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "0" [37] "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "1" "0" "1" "0" "1" [55] "0" "0" "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "1" "0" "1" [73] "0" "1" "0" "0" "0" "1" "0" "0" "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" [91] "0" "1" "0" "1" "0" "0" "0" "1" "0" "0" > simulate.vlmc(vlmc.dt1c01, 100, int = TRUE) [1] 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 [38] 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 [75] 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 > # n.start = 0: 1st few observations will resemble the data > simulate.vlmc(vlmc.dt1c01, 20, n.start=0, int = TRUE) [1] 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 > > > > > cleanEx(); ..nameEx <- "summary.vlmc" > > ### * summary.vlmc > > flush(stderr()); flush(stdout()) > > ### Name: summary.vlmc > ### Title: Summary of Fitted Variable Length Markov Chain (VLMC) > ### Aliases: summary.vlmc print.summary.vlmc > ### Keywords: ts models > > ### ** Examples > > data(bnrf1) > vb <- vlmc(bnrf1EB) > svb <- summary(vb) > svb `vlmc', a Variable Length Markov Chain; alphabet 'acgt', |alphabet| = 4, n = 3954. Call: vlmc(dts = bnrf1EB); default cutoff = 3.907364 -> extensions (= $size ) : ord.MC context nr.leaves total 6 73 28 618 AIC = 10554.84 R^2 = %{correctly predicted} = 39.2% Confusion matrix: predicted data a c g t a 78 316 338 11 c 34 613 524 24 g 34 375 811 12 t 44 339 352 48 Markov chain depths along the data: Min. 1st Qu. Median Mean 3rd Qu. Max. 1 2 2 2.45 3 6 > ## Don't show: > try(## since it currently fails (".. nested too deeply") -- FIXME -- ! + print(svb, vvec.print = TRUE) + ) `vlmc', a Variable Length Markov Chain; alphabet 'acgt', |alphabet| = 4, n = 3954. Call: vlmc(dts = bnrf1EB); default cutoff = 3.907364 -> extensions (= $size ) : ord.MC context nr.leaves total 6 73 28 618 AIC = 10554.84 R^2 = %{correctly predicted} = 39.2% Confusion matrix: predicted data a c g t a 78 316 338 11 c 34 613 524 24 g 34 375 811 12 t 44 339 352 48 Markov chain depths along the data: Min. 1st Qu. Median Mean 3rd Qu. Max. 1 2 2 2.45 3 6 context tree encoding `vvec'tor: {0} [0, 0, 0, 0] {1} [0, 0, 0, 1] {2} [13, 20, 24, 19] - {3} [4, 18, 9, 2] - - {4} [3, 2, 10, 6] - - - - - - - {2} [43, 47, 70, 37] - {3} [9, 15, 31, 13] - {4} [2, 12, 4, 0] - - - - - - - - {2} [32, 49, 73, 35] - - - {3} [7, 11, 6, 8] {4} [3, 5, 3, 0] - - {5} [0, 0, 0, 3] - - - - - - - {4} [0, 4, 5, 0] - - - - {2} [14, 36, 12, 24] - - - - {1} [58, 75, 43, 53] {2} [49, 65, 67, 38] - - - - {2} [53, 54, 43, 54] {3} [2, 6, 5, 5] - {4} [2, 3, 5, 5] - - {5} [3, 4, 0, 0] - - - - - {4} [4, 7, 4, 3] - - {5} [4, 0, 0, 3] - - - - - - {3} [11, 23, 22, 17] - - {4} [4, 14, 4, 12] {5} [3, 0, 1, 0] - - - - - - - - - - {2} [23, 59, 29, 39] {3} [20, 3, 6, 8] - {4} [7, 4, 4, 3] - - {5} [3, 0, 3, 0] {6} [0, 1, 0, 4] - - - - - - - - - - - {3} [7, 10, 3, 6] - {4} [2, 19, 9, 4] - - - - {4} [13, 8, 6, 7] - - {5} [1, 9, 0, 2] - - - - - {4} [12, 16, 8, 4] - - - {5} [2, 0, 0, 3] - - - - - - {1} [0, 0, 0, 0] {2} [52, 54, 70, 27] {3} [12, 12, 8, 8] - - - {4} [0, 0, 3, 0] - - - - - - - {2} [24, 39, 57, 32] {3} [10, 14, 23, 7] - - - {4} [3, 0, 0, 3] - {5} [0, 4, 3, 0] - - - - - - - - {3} [4, 8, 3, 9] - {4} [2, 1, 13, 3] - - - - - - {2} [57, 106, 90, 47] - - - {3} [17, 27, 19, 9] - {4} [3, 11, 6, 4] - {5} [2, 3, 5, 4] - {6} [3, 4, 0, 0] - - - - - - - - - - {2} [27, 42, 63, 25] {3} [12, 13, 19, 12] {4} [2, 6, 1, 3] - {5} [0, 0, 3, 0] - - - - - - - - - - {3} [10, 15, 19, 5] - - {4} [2, 4, 12, 3] - - {5} [2, 4, 0, 2] - - - - - - - {1} [0, 0, 0, 0] {2} [8, 28, 52, 14] - - {3} [1, 7, 12, 10] {4} [2, 7, 7, 0] - - - - - - - - {2} [11, 21, 41, 7] {3} [13, 7, 6, 12] - - - - {3} [5, 22, 31, 12] - {4} [2, 7, 12, 4] - - - {5} [1, 0, 0, 3] - - - - - - - {3} [4, 15, 15, 7] - - {4} [0, 6, 0, 0] - {5} [0, 0, 0, 3] - - - - {5} [0, 0, 3, 0] - - - - - - {2} [6, 18, 40, 25] - - {3} [8, 21, 29, 6] - - - - {3} [4, 15, 9, 22] - - - - {2} [9, 34, 26, 32] {3} [7, 11, 4, 2] - - - - - - {3} [5, 10, 19, 3] - - - - > ## End Don't show > > > > cleanEx(); ..nameEx <- "vlmc" > > ### * vlmc > > flush(stderr()); flush(stdout()) > > ### Encoding: latin1 > > ### Name: vlmc > ### Title: Fit a Variable Length Markov Chain (VLMC) > ### Aliases: vlmc print.vlmc is.vlmc > ### Keywords: ts models > > ### ** Examples > > f1 <- c(1,0,0,0) > f2 <- rep(1:0,2) > (dt1 <- c(f1,f1,f2,f1,f2,f2,f1)) [1] 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 > > (vlmc.dt1 <- vlmc(dt1)) `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 > vlmc(dt1, dump = 1, + ctl.dump = c(wid = 3, nmax = 20), debug = TRUE) vlmc: Alpha = '01' ; |X| = 2 vlmc: ctl.dump = 3 20 vlmc: n = |data| = 28, cutoff{prune} = 1.92073, threshold{gen} = 2 vlmc: |alphabet| = 2, alphabet = 01 generating... pruning... Dump{Tree} __after__ pruning: Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+-------------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 22 24 26 27 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 computing differences[`completing'] ... writing tree... [0]01 0 0 0 [1]1 4 6 [2]2 0 0 [3]3 0 3 - -1 - -1 [3]3 4 0 - -1 - -1 - -1 [1]1 10 0 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, debug = TRUE, dump = 1, ctl.dump = c(wid = 3, nmax = 20)); default cutoff = 1.921 -> extensions (= $size ) : ord.MC context nr.leaves total 3 4 3 26 AIC = 21.46 > (vlmc.dt1c01 <- vlmc(dts = dt1, cutoff.prune = .1, dump=1)) Lev Ch| 0 1 | tot | num size : ..{set->list}.. ------+--------------------------------------------- 0 x | 18 10 | 28 | 28 32 : 0 1 2 3 4 5 6 7 8 9 10 11 12 .. 1 0 | 8 9 | 17 | 17 32 : 2 3 4 6 7 8 10 12 14 15 16 18 20 .. 2 0 | 4 3 | 7 | 7 16 : 3 4 7 8 15 16 27 3 0 | 0 3 | 3 | 3 16 : 4 8 16 3 1 | 4 0 | 4 | 4 16 : 3 7 15 27 2 1 | 4 6 | 10 | 10 16 : 2 6 10 12 14 18 20 22 24 26 3 0 | 3 6 | 9 | 9 16 : 6 10 12 14 18 20 22 24 26 4 0 | 1 2 | 3 | 3 16 : 6 10 18 5 0 | 1 2 | 3 | 3 16 : 6 10 18 6 1 | 1 2 | 3 | 3 16 : 6 10 18 7 0 | 0 2 | 2 | 2 16 : 10 18 4 1 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 5 0 | 2 4 | 6 | 6 16 : 12 14 20 22 24 26 6 0 | 0 2 | 2 | 2 16 : 12 20 6 1 | 2 2 | 4 | 4 16 : 14 22 24 26 1 1 | 10 0 | 10 | 10 16 : 1 5 9 11 13 17 19 21 23 25 `vlmc', a Variable Length Markov Chain; alphabet '01', |alphabet| = 2, n = 28. Call: vlmc(dts = dt1, cutoff.prune = 0.1, dump = 1) -> extensions (= $size ) : ord.MC context nr.leaves total 7 11 6 66 AIC = 27.55 > > data(presidents) > dpres <- cut(presidents, c(0,45,70, 100)) # three values + NA > table(dpres <- factor(dpres, exclude = NULL)) # NA as 4th level (0,45] (45,70] (70,100] 28 60 26 6 > vlmc.pres <- vlmc(dpres, debug = TRUE) Warning in vlmc(dpres, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 3.90736, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 1 2 1 2 [1]1 18 7 0 2 - -1 - -1 - -1 - -1 [1]1 9 33 5 1 - -1 - -1 [2]2 0 6 6 0 - -1 - -1 - -1 - -1 - -1 [1]1 0 12 14 0 - -1 - -1 - -1 - -1 - -1 > vlmc.pres `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, debug = TRUE); default cutoff = 3.907 -> extensions (= $size ) : ord.MC context nr.leaves total 2 5 3 42 AIC = 227.6 > > ## alphabet & and its length: > vlmc.pres$alpha [1] "0123" > stopifnot( + length(print(strsplit(vlmc.pres$alpha,NULL)[[1]])) == vlmc.pres$ alpha.len + ) [1] "0" "1" "2" "3" > > > > cleanEx(); ..nameEx <- "vlmc.version" > > ### * vlmc.version > > flush(stderr()); flush(stdout()) > > ### Name: vlmc.version > ### Title: Version of VLMC Package > ### Aliases: vlmc.version > ### Keywords: data > > ### ** Examples > > vlmc.version [1] "VLMC 1.3-8; after $Date: 2005/03/25 13:48:33 $ UTC" > ## Not run: > ##D [1] "VLMC 1.3-7; after $Date: 2004/05/05 16:41:47 $ UTC" > ## End(Not run) > > > > cleanEx(); ..nameEx <- "vlmctree" > > ### * vlmctree > > flush(stderr()); flush(stdout()) > > ### Name: vlmctree > ### Title: Compute the tree structure of a "vlmc" object > ### Aliases: vlmctree .vvec2tree str.vtree > ### Keywords: ts models > > ### ** Examples > > data(presidents) > dpres <- cut(presidents, c(0,45,70, 100)) # three values + NA > table(dpres <- factor(dpres, exclude = NULL)) # NA as 4th level (0,45] (45,70] (70,100] 28 60 26 6 > > (vlmc.prc1 <- vlmc(dpres, cut = 1, debug = TRUE)) Warning in vlmc(dpres, cut = 1, debug = TRUE) : alphabet with >1-letter strings; trying to abbreviate vlmc: Alpha = '0123' ; |X| = 4 vlmc: ctl.dump = 3.079181 -1 vlmc: n = |data| = 120, cutoff{prune} = 1, threshold{gen} = 2 vlmc: |alphabet| = 4, alphabet = 0123 generating... pruning... computing differences[`completing'] ... writing tree... [0]0123 0 0 0 0 0 [1]1 11 5 0 2 - -1 [2]2 3 2 0 0 - -1 [3]3 4 0 0 0 - -1 - -1 - -1 - -1 - -1 - -1 - -1 - -1 [1]1 1 1 0 0 [2]2 2 2 1 0 - -1 [3]3 2 0 0 0 - -1 - -1 - -1 - -1 - -1 - -1 [2]2 1 7 1 0 - -1 [3]3 0 1 0 0 [4]4 1 1 0 0 - -1 - -1 - -1 - -1 [4]4 1 3 0 0 - -1 [5]5 0 3 0 0 - -1 [6]6 0 12 1 1 - -1 - -1 [7]7 1 1 0 0 - -1 - -1 - -1 - -1 - -1 - -1 - -1 - -1 - -1 [4]4 0 2 2 0 - -1 - -1 - -1 - -1 - -1 - -1 - -1 [2]2 0 4 3 0 - -1 [3]3 0 0 0 0 - -1 [4]4 0 2 0 0 - -1 - -1 - -1 - -1 [4]4 0 0 3 0 - -1 - -1 - -1 - -1 - -1 - -1 - -1 - -1 [1]1 0 5 7 0 - -1 - -1 [2]2 0 4 3 0 - -1 - -1 [3]3 0 3 2 0 - -1 [4]4 0 0 2 0 - -1 - -1 - -1 - -1 - -1 - -1 - -1 - -1 [1]1 0 0 1 1 [2]2 1 0 0 1 - -1 - -1 - -1 - -1 - -1 - -1 [2]2 0 2 0 0 - -1 - -1 - -1 - -1 `vlmc', a Variable Length Markov Chain; alphabet '0123', |alphabet| = 4, n = 120. Call: vlmc(dts = dpres, cutoff.prune = 1, debug = TRUE) -> extensions (= $size ) : ord.MC context nr.leaves total 7 25 10 210 AIC = 292.3 > str(vv.prc1 <- vlmctree(vlmc.prc1)) `vtree': {7} 0 0 0 0 | 0 ; 4 children .. {2} 11 5 0 2 | 18 ; 4 children .. .. {1} 3 2 0 0 | 5 ; 4 children .. .. .. {0} 4 0 0 0 | 4 ; _leaf_ .. {6} 1 1 0 0 | 2 ; 4 children .. .. {1} 2 2 1 0 | 5 ; 4 children .. .. .. {0} 2 0 0 0 | 2 ; _leaf_ .. .. {5} 1 7 1 0 | 9 ; 4 children .. .. .. {4} 0 1 0 0 | 1 ; 4 children .. .. .. .. {0} 1 1 0 0 | 2 ; _leaf_ .. .. .. .. {3} 1 3 0 0 | 4 ; 4 children .. .. .. .. .. {2} 0 3 0 0 | 3 ; 4 children .. .. .. .. .. .. {1} 0 12 1 1 | 14 ; 4 children .. .. .. .. .. .. .. {0} 1 1 0 0 | 2 ; _leaf_ .. .. .. .. {0} 0 2 2 0 | 4 ; _leaf_ .. .. {2} 0 4 3 0 | 7 ; 4 children .. .. .. {1} 0 0 0 0 | 0 ; 4 children .. .. .. .. {0} 0 2 0 0 | 2 ; _leaf_ .. .. .. .. {0} 0 0 3 0 | 3 ; _leaf_ .. {3} 0 5 7 0 | 12 ; 4 children .. .. {2} 0 4 3 0 | 7 ; 4 children .. .. .. {1} 0 3 2 0 | 5 ; 4 children .. .. .. .. {0} 0 0 2 0 | 2 ; _leaf_ .. {1} 0 0 1 1 | 2 ; 4 children .. .. {0} 1 0 0 1 | 2 ; _leaf_ .. .. {0} 0 2 0 0 | 2 ; _leaf_ > > > > ### *