開発経済学とその他応用分野を学ぶ院生

人間万事塞翁が馬を大切にしている応用経済学徒. 2020年4月から開発・計量・プログラミング関連の記事を書きます.

社会科学のためのデータ分析入門 章末問題解答(2章-3) Rコード

これまでの章の解答はこちら

章末問題解答(1章-1)と(1章-2)のRコードの記事はこちらこちら(1章-1)(1章-2)から確認できます。
 

はじめに (Textbook Solution: Quantitative Social Science: An Introduction )

Rを使った統計学の日本語のテキストとして非常に定評のある社会科学のためのデータ分析入門の章末問題の解答(Rコード)です。

 
欠点なのかは分かりませんが、こちらのテキストには章末問題の解答がついていません。そして日本語でも英語でもwebで公開されていません(2018年冬ごろの時点では)。2018年冬に私が上巻の章末問題を解いたのですが、一度公開してみようと思ったので複数の記事に分けて投稿していこうと思います。誰かの役に立てればとも思っているのですが、私のコードにミスがあった場合に指摘していただけると嬉しいです。
I would highly appreciate if you could point out mistakes.
 
また同じ変数に関するプロットをする場合でも複数の方法を使ったりもしています。
 

2章-1 (Chapter2 - Section 1)

 
スクリプトをベタ張りしています。

## Chapter 2 Causality 
## Exercise Solution

setwd("~/qss/CAUSALITY") # ご自身のディレクトリを選択

## -----------------------------------------------------
## The author of this script uses Japanese-Version QSS.
## -----------------------------------------------------
## -----------------------------------------------------
## -----------------------------------------------------
## Section 3
## Q1

leader <- read.csv("leaders.csv")

dim(leader)
summary(leader$result)

length(unique(leader$country)) # 88 countries
y <- nrow(leader) # 250 countries in total
z <- length(unique(leader$year)) # n of plots w/o overlaps
y / z # year mean of plots in those 88 countries


## Q2

unique(leader$result)

## Create variable success in the original data frame.
## We use the logical disjunction (|) here 
## because the way to die does not matter. 

leader$success <- NA 
leader$success[leader$result == "dies between a day and a week" 
               | leader$result == "dies between a week and a month" 
               | leader$result == "dies within a day after the attack"
               | leader$result == "dies, timing unknown"
               ] <- 1

leader$success[leader$result == "hospitalization but no permanent disability" 
               | leader$result == "not wounded"
               | leader$result == "plot stopped"
               | leader$result == "survives but wounded severely"
               | leader$result == "survives, whether wounded unknown"
               | leader$result == "wounded lightly"
               ] <- 0

## leader <- leader[, -"sucess"] # Delete wrong variable name

mean(leader$success) # 21.6%

## Success of assassination attempt seems not to be done randomly. 
## Thus, the assumption is not valid. 


## Q3

tapply(leader$politybefore, leader$success, mean)
## The difference b/w the two is about 1.04, seems not to be trivial. 

tapply(leader$age, leader$success, mean)
## Aimed leader among sccuees is 3 years higher than among failure. 


## Q4

## Create a variable warbefore
leader$warbefore <- NA

leader$warbefore[leader$civilwarbefore == 1 
                 | leader$interwarbefore == 1] <- 1

## Notice that we have to use the logical cunjunction (&). 
leader$warbefore[leader$civilwarbefore != 1 
                 & leader$interwarbefore != 1] <- 0

summary(leader$warbefore)

war3b <- subset(leader, leader$warbefore == 1)

tapply(war3b$politybefore, war3b$success, mean)
tapply(war3b$age, war3b$success, mean)

## Among the countries where experience war have higher polity score. 
## Among them, age of ploted leaders are higher among success, diff. is trivial though. 


## Q5

## Create a variable warbefore
leader$warafter <- NA

leader$warafter[leader$civilwarafter == 1
                | leader$interwarafter == 1] <- 1

leader$warafter[leader$civilwarafter != 1
                & leader$interwarafter != 1] <- 0

war3a <- subset(leader, subset = (warafter == 1))

tapply(leader$warafter, leader$success, mean)
tapply(leader$polityafter, leader$success, mean)

## After success the rate of breaking out war is about 20%, 
## while its rate after failure is 30%. 

 
章末問題解答(1章-1) Rコード
https://www.econ-stat-grad.com/entry/statistics/qss/solution/ch1-1www.econ-stat-grad.com
章末問題解答(1章-2) Rコード https://www.econ-stat-grad.com/entry/statistics/qss/solution/ch1-2www.econ-stat-grad.com

f:id:econgrad:20201012235846p:plain