This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(coefplot) | |
| results <- fitsur$eq[1] | |
| coefplot(results) | |
| coefplot(fitsur) | |
| summary(fitsur)$eq[1] | |
| coefplot(fitsur$eq[[1]], title="Elections and Political Processes (AID110) and Civil\n Society (AID130) measured by Voice and Accountability", | |
| coefficients=c("AID110","AID130","RAID110","RAID130", "DIF02"), newNames=c("AID110"="AID110(†)","RAID110"="RAID110(*)","DIF02"="DIF02"), zeroColor = "black", decreasing=T, intercept=F) | |
| coefplot(fitsur$eq[[2]], title="Rule of Law measured by Rule of Law in WGI", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib.request | |
| import urllib.parse #参考: http://d.hatena.ne.jp/motie/20100728/1280335730 | |
| search = "startRecord=1&maximumRecords=5&any=アベノミクス&speaker=安倍晋三" | |
| def DietSearch(search): | |
| request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib.request | |
| import urllib.parse #参考: http://d.hatena.ne.jp/motie/20100728/1280335730 | |
| search = "startRecord=1&maximumRecords=5&any=アベノミクス&speaker=安倍晋三" | |
| def DietSearch(search): | |
| request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8") | |
| returnXML = "" | |
| with urllib.request.urlopen(request) as page: | |
| for line in page.readlines(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import httplib2 | |
| def DietSearch(search): | |
| request = "http://kokkai.ndl.go.jp/api/1.0/speech?" + urllib.parse.quote_plus(search,encoding="utf-8") | |
| h = httplib2.Http('.cache') #by httplib2 | |
| response, content = h.request(request) | |
| #content = content.decode("utf-8") #etree.fromstringをするときに、"Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration."というエラーを回避するためにこの行は使わない | |
| return content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| XMLcontent = DietSearch(search) | |
| from lxml import etree | |
| def XMLextractSpeech(XMLcontent): | |
| root = etree.fromstring(XMLcontent) # 文字列からの読み込み | |
| ##第二階層の情報を取得## | |
| numberOfRecords = root.find('numberOfRecords').text #リクエストされた検索条件に該当する総件数 | |
| numberOfReturn = root.find('numberOfReturn').text #当該リクエストで返戻した件数 | |
| startRecord = root.find('startRecord').text #当該リクエストの返戻結果に含まれる最初のレコードの総結果件数の中での出現位置 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def recordDiet(speaker, startRec=1, any="", Dfrom="0000-01-01", Duntil="9999-12-31"): | |
| search = searchKeyword(speaker, startRec, any, Dfrom, Duntil) | |
| XMLcontent = DietSearch(search) | |
| temp = XMLextractSpeech(XMLcontent) | |
| newStart = int(temp[0]) | |
| extracted = temp[1] | |
| while newStart != 0: #newStartが0じゃない、つまり先がある限り続ける | |
| search = searchKeyword(speaker, newStart, any, Dfrom, Duntil) | |
| XMLcontent = DietSearch(search) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def searchKeyword(speaker, startRec=1, any="", Dfrom="0000-01-01", Duntil="9999-12-31"): | |
| if len(any) > 0: | |
| search = "startRecord=" + str(startRec) + "&" + "any=" + any + "&" + "speaker=" + speaker + "&from=" + Dfrom + "&until=" + Duntil | |
| else: | |
| search = "startRecord=" + str(startRec) + "&" + "speaker=" + speaker + "&from=" + Dfrom + "&until=" + Duntil | |
| return search |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(WDI) | |
| x <- WDI(country="all", indicator=c("AG.AGR.TRAC.NO"), | |
| start=1980, end=2011) | |
| y <- na.exclude(x[,3]) | |
| mean(y) | |
| # 881532.3 | |
| sd(y) | |
| # 2925623 | |
| var(y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # recordDietの結果をPandasにまとめる | |
| import pandas as pd | |
| import os.path | |
| def cleanText(text): | |
| cleaned = text.replace('\n', ' ') # 改行記号を除く | |
| cleaned = cleaned.strip() # 半角スペースを除く | |
| cleaned = cleaned.replace(" ", "") # 全角スペースを除く | |
| return cleaned |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def resultsSave(ext_df, path="/Users/S/Desktop/", name="diet",type="csv"): | |
| # 2種類の形式での保存を可能にする | |
| if type=="csv": | |
| save_path = os.path.join(path, (name + ".csv")) | |
| ext_df.to_csv(save_path) # Excelでは開くと文字化けしたが、Google Spreadsheetで見る限り問題なし | |
| elif type=="pandas": | |
| save_path = os.path.join(path, (name + ".pkl")) | |
| ext_df.to_pickle(save_path) | |
| else: | |
| print("File type you specified is not defined") |
OlderNewer