---
title: "Spacekime Analytics (Time Complexity and Inferential Uncertainty)"
subtitle: "EU Country Economic Indicators - 3D Longitudinal Tracker"
author: 'SOCR Team '
date: "`r format(Sys.time(),'%m/%d/%Y')`"
output:
  html_document:
    theme: spacelab
    highlight: tango
    includes:
      before_body: TCIU_header.html
    toc: yes
    number_sections: yes
    toc_depth: 2
    toc_float:
      collapsed: no
      smooth_scroll: yes
    code_folding: hide
  word_document:
    toc: yes
    toc_depth: '2'
  pdf_document:
    toc: yes
    toc_depth: '2'
always_allow_html: yes
---

# Data Preprocessing

Loading all required packages.

```{r, message=F, warning=F}
library(dplyr)
library(arm)
library(tidyr)
library(ggplot2)
library(ggrepel)
library(plot3D)
library(scatterplot3d)
library(plotly)
library(fastDummies)
library(forecast)

setwd("C:/Users/dinov/Desktop/Ivo.dir/Eclipse_Projects/HTML5_WebSite/TCIU/Chapter6/")

# Load Previously Computed Workspace:
load("C:/Users/dinov/Desktop/Ivo.dir/Eclipse_Projects/HTML5_WebSite/TCIU/Chapter6/EuEco_Yuming.RData")
```

Load and and preprocess the EU Economic data.

```{r}
eu <- read.csv("Master_Aggregate_EU_Econ_Data_11_29_2018_TimeTransform.csv", stringsAsFactors = F)[,-5]
colnames(eu) <- c("country","time","feature","value")
eu <- filter(eu,!country %in% c("European Union (25 countries)","D1_Country",""))
eu$value <- sapply(c(1:nrow(eu)),function(x) as.numeric(gsub(":|,","",eu$value[x])))
eu <- filter(eu, feature != "")
dim(eu)
```

# Reformat the data into a 3D array (country $\times$ feature $\times$ time)

```{r warning=F}
unq_country <- sort(unique(eu$country))
unq_time <- sort(unique(eu$time))
unq_fea <- sort(unique(eu$feature))
num_country <- length(unq_country)
num_time <- length(unq_time)
num_fea <- length(unq_fea)
eu <- arrange(eu,country,time,feature)
eu_3d_array <- array(NA,dim = c(num_country,num_time,num_fea),dimnames = list(unq_country,unq_time,unq_fea))
for (i in 1:num_country){
  for (j in 1:num_time){
    for (k in 1:num_fea){
      eu_3d_array[i,j,k] = eu$value[(i-1)*num_time*num_fea + (j-1)*num_fea + k]
    }
  }
}
eu_3d_array[1:10,1:10,1]
```

# 3D Data visualization

```{r warning=F, fig.width=10, fig.height=10}
eu <- arrange(eu,time,feature,country)
# eu_visualization <- select(eu,time,feature,country,value)
eu_visualization$time <- sapply(c(1:nrow(eu_visualization)),function(x) as.numeric(gsub("Q",".",eu_visualization$time[x])))
eu_visualization$feature <- as.factor(eu_visualization$feature)
eu_visualization$country <- as.factor(eu_visualization$country)
eu_visualization$value <- as.numeric(eu_visualization$value)
eu_visualization$feature <- sapply(c(1:nrow(eu_visualization)),function(x) substr(eu_visualization$feature[x],1,20))

plot_ly(eu_visualization, x = ~time, y = ~country, z = ~value, color = ~feature,split = ~ country,type = 'scatter3d', mode = 'lines')
```

# Time series format

```{r warning=F}
#Find the duplicates
eu_time_series <- na.omit(eu)
allFeatures = as.character(unique(eu_time_series$feature))
allTime = unique(eu_time_series$time)
allCountry = as.character(unique(eu_time_series$country))
allCombination = length(allFeatures)*length(allTime)*length(allCountry)
dup = c()
for (i in 1:length(allFeatures)){
  for (j in 1:length(allCountry)){
    for (k in 1:length(allTime)){
      if (nrow(filter(eu_time_series,country == allCountry[j] & 
                      feature == allFeatures[i] & time == allTime[k]))>1){
        dup = c(dup,as.character(allFeatures[i]))
        break
      }
    }
    break
  }
}
dup # These features have mutiple observations at the same time point
```

Remove duplicates.

```{r warning=F}
removeDup = filter(eu_time_series, feature != "Employment by sex, age and educational attainment level, Total, From 15 to 64 years, All ISCED 2011 levels" &
                     feature != "Labor cost for LCI excluding bonuses" &
                     feature != "Labor costs other than wages or salaries" &
                     feature != "Labour cost for LCI (compensation of employees plus taxes minus subsidies)" &
                     feature != "Labour cost for LCI excluding bonuses" &
                     feature != "Labour costs other than wages and salaries" &
                     feature != "Wages and salaries (total)")
time_series = spread(removeDup,feature,value)
dim(time_series)
```

Additional details, examples, modeling strategies for interpreting the EU Economics data are available in [TCIU Chapter 6 (Applications)](https://tciu.predictive.space/).

<!--html_preserve-->
<div>
    	<footer><center>
			<a href="https://www.socr.umich.edu/">SOCR Resource</a>
				Visitor number <img class="statcounter" src="https://c.statcounter.com/5714596/0/038e9ac4/0/" alt="Web Analytics" align="middle" border="0">
				<script type="text/javascript">
					var d = new Date();
					document.write(" | " + d.getFullYear() + " | ");
				</script> 
				<a href="https://socr.umich.edu/img/SOCR_Email.png"><img alt="SOCR Email"
	 			title="SOCR Email" src="https://socr.umich.edu/img/SOCR_Email.png"
	 			style="border: 0px solid ;"></a>
	 		 </center>
	 	</footer>

	<!-- Start of StatCounter Code -->
		<script type="text/javascript">
			var sc_project=5714596; 
			var sc_invisible=1; 
			var sc_partition=71; 
			var sc_click_stat=1; 
			var sc_security="038e9ac4"; 
		</script>
		
		<script type="text/javascript" src="https://www.statcounter.com/counter/counter.js"></script>
	<!-- End of StatCounter Code -->
	
	<!-- GoogleAnalytics -->
		<script src="https://www.google-analytics.com/urchin.js" type="text/javascript"> </script>
		<script type="text/javascript"> _uacct = "UA-676559-1"; urchinTracker(); </script>
	<!-- End of GoogleAnalytics Code -->
</div>
<!--/html_preserve-->

