Skip to content

Commit

Permalink
Style code
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 9, 2023
1 parent 8161e89 commit 425dc96
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 154 deletions.
157 changes: 77 additions & 80 deletions R/hd.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#'
hd <- function(
x,
...
) {
...) {
UseMethod("hd")
}

Expand All @@ -28,45 +27,44 @@ hd <- function(
#'
hd.svars <- function(
x,
...
) {
k <- x$K
t <- x$n

## Set shock names
impulse_names <- colnames(x$y)
response_names <- colnames(x$y)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
response_names <-
factor(response_names, levels = response_names, ordered = TRUE)

## Get historical decompositions
hidec <- lapply(1:k, function(i) {
svars::hd(x, series = i)$hidec
})

## Pull out just each shock contribution
hd <- unlist(lapply(hidec, function(i) i[, -(1:2)]))

## Pull out the total variation
total <- unlist(lapply(hidec, function(i) rep(i[, 2], times = k) ))

## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k * k),
impulse = rep(impulse_names, each = t, times = k),
response = rep(response_names, each = t * k),
hd = hd,
total = total
)

hd <- list(hd = df)
class(hd) <- "fevdhd"

return(hd)
...) {
k <- x$K
t <- x$n

## Set shock names
impulse_names <- colnames(x$y)
response_names <- colnames(x$y)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
response_names <-
factor(response_names, levels = response_names, ordered = TRUE)

## Get historical decompositions
hidec <- lapply(1:k, function(i) {
svars::hd(x, series = i)$hidec
})

## Pull out just each shock contribution
hd <- unlist(lapply(hidec, function(i) i[, -(1:2)]))

## Pull out the total variation
total <- unlist(lapply(hidec, function(i) rep(i[, 2], times = k)))

## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k * k),
impulse = rep(impulse_names, each = t, times = k),
response = rep(response_names, each = t * k),
hd = hd,
total = total
)

hd <- list(hd = df)
class(hd) <- "fevdhd"

return(hd)
}

#' Method to calculate fevdfd for fevdvar (id_fevdfd or id_fevdtd)
Expand All @@ -82,43 +80,42 @@ hd.svars <- function(
hd.fevdvar <- function(
x,
cummulative = FALSE,
...
) {
k <- x$K
t <- x$n

## Set shock names
impulse_names <- c("Main", paste0("Orth_", 2:k))
response_names <- colnames(x$y)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
response_names <-
factor(response_names, levels = response_names, ordered = TRUE)

## Get historical decompositions
hidec <- lapply(1:k, function(i) {
svars::hd(x, series = i)$hidec
})

## Pull out just each shock contribution
hd <- unlist(lapply(hidec, function(i) i[, -(1:2)]))

## Pull out the total variation
total <- unlist(lapply(hidec, function(i) rep(i[, 2], times = k) ))

## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k * k),
impulse = rep(impulse_names, each = t, times = k),
response = rep(response_names, each = t * k),
hd = hd,
total = total
)

hd <- list(hd = df)
class(hd) <- "fevdhd"

return(hd)
...) {
k <- x$K
t <- x$n

## Set shock names
impulse_names <- c("Main", paste0("Orth_", 2:k))
response_names <- colnames(x$y)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
response_names <-
factor(response_names, levels = response_names, ordered = TRUE)

## Get historical decompositions
hidec <- lapply(1:k, function(i) {
svars::hd(x, series = i)$hidec
})

## Pull out just each shock contribution
hd <- unlist(lapply(hidec, function(i) i[, -(1:2)]))

## Pull out the total variation
total <- unlist(lapply(hidec, function(i) rep(i[, 2], times = k)))

## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k * k),
impulse = rep(impulse_names, each = t, times = k),
response = rep(response_names, each = t * k),
hd = hd,
total = total
)

hd <- list(hd = df)
class(hd) <- "fevdhd"

return(hd)
}
126 changes: 61 additions & 65 deletions R/hs.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#'
hs <- function(
x,
...
) {
...) {
UseMethod("hs")
}

Expand All @@ -32,44 +31,43 @@ hs <- function(
hs.svars <- function(
x,
cumulative = FALSE,
...
) {
k <- x$K
...) {
k <- x$K

## Set shock names
impulse_names <- colnames(x$y)
## Set shock names
impulse_names <- colnames(x$y)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)

## Get residuals and Sigma
residuals <- stats::resid(x$VAR)
sigma <- x$B
t <- nrow(residuals)
## Get residuals and Sigma
residuals <- stats::resid(x$VAR)
sigma <- x$B
t <- nrow(residuals)

hs <- hist_shocks(residuals, sigma)
hs <- hist_shocks(residuals, sigma)

## Tidy to DF
## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(hs)
)

## Cummulative shocks
if (cumulative == TRUE) {
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(hs)
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(apply(hs, 2, cumsum))
)
}

## Cummulative shocks
if (cumulative == TRUE) {
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(apply(hs, 2, cumsum))
)
}

hs <- list(hs = df)
class(hs) <- "fevdhs"
hs <- list(hs = df)
class(hs) <- "fevdhs"

return(hs)
return(hs)
}

#' Method to calculate hs for fevdvar (id_fevdfd or id_fevdtd)
Expand All @@ -85,44 +83,43 @@ hs.svars <- function(
hs.fevdvar <- function(
x,
cumulative = FALSE,
...
) {
k <- x$K
...) {
k <- x$K

## Set shock names
impulse_names <- c("Main", paste0("Orth_", 2:k))

## Set shock names
impulse_names <- c("Main", paste0("Orth_", 2:k))
## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)

## Set as factors
impulse_names <-
factor(impulse_names, levels = impulse_names, ordered = TRUE)
## Get residuals and Sigma
residuals <- stats::resid(x$VAR)
sigma <- x$B
t <- nrow(residuals)

## Get residuals and Sigma
residuals <- stats::resid(x$VAR)
sigma <- x$B
t <- nrow(residuals)
hs <- hist_shocks(residuals, sigma)

hs <- hist_shocks(residuals, sigma)
## Tidy to DF
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(hs)
)

## Tidy to DF
## Cummulative shocks
if (cumulative == TRUE) {
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(hs)
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(apply(hs, 2, cumsum))
)
}

## Cummulative shocks
if (cumulative == TRUE) {
df <- data.frame(
t = rep(1:t, times = k),
impulse = rep(impulse_names, each = t),
hs = c(apply(hs, 2, cumsum))
)
}
hs <- list(hs = df)
class(hs) <- "fevdhs"

hs <- list(hs = df)
class(hs) <- "fevdhs"

return(hs)
return(hs)
}


Expand All @@ -135,9 +132,8 @@ hs.fevdvar <- function(
#'
hist_shocks <- function(
residuals,
sigma
) {
hs <- residuals %*% sigma
sigma) {
hs <- residuals %*% sigma

return(hs)
}
return(hs)
}
18 changes: 9 additions & 9 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,21 @@ plot.fevdhs <- function(x, y = NULL, impulse_as = "colors", ...) {
ggplot2::scale_color_hue() +
ggplot2::scale_fill_hue()

if (impulse_as == "colors") {
plot <- plot +
if (impulse_as == "colors") {
plot <- plot +
ggplot2::geom_line(
ggplot2::aes(color = impulse)
)
} else if (impulse_as == "cols") {
plot <- plot +
} else if (impulse_as == "cols") {
plot <- plot +
ggplot2::geom_line() +
ggplot2::facet_wrap(
ggplot2::vars(impulse),
ncol = 1
)
} else {
stop("Please set imuplse to 'colors' or 'cols'.")
}
} else {
stop("Please set imuplse to 'colors' or 'cols'.")
}

return(plot)
}
Expand Down Expand Up @@ -378,8 +378,8 @@ plot.fevdhd <- function(x, y = NULL, ...) {
ggplot2::scale_fill_hue() +
ggplot2::scale_color_manual(
values = c(Total = "black")
) +
) +
ggplot2::labs(color = NULL)

return(plot)
}
}

0 comments on commit 425dc96

Please sign in to comment.