Skip to content

Instantly share code, notes, and snippets.

View mcfrank's full-sized avatar

Michael Frank mcfrank

View GitHub Profile
@mcfrank
mcfrank / gamlss_demo.R
Created April 18, 2024 16:19
Demo of making percentiles from CDI data using gamlss
library(wordbankr)
library(langcog)
library(tidyverse)
library(brms)
library(forcats)
library(survey)
library(gamlss)
theme_set(theme_mikabr())
font <- theme_mikabr()$text$family
@mcfrank
mcfrank / habituators.R
Created March 5, 2024 18:22
Finding habituators
# relies on zoo package
# function to find the baseline value
baseline_looking <- function (lts) {
# select lts > 12s
lts_12 <- lts[lts > 12 & !is.na(lts)]
# sum the first three
baseline <- sum(lts_12[1:3], na.rm=TRUE)
@mcfrank
mcfrank / kl_sim.R
Last active October 12, 2023 20:57
pseudocode for knower level simulation
library(tidyverse)
library(assertthat)
shuffle_data <- function(d) {
d$participant_id <- shuffle(d$participant_id)
return(d)
}
get_knower_level <- function (q, r, technique = "perfect") {
@mcfrank
mcfrank / bayesian_survival.R
Created May 19, 2023 18:52
Bayes factors for Cox regression
# approach from
# https://www.medrxiv.org/content/10.1101/2022.11.02.22281762v1.full.pdf
library(tidyverse)
library(survival)
library(ggsurvfit)
#devtools::install_github("maxlinde/baymedr")
library(baymedr)
# library(BayesSurvival)
---
title: "mtcars example markdown"
author: "Mike Frank"
date: "2023-03-21"
output:
html_document:
toc: true
toc_float: true
---
@mcfrank
mcfrank / contrast_demo.R
Created February 14, 2023 22:02
Lab meeting contrasts demo
library(tidyverse)
library(lme4)
sgf <- read_csv("https://raw.githubusercontent.com/langcog/experimentology/main/data/tidyverse/stiller_scales_data.csv") |>
mutate(age_group = cut(age, 2:5, include.lowest = TRUE),
condition_f = factor(ifelse(condition == "Label",
"Experimental", "Control")),
age_centered = age - mean(age))
mod1 <- glmer(correct ~ age * condition + (1|subid) + (1|item),
# starts at line 716 of paper.Rmd
d_lmer_scale <- d %>%
filter(trial_type != "train") %>%
mutate(log_lt = log(looking_time),
age_mo = scale(age_mo, scale = FALSE),
trial_num = trial_num - 8.5,
item = paste0(stimulus_num, trial_type)) %>%
filter(!is.na(log_lt), !is.infinite(log_lt))
@mcfrank
mcfrank / gam_cdi.R
Created November 2, 2022 22:11
simple example of fitting GAMs to CDI data
library(tidyverse)
library(gamlss)
d <- readxl::read_excel("sample_data.xlsx")
# model
max_vocab <- max(d$`Vocabulary production`)
# transformation to 0-1 for beta model
# note that beta data cannot be exactly 0 or 1, it may be necessary to add/subtract .001 for data including 0s and 1s
# delta rule formula
# (i-o) o (1-o)
library(tidyverse)
d <- expand_grid(i = seq(0.05,.95,.05),
o = seq(0.05,.95,.05)) |>
mutate(last_part = o * (1-o),
first_part = abs(i-o),
delta = abs((i-o) * o * (1-o)),
divergence = i * log(i/o))
@mcfrank
mcfrank / scopus_bib.R
Created May 11, 2022 19:32
example use of Scopus API for a citation network
library(rscopus)
library(tidyverse)
library(igraph)
library(GGally)
faculty <- read_csv("faculty.csv")
faculty$au_id <- NA
for (i in 1:nrow(faculty)) {
print(i)