ICDR2

Pacotes

library(ggthemes)
library(quantmod)
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::first()  masks xts::first()
✖ dplyr::lag()    masks stats::lag()
✖ dplyr::last()   masks xts::last()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
library(zoo)

Taxa de Juros 2 Anos

# Defina o período para os dados
start_date <- "1970-01-01"
end_date <- Sys.Date()

# Obtenha os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obtenha os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajuste os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomeie as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Crie um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Janelas para a taxa de juros (1978-01-01 a 1981-01-01)

# Definir as datas de início e fim
start_date <- "1970-01-01"
end_date <- "1981-01-01"

# Obter os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obter os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajustar os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomear as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Criar um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Janelas para a taxa de juros (1989-01-01 a 1991-01-01)

# Definir as novas datas de início e fim
start_date <- "1989-01-01"
end_date <- "1991-01-01"

# Obter os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obter os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajustar os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomear as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Criar um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Janelas para a taxa de juros (2000-01-01 a 2002-01-01)

# Definir as novas datas de início e fim
start_date <- "2000-01-01"
end_date <- "2002-01-01"

# Obter os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obter os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajustar os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomear as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Criar um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Janelas para a taxa de juros (2006-01-01 a 2008-01-01)

# Definir as novas datas de início e fim
start_date <- "2006-01-01"
end_date <- "2008-01-01"

# Obter os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obter os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajustar os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomear as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Criar um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Janelas para a taxa de juros (2019-01-01 até agora)

# Definir a data de início como 2019-01-01
start_date <- "2019-01-01"
# Definir a data final como a data atual
end_date <- Sys.Date()

# Obter os dados da Federal Funds Rate
fed_funds <- getSymbols("FEDFUNDS", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Obter os dados da taxa de juros de Treasury Bills de 2 anos
treasury_bills_2yr <- getSymbols("DGS2", src = "FRED", from = start_date, to = end_date, auto.assign = FALSE)

# Ajustar os dados para terem o mesmo número de linhas
data <- na.omit(merge(fed_funds, treasury_bills_2yr))

# Renomear as colunas
colnames(data) <- c("Federal_Funds_Rate", "Treasury_Bills_2yr_Rate")

# Criar um gráfico usando ggplot2
ggplot(data, aes(x = index(data))) +
  geom_line(aes(y = Federal_Funds_Rate, color = "Federal Funds Rate")) +
  geom_line(aes(y = Treasury_Bills_2yr_Rate, color = "Treasury Bills 2yr Rate")) +
  scale_color_manual(values = c("red", "blue")) +
  labs(title = "Federal Funds Rate vs Treasury Bills 2 anos Rate",
       y = "Interest Rate (%)",
       color = "Interest Rate Type") +
  theme_economist_white()

Desemprego

# Defina o período para os dados
start_date <- "1970-01-01"
end_date <- Sys.Date()

# Obtenha os dados da taxa de desemprego dos EUA
getSymbols("UNRATE", src = "FRED", from = start_date, to = end_date)
[1] "UNRATE"
unemployment_rate <- na.omit(UNRATE)

# Crie um data frame com as datas e a taxa de desemprego
data <- data.frame(Date = index(unemployment_rate), 
                   Unemployment_Rate = as.numeric(coredata(unemployment_rate)))

# Crie um gráfico usando ggplot2 com a taxa de desemprego
ggplot(data, aes(x = Date, y = Unemployment_Rate)) +
  geom_line(color = "red") +
  labs(title = "Taxa de Desemprego",
       y = "Taxa de Desemprego (%)",
       x = "Year") +
  theme_economist_white()+
   geom_vline(xintercept = as.numeric(as.Date(c("1978-01-01", "1980-12-31"))), linetype = "dashed", color = "black") +
  geom_vline(xintercept = as.numeric(as.Date(c("1989-01-01", "1990-12-31"))), linetype = "dashed", color = "red") +
  geom_vline(xintercept = as.numeric(as.Date(c("2000-01-01", "2001-12-31"))), linetype = "dashed", color = "black") +
  geom_vline(xintercept = as.numeric(as.Date(c("2006-01-01", "2007-12-31"))), linetype = "dashed", color = "red") +
  geom_vline(xintercept = as.numeric(as.Date(c("2019-01-01"))), linetype = "dashed", color = "black")

PIB POTENCIAL E REAL

start_date <- "1970-01-01"
end_date <- Sys.Date()

# Obtenha os dados do Produto Interno Bruto (PIB) real e do PIB potencial dos EUA
getSymbols(c("GDPC1", "GDPPOT"), src = "FRED", from = start_date, to = end_date)
[1] "GDPC1"  "GDPPOT"
# Ajuste o comprimento dos dados do PIB para corresponder aos outros dados
gdp_real <- na.omit(GDPC1)
gdp_potential <- na.omit(GDPPOT)
gdp <- merge(gdp_real, gdp_potential)

# Crie um data frame com as datas, o PIB real e o PIB potencial
data <- data.frame(Date = index(gdp), 
                   GDP_Real = as.numeric(coredata(gdp[,1])),
                   GDP_Potential = as.numeric(coredata(gdp[,2])))

# Crie um gráfico usando ggplot2 com o PIB real e o PIB potencial
ggplot(data, aes(x = Date)) +
  geom_line(aes(y = GDP_Real, color = "Real GDP")) +
  geom_line(aes(y = GDP_Potential, color = "Potential GDP")) +
  scale_color_manual(values = c("blue", "red")) +
  labs(title = "US Real GDP vs Potential GDP",
       y = "GDP (billions of dollars)",
       x = "Data",
       color = "GDP Type") +
  theme_economist_white()+
   geom_vline(xintercept = as.numeric(as.Date(c("1978-01-01", "1980-12-31"))), linetype = "dashed", color = "black") +
  geom_vline(xintercept = as.numeric(as.Date(c("1989-01-01", "1990-12-31"))), linetype = "dashed", color = "red") +
  geom_vline(xintercept = as.numeric(as.Date(c("2000-01-01", "2001-12-31"))), linetype = "dashed", color = "black") +
  geom_vline(xintercept = as.numeric(as.Date(c("2006-01-01", "2007-12-31"))), linetype = "dashed", color = "red") +
  geom_vline(xintercept = as.numeric(as.Date(c("2019-01-01"))), linetype = "dashed", color = "black")
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).