Skip to content

Instantly share code, notes, and snippets.

View mpettis's full-sized avatar

Matt Pettis mpettis

  • Personal
  • Minneapolis, MN
View GitHub Profile
@mpettis
mpettis / timeseries-forecasting-misc.R
Created June 26, 2020 23:11
Timeseries forecasting, converting ts objects to real dates, controlling charting, autoplot, etc.
#' ---
#' title: "Beer example"
#' author: "Matt Pettis (matthew.pettis@gmail.com)"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' toc: true
#' toc_depth: 3
#' code_folding: show
#' editor_options:
library(lubridate)
library(tidyverse)
seq(
from=as.POSIXct("2012-1-1 0:00", tz="UTC"),
to=as.POSIXct("2012-1-3 23:00", tz="UTC"),
by="hour"
) ->
dt
@mpettis
mpettis / comparing-polynomial-models-covid.R
Created May 5, 2020 21:03 — forked from dgrtwo/comparing-polynomial-models-covid.R
Comparing the CEA's "cubic model" to quadratic and quartic models
library(tidyverse)
library(broom)
US <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv") %>%
mutate(new_deaths = deaths - lag(deaths)) %>%
filter(date >= "2020-02-26")
models <- tibble(degrees = 2:4) %>%
mutate(model = map(degrees, ~ lm(log(new_deaths + 1) ~ poly(date, .), data = US)))
@mpettis
mpettis / rle-groups.R
Created March 24, 2020 15:41
Run-length-encoding, making groups
## Using run-length-encoding, create groups of identical values and put that
## common grouping identifier into a `grp` column.
library(tidyverse)
set.seed(42)
df <- tibble(x = sample(c(0,1), size=20, replace=TRUE, prob = c(0.2, 0.8)))
df %>%
mutate(grp = rle(x)$lengths %>% {rep(seq(length(.)), .)})
@mpettis
mpettis / spread-group-percent.Rmd
Last active March 22, 2019 20:22
dplyr spread group percent
---
title: "spread-group-percent"
author: "Matt Pettis"
date: "March 22, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@mpettis
mpettis / geosphere-haversine-example.R
Last active February 4, 2019 18:10
Using geosphere/haversine matrix function with dplyr mutate operations
library(geosphere)
library(tidyverse)
# Simple dataframe and use case:
ref_ll <- c(-95.0, 45.0)
df__ <- tribble(
~lon, ~lat,
-95.0, 45.0,
-96.0, 45.0,
@mpettis
mpettis / run-length-encoding-to-dataframe.R
Last active August 28, 2019 19:19
run-length-encoding-to-dataframe
################################################################################
# Make a function that does what I want
# Take a dataframe, identify the target column with runs, ID the index column,
# which is the column that I want to capture the first and last values of for
# runs.
#' rle_as_df
#'
#' Turn run-length encoding into a dataframe so it can be used with ggplot/geom_area.
# Dealing with NULLs as list values
library(tidyverse)
# As advertised on the tin:
is.null(NULL)
#> [1] TRUE
# is.null() is not vectorized?
is.null(list(NULL))
@mpettis
mpettis / R-reduce-lists-of-vectors.R
Last active November 14, 2018 22:22
R: Reduce, functional programing, boolean condition reduction
``` r
## 'or'ing together same-length vectors of booleans.
## use case is to set up sets of booleans that have the same length and should
## be all 'or'ed together to form a condition.
# Base R way:
lstt <- list(
a = c(T, F, F, F)
, b = c(F, T, F, F)
, c = c(F, F, T, F)
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#>
#> flatten
# Ref: https://stackoverflow.com/questions/7944809/assigning-null-to-a-list-element-in-r