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

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

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

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

私は学部のエコノメの授業でRを触った程度で、修士に入ってから本格的に(自分で)Rについて学ぼうと思い、日本語のテキストとして近年定評のある社会科学のためのデータ分析入門を使いました。Rを使った入門レベルの計量経済学のテキストも洋書では既にたくさんの良書がありますが(Applied Econometrics with RやStock&WatsonのR版)、日本語で分かりやすくサクサク進められるテキストは数少ないと思います。社会科学のためのデータ分析入門は"計量経済学"のテキストではありませんが、手法として重複している箇所は非常に多く、Rでの統計分析のノウハウを学ぶ初めのテキストとして最適だと思います。

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

1章-1 (Chapter1 - Section 1)

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

## Chapter 1 Introduction
## Exercise Solution

## -----------------------------------------------------
## Taka(the author of this script) uses Japanese-Version QSS.
## -----------------------------------------------------
## Section 1
## Q1

## Read the turnout data file. 
turn <- read.csv("turnout.csv")

dim(turn)
head(turn)
summary(turn)
turn$year # 1980 
length(turn) # length of a vector


## Q2 

## Add overseas electorate to VAP.
turn$electorate <- turn$VAP + turn$overseas

## Calculate voter turnout of VAP. 
vap <- (turn$total / turn$electorate) * 100

## Calculate voter turnout of VEP. 
vep <- (turn$total / turn$VEP) * 100

vap; vep
mean(vap); mean(vep) # The diff. b/w the two seems to be trivial. 


## Q3

d1 <- turn$ANES - vap
d2 <- turn$ANES - vep

mean(d1); mean(d2) 
range(d1); range(d2) # range of the difference


## Q4

turn$year
## 1980, '84, '88, '92, '96, '00, '04, '08: presidential election
## 1982, '86, '90, '94, '98, '02,         : midterm election

pe <- turn[seq(1, 14, 2), ] # create presidential election
pe[8, ] <- turn[14, ]       # add year 2008
me <- turn[seq(2, 12, 2), ] # create midterm election

vepP <- (pe$total / pe$VEP) * 100 # voter turnout of VEP predident
vepM <- (me$total / me$VEP) * 100 # voter turnout of VEP midterm

pe$ANES # voter turnout of ANES president
me$ANES # voter turnout of ANES midterm

## Big difference b/w presidential & midterm elec, in ANES

## Create difference. 
diff1 <- mean(pe$ANES) - mean(vepP) # VEP vs. ANES in presidential election
diff2 <- mean(me$ANES) - mean(vepM) # VEP vs. ANES in midterm election

diff1 - diff2

## Estimation of (ANES - VEP) is 2.46% higher in presidential election. 
## Voter turnout is higher in presidential election. 


## Q5

old <- turn[c(1:7), ]  # Extract first hald of data (up to 1992).
## old <- turn[seq(1, 7), ] # Do same thing as shown above. 
new <- turn[c(8:14), ] # Extract second hald of data (from '94 to the end). 

VEPo <- (old$total / old$VEP) * 100
VEPn <- (new$total / new$VEP) * 100
ANESo <- old$ANES
ANESn <- new$ANES

mean(ANESo) - mean(VEPo)
mean(ANESn) - mean(VEPn)

## In terms of the mean of two periods, the ANES bias increased. 


## Q6

## Subtract felons & noncitisen from VAP.
VAP1 <- turn$VAP - turn$felons - turn$noncit

## Extract the year 2008
turn08 <- turn[turn$year==2008, ] 
## turn08 <- turn[14, ] ## same as above 

## Subtract osvoters from total in 2008.
turn$total[14] <- turn08$total - turn08$osvoters

## adjusted voter turnout VAP
adjVAP <- turn$total / VAP1

mean(adjVAP)
mean(vap)
mean(vep)
mean(turn$ANES)

f:id:econgrad:20201012235846p:plain