Skip to content

Instantly share code, notes, and snippets.

View brianlevey's full-sized avatar

Brian Levey brianlevey

View GitHub Profile
@brianlevey
brianlevey / Serialize R Model, Store in DB, Unserialize to Model
Created May 7, 2020 15:47
Example script showing how to serialize a model in R for storage in a SQLite DB and retrieve the model for future use.
library(tidyverse)
library(RSQLite)
tmp <- tibble(x = rnorm(100),
y = 1.8*x + rnorm(100))
mod <- lm(y~x, tmp)
mod$fitted.values <- NULL
mod$residuals <- NULL
mod$effects <- NULL
@brianlevey
brianlevey / Example Cross-Correlation
Created April 1, 2020 21:05
Example use of CCF from Feasts
library(tidyverse)
library(tsibble)
library(feasts)
tmp <- tibble(id = 1:100, x = rnorm(100)) %>%
mutate(x = cumsum(x),
y = 1.2 + 2.45*lag(x, 2) + rnorm(100)) %>%
as_tsibble(index = id)
tmp %>% CCF(x, y) %>%
@brianlevey
brianlevey / growth_mod.R
Created November 15, 2019 17:23
Logistic Growth Model
library(tidyverse)
l = .90 # controls the maximum probability
k = -0.03 # controls the steepnees of the curve
m = 50 # sets the midpoint of the curve
tmp <- tibble(x = 1:200) %>%
mutate(y = l / (1 + exp(k*(x - m))))
ggplot(tmp) + geom_line(aes(x, y)) +
geom_vline(xintercept = tmp$x[round(tmp$y, 1)==.5][1], alpha = .5, color = "red") +
@brianlevey
brianlevey / cpred.R
Last active September 23, 2025 19:53
Calculate classification error
cpred <- function(dv, pr, id=NULL, threshold=.5){
df <- tibble(id, dv, pr) %>%
mutate(pr = ifelse(pr > threshold, 1, 0)) %>%
drop_na()
if(!is.null(id)){
df <- df %>% group_by(id)
}
df <- df %>%
summarize(P = sum(dv),
N = sum(dv == 0),
@brianlevey
brianlevey / bullet_graph.R
Created June 7, 2019 16:43
Minimal example of a bullet graph
library(tidyverse)
tmp <- tibble(mod = "effect", est = 2.2, se = .75)
ggplot(tmp) +
geom_segment(aes(x = est - (2*se),
xend = est + (2*se),
y = mod,
yend = mod,
color = "gray",
@brianlevey
brianlevey / grouped_lm_with_predictions.R
Created May 30, 2019 16:21
Use broom to perform grouped regression and make predictions with new data.
library(tidyverse)
library(broom)
data <- nest(mtcars, -cyl)
data100 <- mutate(mtcars, disp = 100) %>% nest(-cyl, .key = data100)
data200 <- mutate(mtcars, disp = 200) %>% nest(-cyl, .key = data200)
data <- full_join(data, data100, by = "cyl")
data <- full_join(data, data200, by = "cyl")
@brianlevey
brianlevey / Matrix_rotation.R
Created February 12, 2019 15:39
Matrix rotation
library(tidyverse)
data <- tibble(id = letters[1:10], x = rnorm(10), y = rnorm(10))
g1 <- ggplot(data) + geom_point(aes(x, y, color = id))
# matrix rotation
theta <- .9
rmat <- matrix(c(cos(theta), -sin(theta),
@brianlevey
brianlevey / TTest implementation.R
Created November 29, 2017 16:45
Implement paired and independent samples ttest in R
# sample
n <- 1000
x <- rnorm(n, 25, 1)
y <- rnorm(n, 25.4, 1)
# unpaired
t.test(y, x)
tibble(x = c(0,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,0)) %>%
mutate(count = ave(x, cumsum(x==0), FUN=cumsum),
phase = cumsum(ifelse(count == 1, 1, 0)),
phase = ifelse(x == 0, 0, phase))
@brianlevey
brianlevey / gradient_descent_1variable.R
Created October 27, 2017 15:36
Gradient descent implementation in R.
library(tidyverse)
# training data, model has no error
data <- tibble(x0 = 1,
x1 = rnorm(100),
y = 0.97 + 1.3*x1)
# ols model
summary(lm(y ~ x1, data))