Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / model_1_1_choice.stan
Last active June 4, 2018 02:36
copy of model_1_1_choice.stan
functions {
real f(real mu, real s, real x){
// separately performing numerical integration to make this computing fast
return(normal_lpdf(x | mu, s));
}
real log_lik_Simpson(real mu, real s, real a, real b, int M) {
// numerical integration using the Simpson's rule
vector[M+1] lp;
real h;
@kagaya
kagaya / kick_model_1_1_choice.R
Last active June 4, 2018 02:39
copy of kick_model_1_1_choice.R
library(rstan)
library(tidyverse)
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 %>%
select(c('shell_width', 'choice',
@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)
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
return(waic)
}
@kagaya
kagaya / across_facet_plot.R
Last active July 13, 2024 12:59
plot a segment connecting points across facets in ggplot
library(tidyverse)
library(grid)
library(gtable)
plot_segment_across_facets <- function(p, from=1, to=2,
from_point_id=1,
to_point_id=1,
plotout = F,
gp=gpar(lty=1, alpha=0.5)){
@kagaya
kagaya / model_selection_by_WAIC.rmd
Last active March 5, 2019 01:15
an example of model selection of hierarchical Bayesian model by WAIC / draft
---
title: "an example of model comparison by WAIC (draft)"
author: "Katsushi Kagaya"
date: "`r Sys.time()`"
output:
html_document: default
---
# motivation and aims
functions {
vector my_student_t_rng(int N, real nu, real mu, real sigma){
vector[N] Y;
for (n in 1:N){
Y[n] = student_t_rng(nu, mu, sigma);
}
return(Y);
}
vector my_ZIP_rng(int N, real q, real lambda){
@kagaya
kagaya / random_variable_with_Tidyeval.ipynb
Last active September 26, 2019 16:51
Tidyevalで関数としての確率変数を表現してみる
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.