left_join or mutate for new variables
tonggne opened this issue · 5 comments
Hello Xpose developer, is there a way to bring new variables into xpose_data object? for example, I forgot including certain variable in the .tab output and would like to bring it from the .csv file using left_join or mutate.
Thanks a lot!
Tong
To add to @tonggne comment it would also be good to see if you could add an external tbl_df
object to your xpose
obj. For example you forget to table a variable in a nm run, you could read your nm run into R via expose extract the data object edit it and re-add it to the xpose object for plotting purposes.
Example Below:
poppk.df <- read_csv("poppk.dat.csv", na= c(".",-99.00)) %>%
select(ID, BWT)%>%
distinct()
xpdb <- xpose_data(dir = "nonmem", prefix = "run", '1')
data = xpdb %>% get_data(table = "patab1") %>% as_tibble() %>%
left_join(poppk.df) %>%
mutate(bwt.bin = ifelse(BWT <60, "<60",
ifelse(BWT >=60 & BWT <=100, "60-100", ">100")))
updated.xpdb <- some_update_function(data, updated_table = "patab1.update")
updated.xpdb %>% ipred_vs_idv()+ facet_wrap(~ bwt.bin)
The implementation of the xxx_join()
family is on the todo list. I would be something similar to what @ldbro0 is suggesting, although there are some subtleties I still need to figure out (i.e., it is not as straight forward as it seems).
Currently I don't think there is an easy way around this issue without manually modifying the data in xpdb$data$data
along with the declaration of the new variable in the corresponding index (xpdb$data$index
) see the example below:
library(dplyr)
library(xpose)
# 1. Define the NONMEM problem to be changed
pb_no <- 1
# 2. Get the xpose example data
xpdb <- xpdb_ex_pk
# 3. Modify the xpdb "data"
## Note: this is only a dummy example by you could use your _join() here
xpdb$data$data[pb_no][[1]] <- xpdb %>%
get_data(.problem = pb_no) %>%
mutate(DV2 = DV * 2)
# 4. Declare the new column in the "index"
index_df <- xpdb$data$index[pb_no][[1]]
xpdb$data$index[pb_no][[1]] <- index_df %>%
slice(1) %>%
mutate(table = "external", col = "DV2", type = "na") %>%
bind_rows(index_df)
# 5. Ensure the xpdb class not broken by the changes above
xpdb <- as.xpdb(xpdb)
# 6. Check wether the new column is recognized by xpose
## Note in this case we asigned the type "na" and it works
list_vars(xpdb, .problem = pb_no)
# 7. Now we can now define DV2 as the new DV using standard xpose features
xpdb <- set_var_types(xpdb, dv = "DV2", .problem = pb_no)
# 8. We check that the new DV2 is now default
list_vars(xpdb, .problem = pb_no)
## 9. You can now use it in plots
dv_vs_ipred(xpdb)
A word of warning though, with the xxx_join()
you could end up with more or less records than the original table, you should be aware that the number of observations/individuals reported by NONMEM could be wrong...