File: Chapter.4.3.R

package info (click to toggle)
r-cran-learnbayes 2.15.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,864 kB
  • sloc: sh: 15; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 880 bytes parent folder | download | duplicates (4)
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
39
###################################################
# Section 4.3 A Multinomial Model
###################################################

library(LearnBayes)

 alpha = c(728, 584, 138)
 theta = rdirichlet(1000, alpha)

 hist(theta[, 1] - theta[, 2], main="")

S=readline(prompt="Type  <Return>   to continue : ")

###########################################

data(election.2008)
attach(election.2008)

prob.Obama=function(j)
{
p=rdirichlet(5000,
  500*c(M.pct[j],O.pct[j],100-M.pct[j]-O.pct[j])/100+1)
mean(p[,2]>p[,1])
}

Obama.win.probs=sapply(1:51,prob.Obama)

sim.election=function()
{
winner=rbinom(51,1,Obama.win.probs)  
sum(EV*winner)         
}

sim.EV=replicate(1000,sim.election())

windows()
hist(sim.EV,min(sim.EV):max(sim.EV),col="blue")
abline(v=365,lwd=3)  # Obama received 365 votes
text(375,30,"Actual \n Obama \n total")