This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
make_explainer_obj <- function(fitted_workflow){ | |
fitted_model <- | |
fitted_workflow %>% | |
extract_fit_parsnip() # <- parsnip model_fit object | |
feature_data <- | |
fitted_workflow %>% | |
extract_mold() %>% | |
pluck("predictors") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script to create sqlite database of daily GHCN weather station data | |
# adding a table of US zipcodes for my own purposes (easily commented out) | |
# cribs heavily from https://github.com/dylburger/noaa-ghcn-weather-data | |
# Settings | |
import sqlalchemy | |
import os | |
import urllib.request | |
import pandas as pd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cumulative_intercept_prior <- function(k, sd = 2, alpha = 1, beta = 1, | |
shape = c("flat", "middle", "rightskewed", "leftskewed")) { | |
## Creates priors on intercepts for cumulative() family regression. | |
## Assumes that probability of response options follow cumulative beta | |
## distribution specified by a and b or by "shape" argument. | |
## | |
## k = number of categories | |
## sd = std dev of normal over intercept | |
## a, b = alpha and beta specifying shape of distribution (defaults to uniform) | |
## shape = string specifying pre-defined distribution shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def horseshoe_prior(name, X, y, m, v, s): | |
''' | |
Regularizing horseshoe prior as introduced by Piironen & Vehtari | |
https://arxiv.org/pdf/1707.01694.pdf | |
name: variable name | |
X: X (2-d array) | |
y: y (for setting pseudo-variance) | |
m: expected number of relevant features (must be < total N) | |
v: regularizing student-t df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gather( df, key, value, cols ): | |
id_vars = [ col for col in df.columns if col not in cols ] | |
id_values = cols | |
var_name = key | |
value_name = value | |
return pd.melt( df, id_vars, id_values, var_name, value_name ) | |
def spread( df, index, columns, values ): | |
return df.pivot(index, columns, values).reset_index(level=index).rename_axis(None,axis=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cbrm <- function(formula, | |
data, | |
family = gaussian(), | |
prior = NULL, | |
autocor = NULL, | |
cov_ranef = NULL, | |
sample_prior = c("no", "yes", "only"), | |
sparse = FALSE, | |
knots = NULL, | |
stan_funs = NULL, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rescale_beta <- function(x, lower, upper) { | |
# rescales onto the open interval (0,1) | |
# rescales over theoretical bounds of measurement, specified by "upper" and "lower" | |
N <- length(x) | |
res <- (x - lower) / (upper - lower) | |
res <- (res * (N - 1) + .5) / N | |
return(as.vector(res)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read_qualtrics_csv <- function(fname) { | |
headers <- as.matrix(read.csv(fname, skip = 0, header = F, nrows = 1, as.is = T)) | |
df <- read_csv(fname, skip = 3, col_names = headers) | |
df <- df %>% | |
filter(DistributionChannel=="anonymous") | |
return(df) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model_name <- brm( | |
DV ~ formula, | |
data = d, | |
family = normal(), # student(), #cumulative(), #bernoulli(), etc | |
control = list(adapt_delta = .80), | |
cores = parallel::detectCores(), | |
iter = 2000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Derek Powell | |
# Date: 8/30/18, 4:49 PM | |
# --- | |
# Script to redact workerIds and ip addresses from qualtrics files. | |
# Script looks for "date_private/" directory, saves resulting data in "data" directory. | |
# Personal info is replaced with a "hash" using xxhash64, | |
# a super fast hash algo w/ short resulting hashes (confirmed appropriate for this use) | |
suppressMessages(library(tidyverse)) | |
suppressMessages(library(digest)) |
NewerOlder