Skip to content

Instantly share code, notes, and snippets.

View bgall's full-sized avatar

bgall bgall

View GitHub Profile
@bgall
bgall / r_na_examples
Last active July 6, 2017 18:32
Demonstrates the idiosyncratic behavior of NA values in R
# The below demonstrates the madness of R's treatment of NA values.
# Some examples taken from https://stackoverflow.com/questions/25100974/na-matches-na-but-is-not-equal-to-na-why/25101796
# Logical examples
NA %in% NA
# [1] TRUE
NA == NA
@bgall
bgall / sample_selection_bias
Created February 14, 2018 19:42
Conditioning on the dependent variable changes the L in LATE
# Load packages
library(dplyr)
# Create data
set.seed(1)
x <- rnorm(1000000, mean = 0, sd = 1)
y <- x*x
full <- data.frame(x, y)
@bgall
bgall / Estimate models with groups of variables.R
Last active June 23, 2018 21:15
Estimate models with groups of variables
# Create sample data
set.seed(1)
data <- data.frame(y = rnorm(10),
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10))
# Function takes a string
#########################################################################################
# This gist contains a quick walk-through of several ways to produce scales capturing
# the average value of one or more variables. Each row (observation) gets its own
# value. We'll assume your data are not fully "tidy." What I mean by this is that you
# have an observation for each row and you want to calculate that observation's value
# on the scale, but each variable that should go into your scale is in its own column.
#########################################################################################
#########################################################################################
# Set-up (packages, fake data, etc.)
@bgall
bgall / load_pwrc_pkgs.R
Created December 1, 2019 20:29
Load packages for simulation-based power calculation
#########################################################################
# 'loadpkg' function
# Checks if a vector of packages are installed. If not, installs the
# package. Then loads all packages in vector.
#########################################################################
loadpkg <- function(toLoad){
for(lib in toLoad){
if(! lib %in% installed.packages()[,1]) {
install.packages(lib, repos='http://cran.rstudio.com/')
@bgall
bgall / create_design_df.R
Last active December 1, 2019 20:46
Creates "skeleton" data frame of conjoint parameters.
#########################################################
# Define function: create_design_df
#
# Description: Generates a data frame with participant
# IDs, choice set ID, profile ID, for specified numbers of
# participants, choice tasks per participant, and profiles
# per choice tasks
#
# Arguments:
# N = # of participants
@bgall
bgall / create_attributes.R
Last active December 1, 2019 20:49
Function to generate arbitrary conjoint attributes with specified probabilities values are selected
##############################################################
# Define function: create_attributes
#
# Creates randomly generated vectors of values from sets
# of potential values for a specified number of attributes
#
# *Arguments*
#
# attr_names (optional)
# vector of attribute names of attr_n length. If no values
@bgall
bgall / create_value_dummies.R
Created December 1, 2019 21:01
Function to generate dummy variables from the possible sets of values
#########################################################
# Define function: create_value_dummies
# Create dummy variables indicating if a given
# profile takes on a specific value from its
# possible values. Create a dummy for the range
# of the variable, independent of whether the actual
# random sampling does (not) draw that value and assign
# it to a profile.
#########################################################
@bgall
bgall / create_predictors.R
Last active December 1, 2019 21:23
Wrapped for create_attributes, create_design_df, and create_value_dummies
################################################
# Define function: create_predictors
# Creates randomly generated data sets containing
# all design variables, attributes, and dummy
# variables for the right-hand side of the conjoint
# analysis (excludes the outcome variable),
# based on specified data parameter values.
# Data are "long," sucht hat each row of the data
# is a conjoint profile.
#
@bgall
bgall / post-hoc-power-plants-mental.r
Last active January 6, 2020 15:26
Calculate post-hoc power of study of the effect of indoor plants on mental health
set.seed(123)
##############################################
# Study parameters
##############################################
# Sample size
n <- 63
# Mean of outcome