Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / datasauRus_svd.R
Created December 14, 2024 04:04
Singular value decomposition of datasauRus
library(datasauRus)
library(ggplot2)
library(dplyr)
X = dplyr::filter(datasaurus_dozen,dataset=="dino") %>%
dplyr::select(-dataset) %>%
as.matrix()
res_svd <- svd(as.matrix(X))
df_svd <- bind_rows(data.frame(res_svd$u, matrix="U"),
@abikoushi
abikoushi / datasauRus.R
Created December 13, 2024 13:15
rotate DataSaurus
library(datasauRus)
library(ggplot2)
library(dplyr)
library(gganimate)
rot <-function(ti, period=16) {
coeff <- 2*pi/period
matrix(c(cos(coeff*ti),-sin(coeff*ti),
sin(coeff*ti), cos(coeff*ti)), byrow = TRUE, nrow = 2)
}
@abikoushi
abikoushi / readmtx.cpp
Last active December 11, 2024 04:29
read multiple lines from mtx file (Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
void readmtx(arma::uvec & row_i,
arma::uvec & col_i,
@abikoushi
abikoushi / readaline.cpp
Created December 10, 2024 03:34
get a line from csv (Rcpp)
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
// [[Rcpp::export]]
std::vector<double> readaline(std::string readtxt, int ind_n) {
std::ifstream file(readtxt);
@abikoushi
abikoushi / query_awk.R
Created December 9, 2024 09:52
Run AWK query from R (change specific rows)
query_awk <- function(vpar, pdir, filename, uid){
chl <- paste0("cat ", pdir, "/", filename)
for(i in 1:nrow(vpar)){
chr <- paste0("\"", paste(vpar[i,], collapse = ", "), "\"")
awk <- paste0(" | awk 'NR==", uid[i], "{sub('/.*/',", chr, ")}1'")
chl <- paste0(chl, awk)
}
chl <- paste0(chl, " > ", pdir, "/tmp.csv")
mvit <- paste0("mv ", pdir, "/tmp.csv " , pdir, "/", filename)
system(chl)
@abikoushi
abikoushi / binormal.r
Created December 6, 2024 07:13
base's `persp` and `contour` example
x = seq(-4,4,length.out=100)
y = seq(-4,4,length.out=100)
df <- expand.grid(x = x, y = y)
prob <- matrix(pnorm(df$x)*pnorm(df$y), 100, 100)
#png("persp.png")
persp(x = x, y = y, z = prob, theta = 20, phi=45)
#dev.off()
#png("contour.png")
@abikoushi
abikoushi / rotate_dino.R
Created December 3, 2024 23:06
rotation matrix with dino
library(datasauRus)
library(ggplot2)
library(dplyr)
library(gganimate)
rot <-function(th) {
matrix(c(cos(th),-sin(th),
sin(th),cos(th)), byrow = TRUE, nrow = 2)
}
@abikoushi
abikoushi / glm_pois.R
Created November 24, 2024 02:19
Comparison the two ratios
n <- 1000
rate <- 1
set.seed(1234)
tau_x <- rgamma(n,1,1)
X <- rpois(n,tau_x*rate)
tau_y <- rgamma(n,2,1)
Y <- rpois(n,tau_y*rate)
#png("ECDF_rate.png")
curve(ecdf(X/tau_x)(x), type = "s", xlim = c(0, 15), ylim=c(0,1), col="royalblue", ylab = "ECDF", xlab = "ratio")
curve(ecdf(Y/tau_y)(x), type = "s", add=TRUE, col="orangered", lty=2)
@abikoushi
abikoushi / Chung2017.R
Created November 21, 2024 22:12
Comparison plots for parallel coordinate vs. UMAP
library(Matrix)
library(readr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(umap)
library(patchwork)
#https://www.weizmann.ac.il/sites/3CA/breast
cells <- read_csv("./data/Data_Chung2017_Breast/Cells.csv")
@abikoushi
abikoushi / pcp_jitter.R
Created November 17, 2024 05:29
Parallel coordinate plot with jitter
library(dplyr)
library(tidyr)
library(ggplot2)
df1 <- mutate(iris, id=1:n()) %>%
pivot_longer(1:4)
ggplot(df1,aes(x=name,y=value,group=id, colour = Species))+
geom_line(alpha=0.5)+
guides(colour=guide_legend(override.aes = list(alpha=1, linewidth=1)))+