x1 <- c(0.9, 0.1) x2 <- c(0.3, 0.7) tp <- rbind(x1, x2) #危襯 ss <- rbind(c(100,0)) #豐 for(t in seq(1:30)){ prev_tp <- ss%*%tp out <- paste(t, "殊姶: ", "蟲覯:", round(prev_tp[1,1], 3), "覯:", round(prev_tp[1,2], 3)) ss <- prev_tp print (out) }
[1] "1 殊姶: 蟲覯: 90 覯: 10" [1] "2 殊姶: 蟲覯: 84 覯: 16" [1] "3 殊姶: 蟲覯: 80.4 覯: 19.6" [1] "4 殊姶: 蟲覯: 78.24 覯: 21.76" [1] "5 殊姶: 蟲覯: 76.944 覯: 23.056" [1] "6 殊姶: 蟲覯: 76.166 覯: 23.834" [1] "7 殊姶: 蟲覯: 75.7 覯: 24.3" [1] "8 殊姶: 蟲覯: 75.42 覯: 24.58" [1] "9 殊姶: 蟲覯: 75.252 覯: 24.748" [1] "10 殊姶: 蟲覯: 75.151 覯: 24.849" [1] "11 殊姶: 蟲覯: 75.091 覯: 24.909" [1] "12 殊姶: 蟲覯: 75.054 覯: 24.946" [1] "13 殊姶: 蟲覯: 75.033 覯: 24.967" [1] "14 殊姶: 蟲覯: 75.02 覯: 24.98" [1] "15 殊姶: 蟲覯: 75.012 覯: 24.988" [1] "16 殊姶: 蟲覯: 75.007 覯: 24.993" [1] "17 殊姶: 蟲覯: 75.004 覯: 24.996" [1] "18 殊姶: 蟲覯: 75.003 覯: 24.997" [1] "19 殊姶: 蟲覯: 75.002 覯: 24.998" [1] "20 殊姶: 蟲覯: 75.001 覯: 24.999" [1] "21 殊姶: 蟲覯: 75.001 覯: 24.999" [1] "22 殊姶: 蟲覯: 75 覯: 25" [1] "23 殊姶: 蟲覯: 75 覯: 25" [1] "24 殊姶: 蟲覯: 75 覯: 25" [1] "25 殊姶: 蟲覯: 75 覯: 25" [1] "26 殊姶: 蟲覯: 75 覯: 25" [1] "27 殊姶: 蟲覯: 75 覯: 25" [1] "28 殊姶: 蟲覯: 75 覯: 25" [1] "29 殊姶: 蟲覯: 75 覯: 25" [1] "30 殊姶: 蟲覯: 75 覯: 25"
x1 <- c(0.9, 0.1) x2 <- c(0.0, 1.0) tp <- rbind(x1, x2) #危襯 ss <- rbind(c(100,0)) #豐 for(t in seq(1:120)){ prev_tp <- ss%*%tp out <- paste(t, "殊姶: ", "蟲覯:", round(prev_tp[1,1], 3), "覯:", round(prev_tp[1,2], 3)) ss <- prev_tp print (out) } [1] "1 殊姶: 蟲覯: 90 覯: 10" [1] "2 殊姶: 蟲覯: 81 覯: 19" [1] "3 殊姶: 蟲覯: 72.9 覯: 27.1" [1] "4 殊姶: 蟲覯: 65.61 覯: 34.39" [1] "5 殊姶: 蟲覯: 59.049 覯: 40.951" [1] "6 殊姶: 蟲覯: 53.144 覯: 46.856" [1] "7 殊姶: 蟲覯: 47.83 覯: 52.17" [1] "8 殊姶: 蟲覯: 43.047 覯: 56.953" [1] "9 殊姶: 蟲覯: 38.742 覯: 61.258" [1] "10 殊姶: 蟲覯: 34.868 覯: 65.132" [1] "11 殊姶: 蟲覯: 31.381 覯: 68.619" [1] "12 殊姶: 蟲覯: 28.243 覯: 71.757" [1] "13 殊姶: 蟲覯: 25.419 覯: 74.581" [1] "14 殊姶: 蟲覯: 22.877 覯: 77.123" [1] "15 殊姶: 蟲覯: 20.589 覯: 79.411" [1] "16 殊姶: 蟲覯: 18.53 覯: 81.47" [1] "17 殊姶: 蟲覯: 16.677 覯: 83.323" [1] "18 殊姶: 蟲覯: 15.009 覯: 84.991" [1] "19 殊姶: 蟲覯: 13.509 覯: 86.491" [1] "20 殊姶: 蟲覯: 12.158 覯: 87.842" [1] "21 殊姶: 蟲覯: 10.942 覯: 89.058" [1] "22 殊姶: 蟲覯: 9.848 覯: 90.152" [1] "23 殊姶: 蟲覯: 8.863 覯: 91.137" [1] "24 殊姶: 蟲覯: 7.977 覯: 92.023" [1] "25 殊姶: 蟲覯: 7.179 覯: 92.821" [1] "26 殊姶: 蟲覯: 6.461 覯: 93.539" [1] "27 殊姶: 蟲覯: 5.815 覯: 94.185" [1] "28 殊姶: 蟲覯: 5.233 覯: 94.767" [1] "29 殊姶: 蟲覯: 4.71 覯: 95.29" [1] "30 殊姶: 蟲覯: 4.239 覯: 95.761" [1] "31 殊姶: 蟲覯: 3.815 覯: 96.185" [1] "32 殊姶: 蟲覯: 3.434 覯: 96.566" [1] "33 殊姶: 蟲覯: 3.09 覯: 96.91" [1] "34 殊姶: 蟲覯: 2.781 覯: 97.219" [1] "35 殊姶: 蟲覯: 2.503 覯: 97.497" [1] "36 殊姶: 蟲覯: 2.253 覯: 97.747" [1] "37 殊姶: 蟲覯: 2.028 覯: 97.972" [1] "38 殊姶: 蟲覯: 1.825 覯: 98.175" [1] "39 殊姶: 蟲覯: 1.642 覯: 98.358" [1] "40 殊姶: 蟲覯: 1.478 覯: 98.522" [1] "41 殊姶: 蟲覯: 1.33 覯: 98.67" [1] "42 殊姶: 蟲覯: 1.197 覯: 98.803" [1] "43 殊姶: 蟲覯: 1.078 覯: 98.922" [1] "44 殊姶: 蟲覯: 0.97 覯: 99.03" [1] "45 殊姶: 蟲覯: 0.873 覯: 99.127" [1] "46 殊姶: 蟲覯: 0.786 覯: 99.214" [1] "47 殊姶: 蟲覯: 0.707 覯: 99.293" [1] "48 殊姶: 蟲覯: 0.636 覯: 99.364" [1] "49 殊姶: 蟲覯: 0.573 覯: 99.427" [1] "50 殊姶: 蟲覯: 0.515 覯: 99.485" [1] "51 殊姶: 蟲覯: 0.464 覯: 99.536" [1] "52 殊姶: 蟲覯: 0.417 覯: 99.583" [1] "53 殊姶: 蟲覯: 0.376 覯: 99.624" [1] "54 殊姶: 蟲覯: 0.338 覯: 99.662" [1] "55 殊姶: 蟲覯: 0.304 覯: 99.696" [1] "56 殊姶: 蟲覯: 0.274 覯: 99.726" [1] "57 殊姶: 蟲覯: 0.247 覯: 99.753" [1] "58 殊姶: 蟲覯: 0.222 覯: 99.778" [1] "59 殊姶: 蟲覯: 0.2 覯: 99.8" [1] "60 殊姶: 蟲覯: 0.18 覯: 99.82" [1] "61 殊姶: 蟲覯: 0.162 覯: 99.838" [1] "62 殊姶: 蟲覯: 0.146 覯: 99.854" [1] "63 殊姶: 蟲覯: 0.131 覯: 99.869" [1] "64 殊姶: 蟲覯: 0.118 覯: 99.882" [1] "65 殊姶: 蟲覯: 0.106 覯: 99.894" [1] "66 殊姶: 蟲覯: 0.096 覯: 99.904" [1] "67 殊姶: 蟲覯: 0.086 覯: 99.914" [1] "68 殊姶: 蟲覯: 0.077 覯: 99.923" [1] "69 殊姶: 蟲覯: 0.07 覯: 99.93" [1] "70 殊姶: 蟲覯: 0.063 覯: 99.937" [1] "71 殊姶: 蟲覯: 0.056 覯: 99.944" [1] "72 殊姶: 蟲覯: 0.051 覯: 99.949" [1] "73 殊姶: 蟲覯: 0.046 覯: 99.954" [1] "74 殊姶: 蟲覯: 0.041 覯: 99.959" [1] "75 殊姶: 蟲覯: 0.037 覯: 99.963" [1] "76 殊姶: 蟲覯: 0.033 覯: 99.967" [1] "77 殊姶: 蟲覯: 0.03 覯: 99.97" [1] "78 殊姶: 蟲覯: 0.027 覯: 99.973" [1] "79 殊姶: 蟲覯: 0.024 覯: 99.976" [1] "80 殊姶: 蟲覯: 0.022 覯: 99.978" [1] "81 殊姶: 蟲覯: 0.02 覯: 99.98" [1] "82 殊姶: 蟲覯: 0.018 覯: 99.982" [1] "83 殊姶: 蟲覯: 0.016 覯: 99.984" [1] "84 殊姶: 蟲覯: 0.014 覯: 99.986" [1] "85 殊姶: 蟲覯: 0.013 覯: 99.987" [1] "86 殊姶: 蟲覯: 0.012 覯: 99.988" [1] "87 殊姶: 蟲覯: 0.01 覯: 99.99" [1] "88 殊姶: 蟲覯: 0.009 覯: 99.991" [1] "89 殊姶: 蟲覯: 0.008 覯: 99.992" [1] "90 殊姶: 蟲覯: 0.008 覯: 99.992" [1] "91 殊姶: 蟲覯: 0.007 覯: 99.993" [1] "92 殊姶: 蟲覯: 0.006 覯: 99.994" [1] "93 殊姶: 蟲覯: 0.006 覯: 99.994" [1] "94 殊姶: 蟲覯: 0.005 覯: 99.995" [1] "95 殊姶: 蟲覯: 0.004 覯: 99.996" [1] "96 殊姶: 蟲覯: 0.004 覯: 99.996" [1] "97 殊姶: 蟲覯: 0.004 覯: 99.996" [1] "98 殊姶: 蟲覯: 0.003 覯: 99.997" [1] "99 殊姶: 蟲覯: 0.003 覯: 99.997" [1] "100 殊姶: 蟲覯: 0.003 覯: 99.997" [1] "101 殊姶: 蟲覯: 0.002 覯: 99.998" [1] "102 殊姶: 蟲覯: 0.002 覯: 99.998" [1] "103 殊姶: 蟲覯: 0.002 覯: 99.998" [1] "104 殊姶: 蟲覯: 0.002 覯: 99.998" [1] "105 殊姶: 蟲覯: 0.002 覯: 99.998" [1] "106 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "107 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "108 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "109 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "110 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "111 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "112 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "113 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "114 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "115 殊姶: 蟲覯: 0.001 覯: 99.999" [1] "116 殊姶: 蟲覯: 0 覯: 100" [1] "117 殊姶: 蟲覯: 0 覯: 100" [1] "118 殊姶: 蟲覯: 0 覯: 100" [1] "119 殊姶: 蟲覯: 0 覯: 100" [1] "120 殊姶: 蟲覯: 0 覯: 100"
企 譬覲 ル 豺襭 轟覲 譴 磯 朱覲朱 企蟇磯 伎, 伎 蟆曙 譴 85% 豺襭 伎覃 15% 襷 蟆曙一企. 豺襭 伎 譴 朱 襦 襦 れ 蠍磯 覃 豺襭 ル 螳 殊螻, 1 危襯 螳り . |
x1 <- c(0.7, 0.2, 0.1) x2 <- c(0.1, 0.7, 0.2) x3 <- c(0.02, 0.01, 0.97) tp <- rbind(x1, x2, x3) #危襯 ss <- rbind(c(1,0,0)) #豐 for(t in seq(1:50)){ prev_tp <- ss%*%tp out <- paste(t, "殊姶: ", "轟覲:", round(prev_tp[1,1], 3), "朱覲:", round(prev_tp[1,2], 3), "伎/襷:", round(prev_tp[1,3], 3) ) ss <- prev_tp print (out) }
[1] "1 殊姶: 轟覲: 0.7 朱覲: 0.2 伎/襷: 0.1" [1] "2 殊姶: 轟覲: 0.512 朱覲: 0.281 伎/襷: 0.207" [1] "3 殊姶: 轟覲: 0.391 朱覲: 0.301 伎/襷: 0.308" [1] "4 殊姶: 轟覲: 0.31 朱覲: 0.292 伎/襷: 0.398" [1] "5 殊姶: 轟覲: 0.254 朱覲: 0.27 伎/襷: 0.476" [1] "6 殊姶: 轟覲: 0.214 朱覲: 0.245 伎/襷: 0.541" [1] "7 殊姶: 轟覲: 0.185 朱覲: 0.22 伎/襷: 0.595" [1] "8 殊姶: 轟覲: 0.164 朱覲: 0.197 伎/襷: 0.64" [1] "9 殊姶: 轟覲: 0.147 朱覲: 0.177 伎/襷: 0.676" [1] "10 殊姶: 轟覲: 0.134 朱覲: 0.16 伎/襷: 0.706" [1] "11 殊姶: 轟覲: 0.124 朱覲: 0.146 伎/襷: 0.73" [1] "12 殊姶: 轟覲: 0.116 朱覲: 0.134 伎/襷: 0.75" [1] "13 殊姶: 轟覲: 0.11 朱覲: 0.125 伎/襷: 0.766" [1] "14 殊姶: 轟覲: 0.104 朱覲: 0.117 伎/襷: 0.779" [1] "15 殊姶: 轟覲: 0.1 朱覲: 0.11 伎/襷: 0.789" [1] "16 殊姶: 轟覲: 0.097 朱覲: 0.105 伎/襷: 0.798" [1] "17 殊姶: 轟覲: 0.094 朱覲: 0.101 伎/襷: 0.804" [1] "18 殊姶: 轟覲: 0.092 朱覲: 0.098 伎/襷: 0.81" [1] "19 殊姶: 轟覲: 0.091 朱覲: 0.095 伎/襷: 0.814" [1] "20 殊姶: 轟覲: 0.089 朱覲: 0.093 伎/襷: 0.818" [1] "21 殊姶: 轟覲: 0.088 朱覲: 0.091 伎/襷: 0.821" [1] "22 殊姶: 轟覲: 0.087 朱覲: 0.089 伎/襷: 0.823" [1] "23 殊姶: 轟覲: 0.086 朱覲: 0.088 伎/襷: 0.825" [1] "24 殊姶: 轟覲: 0.086 朱覲: 0.087 伎/襷: 0.827" [1] "25 殊姶: 轟覲: 0.085 朱覲: 0.087 伎/襷: 0.828" [1] "26 殊姶: 轟覲: 0.085 朱覲: 0.086 伎/襷: 0.829" [1] "27 殊姶: 轟覲: 0.085 朱覲: 0.085 伎/襷: 0.83" [1] "28 殊姶: 轟覲: 0.084 朱覲: 0.085 伎/襷: 0.831" [1] "29 殊姶: 轟覲: 0.084 朱覲: 0.085 伎/襷: 0.831" [1] "30 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.832" [1] "31 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.832" [1] "32 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.832" [1] "33 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.832" [1] "34 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.833" [1] "35 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.833" [1] "36 殊姶: 轟覲: 0.084 朱覲: 0.084 伎/襷: 0.833" [1] "37 殊姶: 轟覲: 0.083 朱覲: 0.084 伎/襷: 0.833" [1] "38 殊姶: 轟覲: 0.083 朱覲: 0.084 伎/襷: 0.833" [1] "39 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "40 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "41 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "42 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "43 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "44 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "45 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "46 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "47 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "48 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "49 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833" [1] "50 殊姶: 轟覲: 0.083 朱覲: 0.083 伎/襷: 0.833"ル 8.3% 螳螳 轟覲螻 朱覲 豺襭譴企 83.34% 襷蟇磯 豺襭 伎 手 豢 .
install.packages("markovchain") library("markovchain") statesNames=c("轟覲","朱覲", "伎/襷") mt <- matrix(c(0.7,0.2,0.1,0.1,0.7,0.2,0.02,0.01,0.97), byrow=TRUE, nrow=3) mc<-new("markovchain", transitionMatrix=mt, states=statesNames) mc^2 #2覯讌 螻 steadyStates(mc)
> mc^2 A Markov chain^2 A 3 - dimensional discrete Markov Chain with following states 轟覲 朱覲 伎/襷 The transition matrix (by rows) is defined as follows 轟覲 朱覲 伎/襷 轟覲 0.5120 0.2810 0.2070 朱覲 0.1440 0.5120 0.3440 伎/襷 0.0344 0.0207 0.9449 > steadyStates(mcA) 轟覲 朱覲 伎/襷 [1,] 0.08333333 0.08333333 0.8333333
raw <- data.frame(name=c("f1","f1","f1","f1","f2","f2","f2","f2"), year=c(83, 84, 85, 86, 83, 84, 85, 86), state=sample(1:3, 8, replace=TRUE) ) transition.probabilities <- function(D, timevar="year", idvar="name", statevar="state") { merged <- merge(D, cbind(nextt=D[,timevar] + 1, D), by.x = c(timevar, idvar), by.y = c("nextt", idvar)) t(table(merged[, grep(statevar, names(merged), value = TRUE)])) } transition.probabilities(raw, timevar="year", idvar="name",statevar="state")
install.packages("markovchain") library("markovchain") sequence<-c("a", "b", "a", "a", "a", "a", "b", "a", "b", "a", "b", "a", "a", "b", "b", "b", "a") mcFitMLE<-markovchainFit(data=sequence) #str(mcFitMLE) #mcFitMLE$estimate@transitionMatrix #mcFitMLE$estimate@states markov<-new("markovchain", states=mcFitMLE$estimate@states, transitionMatrix=mcFitMLE$estimate@transitionMatrix) plotMc(markov)
R <- matrix(c(0.2,0, 0.3,0,0.5,0.5), byrow=TRUE, nrow=3) Q <- matrix(c(0,0.8,0,0,0,0.7,0,0,0), byrow=TRUE, nrow=3) IQ <- diag(rep(1,3)) - Q IQ iIQ<-ginv(IQ) iIQ %*% R rowSums(iIQ)
觜 | ||
觜 | 0.7 | 0.3 |
0.4 | 0.6 |
#install.packages("RHmm") library("RHmm") weatherTransitions <- rbind( c(0.7, 0.3), c(0.4, 0.6) ) s1 <- c(0.1, 0.4, 0.5) s2 <- c(0.6, 0.3, 0.1) dist <- distributionSet(dis="DISCRETE", proba=list(s1, s2), labels =c("一", "狩", "豌")) dist weatherHmm <- HMMSet(initProb=c(0.6, 0.4), transMat=weatherTransitions, distribution=dist) weatherPath <- viterbi(HMM=weatherHmm, obs=c("一", "一", "豌", "狩")) weatherPath
> weatherPath $states [1] 2 2 1 1 $logViterbiScore [1] -5.331171 $logProbSeq [1] -0.8306799 attr(,"class") [1] "viterbiClass" >
library("msm") data("cav") str(cav) cav <- cav[!is.na(cav$pdiag),] cav[1:11,] m <- statetable.msm(state, PTNUM, data = cav) class(m) twoway4.q <- rbind(c(0, 0.25, 0, 0.25), c(0.166, 0, 0.166, 0.166), c(0, 0.25, 0, 0.25), c(0, 0, 0, 0)) rownames(twoway4.q) <- colnames(twoway4.q) <- c("Well", "Mild", "Severe", "Death") cav.msm <- msm(state ~ years, subject = PTNUM, data = cav, qmatrix = twoway4.q, death = 4) #pmatrix.msm(cav.msm, t = 1, ci = "normal") pmatrix.msm(cav.msm, t = 1, ci = "none")
> pmatrix.msm(cav.msm, t = 1, ci = "none") Well Mild Severe Death Well 0.853040629 0.08916579 0.01486643 0.04292715 Mild 0.156269251 0.56585635 0.20550354 0.07237086 Severe 0.009996569 0.07884756 0.66057662 0.25057925 Death 0.000000000 0.00000000 0.00000000 1.00000000
觜 | |
螳 | 50% |
螳->危 | 30% |
螳->蟲襷->蟲襷->危 | 10% |
螳->蟲襷 | 5% |
螳->蟲襷->危 | 3% |
螳->蟲襷->蟲襷->蟲襷 | 2% |