Skip to content

Instantly share code, notes, and snippets.

View TysonStanley's full-sized avatar
🏠
Working from home

Tyson Barrett TysonStanley

🏠
Working from home
View GitHub Profile
@TysonStanley
TysonStanley / DensityPlots
Created May 4, 2015 17:55
Two Dimensional Density and Hexbin Plots with Simulated Data (ggplot2 & hexbin)
# Simulating Correlated Data
# (this is a variant of something I found online but I can't find out who posted it...)
n <- 2000 # length
rho <- 0.6 # desired correlation = cos(angle)
theta <- acos(rho) # corresponding angle
pred <- rnorm(n, 1, 1) # predictor vector
x2 <- rnorm(n, 2, 0.5) # new random data for response vector
X <- cbind(pred, x2) # matrix of pred and x2
Xctr <- scale(X, center=TRUE, scale=FALSE) # centered columns (mean 0)
Id <- diag(n) # an identity matrix
@TysonStanley
TysonStanley / Density_ShotChart
Last active August 29, 2015 14:20
Density Shot Chart of Stephen Curry 2015 (ggplot2 & hexbin & png)
# Steph Curry Shot Selection in 2015
# Data from: NBAsavant.com (awesome data source)
packs <- c("png","grid","ggplot2")
lapply(packs, require, character.only=TRUE)
steph <- read.csv("data.csv") # from NBAsavant.com
court <- readPNG("court.png") # diagram of NBA court
g <- rasterGrob(court, interpolate=TRUE)
## Code for Example Plot for @evergreendata
## Tyson S. Barrett
## Data
x_num = c(4.2, 4.5, 4, 4.5, 3.9, 3.6)
year = c(2011:2016)
x_per = c(.52, .58, .6, .63, .64, .67)
df = data.frame(x_num, year, x_per)
## Libraries and Plots
@TysonStanley
TysonStanley / join-animations-with-gganimate.R
Created August 17, 2018 19:48 — forked from gadenbuie/join-animations-with-gganimate.R
Animated dplyr joins with gganimate
# Animated dplyr joins with gganimate
# * Garrick Aden-Buie
# * garrickadenbuie.com
# * MIT License: https://opensource.org/licenses/MIT
# Note: I used Fira Sans and Fira Mono fonts.
# Use search and replace to use a different font if Fira is not available.
library(tidyverse)
library(gganimate)
@TysonStanley
TysonStanley / beta_power.R
Last active February 26, 2019 22:07
Preliminary Power Analysis with Beta Regression
## Beta Regression Power Analysis
## Simulations to assess statistical power given alpha, n, effect size
## Tyson S. Barrett
library(tidyverse)
library(betareg)
set.seed(84322)
## Population Model
@TysonStanley
TysonStanley / server.R
Created July 16, 2019 00:10 — forked from withr/server.R
Encrypt password with md5 for Shiny-app.
library(shiny)
library(datasets)
Logged = FALSE;
PASSWORD <- data.frame(Brukernavn = "withr", Passord = "25d55ad283aa400af464c76d713c07ad")
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
source("www/Login.R", local = TRUE)
observe({
if (USER$Logged == TRUE) {
@TysonStanley
TysonStanley / dplyr_datatable_memory.R
Created October 12, 2019 17:34
Using `profmem` package to understand data summaries by group for both `data.table` and `dplyr`
library(bench) # assess speed and memory
library(data.table) # data.table for all of its stuff
library(dplyr) # compare it to data.table
library(profmem) # assess the process of R functions
set.seed(84322)
# Example Data
d <- data.table(
grp = sample(c(1,2), size = 1e6, replace = TRUE) %>% factor,
# Tidy Tuesday (on Friday)
# 25 October 2019
# Tyson S. Barrett
# Packages ----
library(tidyverse) # CRAN
library(dtplyr) # CRAN
library(data.table) # CRAN
library(tidyfast) # remotes::install_github("tysonstanley/tidyfast")
``` r
library(tibble)
df = tibble(
var1 = rnorm(5),
var2 = rnorm(5)
)
first = function(data, x){
x = substitute(x)