## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5, fig.alt = "Random walk visualization examples" ) ## ----setup, echo=FALSE, message=FALSE----------------------------------------- library(RandomWalker) library(dplyr) library(ggplot2) ## ----install_example, eval=FALSE---------------------------------------------- # # From CRAN (stable) # install.packages("RandomWalker") # # # From GitHub (development) # devtools::install_github("spsanderson/RandomWalker") ## ----dependencies_example, eval=FALSE----------------------------------------- # install.packages(c("dplyr", "tidyr", "purrr", "rlang", "patchwork", "NNS", "ggiraph")) ## ----simple_walk_example------------------------------------------------------ library(RandomWalker) rw30() |> head(10) # Generates 30 walks with 100 steps each ## ----visualize_example, fig.alt="Visualization of random walks showing multiple panels"---- library(RandomWalker) rw30() |> visualize_walks() ## ----custom_walk_example, fig.alt="Custom normal random walk with specified parameters"---- random_normal_walk( .num_walks = 10, .n = 100, .mu = 0, .sd = 1, .initial_value = 0 ) |> visualize_walks() ## ----seed_example------------------------------------------------------------- set.seed(123) walks <- rw30() # Same seed produces same result set.seed(123) walks2 <- rw30() identical(walks, walks2) # TRUE ## ----custom_distribution_example, eval=FALSE---------------------------------- # # Custom displacement function # my_displacement <- function() { # # Your custom logic here # return(some_value) # } # # custom_walk( # .num_walks = 10, # .n = 100, # .custom_fns = my_displacement # ) ## ----twod_example------------------------------------------------------------- random_normal_walk(.num_walks = 10, .n = 100, .dimensions = 2) ## ----visualize_2d, fig.alt="2D random walk visualization with x-y coordinates"---- library(ggplot2) walk_2d <- random_normal_walk(.num_walks = 10, .n = 100, .dimensions = 2) ggplot(walk_2d, aes(x = cum_sum_x, y = cum_sum_y, color = walk_number)) + geom_path() + coord_equal() + theme_minimal() ## ----interactive_example, eval=FALSE------------------------------------------ # rw30() |> visualize_walks(.interactive = TRUE) ## ----pluck_example, fig.alt="Single panel visualization showing cumulative sum"---- # Single panel random_normal_walk() |> visualize_walks(.pluck = "cum_sum_y") ## ----pluck_multiple, fig.alt="Multiple panel visualization showing y, cumulative sum, and cumulative mean"---- # Multiple panels random_normal_walk() |> visualize_walks(.pluck = c("y", "cum_sum_y", "cum_mean_y")) ## ----alpha_example, eval=FALSE------------------------------------------------ # rw30() |> visualize_walks(.alpha = 0.3) # More transparent # rw30() |> visualize_walks(.alpha = 0.9) # More opaque ## ----export_example, eval=FALSE----------------------------------------------- # library(ggplot2) # # p <- rw30() |> visualize_walks() # ggsave("my_plot.png", p, width = 12, height = 8, dpi = 300) ## ----colors_example, fig.alt="Random walk with custom color palette"---------- p <- random_normal_walk(.num_walks = 5) |> visualize_walks(.pluck = "y") p + scale_color_viridis_d() ## ----summary_example---------------------------------------------------------- walks <- rw30() # Overall summary walks |> summarize_walks(.value = y) ## ----summary_by_walk---------------------------------------------------------- # By walk walks |> summarize_walks(.value = y, .group_var = walk_number) |> head() ## ----subset_example, fig.alt="Maximum and minimum walks visualization"-------- walks <- rw30() # Get walk with maximum final value max_walk <- walks |> subset_walks(.value = "y", .type = "max") # Get walk with minimum final value min_walk <- walks |> subset_walks(.value = "y", .type = "min") # Visualize both walks together combined <- dplyr::bind_rows( dplyr::mutate(max_walk, type = "Maximum"), dplyr::mutate(min_walk, type = "Minimum") ) visualize_walks(combined, .pluck = "y") + ggplot2::facet_wrap(~type) ## ----speed_example, eval=FALSE------------------------------------------------ # # Sample walks # walks_large |> # filter(walk_number %in% sample(levels(walk_number), 50)) |> # visualize_walks(.alpha = 0.2) # # # Downsample steps # walks_large |> # filter(step_number %% 10 == 0) |> # visualize_walks() ## ----parallel_example, eval=FALSE--------------------------------------------- # library(future) # library(furrr) # # plan(multisession, workers = 4) # # walks_list <- future_map(1:10, ~random_normal_walk(.num_walks = 100), .options = furrr_options(seed = 123)) ## ----attributes_example------------------------------------------------------- walks <- rw30() atb <- get_attributes(walks) names(atb) ## ----convert_example, eval=FALSE---------------------------------------------- # # To base R data.frame # as.data.frame(walks) # # # To matrix (values only) # walks |> select(y) |> as.matrix() # # # To time series # ts(walks$y, frequency = 1) # # # To wide format # walks |> tidyr::pivot_wider(names_from = walk_number, values_from = y) ## ----error_example1, eval=FALSE----------------------------------------------- # # Wrong # walks |> summarize_walks() # # # Correct # walks |> summarize_walks(.value = y) ## ----error_example2, eval=FALSE----------------------------------------------- # walk_2d <- random_normal_walk(.dimensions = 2) # # # Wrong # walk_2d |> summarize_walks(.value = y) # # # Correct # walk_2d |> summarize_walks(.value = cum_sum_y) ## ----dplyr_example------------------------------------------------------------ library(dplyr) random_normal_walk(.num_walks = 10) |> filter(step_number > 50) |> mutate(positive = cum_sum_y > 0) |> group_by(walk_number) |> summarize(prop_positive = mean(positive)) ## ----shiny_example, eval=FALSE------------------------------------------------ # library(shiny) # library(RandomWalker) # # ui <- fluidPage( # numericInput("num_walks", "Number of Walks:", 10), # plotOutput("walks_plot") # ) # # server <- function(input, output) { # output$walks_plot <- renderPlot({ # random_normal_walk(.num_walks = input$num_walks) |> # visualize_walks(.pluck = "cum_sum_y") # }) # } # # shinyApp(ui, server) ## ----ggplot2_example, fig.alt="Custom ggplot2 theme applied to random walk"---- library(ggplot2) p <- rw30() |> visualize_walks(.pluck = "y") # Customize further p + labs(title = "My Custom Title") + theme_bw() ## ----stock_example, fig.alt="Stock price simulation using geometric Brownian motion"---- stock_prices <- geometric_brownian_motion( .num_walks = 100, .n = 252, # Trading days .mu = 0.08, # 8% expected return .sigma = 0.25, # 25% volatility .initial_value = 100 ) visualize_walks(stock_prices) ## ----particle_example, eval=FALSE--------------------------------------------- # particles <- brownian_motion( # .num_walks = 50, # .n = 1000, # .dimensions = 3 # ) ## ----algorithm_example, eval=FALSE-------------------------------------------- # # Generate test walks # test_data <- discrete_walk( # .num_walks = 1000, # .n = 100, # .upper_probability = 0.5 # ) # # # Run your algorithm # result <- my_algorithm(test_data) ## ----citation_example, eval=FALSE--------------------------------------------- # citation("RandomWalker")