Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / rcate.cpp
Created June 7, 2024 03:05
Categorical sampling (for Rcpp)
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
mat rcate(const int N, const rowvec & p){
int K = p.n_cols;
rowvec cump = cumsum(p);
mat x(N, K);
@abikoushi
abikoushi / poisreg_wsp.r
Created June 6, 2024 15:57
Doubly-Sparse variational poisson regression (proto-type)
doVB <- function(y,X,a=0.5,b=0.001,iter=200){
K <- ncol(X)
sxy = t(X)%*%y
lambda <- rgamma(K,1,1)
ahat <- rep(a,K)
bhat <- rep(b,K)
for (i in 1:iter) {
for(j in 1:K){
ahat[j] = sxy[j]+a
bhat[j] = X[,j]%*%exp(X[,-j,drop=FALSE]%*%log(lambda[-j]))+b
@abikoushi
abikoushi / poisreg_sp.R
Last active June 6, 2024 15:01
Sparse poisson regression (proto-type)
####
#Negative sampling
####
library(Matrix)
library(ggplot2)
##tau = prior param.
#grad
dlogll <- function(lambda,Y,X,W,tau){
as.vector(t(X)%*%(lambda-Y)) + 0.5*tau*W
}
@abikoushi
abikoushi / signP.r
Created June 6, 2024 07:49
P-value function of sign test
library(ggplot2)
library(dplyr)
sign_p <- function(mu, x, prob=0.5){
x2 <- x-mu
x2 <- x2[x2 != 0]
pos <- sum(x2>0)
p1 = pbinom(pos, length(x2), prob = prob, lower.tail = FALSE)
p2 = pbinom(pos, length(x2), prob = prob, lower.tail = TRUE)
data.frame(p = 2*min(p1, p2), location=mu)
@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)