Skip to content

Instantly share code, notes, and snippets.

View saudiwin's full-sized avatar

Robert Kubinec saudiwin

View GitHub Profile
@saudiwin
saudiwin / auto_google_docs.R
Last active September 26, 2025 15:04
Automate the creation of google documents for class
# generate google drive files for class
library(googledrive)
library(readr)
library(readxl)
library(tidyverse)
library(stringr)
# need class roster/blackboard export
@saudiwin
saudiwin / emoji_R_code.R
Created January 16, 2025 18:31
How to use emojis in your #rstats code
library(dplyr)
# define code as emojis
# simulate data from Normal distribution and fit OLS regression
`🙄` <- lm
`🤩` <- tibble
`🤪` <- mutate
@saudiwin
saudiwin / chatgpt_exponential_beta_mixture.R
Last active December 4, 2024 02:22
ChatGPT-created statistical distribution with R and Stan Code:
# ChatGPT 4o created this distribution with the following prompt:
# "can you define a new statistical distribution that no one
# has ever used before and
# write out the stan code for the likelihood"
# it required one additional prompt to ask it to provide
# a function for initial starting values after Stan
# failed to initialize the MCMC chain
# it did so successfully and no alterations were made to the code below
@saudiwin
saudiwin / ordbetareg_declare_design.R
Last active November 26, 2024 15:32
Comparing using a ordered beta (proportion) distribution to estimate a treatment effect vs a dichotomized proportion (rounding to either 0 or 1) in DeclareDesign power simulation
library(DeclareDesign)
library(ordbetareg)
library(fabricatr)
library(future)
library(marginaleffects)
library(broom)
library(glmmTMB)
set.seed(20241125)
@saudiwin
saudiwin / calc_pry1.R
Last active October 3, 2024 20:46
How to calculate the probability of the discrete end points of a response in an ordbetareg model
# calculate Pr(Y=1) and Pr(Y=0) (i.e. the discrete categories of the scale)
# for different groups in the data
# useful for treatment/control comparisons
library(ordbetareg)
library(dplyr)
library(tidyr)
library(posterior)
library(tidybayes)
library(ggplot2)
@saudiwin
saudiwin / compare_ordbeta_glmmTMB
Created December 20, 2022 08:16
Comparison of ordbetareg and glmmTMB versions of the ordered beta regression model
# test glmmTMB's MLE implementation of the ordered beta regression model
# and compare it to the ordbetareg package, which uses
# Bayesian estimation (Stan/brms)
library(glmmTMB)
library(ordbetareg)
library(dplyr)
data("pew")
@saudiwin
saudiwin / powercalc_ord_binary.R
Created January 31, 2022 07:25
Power calculation showing why ordinal outcomes are more efficient than binary (dichotomized) outcomes
# Ordinal versus binary outcome power calculation
# Robert Kubinec
# New York University Abu Dhabi
# January 31, 2022
library(dplyr)
library(ordinal)
library(ggplot2)
@saudiwin
saudiwin / create_array.R
Created June 7, 2018 16:30
A tidy function that converts long data frames to arrays.
#' Helper function to create arrays
#'
#' Function takes a data.frame in long mode and converts it to an array. Function can also repeat a
#' single matrix to fill out an array.
#'
#' @param input_matrix Either a data.frame in long mode or a single matrix
#' @param arr_dim If \code{input_matrix} is a single matrix, \code{arr_dim} determines the length of the resulting array
#' @param row_var Unquoted variable name that identifies the data.frame column corresponding to the rows (1st dimension) of the array (must be unique)
#' @param col_var_name Unquoted variable name that identifies the data.frame column corresponding names of the columns (2nd dimension) of the array
#' @param col_var_value Unquoted variable name that identifies the data.frame column corresponding to the values that populate the cells of the array
@saudiwin
saudiwin / .gistr
Created June 7, 2018 16:30
A tidy function that converts long data frames to arrays.
gistr
@saudiwin
saudiwin / .gistr
Created June 7, 2018 16:29
A tidy function that converts long data frames to arrays.
gistr