推定地が実際のデータをうまく再現できるかをチェックする
simulationの対象はmunicipality別の、候補者ごとの得票率
実際のデータではdataの3~6行が候補者別のvote数(clark, dean, edwards, kerryの順番)
データの見せ方は以下の3つ
ここでは実際のデータを上のようにプロットする
In [1]:
using Optim
using StatsFuns
using DataFrames
using Gadfly
using PyPlot
# inverse beta distribution function を行列に対応するように拡張
import StatsFuns.betainvcdf
betainvcdf(alpha::Number, beta::Number, x::Array) = reshape([betainvcdf(alpha, beta, i) for i in reshape(x, 1, size(x, 1)*size(x, 2)) ], size(x, 1), size(x, 2))
# maxをArray{String, 1}に対応するように拡張
# しかしArray型で入っているのでAnyに対応させる
import Base.max
max(number::Real, comparison::Any) = [max(number, parse(Int, i)) for i in comparison]
# normpdfを配列に拡張
import StatsFuns.normpdf
normpdf(array::Array{Float64, 2}) = reshape([normpdf(i) for i in reshape(array, 1, size(array, 1)*size(array, 2))], size(array, 1), size(array, 2))
# Candを作る
include("bayes.jl");
In [10]:
# Votes_sを用いる
plt = PyPlot
names = ["clark", "dean", "edwards", "kerry"]
DeltaO = 0.6891
DeltaMO = 0.5366
combination = [(i, i+j) for i in 1:3 for j in 1:(4-i)]
for S in 1:size(Cand,1)
DELTA = DeltaO*Open+DeltaMO*MOpen
RTOT = RDemHat.*(1+DELTA)-VOther
RTot_s = RTOT[Cand[S, 14]:Cand[S, 15], :]
RTot_s = max(RTot_s, sum(Votes[Cand[S, 14]:Cand[S, 15], :], 2))
Votes_s = Votes[Cand[S, 14]:Cand[S, 15], :]./(RTot_s*ones(1,4))
num_rows, num_cols = 2, 3
fig, axes = subplots(num_rows, num_cols, figsize=(12, 8))
axes = vec(axes)
# cand1 vs cand2で、cand1が横軸、cand2が縦軸
for (n,c) in enumerate(combination)
cand1 = names[c[1]]
cand2 = names[c[2]]
ax = axes[n]
ax[:scatter](Votes_s[:, c[1]], Votes_s[:, c[2]], s = 3)
ax[:set_title]("$cand1 vs $cand2")
#ax[:set_xticks]([0,0.25,0.5,0.75])
#ax[:set_yticks]([0,0.25,0.5,0.75])
savefig("state_number_$S")
end
end
In [35]:
shares = Array(Float64, size(Cand,1), 4)
for S in 1:size(Cand,1)
shares[S,:] = sum(Votes[Cand[S, 14]:Cand[S, 15], :], 1)./ sum(RTOT[Cand[S, 14]:Cand[S, 15], :])
end
fig, ax = subplots()
for i in 1:4
cand = names[i]
ax[:plot](shares[:, i], linewidth=2, alpha=0.6, label="$cand")
end
ax[:legend]()
savefig("vote_share_states")
In [12]:
# 人種の割合
# (white,black,otherasian+indian+other)の順番
dFXRace
Out[12]:
In [17]:
# 教育水準の割合
# (overba,underba,hs,other)の順番
dFXEduc
Out[17]:
In [18]:
# 足せば1になるのでやはりこいつも各収入階層の割合を示している。
# [20000;35000;72500;120000]の順番
dFXIncm
Out[18]:
In [13]:
# 人種
plt = PyPlot
names = ["clark", "dean", "edwards", "kerry"]
DeltaO = 0.6891
DeltaMO = 0.5366
DELTA = DeltaO*Open+DeltaMO*MOpen
RTOT = RDemHat.*(1+DELTA)-VOther
voterate = sum(Votes, 2)./RTOT
whiterate = dFXRace[:, 1]./sum(dFXRace[:,2:3],2)
kerry = Votes[:, 4]./RTOT
num_rows, num_cols = 7, 5
fig, axes = subplots(num_rows, num_cols, figsize=(12, 20))
axes = vec(axes)
for S in 1:size(Cand,1)
voterate_s = voterate[Cand[S, 14]:Cand[S, 15], :]
whiterate_s = whiterate[Cand[S, 14]:Cand[S, 15], :]
kerry_s = kerry[Cand[S, 14]:Cand[S, 15], :]
ax = axes[S]
ax[:scatter](whiterate_s, voterate_s, s = 50*kerry_s, alpha = 0.5)
ax[:set_title]("state_number_$S")
#ax[:set_xticks]([0,0.25,0.5,0.75])
#ax[:set_yticks]([0,0.25,0.5,0.75])
end
# matplotlibそのまんま使えるのね
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
savefig("race_plot")
In [43]:
# 学歴
plt = PyPlot
names = ["clark", "dean", "edwards", "kerry"]
DeltaO = 0.6891
DeltaMO = 0.5366
DELTA = DeltaO*Open+DeltaMO*MOpen
RTOT = RDemHat.*(1+DELTA)-VOther
voterate = sum(Votes, 2)./RTOT
educrate = dFXEduc[:, 1]./sum(dFXEduc[:,2:4],2)
kerry = Votes[:, 4]./RTOT
num_rows, num_cols = 7, 5
fig, axes = subplots(num_rows, num_cols, figsize=(12, 20))
axes = vec(axes)
for S in 1:size(Cand,1)
voterate_s = voterate[Cand[S, 14]:Cand[S, 15], :]
educrate_s = educrate[Cand[S, 14]:Cand[S, 15], :]
kerry_s = kerry[Cand[S, 14]:Cand[S, 15], :]
ax = axes[S]
ax[:scatter](educrate_s, voterate_s, s = 50*kerry_s, alpha = 0.5)
ax[:set_title]("state_number_$S")
#ax[:set_xticks]([0,0.25,0.5,0.75])
#ax[:set_yticks]([0,0.25,0.5,0.75])
end
# matplotlibそのまんま使えるのね
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
savefig("education_plot")
In [44]:
# 収入
plt = PyPlot
names = ["clark", "dean", "edwards", "kerry"]
DeltaO = 0.6891
DeltaMO = 0.5366
DELTA = DeltaO*Open+DeltaMO*MOpen
RTOT = RDemHat.*(1+DELTA)-VOther
voterate = sum(Votes, 2)./RTOT
lowincome = dFXIncm[:, 1]
kerry = Votes[:, 4]./RTOT
num_rows, num_cols = 7, 5
fig, axes = subplots(num_rows, num_cols, figsize=(12, 20))
axes = vec(axes)
for S in 1:size(Cand,1)
voterate_s = voterate[Cand[S, 14]:Cand[S, 15], :]
lowincome_s = lowincome[Cand[S, 14]:Cand[S, 15], :]
kerry_s = kerry[Cand[S, 14]:Cand[S, 15], :]
ax = axes[S]
ax[:scatter](lowincome_s, voterate_s, s = 50*kerry_s, alpha = 0.5)
ax[:set_title]("state_number_$S")
#ax[:set_xticks]([0,0.25,0.5,0.75])
#ax[:set_yticks]([0,0.25,0.5,0.75])
end
# matplotlibそのまんま使えるのね
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
savefig("lowincome_plot")