Simulate a random walk with purrr in R

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:16:15

问题


Can purrr simulate a random walk in fewer lines of code than base R?

For example what would the following look like in purrr?

n <- 5
x <- numeric(n)
for(i in 2:n){
  x[i] <- x[i-1] + rnorm(1)
}

Edit: @Imo had exactly what I was looking for. To take this one step further I am assuming a random walk can be created and plotted with the following code

tibble(x = 1:1000, y = cumsum(rnorm(1000, mean = 0))) %>% 
  ggplot(aes(x=x,y=y))+
  geom_point()+
  geom_line()

来源:https://stackoverflow.com/questions/45883887/simulate-a-random-walk-with-purrr-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!