Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / SIR_randomwalk.R
Created January 13, 2022 13:13
SIR model(1d)
library(ggplot2)
library(gganimate)
library(dplyr)
rwSIR1d <- function(N,Tmax,ini,scaling,r,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)
@abikoushi
abikoushi / truncEDF.jl
Last active February 6, 2022 12:19
Empirical Survival Function with Interval Censored and Truncated Data
using Distributions
using Random
using Plots
function p_up!(p,n,m,j1,j2,dj)
bj = zeros(m)
for i in 1:n
q = sum(p[j1[i]:j2[i]])
if q < 1.0
b = (1.0-q)/q
@abikoushi
abikoushi / DICRT.jl
Created February 6, 2022 13:56
Empirical Survival Function with Doubly Interval Censored and Right Truncated Data
using Distributions
using Random
using Plots
function p_up!(p,n,m,j1,j2,dj)
bj = zeros(m)
for i in 1:n
if j1[i]>0 && j2[i]>0
q = sum(p[j1[i]:j2[i]])
if q > 0.0
@abikoushi
abikoushi / RMST.jl
Last active February 10, 2022 14:42
Restricted Mean Survival Time
using Distributions
using SpecialFunctions
using QuadGK
using Plots
#integral erfc((log(x) - a)/b) dx = x erfc((log(x) - a)/b) - e^(a + b^2/4) erf((2 a + b^2 - 2 log(x))/(2 b)) + 定数
function lbcdf(d::LogNormal, x::Real)
mu, sigma = params(d)
return 0.5*(1+x*erfc((log(x) - mu)/(sigma*sqrt(2)))*exp(-(mu + (sigma^2)/2)) - erf((mu + sigma^2 - log(x))/(sigma*sqrt(2))))
end
@abikoushi
abikoushi / SIRgrid.R
Created February 27, 2022 10:40
SIR model on the lattice
library(dplyr)
library(tidyr)
library(gganimate)
SIRsim <- function(beta,gamma,nx,ny,nt,seed=NULL){
if(is.null(seed)){
set.seed(seed)
}
mat <- matrix(0L,nx+2,ny+2)
mat[sample.int(nx,1)+1L,sample.int(ny,1)+1L] <- 1L
@abikoushi
abikoushi / MLE_Gamma.jl
Created February 27, 2022 14:53
ニュートン法(ガンマ分布の最尤推定)
using Distributions
using SpecialFunctions
using Random
using Plots, StatsPlots
function Mstep(d::Gamma, y)
shp, scl = params(d)
rho = log(shp)
meanlogy = mean(log,y)
#f(x,shp) = -shp*log(scl) + (shp-1)*log(x) - (x/scl) -loggamma(shp)
@abikoushi
abikoushi / wilkox1.R
Created April 7, 2022 13:07
An comparison between Wilcoxon and Student's t test
library(parallel)
###
#panel function for pairs-plot
panel.ecdf <- function(x,...){
x <- sort(x)
y <- seq(0,1,length.out=length(x))
lines(x, y, type="s", col="black",lwd=2)
abline(a = 0, b = 1, col="cornflowerblue", lty="dotted", lwd=2)
}
@abikoushi
abikoushi / LWR.tex
Created May 26, 2022 12:57
TikZ example
\documentclass[dvipdfmx, tikz, margin=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step=1cm, color = gray] (0, 0) grid (3, 3);
\foreach \x in {0, 1, 2}
{
\draw [->, thick](\x + 0.5 , 2 + 0.5) -- (\x + 1.5 - 0.1, 2 - 0.5 + 0.1);
}
\foreach \x in {0, 1, 2}
@abikoushi
abikoushi / rect.tex
Created May 28, 2022 06:00
TikZ example
\documentclass[dvipdfmx, tikz, margin = 1pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{var/.style = {draw, circle}};
\node[var](A){A};
\node[var, below left of = A](B){B};
\node[var, below right of = A](C){C};
\draw[->] (B)--(A);
\draw[->] (C)--(A);
@abikoushi
abikoushi / nuriwake_chizu.R
Created May 30, 2022 11:30
choropleth map of Japan
nuriwake_chizu <-function(d,pal.name="Oranges"){
n.FD <-nclass.FD(d)
n.bins <-min(n.FD,9)
colclass=cut(d,seq(min(d),max(d),length.out = n.bins+1),include.lowest = TRUE,dig.lab = 2)
col_ <-RColorBrewer::brewer.pal(n.bins,name=pal.name)
col_tab <-data.frame(class=factor(levels(colclass)),col=col_,stringsAsFactors = FALSE)
outcol <-merge(data.frame(id=1:47,class=colclass),col_tab,by="class")
outcol <- outcol[order(outcol$id),]
old_par =par(no.readonly = TRUE)
par(mai=c(0,0,0,0))