In [1]:
%load_ext rpy2.ipython
In [2]:
%%R
y <- c(35,34,38,35,37)
y
In [3]:
%%R
n <- length(y)
n
In [4]:
%%R
estimate <- mean(y)
estimate
In [5]:
%%R
se <- sd(y)/sqrt(n)
se
In [6]:
%%R
int.50 <- estimate + qt(c(.25,.75),n-1)*se
int.50
In [7]:
%%R
int.95 <- estimate + qt(c(.025,.975),n-1)*se
int.95
In [8]:
%%R
y <- 700
y
In [9]:
%%R
n <- 1000
n
In [10]:
%%R
estimate <- y/n
estimate
In [11]:
%%R
se <- sqrt(estimate*(1-estimate)/n)
se
In [12]:
%%R
int.95 <- estimate + qnorm(c(.025,.975))*se
int.95
In [13]:
%%R
y <- rep(c(0,1,2,3,4), c(600,300,50,30,20))
y
In [14]:
%%R
n <- length(y)
n
In [15]:
%%R
estimate <- mean(y)
estimate
In [16]:
%%R
se <- sd(y)/sqrt(n)
se
In [17]:
%%R
int.50 <- estimate + qt(c(.25,.75),n-1)*se
int.50
In [18]:
%%R
int.95 <- estimate + qt(c(.025,.975),n-1)*se
int.95
In [19]:
%%R
# Data is available in death.polls directory of ARM_Data
# Note: data seems to come from Gallup
# See: http://www.gallup.com/poll/1606/Death-Penalty.aspx
#
# Columns are
# 1- year
# 2- month
# 3- percentage support
# 4- percentage against
# 5- percentage no opinion
polls <- matrix(scan("../../ARM_Data/death.polls/polls.dat"),
ncol=5, byrow=TRUE)
polls
In [20]:
%%R
# Note: this is the percentage support **among those that have an opinion** (pg 18)
#
# -- "No opinion" percentages are ignored
# -- !s a result, our plot below looks different from the Gallup plot
support <- polls[,3]/(polls[,3]+polls[,4])
support
In [21]:
%%R
year <- polls[,1] + (polls[,2]-6)/12
year
In [22]:
%%R
# confidence intervals in plot assume sample size N=1000
# itervals are 68% confidence
par (mar=c(5,5,4,2)+.1)
plot(year, support*100, xlab="Year", ylim=c(min(100*support)-1, max(100*support)+1),
ylab="Percentage support for the death penalty", cex=1.1, cex.main=1.2,
cex.axis=1.1, cex.lab=1.1, pch=20)
for (i in 1:nrow(polls))
lines (rep(year[i],2), 100*(support[i]+c(-1,1)*sqrt(support[i]*(1-support[i])/1000)))
In [23]:
%%R
N <- c(66030000, 81083600, 60788845)
p <- c(0.55, 0.61, 0.38)
se <- c(0.02, 0.03, 0.03)
In [24]:
%%R
w.avg <- sum(N*p)/sum(N)
w.avg
In [25]:
%%R
se.w.avg <- sqrt(sum((N*se/sum(N))^2))
se.w.avg
In [26]:
%%R
# this uses +/- 2 std devs
int.95 <- w.avg + c(-2,2)*se.w.avg
int.95
In [27]:
%%R
n.men <- 500
n.men
In [28]:
%%R
p.hat.men <- 0.75
p.hat.men
In [29]:
%%R
se.men <- sqrt (p.hat.men*(1-p.hat.men)/n.men)
se.men
In [30]:
%%R
n.women <- 500
n.women
In [31]:
%%R
p.hat.women <- 0.65
p.hat.women
In [32]:
%%R
se.women <- sqrt(p.hat.women*(1-p.hat.women)/n.women)
se.women
In [33]:
%%R
n.sims <- 10000
n.sims
In [34]:
%%R
p.men <- rnorm(n.sims, p.hat.men, se.men)
p.men[1:10] # show first ten
In [35]:
%%R
p.women <- rnorm(n.sims, p.hat.women, se.women)
p.women[1:10] # show first ten
In [36]:
%%R
ratio <- p.men/p.women
ratio[1:10] # show first ten
In [37]:
%%R
int.95 <- quantile(ratio, c(.025,.975))
int.95