Skip to content

Instantly share code, notes, and snippets.

@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) +
@abikoushi
abikoushi / statfuns.R
Created April 2, 2024 05:28
geom_function with facet_wrap
library(dplyr)
library(ggplot2)
set.seed(123)
fdf <- expand_grid(year=1:4, generation=1:4) %>%
mutate(mu=rnorm(16),sigma=abs(rnorm(16)))
layerfun <- split(fdf, interaction(fdf$year, fdf$generation)) %>%
lapply(function(x){
@abikoushi
abikoushi / lwrmodel.R
Created March 26, 2024 14:02
LWR model (deterministic)
#1D
simLWR <- function(r, m, u0,
sys_noise=0,
obs_noise=0,
explicit=FALSE){
n <- length(u0)
if(explicit){
A <- diag(1-r,n)
A[cbind(2:n,1:(n-1))] <- r
}else{
@abikoushi
abikoushi / bob.r
Created March 25, 2024 12:25
Lissajous Curve
library(gganimate)
library(dplyr)
th <- seq(-pi,pi,by=0.01)
a <- seq(1,6, by=0.1)
df <- data.frame(x=c(cos(outer(th,a))),
y=rep(sin(th), length(a)),
a=rep(a, each=length(th)))
ggplot(df, aes(x=x, y=y, group=a, colour=a))+
@abikoushi
abikoushi / diff2d.r
Created March 25, 2024 11:09
Numerical solution of the diffusion equation (finite difference approximation )
library(gganimate)
library(ggplot2)
#install.packages("gganimate")
diffeq <- function(u_ini, r, timestep){
M <- nrow(u_ini)
N <- ncol(u_ini)
u_old <- u_ini
U <- array(0, dim=c(M,N,timestep+1))
U[,,1] <- u_ini
tmp <- matrix(0,M,N)
@abikoushi
abikoushi / dlm_diffeq.R
Last active March 22, 2024 12:19
Dynamic linear model (diffusion equation)
library(ggplot2)
library(reshape2)
library(patchwork)
diffeq <- function(r, u_ini, m,
sys_noise=0,
obs_noise=0,
implicit=FALSE){
n <- length(u_ini)
if(implicit){
@abikoushi
abikoushi / difusion2d.R
Created March 19, 2024 11:48
gganimate; discretize heat equation
difueq <- function(u_ini, timestep){
Deltat <-1/400
Deltax <-1/10
Deltay <-1/10
r <- Deltat/(Deltax^2+Deltay^2)
M <- nrow(u_ini)
N <- ncol(u_ini)
u_old <- u_ini
U <- array(0, dim=c(M,N,timestep+1))
U[,,1] <- u_ini
@abikoushi
abikoushi / PELT.jl
Created March 18, 2024 13:58
Change point finder (PELT algorithm)
module pelt
using Distributions
using Statistics
function costlpnorm(y, sta, en)
suby = y[sta:en]
muhat = mean(suby)
sigmahat = std(suby)
return -sum(x -> logpdf(Normal(muhat, sigmahat), x), suby)
@abikoushi
abikoushi / heatmap_gg.R
Created March 17, 2024 03:32
annotated heat-map in ggplot2
library(ggplot2)
library(tidyr)
library(tibble)
library(dplyr)
#library(pheatmap)
head(iris)
Y <- log(as.matrix(iris[,1:4]))
d <- dist(Y)
h <- hclust(d,method = "ward.D")
@abikoushi
abikoushi / heatmap_gg.R
Created March 16, 2024 00:37
annotated heatmap using ggplot2
library(ggplot2)
library(tidyr)
library(tibble)
library(dplyr)
library(pheatmap)
Y <- log(as.matrix(iris[,-5]))
drow <- rowMeans(sweep(Y,2,Y[1,])^2)
dcol <- colMeans(sweep(Y,1,Y[,1])^2)
df <- rowid_to_column(iris) %>%
mutate(rrow = order(drow)) %>%