Skip to content

Instantly share code, notes, and snippets.

View pachevalier's full-sized avatar

Paul-Antoine pachevalier

  • Paris
View GitHub Profile
@pachevalier
pachevalier / arrange.R
Last active August 29, 2015 13:57
Problem with the output of arrange
library("dplyr")
set.seed(123)
N <- 100
df <- data.frame(id = 1:N, x = rnorm(N))
df$x[runif(N) < .1] <- NA
table(is.na(tdf$x))
tdf <- tbl_df(df)
out <- arrange(tdf, desc(x))
out2 <- tdf[order(tdf$x, decreasing = TRUE),]
library("ggplot2")
set.seed(1234)
N <- 100
df <- data.frame(i = 1:N, x = rnorm(N))
df$y <- 1 + df$x + rnorm(N)
df$z <- (runif(N) < .3)
pdf("output/test.pdf")
ggplot(data = df, aes(x = x, y = y, shape = z)) +
geom_point(size = 3) +
library(ggplot2)
library(maps)
world <- map_data("world")
ggplot() +
geom_map( data=world, aes(x=long, y=lat, group = group, map_id = region),colour="white", fill="grey10", map = world ) +
coord_map()
ggplot() +
geom_map( data=world, aes(x=long, y=lat, group = group, map_id = region),colour="white", fill="grey10", map = world )
We can't make this file beautiful and searchable because it's too large.
"long","lat","order","hole","piece","group","id"
5.25559492,45.78458958,1,FALSE,"1","01.1","01"
5.23987135,45.77757781,2,FALSE,"1","01.1","01"
5.22391493,45.76913392,3,FALSE,"1","01.1","01"
5.19950932,45.77145701,4,FALSE,"1","01.1","01"
5.18966522,45.77258731,5,FALSE,"1","01.1","01"
5.18661122,45.78215759,6,FALSE,"1","01.1","01"
5.17711693,45.79331919,7,FALSE,"1","01.1","01"
5.16818192,45.79570866,8,FALSE,"1","01.1","01"
5.15757866,45.80378018,9,FALSE,"1","01.1","01"
@pachevalier
pachevalier / spread.R
Last active August 29, 2015 14:06
A problem with spread function
library("dplyr")
library("tidyr")
set.seed(10)
messy <- data.frame(
id = 1:4,
trt = sample(rep(c('control', 'treatment'), each = 2)),
work.T1 = runif(4),
home.T1 = runif(4),
work.T2 = runif(4),
home.T2 = runif(4)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
library("venneuler")
library("ggplot2")
# https://www.data.gouv.fr/fr/datasets/contours-des-cantons-electoraux-departementaux-2015/
library("rgdal")
library("rgeos")
library("maptools")
library("ggplot2")
library("dplyr")
library("stringr")
library("mapproj")
library("ggthemes")
cantons <- readOGR("data/cantons2015/", layer= "CANTONS_2015_WGS84")
@pachevalier
pachevalier / Makefile
Created November 24, 2015 15:36
makefile for converting EPCI Shapefiles to KML
all: epci-20150303-5m.kml epci-20150303-50m.kml epci-20150303-100m.kml
epci-20150303-5m.kml: epci-20150303-5m.shp
ogr2ogr -f KML $@ $<
epci-20150303-5m.shp: epci-20150303-5m-shp.zip
unzip $<
epci-20150303-5m-shp.zip:
curl -o $@ "http://osm13.openstreetmap.fr/~cquest/openfla/export/epci-20150303-5m-shp.zip"
library(ggplot2)
library(magrittr)
library(ggthemes)
library(scales)
library(gridExtra)
set.seed(1234)
N <- 5
df <- data.frame(x = runif(N), y = letters[1:10])
@pachevalier
pachevalier / gmm_vs_lm.R
Created July 30, 2013 10:48
This gist compare the results of the gmm() and the lm() function in R using simulated data.
library("gmm")
set.seed(1234567)
N <- 1000
dd <- data.frame(id = 1:N)
dd$u <- rnorm(N)
dd$x <- 1 + rnorm(N)
dd$y <- 1 + dd$x + dd$u
m1 <- lm(y ~ x, data = dd)
m2 <- gmm(y ~ x, x = ~ x, wmatrix = "ident", data = dd)
coefficients(m1)