xpose icon indicating copy to clipboard operation
xpose copied to clipboard

left_join or mutate for new variables

Open tonggne opened this issue 3 years ago • 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

tonggne avatar Mar 02 '22 00:03 tonggne

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)                              

ldbro0 avatar Mar 02 '22 18:03 ldbro0

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...

bguiastr avatar Mar 14 '22 20:03 bguiastr

Thanks Ben for the information. Looking forward to it!

Best, Tong

On Mon, Mar 14, 2022 at 1:39 PM Benjamin @.***> wrote:

Hello, The implementation of the xxx_join() family is on the todo list. I would be something similar to what @ldbro0 https://github.com/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).

Best,

Ben

— Reply to this email directly, view it on GitHub https://github.com/UUPharmacometrics/xpose/issues/199#issuecomment-1067265034, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIDQ2EKT6OIGWWVLIN2EN7DU76PW3ANCNFSM5PVUOU4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

tonggne avatar Mar 14 '22 21:03 tonggne

@guiastrennec thanks for the example, I'm curious if PR #153 is another approach?

ldbro0 avatar Mar 14 '22 22:03 ldbro0

@ldbro0 the PR #153 aims to get rid of step 5. in the example above by preserving the xpdb_data class

bguiastr avatar Mar 18 '22 13:03 bguiastr