Skip to content

Instantly share code, notes, and snippets.

View briatte's full-sized avatar

François Briatte briatte

View GitHub Profile
##programmer: JEY
##2/28/2013
##District-month forecasts
##note: this takes about 5 minutes to run. It throws lots of errors due to many all zero time series, but it will eventually complete and build predictions
rm(list=ls())
library(foreign)
library(forecast)
library(sos)
data<-read.dta("raw_AFG.dta")
@briatte
briatte / useful.r
Created April 23, 2013 21:43
Useful R functions for psychometrics and personality research, by William Revelle -- copied from http://personality-project.org/r/useful.r
#useful functions for psychometrics and personality research
#a growing compilation of simple functions that I have found useful for doing personality research
#last updated Feb 14, 2005
#by William Revelle
#includes
#alpha.scale #find coefficient alpha for a scale and a dataframe of items
#describe give means, sd, skew, n, and se
#summ.stats #basic summary statistics by a grouping variable
#error.crosses (error bars in two space)
#skew find skew
@briatte
briatte / cleaning_characters.r
Last active August 12, 2020 18:13
a few lines of code to load packages, download and scrape data, convert, sort, etc.
# removes characters from multiple data frame columns
# example removes * , % characters
dw <- data.frame(gsub("\\*|,|%", "", as.matrix(dw)), stringsAsFactors = FALSE)
@briatte
briatte / README.md
Last active December 16, 2015 19:39
Graphiques ESR - DPG CJC 2013-04-30

Graphiques pour la CJC.

DATA

stats_esr.csv : extrait du RERS 2012, tableaux 6.4.2 et 8.18.1.

jce.R : extrait de l'enquête CJC "Les jeunes chercheurs étrangers en France. Résultats de l’enquête 2010", 2012, section VII.3. Les données sont générées directement dans le code.

vacataires.tsv : extrait de MESR-DGRH, Étude sur la situation des personnels enseignants non permanents de l’enseignement supérieur, 2009, tableaux 2 et 17. Variables utilisées :

@briatte
briatte / map_QOG.R
Created April 30, 2013 15:09
maps with World Bank and Quality of Government data
require(downloader)
require(foreign)
require(ggplot2)
# download Quality of Government Standard dataset
file = "data/qog-cs.txt"
if(!file.exists(file)) {
if(!file.exists(dta <- "data/qog-cs.dta")) {
url = "http://www.qogdata.pol.gu.se/data/qog_std_cs.dta"
download(url, dta, mode = "wb")
@briatte
briatte / stats.table.R
Last active December 16, 2015 20:10
basic summary statistics from R (see example use at bottom); the plain text file it produces can be copy-pasted in Google Docs and formatted from there with styles and tabs
stats.table <- function(data, continuous, categorical,
file = "stats.txt", digits = 1) {
if(!require(pastecs)) stop("Install.packages('pastecs') first.")
# continuous summary (n, mean, sd, min, max) of selected variable columns
con.stats <- round(t(stat.desc(data[, continuous]))[, c(1, 9, 13, 4:5)], digits)
# categorical summary (n, frequencies) of selected variable columns
cat.stats <- lapply(data[, categorical], FUN = function(x) {
data.frame(x <- table(x), percent = round(100 * x / nrow(data), digits))[-3]
@briatte
briatte / quicker_plot.R
Created May 16, 2013 13:43
scrape and plot Beijing's pollution data
# Target data source.
link = "https://raw.github.com/briatte/ida/master/data/beijing.aqi.2013.txt"
file = "data/beijing.aqi.2013.txt"
if(!file.exists(file)) download(link, file)
# Read CSV file.
bp <- read.csv(file, stringsAsFactors = FALSE)
# Check result.
head(bp)
bp$time <- strptime(bp$time, format = "%Y-%m-%d %T")
ggplot(data = bp, aes(x = time, y = PM)) +
@briatte
briatte / us_recessions.R
Created May 16, 2013 18:43
quick replication of an Excel graph that was recoded in SPSS in 21 lines (excluding data preparation)
require(ggplot2)
require(plyr)
require(xlsx)
# http://www.calculatedriskblog.com/2013/05/april-employment-report-165000-jobs-75.html
# https://andrewpwheeler.wordpress.com/2013/03/18/the-junk-charts-challenge-remaking-a-great-line-chart-in-spss/
# http://www.minneapolisfed.org/publications_papers/studies/recession_perspective/
link = "http://www.minneapolisfed.org/publications_papers/studies/recession_perspective/data/historical_recessions_recoveries_data_05_03_2013.xls"
file = "data/us.recessions.4807.xls"
### Takes as input a Google Docs spreadsheet
### Outputs national and regional level estimates of support
### over time
### Load libraries
library(R2WinBUGS)
library(rjags)
library(RCurl)
library(reshape)
library(foreach)
## Please note, this script is entirely derivative of work done
## by Simon Jackman for his article `Pooling the Polls...'
## Aust. J. Pol. Science, 40(4): 499-517
## Libraries
library(reshape)
library(rjags)
library(R2WinBUGS)
library(ggplot2)
library(scales)