Skip to content

Instantly share code, notes, and snippets.

View debruine's full-sized avatar
🏳️‍🌈

Lisa DeBruine debruine

🏳️‍🌈
View GitHub Profile
@debruine
debruine / ocrdata.R
Last active October 17, 2021 13:03
Get data from an image
library(dplyr) # for data processing
library(tidyr) # for data processing
library(magick) # for image reading and OCR
library(tesseract) # for OCR
# read image
dataimg <- magick::image_read("https://pbs.twimg.com/media/FBv8P8XXEBgCBvS?format=jpg&name=medium")
# convert image to text
text <- magick::image_ocr(dataimg)
@debruine
debruine / trueplot.Rmd
Last active October 12, 2021 17:58
ggplot printing function to make plots the same in interactive RStudio sessions and knitted version
---
title: "True Plots"
author: "Lisa DeBruine"
date: "12/10/2021"
output: html_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(dplyr)
@debruine
debruine / within_check.R
Last active September 12, 2021 18:54
Check possible p, t, and SD of difference score for within-subject means and SDs over a range of r-values
library(dplyr)
library(faux)
library(tidyr)
library(purrr)
library(ggplot2)
library(glue)
within_t <- function(n = 100, mu1 = 0, mu2 = 0, sd1 = 1, sd2 = 1, r = 0,
alternative = c("two.sided", "less", "greater")) {
x <- faux::rnorm_multi(n = n,
@debruine
debruine / ariely_arousal_paper.R
Last active August 20, 2021 12:20
Data from Ariely arousal paper tables
library(tidyverse)
library(truncnorm)
# transcribed table
table2 <- tribble(
~question, ~non_mean, ~non_sd, ~aro_mean, ~aro_sd, ~p,
"shoes", 42, 5.9, 65, 4.06, .001,
"girl12", 23, 4.11, 46, 6.08, .001,
"woman40", 58, 3.32, 77, 2.07, .001,
"woman50", 28, 4.80, 55, 4.69, .001,
@debruine
debruine / sim_multi_decision.R
Created July 20, 2021 18:29
Simulate a multi-test hypothesis decision.
library(faux)
library(tidyverse)
# simulate null data with realistic correlations
dat <- sim_design(
between = list(B = 0:1),
within = list(W = 0:1),
n = 100,
r = 0.25,
long = TRUE,
@debruine
debruine / sheet_append_safe.R
Last active July 4, 2021 13:37
Safer alternative to googlesheets4::sheet_append() plus tests
library(googlesheets4)
library(googledrive)
library(testthat)
library(dplyr)
#' Append rows to a sheet safely
#'
#' Adds one or more new rows after the last row with data in a (work)sheet,
#' handling data with new columns, missing columns, columns in a different order,
#' or columns with a different data type.
@debruine
debruine / JB_efficient.R
Created June 18, 2021 13:03
More efficient version of simulation code for JB
library(faux)
library(afex)
library(tidyverse)
sd = 1.5
n = 95
r = .7
nsims = 1e2
roderingel <- sim_design(
@debruine
debruine / normies.R
Last active April 16, 2021 08:40
Simulation of how many people are within 1SD of the mean on all qualities, varying number of qualities and their correlations
library(tidyverse)
dat <- crossing(v = 1:10, r = seq(0, .9, .1)) %>%
mutate(normies = map2_dbl(v, r, function(v, r) {
faux::rnorm_multi(n = 10000, vars = v, r = r, empirical = TRUE) %>%
filter_all(~(abs(.x) < 1)) %>%
nrow() / 10000
}),
r = as.character(r))
@debruine
debruine / splithalf.R
Created April 6, 2021 16:40
splithalf
library(tidyverse)
library(splithalf)
library(faux)
n_participants = 60 ## sample size
n_trials = 80
n_blocks = 2
# your method ----
@debruine
debruine / xget
Created March 28, 2021 12:09
Get named item when unsure of name
#' Get named item when unsure of name
#'
#' If a user gives you a named vector or list and there are a few possibilities for the item you want to get, list the possible names in order, using integers for indices.
#'
#' @param x named vector or list
#' @param ... possible names in x in priority order
#' @param .default default value if no names found
#'
#' @return list or vector item
#' @export