Skip to content

Instantly share code, notes, and snippets.

View JakeJing's full-sized avatar

Yingqi Jing JakeJing

  • Uppsala University
View GitHub Profile
@JakeJing
JakeJing / simulate-multinomial-LR.R
Created September 12, 2022 19:33
simulate data from multinomial logistic regression
N <- 1000
J <- 3 # number of predictors (intercept, slp1, slp2)
X <- cbind(1, rnorm(N, 0, 1), rnorm(N, 0, 1)) # N * J
K <- 3 # num of classes
beta <- rbind(c(1, -0.5, 0),
c(-2, 4, 0),
c(3, -1.5, 0))
# J * K (features * classes) with the last class as the reference
# [cls1_intercept, cls2_intercept, cls3_intercept]
# [cls1_x1, cls2_x1, cls3_x1 ]
@JakeJing
JakeJing / multinomial-LR.stan
Created September 12, 2022 18:06
multinomial logistic regression model in stan
data {
int J; // number of predictors or features
int K; // outcome classes
int N;
array[N] int y;
matrix[N, J] X;
}
parameters {
matrix[J, K-1] beta; // number of predictor * number of classes-1
}
fit_lkj_corr = sampling(stan_lkj_corr, data=data_lkj_corr,
chains = 4, iter = 2000, cores = 4)
print(fit_lkj_corr)
# mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#eta 1.48 0.01 0.18 1.15 1.36 1.47 1.60 1.87 1263 1
#lp__ -65.07 0.02 0.73 -67.08 -65.21 -64.78 -64.61 -64.56 1664 1
# y: a list of 100 matrices
y = map(1:100, ~ my_lkj_corr_rng(K = 2, eta = 1.4))
# data_lkj_corr: a list of list
data_lkj_corr = list(N = 100, y = y)
# randomly generate a correlation matrix (R)
my_lkj_corr_rng(K = 2, eta = 1.4)
# [,1] [,2]
#[1,] 1.0000000 -0.8743114
#[2,] -0.8743114 1.0000000
# randomly generate a Cholesky factor (L)
(L_chol = my_lkj_corr_chol_rng(K = 2, eta = 1.4))
# [,1] [,2]
#[1,] 1.00000000 0.000000
my_lkjcorr_fun = "
functions {
// generate lkj correlation matrix (R)
matrix my_lkj_corr_rng(int K, real eta) {
return lkj_corr_rng(K, eta);
}
// generate cholesky factor L_corr of a correlation matrix R
matrix my_lkj_corr_chol_rng(int K, real eta){
return lkj_corr_cholesky_rng(K, eta);
data {
int N; // number of observations
array[N] corr_matrix[2] y; // A n-element arrary of observed correlation matrix [N, 2, 2]
}
parameters {
real<lower=0> eta;
}
model {
for (i in 1 : N) {
y[i, : , : ] ~ lkj_corr(eta); // y[i,] and y[i, ,] should also be fine
@JakeJing
JakeJing / vifmrc_videos.vifmrc
Last active November 11, 2021 17:01
preview videos in vifm
" Video
filetype *.gif,*.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,
\*.fl[icv],*.m2v,*.mov,*.webm,*.ts,*.mts,*.m4v,*.r[am],*.qt,*.divx,
\*.as[fx]
\ {Open in IINA}
\ open -a IINA.app,
\ {Open in QuickTime Player}
\ open -a QuickTime\ Player.app,
\ {Open in MPlayerX}
\ open -a MPlayerX.app,
@JakeJing
JakeJing / vifmrc_pdfs.vifmrc
Last active November 11, 2021 16:59
preview pdfs in vifm
" Pdf
filetype *.pdf open -a Preview %f &
fileviewer *.pdf
\ tput cup %py %px > /dev/tty && sips -s format jpeg %c --out /tmp/tempfile.jpg > /dev/null&& kitty +kitten icat --transfer-mode=file --place=%pwx%ph@%pxx%py /tmp/tempfile.jpg %N
\ %pc
\ kitty +kitten icat --transfer-mode=file --place=%pwx%ph@%pxx%py --clear %N
@JakeJing
JakeJing / vifmrc_pictures.vifm
Last active November 11, 2021 16:58
preview pictures in vifm
" Pictures
filetype *.bmp,*.jpg,*.jpeg,*.png,*.xpm open -a Preview %f &
fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.xpm
\ kitty +kitten icat --transfer-mode=file --place=%pwx%ph@%pxx%py %c %N
\ %pc
\ kitty +kitten icat --transfer-mode=file --place=%pwx%ph@%pxx%py --clear %N