Skip to content

Instantly share code, notes, and snippets.

@jirilukavsky
jirilukavsky / recovery_pois_binomial.R
Created June 26, 2024 07:23
Testing how accurate is parameter recovery for Poisson and binomial models for a large N=1000. Combinations of zeros (0 hits of 0 trials) do not pose a problem.
library(tidyverse)
library(brms)
library(tidybayes)
n <- 1000
lambda <- 2
p <- 0.50
x <- rpois(n, lambda)
y <- rbinom(n, x, p)
@jirilukavsky
jirilukavsky / murdock.R
Last active June 17, 2024 07:18
Reconstruction of the distinctiveness measure from Murdock 1960 paper
murdock_D <- function(x, pc = T) {
n <- length(x)
mr <- matrix(x, nrow = n, ncol = n)
mc <- matrix(x, nrow = n, ncol = n, byrow = T)
md <- abs(mr - mc)
td <- rowSums(md)
dpc <- td / sum(td)
if (pc) {
# percentages or D%
return(dpc)
@jirilukavsky
jirilukavsky / new_package.R
Created February 16, 2024 08:36
New R package
# adapted from https://usethis.r-lib.org/
library(usethis)
# Create a new package -------------------------------------------------
path <- file.path(getwd(), "jatosR")
create_package(path)
# only needed since this session isn't interactive
proj_activate(path)
use_mit_license("Jiri Lukavsky")
@jirilukavsky
jirilukavsky / motrack_planets.R
Last active April 7, 2021 17:55
Accelerating circular movements in motrack. Example of custom step_function and variable speed.
library(tidyverse)
library(motrack)
f1 <- function(time, moment) {
# base speed = 3, acceleration (increasing by) 1 deg/s^2
3 + time
}
position <- tibble(object = 1:4, x = c(-7, 5, 7, -5), y = c(5, 7, -5, -7))
@jirilukavsky
jirilukavsky / make_grid.R
Created August 6, 2020 11:30
Processing images for memory experiments
# make the grid file
grid_size <- 900
n_square <- 6
ss <- 900 / n_square # square size
d <- array(0, c(grid_size, grid_size, 4))
fillcolor <- c(235, 220, 36, 128)
for (i in 1:n_square) {
@jirilukavsky
jirilukavsky / variance_estimate.R
Created May 5, 2020 09:55
Single variance estimate
# When we calculate variance (SD) from single observation (one group),
# how good/bad estimate this can be of the underlying SD?
B <- 100
n <- 6
m <- 50
s <- 15
estim_sd <- numeric(B)
set.seed(1010)
for (i in 1:B) {
---
title: "Sample estimation for correlation coefficients"
author: "Jiri Lukavsky"
date: "12/20/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
@jirilukavsky
jirilukavsky / quickpsy_example.R
Created February 6, 2019 09:49
Sample data and minimalistic code to show analysis of psychophysics data. Data are from Muller-Lyer illusion experiment, where people compare a length of the line with a standard (featuring Muller-Lyer endings).
# example of fitting psychophysic curve to data are from one subject
# data from Muller-Lyer illusion, subjects are supposed to compare stimulus to
# standard (length = 100)
# columns:
# - length - length of stimulus line
# - pp - proportion of saying "longer"
# - id
# - N - is 10 for each value of length
# - k - count of saying "longer" (k = pp * N)
@jirilukavsky
jirilukavsky / rtips.R
Last active June 24, 2019 19:54
Useful R code snippets that I often look for
# --------- RStudio -------------------
# knit from CLI
rmarkdown::render("test.Rmd", "html_document")
# --------- data manipulation ---------
# wide-to-long = gather, long-to-wide = spread
wide %>%
gather(key = column_name_of_column_names,
value = column_name_of_column_data,
-allvariables, -whichwewant, -ineachrow)
@jirilukavsky
jirilukavsky / wilcox_effectsize.R
Created February 15, 2018 09:58
How to report effect size in Wilcoxon signed-rank test
# How to report effect size in Wilcoxon signed-rank test
# links
# [1] https://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test
# [2] https://stats.stackexchange.com/questions/229760/wilcoxon-signed-rank-test-in-r/229761
# [3] https://stats.stackexchange.com/questions/41620/output-of-one-tailed-wilcoxon-sign-rank-test-in-r
# [4] https://stats.stackexchange.com/questions/133077/effect-size-to-wilcoxon-signed-rank-test
# [5] Acion, L., Peterson, J. J., Temple, S., & Arndt, S. (2006). Probabilistic index: an intuitive non-parametric approach to measuring the size of treatment effects. Statistics in Medicine, 25(4), 591–602. https://doi.org/10.1002/sim.2256
# How Wilcoxon signed-rank test works + what it reports