Skip to content

Instantly share code, notes, and snippets.

@ito4303
ito4303 / Diameter_class_Weibull.Rmd
Created January 21, 2022 23:51
Fitting diameter class data to Weibull distribution
---
title: "R Notebook"
output: html_notebook
---
## Fitting diameter class data to Weibull distribution
```{r setup}
library(cmdstanr)
library(ggplot2)
@ito4303
ito4303 / Truncated_and_censored_Weibull.Rmd
Created January 18, 2022 09:30
Fitting truncated and censored data to Weibull distribution
---
title: "R Notebook"
output: html_notebook
---
## Fitting truncated and censored data to Weibull distribution
Supposing tree diameter data, fitting truncated and censored data to Weibull distribution.
See Chapter 4 [Truncated or Censored Data](https://mc-stan.org/docs/2_28/stan-users-guide/truncated-or-censored-data.html) in the Stan User's Guide.
---
title: "R Notebook"
output: html_notebook
---
Three parameter Weibull distribution
```{r}
library(ggplot2)
library(cmdstanr)
/*
* 2-d integral using Simpson's rule
* based on
* https://statmodeling.hatenablog.com/entry/waic-with-hierarchical-models
*/
functions {
real f(real z, array[] real theta, real x, real y) {
real v;
@ito4303
ito4303 / Stan_2.27_syntax.R
Created June 7, 2021 08:18
A test for Stan 2.27 syntax
library(ggplot2)
library(cmdstanr)
library(posterior)
library(bayesplot)
# data
set.seed(1234)
n <- 160
n_block <- 8
x <- runif(n, 0, 1)
@ito4303
ito4303 / real_multinomial.Rmd
Created December 15, 2020 08:38
Multinomial distribution with real valued outcome in Stan
---
title: "R Notebook"
output: html_notebook
---
## Library
```{r}
library(cmdstanr)
```
@ito4303
ito4303 / test_code.stan
Created October 25, 2020 01:07
multithreaded N-mixture model using Reduce Sum and bivariate Poisson distribution in Stan
functions {
/**
* Returns log likelihood of N-mixture model
* with 2 replicated observations using
* bivariate Poisson distibution
*
* References
* Dennis et al. (2015) Computational aspects of N-mixture models.
* Biometrics 71:237--246. DOI:10.1111/biom.12246
* Stan users mailing list
@ito4303
ito4303 / n-mixture_reduce_sum.R
Last active October 25, 2020 01:08
multithreaded N-mixture model using Reduce Sum in Stan
# The simplest possible N-mixture model from Section 6.3 of
# Applied Hierarchical Modeling in Ecology.
library(cmdstanr)
set_cmdstan_path("/usr/local/cmdstan")
# generate simulated data
lambda <- 2.5 # mean abundance
p <- 0.4 # detection probability
@ito4303
ito4303 / test_code.stan
Created October 24, 2020 06:51
Testing Reduce Sum in CmdStan 2.24 using N-mixture model
functions {
real partial_sum(int[] dummy_k,
int start, int end,
int[] count,
real lambda,
real p) {
vector[end - start + 1] lp;
for (k in start:end) {
if (k - 1 < max(count))
@ito4303
ito4303 / hmm_test3.R
Last active August 24, 2020 04:34
Testing a hidden Markov model function incorporated into CmdStan 2.24
#
# HMM Test for CmdStan 2.24
#
library(rstan)
options(mc.cores = parallel::detectCores())
library(cmdstanr)
set_cmdstan_path("/usr/local/cmdstan")
library(extraDistr)