Skip to content

Instantly share code, notes, and snippets.

@jirilukavsky
jirilukavsky / minimal-TeX-for-Rstudio.md
Last active August 10, 2016 08:29
TeX packages for RMarkdown using minimal BasicTeX

Date: July 1, 2016

I use TeX only to create PDFs in RMarkdown (using RStudio). After clean install, I wanted to save some space and include only the essential TeX packages. I chose BasicTeX 2016 (slim version of MacTeX). I created a new .Rmd document and installed following packages until it compiled successfully.

I installed the packages using TeX Live Utility, maybe it installed also some additional dependencies.

Required packages

  • framed
  • titling
@jirilukavsky
jirilukavsky / tools.R
Last active October 17, 2016 16:58
Useful functions for R
# dplyr ====================
# descriptive stats per group, some columns omitted
selvars <- c(2, 3, 4, 5, 8, 9, 13)
d %>% do(describe(.[["varname"]])[, selvars])
# ggplot2 ==================
# set default theme to bw
theme_set(theme_bw())
# other ====================
@jirilukavsky
jirilukavsky / power-analysis.R
Last active August 10, 2016 07:56
Quick power analysis
# Power analysis examples to answer quick questions.
# - using pwr package in R
# - see also http://www.statmethods.net/stats/power.html
# --------------------------------------------------
# setup
library(pwr)
p <- 0.05
power <- 0.80
@jirilukavsky
jirilukavsky / generate.R
Last active May 23, 2019 03:28
Make MOT videos
library(ggplot2)
library(animation)
library(circular)
# utility functions for trajectory generation -------------------
# - these function came from motAnalysis package
random.positions <- function(n, xlim = c(-10, +10), ylim = xlim,
dot.radius = 0.5, min.dist = 1) {
pos <- list(
@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):
@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 / 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 / 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 / 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)
---
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)