This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Graphical LASSO copula model | |
library(glasso) | |
library(MASS) | |
library(Matrix) | |
set.seed(45) | |
glasso_copula <- function(df, rho, penalize.diagonal = FALSE, ...) { | |
N <- nrow(df) | |
P <- ncol(df) | |
if (missing(rho)) rho <- 2*sqrt(log(P)/N) # sls ch 9 p 252 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup | |
set.seed(45) | |
N <- 1000 | |
## alice | |
Pa <- 20 | |
Xa <- matrix(rnorm(N*Pa), N) | |
## bob | |
Pb <- 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(45) | |
N <- 100 | |
P <- 2 | |
A <- matrix(rnorm(N*P), N) | |
translation <- c(-0.9, .6) | |
rotation <- function(theta) matrix(c(cos(theta), sin(theta), -sin(theta), cos(theta)), 2) | |
R <- rotation(pi/5) | |
B <- t(apply(A, 1, function(x) R %*% (x + translation))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load packages | |
library(tidyverse) | |
library(sf) | |
# read data | |
flow <- read_csv("https://data.mendeley.com/public-files/datasets/rtn8t47t6j/files/7f85a66d-a80d-4e3d-ada8-694a9c6e0a28/file_downloaded") | |
gem <- st_read("WFS:https://geodata.nationaalgeoregister.nl/wijkenbuurten2018/wfs?&request=GetCapabilities&service=WFS", | |
"wijkenbuurten2018:gemeenten2018") | |
# clean gemeente dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lavPredict from tensorsem parameters | |
library(lavaan) | |
library(tensorsem) | |
mod <- " | |
# three-factor model | |
visual =~ x1 + x2 + x3 | |
textual =~ x4 + x5 + x6 | |
speed =~ x7 + x8 + x9 | |
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gaussian mixture modeling with EM | |
priorp <- 0.6 | |
m1 <- 0 | |
m2 <- 2 | |
s1 <- 1 | |
s2 <- 0.707 | |
# generate some data with 2 classes | |
N <- 1000 | |
cl <- rbinom(N, 1, priorp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kmedoids <- function(X, K, max_iter = 100) { | |
X <- as.matrix(X) | |
N <- nrow(X) | |
P <- ncol(X) | |
C <- matrix(0, nrow = K, ncol = P) | |
clus <- sample(K, N, replace = TRUE) | |
converged <- FALSE | |
iter <- 0 | |
while (!converged && iter < max_iter) { | |
old_clus <- clus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bayesian test for partial intransitivity using monte-carlo posterior approximation | |
# (c) Erik-Jan van Kesteren, 2021 | |
# Based on an e-mail discussion with Tony Marley | |
library(expm) | |
library(tidyverse) | |
# Beta prior (1, 1 means uniform) | |
prior_a <- 1 | |
prior_b <- 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Meuse universal kriging for a large grid | |
# The grid is enriched with osm data | |
# CC-BY @vankesteren | |
# load packages ---- | |
library(tidyverse) | |
library(sf) | |
library(ggspatial) | |
library(osmdata) | |
library(gstat) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gierzwaluw (common swift) analysis using osmenrich | |
# Last edited 2021-02-09 by @vankesteren | |
# CC-BY ODISSEI SoDa team | |
# Packages | |
# Data | |
library(tidyverse) | |
library(sf) | |
library(osmenrich) |