Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / sim_signedrank.r
Created June 4, 2024 12:12
distribution of "signed rank" in Wilcoxon's test
library(ggplot2)
out <- vector("list", 10000)
for(i in 1:10000){
x <- rt(10,3)
out[[i]] <- sign(x)*rank(abs(x))
}
ggplot(data=NULL)+
@abikoushi
abikoushi / signedrank.r
Created June 4, 2024 08:30
Singed rank plot
signTable <- function(mu,x){
x2 <- x-mu
x2 <- x2[x2 != 0]
data.frame(table(sign(x2)), loc=mu)
}
wilcoxTable <- function(mu,x){
x2 <- x-mu
x2 <- x2[x2 != 0]
data.frame(w=sign(x2)*rank(abs(x2)), loc=mu)
@abikoushi
abikoushi / ciplot.r
Created June 3, 2024 03:30
Confidence interval via Wilcoxon and sign test
library(ggplot2)
binomfun <- function(c,n){
sum(choose(n,floor(n/2+seq(-c,c)))/(2^n))
}
wilcoxCI <- function(x,level){
xx <- outer(x,x,"+")
u <- sort(xx[lower.tri(xx, diag = TRUE)]/2)
n <- length(u)
@abikoushi
abikoushi / wilcox.r
Created June 2, 2024 01:31
simulation study for wilcox.test
library(ggplot2)
#check the moments
integrate(function(x)x*dexp(x), 0,Inf)
integrate(function(x)x^2*dexp(x), 0,Inf)
integrate(function(x)x*dnorm(x,1,1), -Inf,Inf)
integrate(function(x)x^2*dnorm(x,1,1), -Inf,Inf)
pv_simfun <- function(n1,n2){
x1 <- rexp(n1)
@abikoushi
abikoushi / signtest.r
Created June 1, 2024 08:46
confidence intervals via sign test
binomfun <- function(c,n){
sum(choose(n,n/2+seq(-c,c))/(2^n))
}
signCI <- function(x,level){
n <- length(x)
sx <- sort(x)
alpha_c = sapply(1:(n/2), binomfun, n=n)
k <- which.max(alpha_c-(level)>0)
lower <- sx[k]
@abikoushi
abikoushi / ncbi_search.R
Last active May 21, 2024 06:50
Search NCBI using R
library(rvest)
library(dplyr)
#browseURL(paste0(url_ncbi,qt))
qt <- "ID3" #searh query
NCBI_search <- function(qt){
url_ncbi <- "https://www.ncbi.nlm.nih.gov/gene/?term="
url_t <- paste0(url_ncbi,qt)
html_t <- read_html(url_t)
@abikoushi
abikoushi / gd_norm.R
Created May 15, 2024 08:54
Gradient Discent normal MLE
library(ggplot2)
library(dplyr)
library(numDeriv)
lp <- function(x,mu,u){
-exp(u)*sum((x-mu)^2)/2 + length(x)*u
}
dlp_mu <- function(x,mu,u){
exp(u)*sum((x-mu))
}
@abikoushi
abikoushi / inner_prod.tex
Created May 10, 2024 07:38
tikz example (inner product)
\documentclass[dvipdfmx, dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes, decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\frame{
\begin{figure}
\begin{tabular}{ccc}
inner product of \Huge $($
\begin{tikzpicture}
@abikoushi
abikoushi / poker.R
Created May 8, 2024 10:07
Monte-Carlo simulation of the five cards
drawfive <- function(iter){
three = 0L
four = 0L
onepair = 0L
twopair = 0L
fullhouse = 0L
straight = 0L
flush = 0L
#iter <- 100000
dec <- expand.grid(suit=1L:4L,rank=1L:13L)
@abikoushi
abikoushi / app.R
Last active May 7, 2024 02:06
Shiny application to learn confidence interval (t-dist)
##
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)