load("Replication data for The Oil Curse - Ross 2012.RData")
浜中コードは以下の通り。(コメントアウトは松尾による)
#パッケージの読み込み
library(dplyr)
#1行目、「全国家・全時代」での片側検定
t.test(x$ttd_SD[x$oil_gas_value100==0],x$ttd_SD[x$oil_gas_value100==1],
alternative="greater")
#収入別グループの作成
tdl<-filter(x,gdpcap2000_sup<5000)#低収入国グループの作成
tdh<-filter(x,gdpcap2000_sup>5000)#高所得グループの作成
#3行目、高所得グループの産油国・非産油国で片側検定
t.test(tdh$ttd_SD[tdh$oil_gas_value100==0],
tdh$ttd_SD[tdh$oil_gas_value100==1],
alternative="greater")
#2行目、低所得グループの産油国・非産油国で片側検定
t.test(tdl$ttd_SD[tdl$oil_gas_value100==0],
tdl$ttd_SD[tdl$oil_gas_value100==1],
alternative="greater")
#時代別グループの作成
tdb80<-filter(x,year<1980)#1979年以前グループの作成
tda80<-filter(x,year>1979)#1980年以降グループの作成
#4行目、1979年以前グループの産油国・非産油国で片側検定
t.test(tdb80$ttd_SD[tdb80$oil_gas_value100==0],
tdb80$ttd_SD[tdb80$oil_gas_value100==1],
alternative="greater")
#5行目、1980年以降グループでの産油国・非産油国で片側検定
t.test(tda80$ttd_SD[tda80$oil_gas_value100==0],
tda80$ttd_SD[tda80$oil_gas_value100==1],
alternative="greater")
#ラテンアメリカの分析
inla<-filter(x,latin>.5)#ラテンアメリカグループの作成
outla<-filter(x,latin<.5)#ラテンアメリカ以外グループの作成
#6行目、ラテンアメリカグループの産油国・非産油国で片側検定
t.test(inla$ttd_SD[inla$oil_gas_value100==0],
inla$ttd_SD[inla$oil_gas_value100==1],
alternative="less")
#7行目、ラテンアメリカ以外の産油国・非産油国で片側検定
t.test(outla$ttd_SD[outla$oil_gas_value100==0],
outla$ttd_SD[outla$oil_gas_value100==1],
alternative="greater")
浜中コードでは、表の2行目と3行目の分析順序が入れ替わっている点に注意。
ラテンアメリカグループの抽出条件が“filter(x,latin>.5)”となっており、非ラテンアメリカグループの抽出条件が“filter(x,latin>.5)”となっているが、これは“latin”変数が0と1の地域ダミーであるため、0.5(.5は0.5を意味する)より大きい(あるいはより小さい)で2グループに分けている(と推測される)。
念のため、変数“latin”の内容を確認すると、以下の通り。
table(x$latin)
0 1
7004 1323
ラテンアメリカ以外(0)と、ラテンアメリカ(1)から構成されていることが確認できる。1の数が1323あるが、これはラテンアメリカに1323ヶ国あるのではなく、ラテンアメリカの対象国×47年分のデータ数を指している。ラテンアメリカ諸国がおよそ30ヶ国であることを考えると、この数(1323/47≒28)は妥当。
ラテンアメリカグループと非ラテンアメリカグループの作成は、より一般的には以下のように記述できる。同じオブジェクト名を使用すると内容が上書きされてしまうので、ここではinla2、outla2としている。以下で記述するコードでは、“inla”と“outla”の組み合わせでも、“inla2”と“outla2”の組み合わせを使用しても、結果は変わらない。
inla2<-filter(x,latin==1)#ラテンアメリカグループの作成
outla2<-filter(x,latin==0)#ラテンアメリカ以外グループの作成
なお、第2章にあった浜中コードの特徴(t検定の比較対象が表と逆–表では「非産油国・産油国」だが、浜中コードでは「産油国・非産油国」)はなくなっている。
Welch Two Sample t-test
data: x$ttd_SD[x$oil_gas_value100 == 0] and x$ttd_SD[x$oil_gas_value100 == 1]
t = 2.3718, df = 2252, p-value = 0.008892
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.003130555 Inf
sample estimates:
mean of x mean of y
0.02218750 0.01196411
ロスの分析と比較すると、平均値は等しいがp値が異なっている(ロスの分析ではp値は0.0212)。サンプルは同じものだが分析方法が異なることが推測されるが、この分析方法の違いは第2章の表と同様、等分散を前提としているかどうかの違いである。このため、浜中コードを等分散を前提とした分析に変更する。また、やはり第2章の表と同様に、“t.test”の書式をよりシンプルなものに変更する。結果は以下の通り。。
t.test(ttd_SD~oil_gas_value100,data = x,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = 2.0311, df = 4201, p-value = 0.02116
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.001942145 Inf
sample estimates:
mean in group 0 mean in group 1
0.02218750 0.01196411
これでロスの分析と同じになった。
Welch Two Sample t-test
data: tdl$ttd_SD[tdl$oil_gas_value100 == 0] and tdl$ttd_SD[tdl$oil_gas_value100 == 1]
t = 1.5132, df = 1050.5, p-value = 0.06527
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-0.0007823435 Inf
sample estimates:
mean of x mean of y
0.02414718 0.01525424
1行目の分析と同様に、平均値は等しいがp値が異なる。10%水準で有意なのは表の通りだが、浜中コードのp値が0.06527であるのに対して、ロスの分析ではp値は 0.0943である。1行目と同様にロスは等分散を前提としているので、ここでも浜中コードを等分散を前提とする形に修正し、加えて“t.test”の書式も改める。
t.test(ttd_SD~oil_gas_value100,data = tdl,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = 1.3151, df = 3197, p-value = 0.09428
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-0.002232861 Inf
sample estimates:
mean in group 0 mean in group 1
0.02414718 0.01525424
これでp値も一致する。
Welch Two Sample t-test
data: tdh$ttd_SD[tdh$oil_gas_value100 == 0] and tdh$ttd_SD[tdh$oil_gas_value100 == 1]
t = 1.987, df = 110.85, p-value = 0.0247
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.008265303 Inf
sample estimates:
mean of x mean of y
0.06122449 0.01119403
この分析では、平均値もp値もロスの分析結果とことなる。第2章の浜中コードでも確認した、STATAとRのサンプル処理の問題だと推測される。
table(is.na(tdh$ttd_SD),tdh$oil_gas_value100)
0 1
FALSE 98 268
TRUE 942 401
Rの集計では、高所得国で非産油国はNAが942、それ以外が98,計1040、産油国はNAが401、それ以外が268、計669。ロスの分析では非産油国が591、産油国が413であり、サンプル数が異なる。このため、以下のように等分散を前提としても、結果は一致しない(“t.test”の書式はシンプルなものに改めてある)。
t.test(ttd_SD~oil_gas_value100,data=tdh,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = 2.7574, df = 364, p-value = 0.00306
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.02010994 Inf
sample estimates:
mean in group 0 mean in group 1
0.06122449 0.01119403
Welch Two Sample t-test
data: tdb80$ttd_SD[tdb80$oil_gas_value100 == 0] and tdb80$ttd_SD[tdb80$oil_gas_value100 == 1]
t = -0.27445, df = 408.84, p-value = 0.6081
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-0.0137565 Inf
sample estimates:
mean of x mean of y
0.01132578 0.01328904
平均値は表と一致するがp値は一致しない。表では有意ではないとされており、これは浜中コードでも一致するが(0.6081)、厳密にはロスが出したp値は0.6139である。これも等分散の問題なので、“t.test”の書式変更とともに、浜中コードを等分散を前提としたt検定に修正する。
t.test(ttd_SD~oil_gas_value100,data = tdb80,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = -0.2895, df = 1800, p-value = 0.6139
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-0.01312347 Inf
sample estimates:
mean in group 0 mean in group 1
0.01132578 0.01328904
これでロスの分析結果と一致する。
Welch Two Sample t-test
data: tda80$ttd_SD[tda80$oil_gas_value100 == 0] and tda80$ttd_SD[tda80$oil_gas_value100 == 1]
t = 3.4864, df = 2080.9, p-value = 0.0002498
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.01076462 Inf
sample estimates:
mean of x mean of y
0.03178340 0.01139601
浜中コードの結果は表と一致するが、厳密にはp値が異なる(浜中コードでは0.0002498、ロスでは 0.0021)。これはロスが等分散を前提としているため。“t.test”の書式変更に加え、等分散を前提とした形に浜中コードを修正する。
t.test(ttd_SD~oil_gas_value100,data = tda80,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = 2.8686, df = 2399, p-value = 0.002079
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.008692881 Inf
sample estimates:
mean in group 0 mean in group 1
0.03178340 0.01139601
これでロスの結果と一致する。
inla2<-filter(x,latin==1)#ラテンアメリカグループの作成
outla2<-filter(x,latin==0)#ラテンアメリカ以外グループの作成
Welch Two Sample t-test
data: inla$ttd_SD[inla$oil_gas_value100 == 0] and inla$ttd_SD[inla$oil_gas_value100 == 1]
t = -1.7787, df = 80.544, p-value = 0.03953
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
-Inf -0.004490686
sample estimates:
mean of x mean of y
0.04303797 0.11267606
平均値はロスと一致するがp値が異なり、浜中コードでは5%水準で有意だが、表では有意ではない(ロスのp値は0.9918)。理由は二つあり、第一に浜中コードはなぜか下側片側検定となっていること(ロスの検定は上側片側検定)、第二にこれまでと同様、ロスは等分散を前提としており、浜中コードが等分散を前提としないこと。この点を修正し、“t.test”の書式をシンプルなものに改めると、以下のようになる。
t.test(ttd_SD~oil_gas_value100,data = inla,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = -2.4074, df = 464, p-value = 0.9918
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-0.1173144 Inf
sample estimates:
mean in group 0 mean in group 1
0.04303797 0.11267606
これでロスの結果と一致した。
Welch Two Sample t-test
data: outla$ttd_SD[outla$oil_gas_value100 == 0] and outla$ttd_SD[outla$oil_gas_value100 == 1]
t = 4.4455, df = 3304.7, p-value = 4.528e-06
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.009422905 Inf
sample estimates:
mean of x mean of y
0.019251337 0.004291845
浜中コードは表と一致するが、厳密にはロスの分析ではp値は0.0007であり、この違いはこれまでと同様に、等分散である。
これまでと同様に、“t.test”の書式変更に加えて、浜中コードを以下の通り等分散を前提としたものに修正する。
t.test(ttd_SD~oil_gas_value100,data = outla,
alternative="greater",var.equal = T)
Two Sample t-test
data: ttd_SD by oil_gas_value100
t = 3.2045, df = 3735, p-value = 0.0006823
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.007278841 Inf
sample estimates:
mean in group 0 mean in group 1
0.019251337 0.004291845
これでロスとの分析結果と一致した。