Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / tanuki.R
Created June 9, 2022 23:58
Japanese traditional cryptogram
tanuki <- function(x,n){
letter <- "た"
len <- nchar(x)
p <- sort(sample(2:len,n))
sv <- character(n+1)
sv[1] <-substr(x,1,p[1])
for(i in 1:(n-1)){
sv[i+1] <- substr(x,p[i]+1L,p[i+1])
}
sv[n+1L] <- substr(x,p[n]+1L,len)
@abikoushi
abikoushi / AbeURI.tex
Created July 16, 2022 23:29
An example of TikZ
\documentclass[dvipdfmx, border=2.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle](t1){$t=1$};
\node[rectangle, below = of t1](t2) {$t=2$};
\node[rectangle, below = of t2](t3) {$t=3$};
\node[rectangle, draw=black, right = of t1](j1) {玉森裕太};
@abikoushi
abikoushi / gengamma.jl
Created July 27, 2022 13:28
Generalized gamma distribution in Julia
module GG
using Random
using Distributions
using SpecialFunctions
import Distributions: ccdf, cdf, logpdf, pdf, quantile, mean, rand, params, shape, scale
import Distributions: @distr_support
struct GeneralizedGamma{Ta, Tb, Tk} <: ContinuousUnivariateDistribution
a::Ta
@abikoushi
abikoushi / 精神科病院.R
Created August 1, 2022 01:55
e-stat API のデモ
library(estatapi)
library(tidyverse)
myappId <-"ここには自分のアプリケーションIDを入れる"
dat1 <-estat_getStatsData(appId = myappId, statsDataId = "0000010209")
#print(unique(dat1$`I 健康・医療`)) #ほしい項目を目で探す
nyuin <-dplyr::filter(dat1,`I 健康・医療`=="#I04103_精神科病院年間新入院患者数(人口10万人当たり)" ,
地域=="全国") %>%
library(mvtnorm)
library(dplyr)
library(ggplot2)
combrect <- function(X){
B <- combn(nrow(X),2)
#shuffle <- sample.int(nrow(B))
apply(B, 2, function(i){
data.frame(x = X[i[1],1], xmax = X[i[2],1],
y = X[i[1],2], ymax = X[i[2],2])})
@abikoushi
abikoushi / estat.R
Created September 17, 2022 04:58
estat api のデモ
library(estatapi)
library(ggplot2)
library(dplyr)
myappId <-"ここには自分のアプリケーションIDを入れる"
#dat_meta <- estat_getStatsList(myappId, "人口動態調査")
#View(dat_meta)
@abikoushi
abikoushi / koyo.R
Created October 24, 2022 09:31
estat API のデモ(雇用)
library(estatapi)
library(ggplot2)
library(dplyr)
library(tidyr)
myappId <-"ここには自分のアプリケーションIDを入れる"
# データを探す
# dat_meta <- estat_getStatsList(myappId, "労働力調査")
# View(dat2)
@abikoushi
abikoushi / pois_anime.R
Last active November 8, 2022 12:18
gganimate ポアソン分布
library(gganimate)
library(ggplot2)
#library(tidyr)
sim_pois1 <- function(t_n, p_n, w, seed){
set.seed(seed)
l <- 5 - 0.5*w
r <- 5 + 0.5*w
counts <- integer(t_n)
p_list <- vector("list", t_n)
@abikoushi
abikoushi / gibbs_logistic.R
Created December 13, 2022 10:37
R implementation of Polson 2013 "Bayesian inference for logistic models using Polya-Gamma latent variables"
library(BayesLogit)
library(ggplot2)
#y: response variable
#n: binomial size parameter
#X: explanatory design matrix
#lambda: prior parameter
gibbs_logistic <- function(y,n,X,lambda,iter){
N <- length(y)
d <- ncol(X)
B <- diag(lambda, d)
@abikoushi
abikoushi / gibbs_mlogit.R
Last active December 14, 2022 04:00
R implementation of Polson 2013 "Bayesian inference for logistic models using Polya-Gamma latent variables" (multinomial version)
library(BayesLogit)
library(ggplot2)
#########
softmax <- function(x){
m <- max(x)
u <- x-m
exp(u)/sum(exp(u))
}
logsumexp <- function(x){