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 / 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
@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 / 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
# 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 / compute-wAIC-in-Jags.R
Last active May 5, 2023 11:17
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
@oliviergimenez
oliviergimenez / lifedead_og.R
Created November 26, 2020 09:13
Fit model combining live recapture and dead recoveries in Jags using SSM formulation of multistate models
# Fit model combining live recapture and dead recoveries in Jags using SSM formulation of multistate models
# Warning: if initial values are generated from the ld.init() function, we failed at recovering the values
# used to simulate the data; everything goes well if we use the true latent states (data are simulated) as initial values
# Define function to simulate multistate capture-recapture data
simul.ms <- function(PSI.STATE, PSI.OBS, marked, unobservable = NA){
# Unobservable: number of state that is unobservable
n.occasions <- dim(PSI.STATE)[4] + 1
CH <- CH.TRUE <- matrix(NA, ncol = n.occasions, nrow = sum(marked))
# Define a vector with the occasion of marking
@oliviergimenez
oliviergimenez / dipper.csv
Created December 3, 2020 04:41
Use Nimble to generate inits for latent states
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
h1;h2;h3;h4;h5;h6;h7;males;females
1;1;1;1;1;1;0;1;0
1;1;1;1;1;0;0;0;1
1;1;1;1;0;0;0;1;0
1;1;1;1;0;0;0;0;1
1;1;0;1;1;1;0;0;1
1;1;0;0;0;0;0;1;0
1;1;0;0;0;0;0;1;0
1;1;0;0;0;0;0;1;0
1;1;0;0;0;0;0;1;0
@oliviergimenez
oliviergimenez / grouped_lasso.R
Created December 29, 2020 08:41
Grouped lasso with both discrete/continuous predictors and normal/non-normal response
# illustrates grouped lasso with simulations
# with normal and non-normal responses
# and a mix and discrete/continuous predictors
#----- normal response
library(gglasso)
create_factor <- function(nb_lvl, n = 100 ){
factor(sample(letters[1:nb_lvl],n, replace = TRUE))}
@oliviergimenez
oliviergimenez / Conway-Game-of-Life-R.R
Created December 29, 2020 19:02
various R implementations of Conway's Game of Life
#------- inspiration: https://www.nytimes.com/2020/12/28/science/math-conway-game-of-life.html
#------- see also https://medium.com/tebs-lab/optimizing-conways-game-of-life-12f1b7f2f54c
#---- implementation 1
install.packages("fun")
library(fun)
demo("GameOfLife")
#---- implementation 2 (https://www.r-bloggers.com/2012/11/fast-conways-game-of-life-in-r/)
@oliviergimenez
oliviergimenez / kuomallick.R
Created February 16, 2021 10:04
Bayesian variable selection à la Kuo & Mallick w/ Jags
# je suis parti de ce post
# https://darrenjw.wordpress.com/2012/11/20/getting-started-with-bayesian-variable-selection-using-jags-and-rjags/
# je n ai utilise que le code de la section "Basic variable selection" c'est a dire que
# j ai laisse tombé les modifs sur le code que l'auteur propose dans les sections
# "Variable selection with random effects" et "Variable selection with random effects and a prior on the inclusion probability"
# j ai rajoute des lignes de code a la fin pour calculer les probabilites a posteriori des modeles
# (voir par exemple la table 2 dans https://dl.dropboxusercontent.com/u/23160641/my-pubs/Gimenezetal2009Evolution.pdf)