Skip to content

Instantly share code, notes, and snippets.

@ito4303
ito4303 / distance_sampling-3.stan
Created September 15, 2018 10:20
Distance modeling for point transect data, translated from Sec. 8.3.4 of "Applied Hierarchical Modeling in Ecology, vol. 1"
data {
int<lower = 1> N_D;
int<lower = 1> N_ind;
real B;
int <lower = 1, upper = N_D> D_class[N_ind];
real<lower = 0, upper = B> Delta; // Width of distance bins
vector<lower = 0, upper = B>[N_D] Midpt; // Interval mid-points
}
transformed data {
vector<lower = 0, upper = 1>[N_D] pi = 2 * Midpt / square(B) * Delta;
@ito4303
ito4303 / distance_sampling-4.stan
Last active September 15, 2018 12:17
Distance sampling for point transect data with data argumentation, translated from Sec. 8.3.4 of "Applied Hierarchical Modeling in Ecology, vol. 1"
data {
int<lower = 1> N_ind; // Number of individuals
int<lower = 1> N_z; // Number of augment observed data
int<lower = 0, upper = 1> Y[N_ind + N_z]; // Augumented inds. have y=0 by
// definition
int<lower = 1> N_D;
real B;
int <lower = 1, upper = N_D> D_class[N_ind];
real<lower = 0, upper = B> Delta; // Width of distance bins
vector<lower = 0, upper = B>[N_D] Midpt; // Interval mid-points
@ito4303
ito4303 / hds1.stan
Created September 16, 2018 11:49
Hierarchical Distance Sampling model using data augmentation, translated from Sec. 8.5.2 of "Advance Hierarchical Modeling in Ecology, vol. 1"
data {
int<lower = 1> N_sites;
int<lower = 0> N_ind;
int<lower = 0> N_z;
real<lower = 0> B;
vector[N_sites] Habitat;
vector[N_sites] Wind;
int<lower = 0, upper = 1> Y[N_ind + N_z];
vector<lower = 0, upper = B>[N_ind] D;
int<lower = 1, upper = N_sites> Site[N_ind];
@ito4303
ito4303 / hds2.stan
Created October 4, 2018 10:44
Hierarchical distance sampling model with binned distance data, translated from Section 8.5.2 of "Applied Hierarchical Modeling in Ecology" by Kéry and Royle
data {
int<lower = 1> N_sites;
int<lower = 0> N_ind;
int<lower = 0> N_z;
int<lower = 1> N_D;
real<lower = 0> B;
real<lower = 0> Delta;
vector<lower = 0, upper = B>[N_D] Midpt;
vector[N_sites] Habitat;
vector[N_sites] Wind;
@ito4303
ito4303 / Hokkaido_2018_earthquake.R
Last active December 25, 2018 21:31
2018年北海道胆振東部地震による山腹崩壊地を図示する
library(ggplot2)
library(sf)
library(ggspatial)
# Data source: http://www.pref.hokkaido.lg.jp/sr/srk/OPD.htm
# CC-BY 北海道水産林務部林務局森林計画課
houkai <- read_sf("山腹崩壊.shp", options = "ENCODING=CP932")
sum(st_area(houkai))
@ito4303
ito4303 / diff_simulation.R
Created January 24, 2019 11:29
Correlation between two variables with one common subtracter
library(GGally)
sc <- 4
ht <- 3
wd <- 4
set.seed(101)
N <- 100
X <- rnorm(N, 0, 1)
Y <- rnorm(N, 0, 1)
@ito4303
ito4303 / diff_model.R
Last active January 25, 2019 06:55
Modeling of two variables with one common subtracter
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
set.seed(105)
N <- 100
X <- rnorm(N, 0, 1)
Y <- rnorm(N, 0, 1)
Z <- rnorm(N, 0, 1)
@ito4303
ito4303 / n_mixtuture_2.stan
Created January 26, 2019 04:04
N-mixture model with 2 replicated observations using Stan
/**
* N-mixture model with 2 replicated observations
*
* References
* Dennis et al. (2015) Computational aspects of N-mixture models.
* Biometrics 71:237--246. DOI:10.1111/biom.12246
* Stan users mailing list
* https://groups.google.com/forum/#!topic/stan-users/9mMsp1oB69g
*/
@ito4303
ito4303 / rounding.rmd
Last active March 6, 2019 06:13
Dealing with rounded data in Stan
---
title: "Rounding"
output: html_notebook
---
```{r}
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
```
@ito4303
ito4303 / read_cmdstan.R
Created July 28, 2019 00:26
Read CSV files containing CmdStan results
library(coda)
library(readr)
read_cmdstan <- function(files) {
m <- lapply(files, function(f) {
d <- read_csv(f, comment = "#")
as.mcmc(d)
})
as.mcmc.list(m)
}