Skip to content

Instantly share code, notes, and snippets.

View drsimonj's full-sized avatar

Simon Jackson drsimonj

View GitHub Profile
@drsimonj
drsimonj / traffic_interaction_simulations.R
Last active July 3, 2024 04:24
Simulating traffic interactions as described in https://vista.io/blog/interaction-effects-in-online-experimentation to see which of two types of tests are best suited
library(tidyverse)
# Simulation and test function --------------------------------------------
#' n = number of user IDs to simulate
#' p = Percentage of Experiment 1 traffic in treatment to exclude from experiment 2
simulate_and_test <- function(n = 1000, p = 0) {
#message("n = ", n, " and p = ", p)
# Simulate a random data set of users in or out of two experiments
@drsimonj
drsimonj / random_metric_results.R
Created May 14, 2020 08:51
Randomly generates between-group tests to see false positives
# Packages
library(tidyverse)
library(broom)
# Set seed for reproducible results
set.seed(20200513)
# Data settings to simulate
n_units <- 100
n_metrics <- 10
@drsimonj
drsimonj / corrr_dif_methods
Created September 10, 2018 19:07
Putting spearman and pearson correlations in upper/lower triangles
library(tidyverse)
library(corrr)
d <- mtcars
stretch_triangle <- function(cordf, upper) {
cordf %>% shave(upper = upper) %>% stretch(na.rm = TRUE)
}
spearman <- correlate(d, method = "spearman") %>% stretch_triangle(upper = FALSE)
@drsimonj
drsimonj / regex.R
Created September 2, 2018 12:21
regex - recursive EBNF
## BASE FUNCTION
base <- function(obj) {
UseMethod("base")
}
base.default <- function(obj) {
as.character(obj)
}
base.regex <- function(obj) {
@drsimonj
drsimonj / efa.R
Created April 17, 2018 09:01
EFA in R
library(tidyverse)
library(lavaan)
library(semTools)
# Function to fit unrotated EFA with specific number of factors
fit_unrotated_efa <- function(n_factors) {
data %>% efaUnrotate(nf = n_factors, estimator = "mlr")
}
# Fit EFAs (unrotated) with a range of factors
@drsimonj
drsimonj / multiple_lags.R
Last active July 8, 2022 14:53
Example of creating multiple lags with dplyr
library(dplyr)
d <- data_frame(x = seq_len(100))
d
#> # A tibble: 100 x 1
#> x
#> <int>
#> 1 1
#> 2 2
#> 3 3
@drsimonj
drsimonj / model and predict problems
Last active June 6, 2017 01:46
Snippets of code that demonstrate issues with model and predict functions, serving as a motivator for the twidlr package
## predict for Analysic of Variance (aov) searches for object in global environment
d <- datasets::mtcars
fit <- aov(hp ~ am * cyl, d)
predict(fit)
d <- NULL
predict(d)
## predict for Principal Components (prcomp) can't recreate new variables defined in formula
fit <- prcomp(~.*., mtcars[1:25, ])
predict(fit, mtcars[26:32,])