Commit 59bae5fc authored by Daniel Liden's avatar Daniel Liden
Browse files

added first pricing linkage code sample

parent 893f02ca
summary_price = function(price_and_hta) {
# Determine whether each price in the pricing history represents a change
# from the
# prior price (new = TRUE) or not
price_and_hta = arrange(price_and_hta, name, form, dose, date_sort) %>%
mutate(new = ifelse(price != lag(price, default = 0) |
ID2 != lag(ID2), TRUE, FALSE)) %>%
filter(hta_decision == TRUE | new == TRUE) %>%
distinct()
# Iterate through the list of drugs/dosages
# to find the next date/price
unique_drugs = unique(price_and_hta$ID2)
out = data.frame()
for(i in 1:length(unique_drugs)) {
drug = unique_drugs[i]
data = filter(price_and_hta, ID2 == drug) %>%
arrange(date_sort)
# Initialize data objects -------------------------------------------------
next_price = numeric(nrow(data))
next_date = numeric(nrow(data))
class(next_date) = "Date"
# The last price won't get a "next price" ---------------------------------
next_price[nrow(data)] = NA
next_date[nrow(data)] = NA
# Consider only prices different from the "current price" and identify the
# price at the earliest following date in the subset as the "next price" --
for(j in 1:(nrow(data) - 1)) {
potential = data$date_sort[which(data$price != data$price[j])]
next_date[j] = suppressWarnings(min(potential[potential >
data$date_sort[j]]))
next_price[j] = ifelse(is.na(next_date[j]), NA,
data$price[which(data$date_sort == next_date[j])])
}
out_tmp = data.frame(drug = rep(drug, nrow(data)), date = data$date,
price = data$price,next_price = next_price,
next_date = next_date,
hta_decision = data$hta_decision,
ID = data$ID)
out = rbind(out, out_tmp)
}
# Get identifier IDs --------------------------------------------------------
lookup_table = data.frame(ID = price_and_hta$ID,
identifier = price_and_hta$identifier)
out_merged <- (merge(lookup_table, out, by = 'ID', all.y = TRUE))
out = out_merged[,-1]
colnames(out) = c("identifier", "ID2", "Date", "Price", "Next.Price",
"Next.Date", "hta_decision")
out=filter(out, CM == TRUE)
#out$next.price = as.numeric(as.character(out$Next.Price))
out = out[-7]
out
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment