Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / app.r
Last active May 5, 2024 07:23
Shiny application to learn p-value
##
p_stat_t <- function(v, df = 1,
alternative = "two.sided"){
if (!alternative %in% c("two.sided", "less", "greater")) {
stop("alternative must be either \"two.sided\", \"less\", or \"greater\".")
}
if(alternative == "two.sided"){
p0 <- 2*pt(abs(v), df, lower.tail = FALSE)
}else if(alternative == "less"){
p0 <- pt(v, df)
@abikoushi
abikoushi / dotdiagram.R
Last active May 2, 2024 05:10
dot diagram (visualization of `formula`)
library(reshape2)
library(ggplot2)
theme_dotdiagram <- function(...){
theme_minimal(...)%+%
theme(axis.text.y = element_text(colour="black"),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.grid.minor = element_blank(),
@abikoushi
abikoushi / hclust_hull.R
Created April 29, 2024 10:56
Comparison of the methods of `hclust`
library(dplyr)
library(ggplot2)
x <- c("ward.D","ward.D2", "single","complete",
"average","mcquitty","median","centroid")
X <- log(as.matrix(iris[,1:2]))
df <- data.frame(X)
d <- dist(X)
@abikoushi
abikoushi / hclust_distplot.R
Created April 29, 2024 09:25
Comparison of the methods of `hclust`
library(dplyr)
library(ggplot2)
x <- c("ward.D","ward.D2", "single","complete",
"average","mcquitty","median","centroid")
X <- log(as.matrix(iris[,1:4]))
d <- dist(X)
dm <- as.matrix(d)
@abikoushi
abikoushi / mat_anno.tex
Created April 24, 2024 06:48
TikZ example: matrix with annotation
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\begin{tikzpicture}[
%Styles
Matrix/.style={
matrix of nodes,
text height=2.5ex,
@abikoushi
abikoushi / heatmap.r
Created April 19, 2024 04:15
pheatmap: colored row labels text-by-text
library(pheatmap)
library(grid)
mid0scale <- function(test, paletteLength=120){
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength)
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))
return(list(color=myColor, breaks=myBreaks))
}
set.seed(123)
@abikoushi
abikoushi / heatmap.r
Created April 18, 2024 11:30
set color-bar's mid-point to 0 (pheatmap)
library(pheatmap)
# length(breaks) == length(paletteLength) + 1
# use floor and ceiling to deal with even/odd length pallettelengths
mid0scale <- function(test, paletteLength=120){
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength)
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))
return(list(color=myColor, breaks=myBreaks))
}
@abikoushi
abikoushi / centraldogma.tex
Created April 12, 2024 02:25
Tikz graph example
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
%\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}
\node[draw, rounded corners, fill=gray!10](dna) at (0,0){DNA};
\node[draw, rounded corners, fill=gray!10, right = of dna, xshift = +15pt](rna){RNA};
\node[draw, rounded corners, fill=gray!10, right = of rna, xshift = +15pt](protein) {protein};
\node[draw, rounded corners, fill=gray!10, right = of protein](phenotype) {phenotype};
@abikoushi
abikoushi / PoisHMM.R
Created April 3, 2024 12:28
Poisson Hidden Markov Model
# 須山『ベイズ推論による機械学習入門』(講談社)の実装例です
softmax <- function(x){
maxx <- max(x)
exp(x-maxx)/sum(exp(x-maxx))
}
logp_x <-function(x,lambda,loglambda){
x*loglambda-lambda
}
logsumexp <- function(x){
@abikoushi
abikoushi / andrews.R
Created April 2, 2024 07:22
Andrews' curve using geom_function
library(ggplot2)
library(dplyr)
Andrews <- function(t,
Sepal.Length, Sepal.Width,
Petal.Length, Petal.Width,...){
root2 <- sqrt(2)
sapply(t, function(t){Sepal.Length/root2 +
Sepal.Width*sin(t) +
Petal.Length*cos(t) +