Skip to content

Instantly share code, notes, and snippets.

@kagaya
kagaya / gist:3918842
Created October 19, 2012 15:28
Convert Data from 'autorec.m' (https://gist.github.com/3802904) to Text Formatted Data
function formatted_data = format2txt()
%%
if ismac == 1
file_names = ls();
elseif ispc == 1
file_names = char(mat2str(ls()));
end
data_dir = regexp(file_names, '[0-9]*-[0-9]*\.[0-9]*', 'match');
data_dir
@kagaya
kagaya / Kagaya_Patek_kinem_control_stat.R
Last active March 25, 2018 08:36
functions for statistical analysis and data visualizations in the paper — Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## functions for statistical analysis and data visualizations in the paper,
## The paper's title: Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## This script is written by Katsushi Kagaya (k.kagaya@me.com)
## Gist URL: https://gist.github.com/kagaya/cbafe0a332e6d6766168
## to load libraries
library(ggplot2) # for plotting data and model prediction
library(mgcv) # for generalized addive modeling (GAM fitting)
library(plyr) # for data manipulation
library(nlme) # for linear mixed effects modeling
@kagaya
kagaya / Kagaya_Patek_kinem_control_hsv.R
Last active March 25, 2018 08:36
functions for 'mdf' files which stores digitized kinematic data from MTrackJ — Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## functions for 'mdf' files which stores digitized kinematic data from MTrackJ
## The paper's title: Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## This script is written by Katsushi Kagaya (k.kagaya@me.com)
## Gist URL: https://gist.github.com/kagaya/540596f6d1191c157cb7
library(ggplot2)
library(plyr)
@kagaya
kagaya / Kagaya_Patek_kinem_control_emg.R
Last active March 25, 2018 08:37
functions for physiological data analyses in the paper — Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## functions for physiological data analyses in the paper,
## The paper's title: Motor control of ultrafast, ballistic movements (by K. Kagaya and S. N. Patek)
## written by Katsushi Kagaya (k.kagaya@me.com)
## Gist URL: https://gist.github.com/kagaya/36145a370a1da0f4ba0d
## to load libraries necessary for the current script
library(ggplot2)
library(plyr)
@kagaya
kagaya / gist:3802904
Last active March 25, 2018 09:25
Automatic Recording of Force Data
function ForceData=autorec(duration, strikeThreshold, rewardThreshold)
%% DISCRIPTION
% This program automatically records foce data by detecting rising phase of
% the voltage change and it produce digital output whether the peak of the
% voltage above a threshold. The condition of output can be customized.
%% HOW TO USE
% The termination of this program is tricky. Please press "Ctrl - c" to
% quit the program and after that, command "daqreset" in the MATLAB prompt.
@kagaya
kagaya / Dromiidae.csv
Last active June 3, 2018 07:10
CSV file for Harada and Kagaya, https://doi.org/10.1101/330787
test_num id shell_width gender start_date end_date choice making_process whole_px hole_px cm_per_px leg_lack cutting
1 13 8.04 M 2015/12/16 2015/12/19 L no_digging NA NA NA no f
2 12 6.78 F 2015/12/19 2015/12/20 M NA 92820 51210 0.024360536 no f
3 6 6.43 M 2015/12/20 2015/12/21 L breaking 365628 225539 0.024154589 no t
4 9 3.54 F 2015/12/21 2015/12/22 M NA 98304 29696 0.024183797 no f
5 14 6.83 F 2015/12/22 2015/12/23 L breaking 143793 96282 0.024242424 no t
6 15 6.35 M 2015/12/23 2015/12/24 L breaking 137267 101706 0.024271845 no t
7 16 10.76 M 2015/12/24 2015/12/31 no_choice NA NA NA no f
8 17 7.35 F 2015/12/31 2016/1/4 no_choice NA NA NA no f
9 18 9.8 M 2016/1/4 2016/1/16 no_choice NA NA NA no f
@kagaya
kagaya / utility.R
Last active June 3, 2018 07:11
utilities for Harada and Kagaya, https://doi.org/10.1101/330787
library(tidyverse)
library(forcats)
library(rstan)
library(shinystan)
waic <- function(log_likelihood) {
# from https://gist.github.com/MatsuuraKentaro/3f6ae5863e700f5039c19e36a9bdf646
training_error <- - mean(log(colMeans(exp(log_likelihood))))
functional_variance_div_N <- mean(colMeans(log_likelihood^2) - colMeans(log_likelihood)^2)
waic <- training_error + functional_variance_div_N
@kagaya
kagaya / model_1_2_choice.stan
Last active June 3, 2018 07:12
Stan scripts of Harada and Kagaya, https://doi.org/10.1101/330787 (other than the best performed models)
functions {
real f(real mu, real s, real x){
return(normal_lpdf(x | mu, s));
}
real log_lik_Simpson(real mu, real s, real a, real b, int M) {
vector[M+1] lp;
real h;
h = (b-a)/M;
lp[1] = f(mu, s, a);
@kagaya
kagaya / kick_model_1_1_choice.R
Last active June 3, 2018 11:26
Harada and Kagaya, https://doi.org/10.1101/330787; kicking R codes for the Stan models
library(rstan)
library(tidyverse)
library(shinystan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
# preprocessing
d <- read_csv(file='Dromiidae.csv') # https://gist.github.com/kagaya/0d309397300b7e8294fe53c44b6fc525
d <- d %>%
@kagaya
kagaya / plot_choice.R
Last active June 3, 2018 13:49
part of plot function, choice
library(tidyverse)
library(ggrepel)
library(rstan)
expose_stan_functions("my_stan_functions.stan") # https://gist.github.com/kagaya/c726f16d82b80026bb1924b408a72b5c
source("utility.R") # https://gist.github.com/kagaya/60c4190ac306840daee54115b3c315c3
plot_choice_random_intercept <- function(d, fit){
# mu[n,2] = bias_l[ID[n]] + cwl*C_width[n] + ll*Leg_lack[n];
# mu[n,3] = bias_no0 + cwno*C_width[n] + lno*Leg_lack[n];
ms <- rstan::extract(fit)