Instantly share code, notes, and snippets.

View header.Rmd

title: "Building up and evaluating models" output: html_notebook: code_folding: none css: ../custom.css theme: flatly toc: yes toc_depth: 3 toc_float: yes

View header.Rmd

title: "Building up and evaluating models" output: html_notebook: code_folding: none css: ../custom.css theme: flatly toc: yes toc_depth: 3 toc_float: yes

View sigma_tween.R
library(tweenr)
library(gganimate)
library(ggplot2)
library(tidyverse)
#' define the 4 states
state1 <- data_frame(mean = 0,
sd = 1)
state2 <- data_frame(mean = 0,
sd = 3)
View purrr_bootstrap.R
library(tidyverse)
replicates <- (1:100000)%>%
map(~sample(faithful$waiting, replace = T))%>%
map(mean)%>%
simplify()
data_frame(replicates = replicates)%>%
ggplot(aes(replicates))+
stat_density()
View purrr_bootstrap.R
library(tidyverse)
replicates <- (1:100000)%>%
map(~sample(faithful$waiting, replace = T))%>%
map(mean)%>%
simplify()
data_frame(replicates = replicates)%>%
ggplot(aes(replicates))+
stat_density()
View many_files_recipe.R
#' I often have individual speaker data files in a nested directory structure.
#' But I also often want to read all speaker's data into R in one big data frame.
#' Here's my current best recipe.
library(tidyverse)
#' glob for the file list. This is dependent on good directory naming practices
all_files <- Sys.glob("path/speakerid*/*.csv")
df <- data_frame(file = all_files) %>% # make a column of all of the file paths
View terror.R
#' rvest for scraping 538
library(rvest)
library(magrittr)
#' scrape the forecast
five38 <- read_html("http://projects.fivethirtyeight.com/2016-election-forecast/?ex_cid=rrpromo#plus")
#' I'd prefer to be using the polls-pluss forecast here, but
#' can only seem to get the polls only
clinton <- five38 %>%
View zero_crossings.R
#' Find zero crossings in an fd object
#'
#' @import fda
#' @import magrittr
#'
#' @param fd an fd object
#' @param Lfdobj the derivative (0, 1, 2)
#' @param slope The slope of interest at the zero crossing
#' @param eps The prediction granularity
#' @param min Localize the zero crossing search to be greater than min
View list2fd.R
list2fd <- function(list, basis){
if(class(list[[1]]) == "fdSmooth"){
coef_list <- lapply(list, function(x)x$fd$coefs)
}else if(class(list[[1]]) == "fd"){
coef_list <- lapply(list, function(x)x$coefs)
}
n_coefs <- unlist(lapply(coef_list, length))
if(!all(n_coefs == max(n_coefs))) stop()