Do unemployed people die soon ? Is there any relation between unemployment and life expectancy ? Just to explore these facts i have pulled some data sets of different countries about the unemployment rate and the life expectacy. I conducted Exploratory Data Analysis and discovered few interesting facts.

# 1. Reading Data sets :

setwd("D:/Raviteja/Raviteja Professional/Data Science/EDA_Course_Materials")
u2<- read.csv("unemployment rate1.csv", sep=',')

l2<- read.csv("life expectancy1.csv", sep=',')

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library('tidyr')
library('gridExtra')
library(ggplot2)

u2.rate <- gather(u2, "year", "unemployment", 2:length(colnames(u2)))

l2.rate <- gather(l2, "year", "life_expectancy", 2:length(colnames(l2)))

#2.combine dataframes into one for analysis

u2.l2 <-inner_join(u2.rate,l2.rate, by=c('country','year'))

#Remove the annoying X's on year

u2.l2$year <- gsub("X", '', u2.l2$year)

#3. Checking the relation between unemployment rate and Life expectacy (in years)

ggplot(aes(x=unemployment,y=life_expectancy),data=u2.l2)+geom_point()+scale_x_continuous(limits=c(2,12),breaks=seq(2,12,0.25))+scale_y_continuous(limits=c(35,85),breaks=seq(35,85,5))+labs(title="unemployment rate VS life_expectancy", x = "unemployment rate", y = "life expectancy in years")+geom_smooth()

with(data= u2.l2,cor.test(unemployment,life_expectancy, method='pearson'))
## 
##  Pearson's product-moment correlation
## 
## data:  unemployment and life_expectancy
## t = 2.14, df = 271, p-value = 0.03325
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.01035152 0.24389481
## sample estimates:
##       cor 
## 0.1289102
# as the corelation co-efficient is 0.128, there is No Considerable corelation between unemployment and life expectancy

#4. Relation between unemployment rate and life expectancy for different countries:

ggplot(aes(x=unemployment,y=life_expectancy),data= u2.l2)+ geom_line(aes(color= country), position= "jitter")+scale_x_continuous(limits=c(2,12),breaks=seq(2,12,0.25))+scale_y_continuous(limits=c(35,85),breaks=seq(35,85,5))

#5. Observing the how  the life expectancy (in years) and the unemployment rate changed over the years for few countries :

selected.countries <- na.omit(u2.l2[,1:4])

set.seed(2221)

levels=c(selected.countries$country)
sample.ids <- sample(levels,16)

sample.ids <- c( "United States","France", "India", "Japan","China")

d1<-ggplot(aes(x = year, y = unemployment), data = subset(selected.countries, country %in% sample.ids))+facet_wrap(~ country,scales="free_x",ncol=1) + geom_line(aes(group=country))+ scale_x_discrete(breaks=seq(2000, 2012,1))+labs(title = "unemployment rate between 2000-2012", x = "Year", y = "unemployment rate")

d2<-ggplot(aes(x = year, y = life_expectancy), data = subset(selected.countries, country %in% sample.ids))+facet_wrap(~ country,scales="free_x", ncol=1) + geom_line(aes(group=country))+ scale_x_discrete(breaks=seq(2000, 2012,1))+labs(title = "life expectancy between 2000-2012", x = "Year", y = "life expectancy")

library("gridExtra")

grid.arrange(d2,d1,ncol=2)

#7. Observing the relation between life expectancy (in years) and the unemployment rate for few of the interesting countreies :

x1<-ggplot(aes(x = year, y = unemployment, color= country), data = subset(selected.countries, country %in% sample.ids)) + geom_line(aes(group= country),position= "jitter")+scale_x_discrete(breaks=seq(2000, 2012,1))+labs(title = "unemployment rate between 2000-2012", x = "Year", y = "unemployment rate")

x2<-ggplot(aes(x = year, y = life_expectancy, color = country), data = subset(selected.countries, country %in% sample.ids))+geom_line(aes(group = country),position= "jitter")+ scale_x_discrete(breaks=seq(2000, 2012,1))+labs(title = "life expectancy between 2000-2012", x = "Year", y = "life expectancy")

grid.arrange(x1,x2,ncol=2)

#Interesting facts from the Above study :

#I have pulled out some data on the life expectancy in years &unemployment rate of few countries and generated the above plot.The Interesting fact: there is no relation between these two factors.So, don't fear if you are unemployed.It won't let you die soon.China is almost 7 to 8 years ahead to India in life expectancy.Though Japan has a history of devastating nuclear attack , it's life expectancy is the highest among the lot. In 2008, where global global recession hit the world, unemployment rate of all the countries is increased except India