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
import numpy as np
import matplotlib.pylab as plt
from scipy import stats
# For the last presidential elections
coalition = ['PT','PSDB','PSTU','PSB','PCO','PPS','PCO','PSL', 'PSDC','PRP','PT','PSDB','PDT', 'PSOL','PT','PSDB','PV','PSOL','PSDC','PCB','PRTB','PCO','PSTU']
year = [2002,2002,2002,2002,2002,2002,2006,2006,2006,2006,2006,2006,2006,2006,2010,2010,2010,2010,2010,2010, 2010,2010,2010]
timeinsec =[312,623,83,133, 83, 257,71,75,75,71,437,613,243,71,639,438,83,62,56,56,56,56,56]
votes = [46.44,23.19,0.47,17.86,0.04,11.97,0,0.06,0.07,0.13, 48.61,41.64,2.64,6.85,46.91,32.61,19.33,0.87, 0.09,0.08,0.06,0.04,0.01]
library(MCMCpack)
## Coping with polls: the more recent, the better they are.
##http://en.wikipedia.org/wiki/Opinion_polling_for_the_Scottish_independence_referendum,_2014
polls = NULL
polls <- data.frame( rbind(
Survation = c(37, 50, 1010),
YouGov = c(35, 55, 1142),
TNSBMRB = c(32, 45, 1003),
IpsosMORI = c(40, 54, 1006),
#!/Library/Frameworks/Python.framework/Versions/3.3/bin/python3
# -*- coding: utf-8 -*-
from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
from pymongo import MongoClient
from pandas import DataFrame
def rodaPesquisa():
client = MongoClient()
my_db = client["pesquisas_tse"]
> summary(mcmc)
forecast class object:
MCMC: 500 draws with adaptation of 500 and a thinning interval of 5.
Predicted at 2014-10-24, using 16 polls from 5 houses.
Evidence starting at 2014-03-10.
Election day 2014-10-26.
Machine runtime: ~ 7 minutes.
Expected results + swing voters:
@dmarcelinobr
dmarcelinobr / prob_AvsB_0.R
Last active August 29, 2015 14:06
Resolve por aproximação o tal do empate técnico estatístico da margem de erro
############## ##################
# Qual é a probabilidade de A estar a frente de B dada a diferença entre eles e o erro amostral?
# Abaixo, a diferença é de .505 - .495 = 1%
A = .505
B = .495
n = c(2500, 1111, 625, 400)
# Resolve a margem de erro por N
(moe = sqrt((A*(1-A)/n)+(B*(1-B)/n)+(2*((A*B)/n)) ) )
doInstall <- TRUE
require(Amelia)
require(ggplot2)
ANES <- read.csv("ANES.csv")
ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents,
head(ANES) # remove some non-helpful variables
with(ANES, plot(jitter(pid7), jitter(ideo7)))
myModel <- lm(pid7 ~ ideo7 + female + age + south, data = ANES)
forecast class object:
MCMC: 1000 samples with a burnin of 500 and a thinning of 5.
Predicted at 2014-09-20, using 79 polls from 9 houses.
Evidence starting at 2014-02-19.
Election day 2014-10-05.
Machine runtime: ~ 42 minutes.
Expected results + swing voters:
Expected results: (Pooled) National samples only
# Data latest polls
polls = NULL
polls <- data.frame( rbind(
Opinium = c(43, 47, 1156),
Survation = c(44, 48, 1000),
ICM = c(41, 45, 1175)
))
# set up for decimals
polls[, 1:2] <- polls[, 1:2]/100
R> summary(mcmc2014)
forecast class object:
MCMC: 1000 samples with a burnin of 500 and a thinning of 5.
Predicted at 2014-09-20, using 33 polls from 5 houses.
Evidence starting at 2014-02-19.
Election day 2014-10-05.
Machine runtime: ~ 28 minutes.
Expected results + swing voters:
R> summary(mcmc2014)
forecast class object:
MCMC: 1000 samples with a burnin of 500 and a thinning of 10.
Predicted at 2014-09-25, using 36 polls from 5 houses.
Evidence starting at 2014-02-19.
Election day 2014-10-05.
Machine runtime: ~ 57 minutes.
Expected results + swing voters: