Skip to content

Instantly share code, notes, and snippets.

@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
@jirilukavsky
jirilukavsky / simpson_paradox.R
Created February 28, 2017 11:40
Simpson's paradox demo inspired by the kidney-stone example on Wikipedia. You can play with proportions and n.
# data from
# https://en.wikipedia.org/wiki/Simpson's_paradox#Kidney_stone_treatment
# in all tabs: row 1 successful, row 2 failed treatment
# small stones
mk = matrix(c(81, 87-81, 234, 270-234), ncol=2)
chisq.test(mk)
# large stones
vk = matrix(c(192, 263-192, 55, 80-55), ncol=2)
chisq.test(vk)
@jirilukavsky
jirilukavsky / psychometric.py
Created February 15, 2017 08:46
Fitting psychometric function in Python
import numpy as np
from scipy.optimize import curve_fit
import scipy as sy
import matplotlib.pyplot as plt
d = np.array([75, 80, 90, 95, 100, 105, 110, 115, 120, 125], dtype=float)
p1 = np.array([6, 13, 25, 29, 29, 29, 30, 29, 30, 30], dtype=float) / 30. # scale to 0..1
# psychometric function
def pf(x, alpha, beta):