R Codes used in videos

To facilitate all those who are watching videos on learning Econometrics with R can use codes used in videos given below

R Codes for V#4

library(dplyr)   ## If you get error write install.packages("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(gapminder)  ## Same as above
gapminder       ## Top 10 values if data are in tibble form of dplyr
glimpse(gapminder)  ## It is used to get a quick overview of data
View(gapminder) ## If one wants to browse full data in spreadsheet
head(gapminder)   ## To get first 6 values of data
tail(gapminder)   ## To get last 6 values of data
?gapminder      ## ?packagename is for help
## starting httpd help server ...
##  done

RStudio Codes for Video#5 for dplyr verbs

gapminder %>% sample_n(size = 5) # 5 values at random 
gapminder %>% filter(year==2007) # getting data only for 2007
gapminder %>% filter(year==2007) %>% arrange(gdpPercap) # arrange verb is used

gapminder %>% filter(year==2007, country=="India") # getting data only for India for 2007
gapminder2007<-gapminder %>% filter(year==2007) ## Now this data is stored in R
gapminderSA<-gapminder %>% filter(country %in% c("Bangladesh","India","Pakistan","Sri Lanka","Nepal", "Afghanistan","Bhutan", "Maldives"))   ## For selecting data for South Asia

gapminderSA %>% filter(year==2007) %>% arrange(lifeExp)

gapminderSA %>% filter(year==2007) %>% arrange(desc(lifeExp))


gapminderSA %>% filter(year==2007) %>%select(country,gdpPercap,lifeExp) %>%  arrange(desc(lifeExp))

gapminder %>% mutate(pop=pop/1000000)

gapminder %>% group_by(continent) %>% summarise(mean=mean(lifeExp),min=min(lifeExp),max=max(lifeExp),med=median(lifeExp))

R Codes for Getting data insight using ggplot2 library

library(ggplot2)

gapminder2007<- gapminder %>% filter(year==2007)

ggplot(gapminder2007)+aes(x=gdpPercap,y=lifeExp,color=continent, size=pop)+geom_point()

p<-ggplot(gapminder2007)+aes(x=gdpPercap,y=lifeExp,color=continent)+
  geom_point()
p

p1<-p+labs(title = "",
       subtitle = "Relationship between life expectancy and income, 2007",
       caption = "Source: Gapminder.org ",
       x = "GDP per capita ($)",
       y = "Age (years)") 
p1

## Facet_wrap() to split data continent wise for better insight
p1+facet_wrap(~continent)

gapminder52<-gapminder %>% filter(year==1952)
p2<-ggplot(gapminder52)+aes(x=gdpPercap,y=lifeExp,color=continent)+geom_point()
p2

gapminder %>% filter(gdpPercap>50000)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.6.2
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
grid.arrange(p1,p2,ncol=2)

gapminder52f<-gapminder52 %>% filter(gdpPercap<50000)

p2+ylim(0,90)

p3<-ggplot(gapminder52f)+aes(x=gdpPercap,y=lifeExp,color=continent)+geom_point()

grid.arrange(p1,p2,p3,ncol=3)

asia52<-gapminder %>% filter(year==1952,gdpPercap<30000,continent=="Asia")

asia07<-gapminder %>% filter(year==2007,continent=="Asia")

p52<-ggplot(asia52)+aes(x=gdpPercap,y=lifeExp)+geom_point()+ylim(20,90)

p07<-ggplot(asia07)+aes(x=gdpPercap,y=lifeExp)+geom_point()+
  ylim(20,90)
grid.arrange(p52,p07,ncol=2)

p52

p52+ylim(25,90)
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.

p07+ylim(25,90)
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.

## Boxplot provides 5 Number Summary Statistics

p11<-ggplot(gapminder2007)+aes(x=continent,y=lifeExp,fill=continent)+
  geom_boxplot()
p11

## Interactive plot
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(p1)
library(skimr)
## 
## Attaching package: 'skimr'
## The following object is masked from 'package:stats':
## 
##     filter
gapminder2007 %>% select(lifeExp,continent) %>% 
  skim()

ggplot(data=gapminder2007)+aes(x=lifeExp)+geom_histogram(binwidth = 5,color="White")+
  labs(x="Life Expectancy in Years",y="Number of Countries",
       title = "Histogram of distributio",caption = "gapminder.org")+
  facet_wrap(~continent,nrow = 2)

#Data for chart from gapminder package
line_df <- gapminder %>%
  filter(country == "Bangladesh")

#Make plot
line <- ggplot(line_df, aes(x = year, y = lifeExp)) +
  geom_line(colour = "#1380A1", size = 1) +
  geom_hline(yintercept = 0, size = 1, colour="#333333")

line

gapminderSA1<-gapminder %>% filter(country %in% c("Bangladesh","India","Pakistan","Sri Lanka","Afghanistan"))

## Multiple Lines
multiple_line <- ggplot(gapminderSA1, aes(x = year, y = lifeExp, colour = country)) +geom_line(size=1)

multiple_line+labs(x="Time",y="Life Expectancy in Years", title = "Life Expectancy over 1952 to 2007 for South Asian Countries")