Skip to content

Instantly share code, notes, and snippets.

library(nimble)
library(coda)
## Data
k <- 10
x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(1, 2, 2, 6, 4, 5, 8, 9, 9, 9, 10)
n <- length(x)
## Code
library(nimble)
library(dplyr)
# Data
set.seed(123)
n <- 40
omega <- 0.4
lambda <- 3
y <- rbinom(n, 1, omega) * rpois(n, lambda)
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
##
set.seed(20180816)
N <- 100
Y <- rnorm(N, 4, 1)
@ito4303
ito4303 / KFAS_with_offset.R
Created August 29, 2018 20:12
KFAS model with offset
# KFAS model with offset
set.seed(20180830)
T <- 100
sigma <- 0.05
dens <- rep(NA, T)
dens[1] <- 0.5
for (t in 2:T)
dens[t] <- exp(rnorm(1, log(dens[t - 1]), sigma))
area <- rep(c(10, 20), T / 2)
@ito4303
ito4303 / Pois_negbin.Rmd
Created September 1, 2018 23:00
fit negative binomial to Poisson data
---
title: "fit negative binomial to Poisson data"
output: html_notebook
---
## Data
```{r}
library(ggplot2)
library(magrittr)
library(sf)
library(jpndistrict)
library(stringr)
library(ggplot2)
# 札幌市
sapporo <- jpn_cities(jis_code = sprintf("%05d", 1101:1110))
ggplot(sapporo) +
geom_sf(aes(fill = city_code)) +
scale_fill_discrete(name = "区",
@ito4303
ito4303 / GP_bin.R
Last active January 25, 2019 07:01
Gaussian Process with Bernoulli outcome
#
# Estimating "wiggly" covariate relationships using Gaussian Process
#
# cf. Section 10.14 of
# Kéry and Royle (2016) "Applied Hierarchical Modeling in Ecology" Vol.1
#
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
@ito4303
ito4303 / occ_cov2.stan
Created September 14, 2018 20:32
Stan model for occupancy model with covariates, translated from Section 10.4 of "Advanced Hierarchical Modeling in Ecology, vol. 1" by Kéry and Royle.
data {
int<lower = 1> M;
int<lower = 1> J;
int<lower = 0, upper = 1> Y[M, J];
vector[M] VegHt;
matrix[M, J] Wind;
vector[M] XvegHt;
vector[M] Xwind;
}
transformed data {
@ito4303
ito4303 / distance_sampling-1.stan
Last active September 16, 2018 12:52
Distance sampling model translated from Sec. 8.3.1 of "Advanced 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
vector<lower = 0>[N_ind] X; // Observed distance
int<lower = 0, upper = 1> Y[N_ind + N_z]; // Augumented inds. have y=0 by
// definition
real B; // Strip half-width
// Larger than max observed distance
}
parameters {
@ito4303
ito4303 / distance_sampling-2.stan
Last active September 15, 2018 12:12
Distance sampling model with binned data, translated from Sec. 8.3.1 of "Advanced 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
real B; // Strip half-width
// Larger than max observed distance
int<lower = 1> N_D; // N intervals
int<lower = 1, upper = N_D> D_class[N_ind]; // Distance class
real<lower = 0, upper = B> Delta; // Width of distance bins