Skip to content

Instantly share code, notes, and snippets.

View oliviergimenez's full-sized avatar
🏠
Working from home

Olivier Gimenez oliviergimenez

🏠
Working from home
View GitHub Profile
@oliviergimenez
oliviergimenez / compute-wAIC-in-Jags.R
Last active October 23, 2024 17:20
Compute wAIC with Jags
##################################################################################
# How to calculate wAIC with JAGS: Linear regression example #
# https://sourceforge.net/p/mcmc-jags/discussion/610036/thread/8211df61/#ea5c #
##################################################################################
#--- Simulate data
set.seed(2020) # set seed
n <- 100 # sample size
x <- sort(rnorm(n)) # covariate values
int <- 30 # true intercept
# https://twitter.com/GeoffZahn/status/1318653420027273216?s=20
library(tidyverse)
data_frame(t=seq(-pi, 0, .001),
x1=16*(sin(t))^2,
x2=-x1,
y=13*cos(t) -5 * cos(2*t) -2*cos(3*t) - cos(4*t)) %>%
gather(side, x, x1, x2) %>%
ggplot(aes(x,y)) +
geom_polygon(fill="red") +
coord_fixed() +
@oliviergimenez
oliviergimenez / map_highways.R
Created October 20, 2020 18:36
map highways in Switzerland - France
# map highways in Switzerland - France
# load packages
library(tidyverse)
library(sf)
library(osmdata)
library(mapview)
# define zone of interest
lonmin <- 6
@oliviergimenez
oliviergimenez / prediction_GAM_by_hand.R
Created October 13, 2020 09:31
mimic what mgcv::predict.gam() does
# simple example to mimic what predict.gam does
library(mgcv) # GAM package, see Wood's book https://www.taylorfrancis.com/books/9781315370279
dat <- MASS::mcycle # get mcycle data
head(dat)
# set up a smoother
sm <- smoothCon(s(times, k = 10), data = dat, knots = NULL)[[1]]
# use it to fit a regression spline model
beta <- coef(lm(dat$accel ~ sm$X - 1))
# plot data
plot(dat$times, dat$accel)
@oliviergimenez
oliviergimenez / montpellier_OSM_city_map.R
Created October 13, 2020 09:28
Make a map of Montpellier with R and openstreetmap data
# code from https://gist.github.com/d-qn/4f1c4ed80a4fd6a76cd1153c89f56134
library(tidyverse)
library(sf)
library(osmdata) # to get openstreetmap geo data
# settings
# olivier: get data on Montpellier
bb <- getbb('Montpellier, France') # define the bbox, will be used to fetch OSM data within that box
dest_proj <- 2056