Skip to content

Instantly share code, notes, and snippets.

#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo)]]
rowvec rdirichlet(const vec & alpha){
int K = alpha.n_rows;
rowvec y(K);
for(int k=0;k<K ;k++){
y[k] = R::rgamma(alpha[k],1);
require(ggplot2)
require(grid)
require(dplyr)
"%||%" <- function(a, b) {
if (!is.null(a)) a else b
}
geom_grid <- function(mapping = NULL, data = NULL,
position = "identity",
require(ggplot2)
####utilities
"%||%" <- function(a, b) {
if (!is.null(a)) a else b
}
rbind_dfs <- function(dfs) {
out <- list()
columns <- unique(unlist(lapply(dfs, names)))
require(ggplot2)
####utilities
"%||%" <- function(a, b) {
if (!is.null(a)) a else b
}
rbind_dfs <- function(dfs) {
out <- list()
columns <- unique(unlist(lapply(dfs, names)))

簡単な計算の実行

3 + 8
30 / 23
3005 %% 3
sin(100)
log(20)

混合正規分布

set.seed(1)
s <- t(rmultinom(100,1,c(0.3,0.7)))
head(s)
s2 <- apply(s==1,1,which)
mu <- c(-2,2)
sigma <- c(1,1)
y <- rnorm(100,mu[s2],sigma[s2])
@abikoushi
abikoushi / PoissonMixtureCollapsedGibbs.R
Created February 12, 2021 10:41
混合ポアソン分布のための崩壊型ギブスサンプリング
#このソースコードの自由な複製・改変・再配布を認める。
set.seed(1234)
N <- 100
ind <- sample.int(2,N,replace = TRUE)
lambda <- c(2,10)
y <- rpois(N,lambda[ind])
a <- 1 #ガンマ事前分布のパラメータ
b <- 1 #ガンマ事前分布のパラメータ
alpha <-c(2,2) #ディリクレ事前分布のパラメータ
softmax <- function(x){
@abikoushi
abikoushi / SIR_randomwalk.R
Created January 13, 2022 05:56
SIR model on the unit square
library(ggplot2)
library(gganimate)
library(dplyr)
rwbounded <- function(U,scaling){
u <- runif(length(U),-1,1)
return(U + scaling*ifelse(u>0,(1-U)*u,U*u))
}
rwSIR <- function(N,Tmax,ini,scaling,r,infect_p,remove_p,rwfun=rwbounded,seed=NULL){
if(is.null(seed)){
@abikoushi
abikoushi / SIR_randomwalk.R
Last active January 13, 2022 11:42
SIR model on the unit square
library(ggplot2)
library(gganimate)
library(dplyr)
rwSIR <- function(N,Tmax,ini,scaling,r2,infect_p,remove_p,
decay=1,seed=NULL){
rwbounded1 <- function(U,scaling){
u <- runif(length(U),-1,1)
scaling*ifelse(u>0,(1-U)*u,U*u)
}
@abikoushi
abikoushi / SIR_randomwalk.R
Created January 13, 2022 12:41
SIR model on the unit square
library(ggplot2)
library(gganimate)
library(dplyr)
rwSIR <- function(N,Tmax,ini,scaling,r2,infect_p,remove_p,
randu = function(U){runif(length(U),-1,1)},
decay=1,seed=NULL){
rwbounded1 <- function(U,scaling){
u <- randu(U)
scaling*ifelse(u>0,(1-U)*u,U*u)