Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / Supplementary_Information.rmd
Last active February 4, 2020 16:01
Supplementary information for the paper entitled "Customizing material into embodied cap by sponge crab" by Harada and Kagaya (https://doi.org/10.1101/330787)
---
title: Supplementary information for the paper 'Individual behavioral type captured by a Bayesian model comparison in cap making by sponge crab'
---
This document is the supplementary information for the paper 'Individual behavioral type captured by a Bayesian framework in cap making by sponge crab' by Harada, Hayashi and Kagaya.
The basic conceptural framework for data analysis in the paper is a Bayesian framework based on the mathematical theory founded by Dr. Akaike and Dr. Watanabe (Watanabe, 'Mathematical Theory of Bayesian Statistics', 2018). The author of this document wrote an example of model selection by Widely Applicable Information Criterion (WAIC) using the data of 'artificial case' (http://rpubs.com/katzkagaya/460937). It may be of help for clarifying what the analysis of this document is doing, by using numerically generated 'artificial' data and several relatively simple hierarchical models.
In this study, we deal with the behavioral data of a 'natural case'. Therefore, we do not know
@kagaya
kagaya / main.rmd
Last active February 1, 2019 11:33
Harada and Kagaya, Customizing material into embodied cap by sponge crab, https://doi.org/10.1101/330787
---
header-includes:
- \usepackage{setspace}
- \doublespacing
- \usepackage{lineno}
- \linenumbers
output:
pdf_document: default
html_document:
df_print: paged
@kagaya
kagaya / plot_f.R
Last active January 30, 2019 06:53
plot functions for Harada and Kagaya, https://doi.org/10.1101/330787
library(tidyverse)
library(ggrepel)
library(rstan)
rstan::expose_stan_functions("my_stan_functions.stan") # https://gist.github.com/kagaya/c726f16d82b80026bb1924b408a72b5c
source("utility.R")
###### choice #####
@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 / 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 / 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_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