## Posts by kjytay

# Author: kjytay

#### Simulating paths from a random walk

Feed: R-bloggers. Author: kjytay. If you’ve ever visited this blog at wordpress.com, you might have noticed a header image that looks like this:Ever wonder how it was generated? The image depicts 100 simulations of an asymmetric random walk. In this post, I’ll go through the code used to generate this image. All the code can also be found here.For , consider a series of i.i.d. random variables such that for any , with probability , and with probability . ( is a parameter that we get to choose.) A random walk simply tracks the cumulative sum of these random variables, ... Read More

#### Using dplyr::filter when the condition is a string

Feed: R-bloggers. Author: kjytay. This took me a while to figure out and so I thought I would post this as future reference. Let’s say I have the mtcars data and I want to filter for just the rows with cyl == 6. I would do something like this:library(tidyverse) data(mtcars) mtcars %>% filter(cyl == 6) # mpg cyl disp hp drat wt qsec vs am gear carb # Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 # Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 # Hornet 4 Drive ... Read More

#### NBA salaries

Feed: R-bloggers. Author: kjytay. I came across a dataset of NBA player salaries from the 1984-1985 season to the 2017-2018 season here, and I thought it would be a fun dataset to practice my tidyverse skills on. All the code for this post can be found here.First, let’s import the tidyverse package, set the plotting theme, and read in the data files.library(tidyverse) theme_set(theme_bw()) # read in data players % select(id, name) salaries % inner_join(players, by = c("player_id" = "id")) For all of the plots and text below, “year” will refer to the year that the season started. For example, year ... Read More

#### Basic manipulation of GIF frames with magick

Feed: R-bloggers. Author: kjytay. The magick package is a really powerful package for image processing in R. The official vignette is a great place to start learning how to use the package.I’ve been playing around with using magick for manipulating GIFs and found some tips and tricks that don’t seem to be documented anywhere. Since the NBA restart is upon us, I’ll illustrate these tips with a GIF featuring one of the greatest of all time:library(magick) img_path If you print img, you will see that the GIF is represented as a tibble, with each row representing a single frame in ... Read More

#### Image contours in R

Feed: R-bloggers. Author: kjytay. I recently came across this short fun post on R-bloggers that demonstrated how to use the image.ContourDetector package (available on CRAN) to extract contours from an image. The image of the contours looked really cool so I thought I would try it out for myself!As an example, I wanted to extract the contours from the image below:(Source: premierleague.com)Here is the code taken from the R-bloggers post (slightly modified):library(magick) library(image.ContourDetector) img_path And here is the result. Not bad for a little chunk of code!Not bad, but can we do better? In particular, we can’t really see any ... Read More

#### tidyr::complete to show all possible combinations of variables

Feed: R-bloggers. Author: kjytay. This is an issue I often face, so I thought it best to write it down. When doing data analysis, we often want to known how many observations there are in each subgroup. These subgroups can be defined by multiple variables. In the code example below, I want to know how many vehicles there are for each (cyl, gear) combination:library(tidyverse) data(mtcars) mtcars %>% group_by(cyl, gear) %>% summarize(count = n()) # # A tibble: 8 x 3 # # Groups: cyl [3] # cyl gear count # # 1 4 3 1 # 2 4 4 8 ... Read More

#### What is nearly-isotonic regression?

Feed: R-bloggers. Author: kjytay.
Let’s say we have data such that . (We assume no ties among the ‘s for simplicity.) Isotonic regression gives us a monotonic fit for the ‘s by solving the problem (See this previous post for more details.) Nearly-isotonic regression, introduced by Tibshirani et al. (2009) (Reference 1), generalizes isotonic regression by solving the problem where and is a user-specified hyperparameter. It turns out that, due to properties of the optimization problem, the nearly-isotonic regression fit can be computed for all values in time, making it a practical method for use. See Section 3 and ... Read More

#### What is isotonic regression?

Feed: R-bloggers. Author: kjytay.
Isotonic regression is a method for obtaining a monotonic fit for 1-dimensional data. Let’s say we have data such that . (We assume no ties among the ‘s for simplicity.) Informally, isotonic regression looks for such that the ‘s approximate the ‘s well while being monotonically non-decreasing. Formally, the ‘s are the solution to the optimization problem (Note: There is a corresponding solution for a monotonically non-increasing fit. Sometimes, the above is referred to as linear ordering isotonic regression, with isotonic regression referring to a more general version of the problem above. For more general ... Read More

#### A small gotcha when comparing lists using testthat

Feed: R-bloggers. Author: kjytay.
I recently encountered a small gotcha when using the testthat package to test the equality of two lists. Imagine I have the following two lists below: list1 Imagine that: I know that the value associated with a is going to be different, so I don’t want to test equality for it. The values associated with the other keys should be the same, so I want to test equality for them. If I run the tests correctly, I should get an error message associated with the key d. The following code snippet using the testthat package ... Read More

#### What is a dgCMatrix object made of? (sparse matrix format in R)

Feed: R-bloggers. Author: kjytay. I’ve been working with sparse matrices in R recently (those created using Matrix::Matrix with the option sparse=TRUE) and found it difficult to track down documentation about what the slots in the matrix object are. This post describes the slots in a class dgCMatrix object. (Click here for full documentation of the Matrix package (and it is a lot–like, 215 pages a lot).) Background It turns out that there is some documentation on dgCMatrix objects within the Matrix package. One can access it using the following code: library(Matrix) ?`dgCMatrix-class` According to the documentation, the dgCMatrix class …is ... Read More

## Recent Comments