Skip to content

Instantly share code, notes, and snippets.

View markhwhiteii's full-sized avatar

Mark H. White II markhwhiteii

View GitHub Profile
@markhwhiteii
markhwhiteii / ashnkrause2
Created January 31, 2022 16:02
using across() to recode likerts
library(tidyverse)
# define levels
levs <- c("agree", "neither", "disagree")
# generate data
set.seed(1839)
(dat <- replicate(20, sample(levs, 100, TRUE)) %>%
as_tibble() %>%
# add other variables so that you have to be specific in across()
@markhwhiteii
markhwhiteii / gamlss_variance.R
Created August 28, 2020 20:11
gamlss_variance
library(tidyverse)
library(gamlss)
set.seed(1839)
# simulate location and scale for measure 1
mu1_i <- rnorm(100)
sigma1_i <- runif(100)
# simulate location and scale for measure 2, correlated with measure 1
mu2_i <- mu1_i + rnorm(100)
library(MASS)
library(tidyverse)
n <- 1000
nvars <- 10
iter <- 500
sim_var <- function(n, r) {
c(mvrnorm(n, mu = c(0, 0), Sigma = matrix(c(1, r, r, 1), ncol = 2)))
}
@markhwhiteii
markhwhiteii / t_table.R
Created March 27, 2020 13:24
t-test Table
#' Make a Summary Table of Multiple t-tests
#'
#' TGenerates a table that includes variable names, means and standard
#' deviations for each condition, t-value and p-value, degrees of
#' freedom, and Cohen's d with confidence intervals.
#'
#' @param data A data.frame.
#' @param dvs String. Variable name(s) of the dependent variables.
#' @param iv String. Variable name for the independent variable.
#' @param var_equal Logical. Assumed equal variance across conditions?
@markhwhiteii
markhwhiteii / pipe_fails_where.R
Created March 26, 2020 16:05
Where does the pipe chain fail?
library(tidyverse)
tibble(x = rep(letters[1:4], 2), y = runif(8)) %>%
mutate(y = 2 * y) %>%
group_by(x) %>%
summarise(mean_y = mean(y)) %>%
filter(z == "a") %>%
spread(x, mean_y) %>%
mutate_all(`*`, 10)
set.seed(1839)
iter <- 1000000
results <- sapply(1:iter, function(zzz) {
m <- matrix(sample(c("X", "X", "X", "X", "X", "O", "O", "O", "O"), 9), 3)
xwin <- c(
all(m[1, ] == "X"),
all(m[2, ] == "X"),
all(m[3, ] == "X"),
all(m[, 1] == "X"),
all(m[, 2] == "X"),
structure(list(x = c(26, 49, 25, 34, 24, 32, 24, 37, 38, 47,
41, 33, 20, 37, 34, 32, 28, 48, 40, 42, 42, 39, 38, 39, 32, 45,
25, 43, 50, 33, 19, 44, 44, 37, 55, 47, 36, 36, 14, 23, 42, 35,
39, 46, 42, 40, 27, 26, 27, 47, 47, 46, 24, 50, 45, 52, 26, 50,
44, 48, 46, 49, 21, 36, 41, 31, 45, 44, 34, 43, 46, 37, 46, 46,
48, 28, 50, 25, 42, 51, 47, 36, 39, 26, 45, 27, 35, 27, 47, 32,
32, 39, 48, 29, 26, 38, 43, 37, 45, 18, 39, 52, 29, 53, 39, 42,
26, 44, 46, 26, 17, 50, 47, 50, 45, 47, 51, 43, 44, 24, 38, 42,
31, 40, 44, 50, 45, 40, 36, 48, 42, 33, 50, 32, 34, 36, 41, 47,
38, 36, 37, 51, 41, 47, 33, 41, 41, 48, 44, 41, 28, 51, 51, 54,
# define functions -------------------------------------------------------------
inv_logit <- function(x) exp(x) / (exp(x) + 1)
sim_pop <- function(N = 5000, b_min = 0, b_max = 1.5) {
X <- cbind(1, rbinom(N, 1, .5), rbinom(N, 1, .5))
B <- c(0, runif(1, b_min, b_max), runif(1, b_min, b_max))
y <- rbinom(N, 1, inv_logit(X %*% B))
return(as.data.frame(cbind(X[, -1], y)))
}
library(emmeans)
inv_logit <- function(x) exp(x) / (1 + exp(x))
set.seed(1839)
n <- 100
x <- rbinom(n, 1, .5)
y <- rbinom(n, 1, inv_logit(x))
model <- glm(y ~ factor(x), binomial)
ci <- confint(model)
# getting y_hat | x = 1 --> my suggestion was too wide
library(tidyverse)
library(simstudy)
library(geepack)
set.seed(1839)
results <- lapply(1:2000, function(zzz) {
dat <- genCorGen(n = 1000, nvars = 2, params1 = c(.50, .53),
dist = "binary", rho = .80, corstr = "cs", wide = FALSE)
gee_mod <- geeglm(X ~ period, binomial, dat, id = id, corstr = "exchangeable")
glm_mod <- glm(X ~ period, binomial, dat)
c(