Skip to content

Commit

Permalink
Allow portfolios to not contain the same years.
Browse files Browse the repository at this point in the history
  • Loading branch information
xfim committed Nov 20, 2019
1 parent 56c307e commit 19994d2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions R/pp_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ pp_measures <- function(D, id = NULL) {
warning("You have given a data frame with more than one sector.\nTargets and instruments will be assumed to be equal for every sector.")
}

# In case of portfolios not covering the same years
clean.years <- FALSE
D.years <- D %>%
select(Country, Year) %>%
unique()
D.years.n <- D.years %>%
group_by(Country) %>%
summarize(N = n())
if (length(unique(D.years.n$N)) > 1) {
message("At least one portfolio contains a different number of years.")
clean.years <- TRUE
# This is used later in the cleaning of the extra years
D.years.yn <- D.years %>%
group_by(Country, Year) %>%
summarize(N = n()) %>%
ungroup() %>%
spread(Year, N, fill = 0) %>%
gather(Year, N, -Country)
}

# Agree in the portfolio(s) to pass and convert into matrix form
# to make the calculations
P.full <- pp_array(D, return_matrix = FALSE)
Expand Down Expand Up @@ -98,5 +118,16 @@ pp_measures <- function(D, id = NULL) {
mutate(Year = as.integer(as.numeric(Year))) %>%
mutate(Measure = factor(as.character(Measure))) %>%
mutate(Measure.label = factor(as.character(Measure.label)))

# Clean years for which some countries do not have data
if (clean.years) {
clean.countries <- as.character(D.years.n$Country[D.years.n$N < max(D.years.n$N)])
for (c in 1:length(clean.countries)) {
message(paste0("Cleaning years from ", clean.countries[c]))
extra.years <- as.vector(filter(D.years.yn, Country == clean.countries[c] & N == 0)$Year)
O <- O %>%
filter(Country != clean.countries[c] | (Country == clean.countries[] & !(Year %in% extra.years)))
}
}
return(O)
}

0 comments on commit 19994d2

Please sign in to comment.