Last data update: 2014.03.03

R: Generates random indexes with a specified probability...
randindxR Documentation

Generates random indexes with a specified probability distribution

Description

Returns an array of T indexes distributed as specified by p (which should be a normalized probability vector).

Usage

randindx(p, Total, NO_CHK)

Arguments

p

A row vector of normalized probabilities that dictate the transition probability from the current state to the next state. For example, p = [0.2, 0.8] indicates that the current state transitoins to state 1 at 0.2 and 2 at 0.8. The current state itself can either be the state 1 or 2.

Total

Total number of states needed to be generated using the input transition vector.

NO_CHK

Check whether the first argument is a valid row vector of normalized probabilities.

Details

The function is used by nbh_gen to generate random data point based on the user-supplied transition probability matrix.

Value

I

Index/Indices or state(s) sampled following the transition.

probability.

Author(s)

Yue Li

References

Capp'e, O. (2001). H2M : A set of MATLAB/OCTAVE functions for the EM estimation of mixtures and hidden Markov models. (http://perso.telecom-paristech.fr/cappe/h2m/)

See Also

nbh_gen

Examples

# Total contains the length of data to simulate
Total <- 100

# number of states
N <- 2

# transition probabilities between states
TRANS <- matrix(c(0.9, 0.1, 0.3, 0.7), nrow=2, byrow=TRUE)

label <- matrix(0, Total, 1)

# Simulate initial state
label[1] <- randindx(matrix(1,ncol=N)/N, 1, 1)

# Use Markov property for the following time index
for(t in 2:Total) {
	
	label[t] <- randindx(TRANS[label[t-1],], 1, 1)
}	

plot(label)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

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 an HTML browser interface to help.
Type 'q()' to quit R.

> library(RIPSeeker)
Loading required package: S4Vectors
Loading required package: stats4
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit


Attaching package: 'S4Vectors'

The following objects are masked from 'package:base':

    colMeans, colSums, expand.grid, rowMeans, rowSums

Loading required package: IRanges
Loading required package: GenomicRanges
Loading required package: GenomeInfoDb
Loading required package: SummarizedExperiment
Loading required package: Biobase
Welcome to Bioconductor

    Vignettes contain introductory material; view with
    'browseVignettes()'. To cite Bioconductor, see
    'citation("Biobase")', and for packages 'citation("pkgname")'.

Loading required package: Rsamtools
Loading required package: Biostrings
Loading required package: XVector
Loading required package: GenomicAlignments
Loading required package: rtracklayer
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/RIPSeeker/randindx.Rd_%03d_medium.png", width=480, height=480)
> ### Name: randindx
> ### Title: Generates random indexes with a specified probability
> ###   distribution
> ### Aliases: randindx
> 
> ### ** Examples
> 
> # Total contains the length of data to simulate
> Total <- 100
> 
> # number of states
> N <- 2
> 
> # transition probabilities between states
> TRANS <- matrix(c(0.9, 0.1, 0.3, 0.7), nrow=2, byrow=TRUE)
> 
> label <- matrix(0, Total, 1)
> 
> # Simulate initial state
> label[1] <- randindx(matrix(1,ncol=N)/N, 1, 1)
> 
> # Use Markov property for the following time index
> for(t in 2:Total) {
+ 	
+ 	label[t] <- randindx(TRANS[label[t-1],], 1, 1)
+ }	
> 
> plot(label)
> 
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>