Paris

Giriş

Çalışma kapsamında Quandl R paketi ile veri setlerinin nasıl çekildiği, meta verinin nasıl elde edildiği, özellikle zaman serileri bulunan veri setlerinde dinamik grafiklerin nasıl oluşturulduğu dygraphs ve plotly paketleri kullanılarak ele alınmıştır.

Quandl R paketi ile premium veri setlerini alabileceğiniz gibi ücretsiz olarak tanımlanmış veri setlerini de alabilirsiniz. Burada bir ücret söz konusu değildir. Bazı veri setlerinin indirilebilmesi için bu pakette ücretli abonelik tanımlanmıştır.Quandl R paketi ücretsizdir. Ancak günde 50’den fazla arama yapmak istiyorsanız, ücretsiz bir Quandl hesabı oluşturmanız ve API anahtarınızı almanız gerekir.Premium veri setlerine ulaşabilmek için de API anahtarının olması gerekir. API anahtarını https://www.quandl.com/login web adresinden kayıt yaptırarak alabilirsiniz.API anahtarını elde ettikten sonra Quandl.api_key(“API anahtarı”) ve Quandl(“FRED/GDP”) veya kombine olarak Quandl(“FRED/GDP”,api_key=“API anahtarı”) şeklinde yazılması gerekir. Buradaki örneğimizde ABD Merkez Bankasının Gayri Safi Milli Hasıla (GDP) verisi çekilmiştir.Fonksiyon içerisinde FRED ise Federal Reserve Economic Data yani Federal Rezerv Ekonomik Verisi’nin kısaltmasıdır. Ücretsiz olarak tanımlanan veri setlerinde bahsedildiği üzere API anahtarına ihtiyaç yoktur.Veri setlerini farklı formatlarda alabilirsiniz. Bunlardan bir kaçı şöyledir:

Eğer veriyi farklı zaman periyodları halinde almak istersek veri setinin yapısına göre saatlik, günlük, haftalık, aylık, çeyrek dönemlik olarak da alabiliriz. Örneğin, Quandl(“FRED/GDP”, type=“ts”, collapse=“annual”) fonksiyonunda ABD GDP verisini yıllık olarak ve ts formatında almış oluruz.

Çalışmada grafikler oluşturulurken veri madenciliği teknikleri kullanarak nasıl grafiklere uygun hale getirildiğini de görmüş olacaksınız.

Yüklenen kütüphaneler

kütüphane<-c("dplyr","tibble","tidyr","ggplot2","ggthemes","readxl", "writexl", "psych", "GGally", "rstatix","RColorBrewer","htmlwidgets","kableExtra","stargazer","readr", "lubridate", "Quandl", "xts", "data.table", "dygraphs", "quantmod", "plotly")
yükle<-sapply(kütüphane, require, character.only = TRUE, warn.conflicts = FALSE)
 
#Kütüphane yüklenme durumunu gösteren tablo
 
tablo=suppressWarnings(yükle, classes = "warning")
   
isim=names(tablo)
   
deger=as_tibble(tablo)
   
data.frame(Sıra=1:length(isim), Kütüphane=isim, Durumu=deger$value) %>% 
mutate(Durumu=if_else(Durumu==TRUE, "Yüklendi", "Paket Kurulumu Gerekli")) %>% 
kable(caption = "Yüklenen Kütüphaneler")%>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F, html_font="calibri")
Yüklenen Kütüphaneler
Sıra Kütüphane Durumu
1 dplyr Yüklendi
2 tibble Yüklendi
3 tidyr Yüklendi
4 ggplot2 Yüklendi
5 ggthemes Yüklendi
6 readxl Yüklendi
7 writexl Yüklendi
8 psych Yüklendi
9 GGally Yüklendi
10 rstatix Yüklendi
11 RColorBrewer Yüklendi
12 htmlwidgets Yüklendi
13 kableExtra Yüklendi
14 stargazer Yüklendi
15 readr Yüklendi
16 lubridate Yüklendi
17 Quandl Yüklendi
18 xts Yüklendi
19 data.table Yüklendi
20 dygraphs Yüklendi
21 quantmod Yüklendi
22 plotly Yüklendi

Veri setlerine ulaşma

National Stock Exchange of India Prices (NSE) OIL veri setinden çeyrek dönemlik ve zoo formatında veri elde edilmiştir.

Quandl('NSE/OIL', collapse = "quarterly", type = "zoo", limit = 10)%>% 
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F, html_font="calibri")
Open High Low Last Close Total Trade Quantity Turnover (Lacs)
443.75 453.50 442.35 452.10 452.10 218310 981.19
329.95 334.85 328.35 334.40 333.80 601495 1998.14
266.90 267.00 257.10 258.40 259.60 1306513 3406.69
345.95 354.95 340.10 353.65 353.65 861320 3013.87
375.50 377.65 369.50 369.55 371.50 493493 1843.38
215.10 218.90 214.10 218.10 216.35 2219838 4797.22
204.00 211.20 201.65 210.45 209.85 710864 1478.56
216.75 224.95 210.60 221.20 219.55 1139867 2457.46
178.10 179.00 174.35 175.00 174.80 761462 1343.75
172.05 174.95 172.05 174.55 174.55 431122 749.99

Türkiye aramasına göre veri setleri

Quandl.search(query="Turkey", silent=T) %>% 
select(id, dataset_code, database_code, name,   description)%>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F, html_font="calibri")
id dataset_code database_code name description
49526158 TUR_NGDPRPPPPCPCH ODA Turkey Turkey . Units: Purchasing power parity; percent change. Estimates begin after 2019. GDP is expressed in constant international dollars per person. Data are derived by dividing constant price purchasing-power parity (PPP) GDP by total population. ‘Source: Haver Analytics. Data from Turkish Statistical Institute (TurkStat) Latest actual data: 2019 National accounts manual used: European System of Accounts (ESA) 2010 GDP valuation: Market prices Start/end months of reporting year: January/December Base year: 2009 Chain-weighted: Yes, from 2009’.
47547766 TUR_NGDPRPPPPC ODA Turkey Turkey . Units: Purchasing power parity; 2011 international dollar. Multiplier: Units. Estimates begin after 2018. GDP is expressed in constant international dollars per person. Data are derived by dividing constant price purchasing-power parity (PPP) GDP by total population. ‘See notes for: Gross domestic product, constant prices (National currency) Population (Persons).’.
120514 TRINTDEXR FRED Turkish Intervention: Central Bank of Turkey Purchases of USD (Millions of USD) Millions of USD Not Seasonally Adjusted, (+) numbers mean purchases of USD, (-) numbers mean sales of USD. Unpublished data. Since 2002, the foreign exchange interventions have started to be published through press releases at the same day when the intervention was made. The amount that was bought/sold at an intervention is published on the first working day of the month which comes after 3 months following the intervention date.
13990273 TURIMVOL SGE Turkey - Imports <p>The total value of a country’s imports of physical goods and payments to foreigners for services like shipping and tourism.</p><p><b>Units: </b>USD Million </p><p><b>Source: </b><a href =http://www.turkstat.gov.tr/Start.do>Turkish Statistical Institute</a></p>
13977410 TURPOP SGE Turkey - Population <p>The number of residents of Turkey.</p><p><b>Units: </b>Million </p><p><b>Source: </b><a href =http://www.turkstat.gov.tr/Start.do>Turkish Statistical Institute</a></p>
13990554 TURBR SGE Turkey - Bankruptcies <p>The monthly number of corporate bankruptcies in Turkey.</p><p><b>Units: </b>http://www.tobb.org.tr/ Business</p><p><b>Source: </b><a href =http://www.tobb.org.tr/Sayfalar/Eng/AnaSayfa.php>TOOB, Turkey</a></p>
13990495 TURREM SGE Turkey - Remittances <p>The monthly net value of remittances into Turkey. Remittances are monies sent into Turkey by workers in foreign countries. </p><p><b>Units: </b>USD Million </p><p><b>Source: </b><a href =http://www.tcmb.gov.tr/yeni/eng/>Central Bank of the Republic of Turkey</a></p>
13989473 TUREXVOL SGE Turkey - Exports <p>The total value of the goods and services produced by Turkey and purchased by foreign entities.</p><p><b>Units: </b>USD Million </p><p><b>Source: </b><a href =http://www.turkstat.gov.tr/Start.do>Turkish Statistical Institute</a></p>
2399401 POPTTLTRA173NUPN FRED Population for Turkey Thousands Not Seasonally Adjusted, For proper citation, see http://pwt.econ.upenn.edu/php_site/pwt_index.php Source Indicator: POP
8526647 POPTTLTRA148NRUG FRED Population for Turkey Millions of Persons Not Seasonally Adjusted, Source ID: pop When using these data in your research, please make the following reference: Feenstra, Robert C., Robert Inklaar and Marcel P. Timmer (2013), “The Next Generation of the Penn World Table” available for download at www.ggdc.net/pwt For more information, see http://www.rug.nl/research/ggdc/data/penn-world-table.

Ekonomik göstergeler veri seti (Global Economic Indicators)’nden bir kesit

Api anahtarı gerektirdiği için kod bloğu kapatılmıştır.

series_id country_code indicator_code identifier description units frequency date value
76937 US 034 US.SP2.034.A.02 United States: Fiscal Balance (% of GDP) [United States: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2019-01-01 -5.3
76937 US 034 US.SP2.034.A.02 United States: Fiscal Balance (% of GDP) [United States: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2018-01-01 -4.9
76937 US 034 US.SP2.034.A.02 United States: Fiscal Balance (% of GDP) [United States: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2017-01-01 -2.8
76937 US 034 US.SP2.034.A.02 United States: Fiscal Balance (% of GDP) [United States: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2016-01-01 -3.8
76937 US 034 US.SP2.034.A.02 United States: Fiscal Balance (% of GDP) [United States: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2015-01-01 -3.2
75978 CA 034 CA.SP2.034.A.02 Canada: Fiscal Balance (% of GDP) [Canada: Government finance: Percentage of GDP: Deficit & financing: Net lending (+) / net borrowing (-)] % A 2019-01-01 0.8

ABD Milli Hasıla verisi

# veri birleştirme

r=Quandl("FRED/GDPC1") %>% 
as_tibble()%>% mutate(tur=rep("Reel GSMH",298))

g=Quandl("FRED/GDP") %>% 
as_tibble() %>% mutate(tur=rep("GSMH",298))

rp=Quandl("FRED/GDPPOT")%>% 
as_tibble() %>% mutate(tur=rep("Reel Potansiyel GSMH",332))

veri=bind_rows(r,g, rp) %>% rename("Hasıla"=Value, "Tarih"=Date)
veri %>% ggplot(aes(x=Tarih, y=Hasıla, group=tur))+geom_line(aes(colour=tur), size = 1) + theme_igray() + ggtitle("ABD Milli Hasıla ($)")+ xlab("Tarih")+ylab("Milyar $")+scale_x_date(date_breaks = "8 years", date_labels = "%Y" )+
theme(plot.title = element_text(hjust = 0.5))+
labs(color='Türü')+
labs(caption = "Veri kaynağı: FRED")

Dinamik grafik oluşturma 1

Burada dygraph kütüphanesi kullanılarak dinamik grafik oluşturulmuştur.

r=Quandl("FRED/GDPC1") %>% 
as_tibble()%>% mutate(tur=rep("Reel GSMH",298))%>% rename("Hasıla"=Value, "Tarih"=Date)
r1=r[,c(1:2)]
r1$Tarih= as.Date(r1$Tarih)

r2=as.xts(as.data.table(r1))

dygraph(r2, main="ABD GSMH") %>%
  dyOptions(labelsUTC = TRUE, fillGraph=TRUE, fillAlpha=0.2, drawGrid = T, colors="red") %>%
  dyRangeSelector() %>%
  dyAxis("y", label = "Milyar ($)") %>%
  dyCrosshair(direction = "vertical") %>%
  dyHighlight(highlightCircleSize = 8, highlightSeriesBackgroundAlpha = 0.4, hideOnMouseOut = FALSE)  %>%
  dyRoller(rollPeriod = 1)

Dinamik grafik oluşturma 2

Burada dygraph kütüphanesi kullanılarak birden fazla değişken aynı grafik üzerinde gösterilmiştir.

r=Quandl("FRED/GDPC1") %>% 
as_tibble()%>% mutate(tur=rep("Reel GSMH",298))

g=Quandl("FRED/GDP") %>% 
as_tibble() %>% mutate(tur=rep("GSMH",298))

rp=Quandl("FRED/GDPPOT")%>% 
as_tibble() %>% mutate(tur=rep("Reel Potansiyel GSMH",332))

veri=bind_rows(r,g, rp) %>% rename("Hasıla"=Value, "Tarih"=Date)


lv=pivot_wider(veri, names_from = tur, values_from = Hasıla)

lv$Tarih= as.Date(lv$Tarih)

lv1=as.xts(as.data.table(lv))


dygraph(lv1, main="ABD GSMH Türleri") %>%
  dyOptions(labelsUTC = TRUE, fillGraph=TRUE, fillAlpha=0.2, drawGrid = T) %>%
  dyRangeSelector() %>%
  dyAxis("y", label = "Milyar ($)") %>%
  dyCrosshair(direction = "vertical") %>%
  dyHighlight(highlightCircleSize = 8, highlightSeriesBackgroundAlpha = 0.4, hideOnMouseOut = FALSE)  %>%
  dyRoller(rollPeriod = 1)

Dinamik grafik oluşturma 3

Burada plotly kütüphanesi kullanılarak birden fazla değişken aynı grafik üzerinde gösterilmiştir.

r=Quandl("FRED/GDPC1") %>% 
as_tibble()%>% mutate(tur=rep("RGSMH",298))

g=Quandl("FRED/GDP") %>% 
as_tibble() %>% mutate(tur=rep("GSMH",298))

rp=Quandl("FRED/GDPPOT")%>% 
as_tibble() %>% mutate(tur=rep("RPGSMH",332))

veri=bind_rows(r,g, rp) %>% rename("Hasıla"=Value, "Tarih"=Date)


lv=pivot_wider(veri, names_from = tur, values_from = Hasıla) #tablonun geniş formata dönüştürülmesi

sekil <- plot_ly(lv, x = ~Tarih, y = ~GSMH, name = 'GSMH', type = 'scatter', mode = 'lines') 
sekil<- sekil %>% add_trace(y = ~RGSMH, name = 'RGSMH', mode = 'lines') 
sekil <- sekil %>% add_trace(y = ~RPGSMH, name = 'RPGSMH', mode = 'lines')%>%
         layout(title = "ABD GSMH Türleri",
         xaxis = list(title = "Tarih"),
         yaxis = list (title = "Hasıla ($)"))

sekil

Dinamik grafik oluşturma 4

Burada plotly kütüphanesi kullanılarak birden fazla değişken aynı grafik üzerinde alan grafiği olarak gösterilmiştir.

r=Quandl("FRED/GDPC1") %>% 
as_tibble()%>% mutate(tur=rep("RGSMH",298))

g=Quandl("FRED/GDP") %>% 
as_tibble() %>% mutate(tur=rep("GSMH",298))

rp=Quandl("FRED/GDPPOT")%>% 
as_tibble() %>% mutate(tur=rep("RPGSMH",332))

veri=bind_rows(r,g, rp) %>% rename("Hasıla"=Value, "Tarih"=Date)


lv=pivot_wider(veri, names_from = tur, values_from = Hasıla)

#alan grafiği
c <- plot_ly(na.omit(lv), x = ~Tarih, y = ~GSMH, type="scatter", mode="markers", fill = "tozeroy", name = 'GSMH')
c <- add_trace(c, x = ~Tarih, y = ~RGSMH, type="scatter", mode="markers", fill = "tozeroy", name = 'RGSMH')
c <- add_trace(c, x = ~Tarih, y = ~RPGSMH, type="scatter", mode="markers", fill = "tonexty", name='RPGSMH')%>%
         layout(title = "ABD GSMH Türleri",
         xaxis = list(title = "Tarih"),
         yaxis = list (title = "Milyar $"))
c

Dünya Bankası metaveri

data <- Quandl.datatable("WB/METADATA")
data %>% head(10) %>% kable() %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F, html_font="calibri")
series_id name description
VC.PKP.TOTL.UN Presence of peace keepers (number of troops, police, and military observers in mandate) Presence of peacebuilders and peacekeepers are active in peacebuilding and peacekeeping. Peacebuilding reduces the risk of lapsing or relapsing into conflict by strengthening national capacities at all levels of for conflict management, and to lay the foundation for sustainable peace and development. Peacekeepers provide essential security to preserve the peace, however fragile, where fighting has been halted, and to assist in implementing agreements achieved by the peacemakers. Peacekeepers deploy to war-torn regions where no one else is willing or able to go and prevent conflict from returning or escalating. Peacekeepers include police, troops, and military observers. Source: UN Department of Peacekeeping Operations, http://www.un.org/en/peacekeeping/.
VC.IHR.PSRC.P5 Intentional homicides (per 100,000 people) Intentional homicides are estimates of unlawful homicides purposely inflicted as a result of domestic disputes, interpersonal violence, violent conflicts over land resources, intergang violence over turf or control, and predatory violence and killing by armed groups. Intentional homicide does not include all intentional killing; the difference is usually in the organization of the killing. Individuals or small groups usually commit homicide, whereas killing in armed conflict is usually committed by fairly cohesive groups of up to several hundred members and is thus usually excluded. Source: UN Office on Drugs and Crime’s International Homicide Statistics database.
VC.IDP.TOCV Internally displaced persons, total displaced by conflict and violence (number of people) Internally displaced persons are defined according to the 1998 Guiding Principles (http://www.internal-displacement.org/publications/1998/ocha-guiding-principles-on-internal-displacement) as people or groups of people who have been forced or obliged to flee or to leave their homes or places of habitual residence, in particular as a result of armed conflict, or to avoid the effects of armed conflict, situations of generalized violence, violations of human rights, or natural or human-made disasters and who have not crossed an international border. â<U+0080><U+009C>People displacedâ<U+0080> refers to the number of people living in displacement as of the end of each year, and reflects the stock of people displaced at the end of the previous year, plus inflows of new cases arriving over the year as well as births over the year to those displaced, minus outflows which may include returnees, those who settled elsewhere, those who integrated locally, those who travelled over borders, and deaths. Source: The Internal Displacement Monitoring Centre (http://www.internal-displacement.org/)
VC.IDP.NWDS Internally displaced persons, new displacement associated with disasters (number of cases) Internally displaced persons are defined according to the 1998 Guiding Principles (http://www.internal-displacement.org/publications/1998/ocha-guiding-principles-on-internal-displacement) as people or groups of people who have been forced or obliged to flee or to leave their homes or places of habitual residence, in particular as a result of armed conflict, or to avoid the effects of armed conflict, situations of generalized violence, violations of human rights, or natural or human-made disasters and who have not crossed an international border. New Displacement refers to the number of new cases or incidents of displacement recorded over the specified year, rather than the number of people displaced. This is done because people may have been displaced more than once. Source: The Internal Displacement Monitoring Centre (http://www.internal-displacement.org/)
VC.IDP.NWCV Internally displaced persons, new displacement associated with conflict and violence (number of cases) Internally displaced persons are defined according to the 1998 Guiding Principles (http://www.internal-displacement.org/publications/1998/ocha-guiding-principles-on-internal-displacement) as people or groups of people who have been forced or obliged to flee or to leave their homes or places of habitual residence, in particular as a result of armed conflict, or to avoid the effects of armed conflict, situations of generalized violence, violations of human rights, or natural or human-made disasters and who have not crossed an international border. New Displacement refers to the number of new cases or incidents of displacement recorded over the specified year, rather than the number of people displaced. This is done because people may have been displaced more than once. Source: The Internal Displacement Monitoring Centre (http://www.internal-displacement.org/)
VC.BTL.DETH Battle-related deaths (number of people) Battle-related deaths are deaths in battle-related conflicts between warring parties in the conflict dyad (two conflict units that are parties to a conflict). Typically, battle-related deaths occur in warfare involving the armed forces of the warring parties. This includes traditional battlefield fighting, guerrilla activities, and all kinds of bombardments of military units, cities, and villages, etc. The targets are usually the military itself and its installations or state institutions and state representatives, but there is often substantial collateral damage in the form of civilians being killed in crossfire, in indiscriminate bombings, etc. All deaths–military as well as civilian–incurred in such situations, are counted as battle-related deaths. Source: Uppsala Conflict Data Program, http://www.pcr.uu.se/research/ucdp/.
TX.VAL.TRVL.ZS.WT Travel services (% of commercial service exports) Travel services (% of commercial service exports) covers goods and services acquired from an economy by travelers in that economy for their own use during visits of less than one year for business or personal purposes. Travel services include the goods and services consumed by travelers, such as lodging and meals and transport (within the economy visited). Source: International Monetary Fund, Balance of Payments Statistics Yearbook and data files.
TX.VAL.TRAN.ZS.WT Transport services (% of commercial service exports) Transport services (% of commercial service exports) covers all transport services (sea, air, land, internal waterway, space, and pipeline) performed by residents of one economy for those of another and involving the carriage of passengers, movement of goods (freight), rental of carriers with crew, and related support and auxiliary services. Excluded are freight insurance, which is included in insurance services; goods procured in ports by nonresident carriers and repairs of transport equipment, which are included in goods; repairs of railway facilities, harbors, and airfield facilities, which are included in construction services; and rental of carriers without crew, which is included in other services. Source: International Monetary Fund, Balance of Payments Statistics Yearbook and data files.
TX.VAL.TECH.MF.ZS High-technology exports (% of manufactured exports) High-technology exports are products with high R&D intensity, such as in aerospace, computers, pharmaceuticals, scientific instruments, and electrical machinery. Source: United Nations, Comtrade database through the WITS platform.
TX.VAL.TECH.CD High-technology exports (current US$) High-technology exports are products with high R&D intensity, such as aerospace, computers, pharmaceuticals, scientific instruments, and electrical machinery. Data are in current U.S. dollars. Source: United Nations, Comtrade database through the WITS platform.

Dünya Bankası (DB) veri seti

Burada DB veri setinden TX.VAL.TRVL.ZS.WT değişkeninden orta yüksek gelirli ülkelerde hizmet ihracatının yayı (%) grafikle verilmiştir.

options(scipen=999)
veri=Quandl.datatable('WB/DATA') %>% 
as_tibble() %>% filter(country_name=="Upper middle income")%>%
filter(series_id=="TX.VAL.TRVL.ZS.WT")%>%select(year, value) 
veri %>%  
ggplot(aes(x=year, y=value))+ geom_line(aes(color="red"), size = 2) + theme_dark() + ggtitle("Orta Yüksek Gelir Grubundaki Ülkelerde Hizmet İhracatının Payı (%)")+ xlab("Yıl")+ylab("%")+
theme(plot.title = element_text(hjust = 0.5))+
theme(legend.position = "none")+
scale_x_continuous(breaks = seq(from = min(veri$year), to = max(veri$year), by = 2))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
labs(caption = "Veri kaynağı: Dünya Bankası")

Sonuç

Çalışma kapsamında Quandl R paketi ile veri setlerinin nasıl çekildiği, meta verinin nasıl elde edildiği, özellikle zaman serileri bulunan veri setlerinde dinamik grafiklerin nasıl oluşturulduğu dygraphs, quantmod ve plotly paketleri kullanılarak ele alınmıştır.

Faydalı olması ve farkındalık oluşturması dileğiyle.

Bilimle ve teknolojiyle kalınız.

Saygılarımla…

Not: Kaynak gösterilmeden alıntı yapılamaz veya kopyalanamaz.

Note: It can not be cited or copied without referencing.

Kaynaklar