Skip to content

Instantly share code, notes, and snippets.

View dmarcelinobr's full-sized avatar
💭
I may be slow to respond

Daniel Marcelino dmarcelinobr

💭
I may be slow to respond
View GitHub Profile
## Exported as an SVG of 1200 width and 525 height
poly_coords<- function(shapefile){
if (nrow(data.frame(shapefile$ID))< 1)
{
print ("No ID field in SpatialPolygon")
}else{
@dmarcelinobr
dmarcelinobr / Tufte's_Weather_Chart.R
Last active October 15, 2015 19:11
Provides a step-by-step approach to creating the Brasilia temperature visualization, which mirrors the illustration in Edward Tufte's classic book Visual Display of Quantitative Information
# The original R code is Bradley Boehmke
# https://rpubs.com/bradleyboehmke/weather_graphic
# Packages
library(dplyr)
library(tidyr)
library(magrittr)
library(ggplot2)
"http://academic.udayton.edu/kissock/http/Weather/gsod95-current/BZBRSLIA.txt" %>%
read.table() %>% data.frame %>% tbl_df -> data
names(data) <- c("month", "day", "year", "temp")
#Comments
1. All code is written with the original, vague priors.
2. Runs were made with the logicals compiled.
3. Note that inprod() could be used, but is much slower (in versions of BUGS up to 3.0.1): with no missing covariate information it should be more efficient.
4. Parameters that I monitored are highlighted in this fetching shade .
5. Initial valus are given: the burn-in was not our main interest, so they were set to make this small.
6. Comments on the no selection, fixed effect, apply to all models. Similarly, comments on fixed effect models also apply to the corresponding random effect model
Data for all models in here
# caTools is handy because it provides write.gif function
library(caTools)
cols <- colorRampPalette(c("#00007F", "brown", "blue", "#007FFF", "green", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000", "magenta"))
m <- 1200 # define size
C <- complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ),
imag=rep(seq(-1.2,1.2, length.out=m), m ) )
C <- matrix(C,m,m) # reshape as square matrix
Z <- 0 # initialize Z to zero
X <- array(0, c(m,m,20)) # initialize output 3D array
for (k in 1:20) { # loop with 20 iterations
require(MCMCpack)
## the more recent polls, the better they are.
##http://es.wikipedia.org/wiki/Elecciones_presidenciales_de_Uruguay_de_2014
polls = NULL
polls <- data.frame( rbind(
Factum = c(52,37, 1004),
Cifra = c(52, 35, 1000),
Equipos = c(52, 39, 600),
> summary(mcmc)
forecast class object:
MCMC: 1000 draws with adaptation of 500 and a thinning interval of 5.
Predicted at 2014-11-30, using 13 polls from 6 houses.
Evidence starting at 2014-07-15.
Election day 2014-11-30.
Machine runtime: ~ 7 minutes.
Expected results + swing voters:
Expected results
# A vector of polls, the newer the better
pctDilma <- c(.53, .54, .52, .467, .52, .505, .436)
simVote = function(n){
# bernoulli: returns TRUE whenever Dilma wins
result = sample(c("Dilma","Aecio"),size=1000,
replace=TRUE, prob = c(pctDilma[n],1-pctDilma[n]))
return(length(result[result=='Dilma'])>500)
}
> summary(mcmc)
forecast class object:
MCMC: 500 draws with adaptation of 500 and a thinning interval of 1.
Predicted at 2014-10-25, using 19 polls from 7 houses.
Evidence starting at 2014-10-06.
Election day 2014-10-26.
Machine runtime: ~ 2 minutes.
Expected results + swing voters:
> summary(mcmc)
forecast class object:
MCMC: 1000 draws with adaptation of 1000 and a thinning interval of 5.
Predicted at 2014-10-16, using 9 polls from 6 houses.
Evidence starting at 2014-10-06.
Election day 2014-10-26.
Machine runtime: ~ 7 minutes.
Expected results + swing voters:
## 2010
> seats2010 = c(88,79,53,43,41,41,34,28,21,17,15,15,12,8,4,3,3,2,2,2,1,1)
> prop2010=seats2010/513
[1] 0.171539961 0.153996101 0.103313840 0.083820663 0.079922027
[6] 0.079922027 0.066276803 0.054580897 0.040935673 0.033138402
[11] 0.029239766 0.029239766 0.023391813 0.015594542 0.007797271
[16] 0.005847953 0.005847953 0.003898635 0.003898635 0.003898635
[21] 0.001949318 0.001949318
> get.enp(seats=prop2010, votes=NULL, total=1, method="Golosov")