forked from avehtari/ROS-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_primaire.R
38 lines (33 loc) · 887 Bytes
/
ps_primaire.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#' ---
#' title: "Regression and Other Stories: French Election"
#' author: "Andrew Gelman, Aki Vehtari"
#' date: "`r format(Sys.Date())`"
#' output:
#' html_document:
#' theme: readable
#' toc: true
#' toc_depth: 2
#' toc_float: true
#' code_download: true
#' ---
#' French Election data.
#'
#' -------------
#'
#+ setup, include=FALSE
knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
#' #### Load packages
library("rprojroot")
root<-has_file(".ROS-Examples-root")$make_fix_file()
#' #### Load data
ps <- read.table(root("FrenchElection/data","ps_primaire.txt"), header=TRUE)
#' #### Analyze
J <- nrow(ps)
n1 <- ps[J,2]
n2 <- ps[J,3] - ps[J,2]
p1 <- ps[1:(J-1),2]/n1
p2 <- (ps[1:(J-1),3] - ps[1:(J-1),2])/n2
diff <- p2 - p1
se_diff <- sqrt(p1*(1-p1)/n1 + p2*(1-p2)/n2)
round(cbind(diff, se_diff)*10000)
print(cbind(p1*n2, p2*n2))