Skip to content

Instantly share code, notes, and snippets.

View pachevalier's full-sized avatar

Paul-Antoine pachevalier

  • Paris
View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Title</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script>
d3.csv("data.csv", function(error, data) {
@pachevalier
pachevalier / xkcd.R
Created June 20, 2013 09:44
Try the xkcd package
library("ggplot2")
library("extrafont")
library("xkcd")
df <- data.frame(x = rnorm(10), y = 1 + rnorm(10))
ggplot(data = df, aes(x = x, y = y)) + geom_point() + theme_xkcd()
@pachevalier
pachevalier / departments.json
Last active December 18, 2015 21:39
Try to use kartograph.js
[{"pop": 2565257, "density": 449, "id": "59", "name": "Nord ", "area": 5743}, {"pop": 2181371, "density": 20605, "id": "75", "name": "Paris ", "area": 105}, {"pop": 1958926, "density": 385, "id": "13", "name": "Bouches-du-Rh\u00f4ne ", "area": 5087}, {"pop": 1677073, "density": 516, "id": "69", "name": "Rh\u00f4ne ", "area": 3249}, {"pop": 1536100, "density": 8619, "id": "92", "name": "Hauts-de-Seine ", "area": 176}, {"pop": 1502340, "density": 6366, "id": "93", "name": "Seine-Saint-Denis ", "area": 236}, {"pop": 1453387, "density": 218, "id": "62", "name": "Pas-de-Calais ", "area": 6671}, {"pop": 1409345, "density": 141, "id": "33", "name": "Gironde ", "area": 10000}, {"pop": 1395804, "density": 611, "id": "78", "name": "Yvelines ", "area": 2284}, {"pop": 1298340, "density": 5220, "id": "94", "name": "Val-de-Marne ", "area": 245}, {"pop": 1289584, "density": 218, "id": "77", "name": "Seine-et-Marne ", "area": 5915}, {"pop": 1268173, "density": 186, "id": "44", "name": "Loire-Atlantique ", "area": 6815}, {"po
@pachevalier
pachevalier / iconv.R
Last active December 18, 2015 21:58
Problem with iconv
tab <- readHTMLTable("http://www.insee.fr/fr/methodes/nomenclatures/cog/fichecommunale.asp?codedep=01&codecom=004", stringsAsFactors = FALSE, as.data.frame = TRUE)
tab$fiche_communale
tab$fiche_communale$name <- iconv(tab$fiche_communale$Nom, from = "latin1", to = "UTF-8")
Encoding(tab$fiche_communale$name)
@pachevalier
pachevalier / rPlot.R
Last active December 18, 2015 22:49
Try the rPlot function in rCharts
library('rCharts')
N <- 10
x <- 1+ rnorm(N)
y <- 1 + x + rnorm(N)
df <- data.frame(x = x, y = y)
rPlot(y ~ x, data = df, type = "point")
rPlot(y ~ x, data = df, type = "point", color = list(const = "red"))
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href='http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.css'>
<script src='http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js' type='text/javascript'></script>
<script src='https://rawgithub.com/leaflet-extras/leaflet-providers/gh-pages/leaflet-providers.js' type='text/javascript'></script>
<style>
@pachevalier
pachevalier / regression_with_dates.R
Created July 10, 2013 13:34
A simple example of a regression with a date variable
N <- 10
id <- 1:10
x <- 1 + rnorm(N) - 1*id
date <- seq(as.Date("2013-07-01"), by = "year", along = x)
df <- data.frame(x = x, date = date)
plot(df$date, df$x)
summary(lm(x ~ date, data = df))
@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)
mc <- lm(formula = SBR ~ Age, data = ch)
m1 <- lm(formula = SBR ~ Age, data = subset(ch, Sex == "M"))
m2 <- lm(formula = SBR ~ Age, data = subset(ch, Sex == "F"))
sc <- sum(mc$residuals^2)
s1 <- sum(m1$residuals^2)
s2 <- sum(m2$residuals^2)
k <- 2
# Test statistic
fstat <- (sc - (s1 + s2)) / k / (s1 + s2) * (length(mc$residuals) - 2*k)
fstat
library("strucchange")
library("lubridate")
set.seed(1234567)
N <- 60
df <- data.frame(id = 1:N)
df$date <- seq(as.Date("2013-07-01"), by = "day", along = df$id)
df$date2 <- difftime(df$date, ymd("2013-07-01"), units = "day")
df$date3 <- difftime(df$date, ymd("2013-08-01"), units = "day")
difftime(ymd("2013-08-01"), ymd("2013-07-01"), units = "day")