Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Created October 4, 2017 01:16
Show Gist options
  • Save dgrtwo/fceac42cd4dafc55ebab4f119d8e607f to your computer and use it in GitHub Desktop.
Save dgrtwo/fceac42cd4dafc55ebab4f119d8e607f to your computer and use it in GitHub Desktop.
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(purrr)
transition_mc <- function(steps, start, mat) {
i <- seq_len(nrow(mat))
transition <- ~ sample(i, 1, prob = (i == .) %*% mat)
accumulate(seq_len(steps), transition, .init = start)
}
# Example: 1 wants to go to 2, 2 could go back to 1, 3 is absorbing
transition_matrix <- rbind(
c(.1, .9, 0),
c(.45, .45, .1),
c(0, 0, 1)
)
# 30 steps starting in state 2
transition_mc(30, 2, transition_matrix)
```
```{r}
library(dplyr)
library(tidyr)
library(ggplot2)
theme_set(theme_light())
walks <- data_frame(trial = 1:50) %>%
mutate(state = map(trial, ~ transition_mc(25, 2, transition_matrix)),
step = map(trial, ~ seq_len(26))) %>%
unnest()
walks %>%
ggplot(aes(step, trial, fill = factor(state))) +
geom_tile()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment