我正在使用R包点击流来估算转换概率。
输出如下:
Higher-Order Markov Chain (order=2)
Transition Probabilities:
Lag: 1
lambda: 1
A Conversion D
A 0,25999823819591261209183 0 0,0605291772381297554784
Conversion 0,02034883720930232703372 0 0,0021747009786154403166
D 0,00162966878083157162491 0 0,6451612903225806272545
E 0,00000000000000000000000 0 0,0000000000000000000000
F 0,00132135306553911205212 0 0,0007249003262051467722
N 0,00603417899929527875441 0 0,0181225081551286693049
Null 0,62182875264270609516615 1 0,1808626313881841196629
O 0,00792811839323467187901 0 0,0094237042406669080385
P 0,00017618040873854828084 0 0,0000000000000000000000
Start 0,00000000000000000000000 0 0,0000000000000000000000
T 0,08073467230443974462784 0 0,0830010873504893054164
E F N Null
A 0,035714285714285712303 0,0675675675675675713183 0,01131221719457013545618 0
Conversion 0,071428571428571424606 0,0405405405405405427910 0,00061703002879473468293 0
D 0,000000000000000000000 0,0022522522522522522327 0,00102838338132455776874 0
E 0,392857142857142849213 0,0000000000000000000000 0,00000000000000000000000 0
F 0,000000000000000000000 0,3355855855855855995884 0,00123406005758946936586 0
N 0,000000000000000000000 0,0090090090090090089309 0,09543397778691896371495 0
Null 0,214285714285714273819 0,4797297297297297147267 0,86960098724804602010607 0
O 0,000000000000000000000 0,0045045045045045044654 0,00143973673385438096298 0
P 0,000000000000000000000 0,0000000000000000000000 0,00020567667626491157001 0
Start 0,000000000000000000000 0,0000000000000000000000 0,00000000000000000000000 0
T 0,285714285714285698425 0,0608108108108108141865 0,01912793089263677484557 0
O P Start
A 0,05175117616309461709978 0,059999999999999997780 0,23964753542773373218289
Conversion 0,01568217459487715459421 0,020000000000000000416 0,00000000000000000000000
D 0,00209095661265028742379 0,000000000000000000000 0,01343022430996705478268
E 0,00026136957658128592797 0,000000000000000000000 0,00022068443701823799359
F 0,00156821745948771567626 0,000000000000000000000 0,00345213512192815144206
N 0,00836382645060114969515 0,020000000000000000416 0,06317880168350699376933
Null 0,62859383167799265379472 0,520000000000000017764 0,00000000000000000000000
O 0,20987976999477261919047 0,000000000000000000000 0,04090543671873768383218
P 0,00000000000000000000000 0,320000000000000006661 0,00045713204810920727824
Start 0,00000000000000000000000 0,000000000000000000000 0,00000000000000000000000
T 0,08180867746994249789516 0,059999999999999997780 0,63870805025299892676571
T
A 0,01751190166393681163504
Conversion 0,00991932891453763294909
D 0,00116337808256922864945
E 0,00003061521269919022833
F 0,00048984340318704365329
N 0,00254106265403278904630
Null 0,62369311310790331859266
O 0,00326052015246375932397
P 0,00000000000000000000000
Start 0,00000000000000000000000
T 0,34139023680867025545282
Lag: 2
lambda: 0
A Conversion D
A 0,31341719077568136464507 0 0,05353982300884956024856
Conversion 0,02154670393664104288689 0 0,00353982300884955747300
D 0,00384346610761705093084 0 0,63628318584070797658825
E 0,00000000000000000000000 0 0,00000000000000000000000
F 0,00279524807826694605267 0 0,00044247787610619468412
N 0,00815280689494526054906 0 0,01548672566371681394437
Null 0,48742138364779874493138 0 0,18008849557522124684716
O 0,01059864896342883715252 0 0,01061946902654867241900
P 0,00058234334963894716659 0 0,00000000000000000000000
Start 0,00000000000000000000000 0 0,00000000000000000000000
T 0,15164220824598181880383 0 0,10000000000000000555112
...
现在我所做的是将输出写入文本文件,然后以矩阵形式读取
sink("mc_output.txt")
mc
sink()
# Lag 1
mydat11 = read.table("mc_output.txt", skip = 6, nrows = 11)
mydat12 = read.table("mc_output.txt", skip = 18, nrows = 11)
mydat13 = read.table("mc_output.txt", skip = 30, nrows = 11)
mydat14 = read.table("mc_output.txt", skip = 42, nrows = 11)
...
当矩阵的尺寸发生变化时,这种方法非常容易出错。想法是直接从内存中读取输出。有没有人有任何聪明的解决方案如何实现呢?
答案 0 :(得分:2)
给出
library(clickstream)
clickstreams <- c("User1,h,c,c,p,c,h,c,p,p,c,p,p,o",
"User2,i,c,i,c,c,c,d",
"User3,h,i,c,i,c,p,c,c,p,c,c,i,d",
"User4,c,c,p,c,d",
"User5,h,c,c,p,p,c,p,p,p,i,p,o",
"User6,i,h,c,c,p,p,c,p,c,d")
csf <- tempfile()
writeLines(clickstreams, csf)
cls <- readClickstreams(csf, header = TRUE)
mc <- fitMarkovChain(cls)
您可以使用
访问转换矩阵mc@transitions[[1]]
# c d h i o p
# c 0.32 0 0.8 0.5714286 0 0.47058824
# d 0.12 0 0.0 0.1428571 0 0.00000000
# h 0.04 0 0.0 0.1428571 0 0.00000000
# i 0.12 0 0.2 0.0000000 0 0.05882353
# o 0.00 0 0.0 0.0000000 0 0.11764706
# p 0.40 0 0.0 0.1428571 0 0.35294118
mc
对象也有其他一些插槽:
slotNames(mc)
# [1] "states" "order" "transitions" "lambda"
# [5] "logLikelihood" "observations" "start" "end"
# [9] "transientStates" "absorbingStates" "absorbingProbabilities"