Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

left_join or mutate for new variables #199

Open
tonggne opened this issue Mar 2, 2022 · 5 comments
Open

left_join or mutate for new variables #199

tonggne opened this issue Mar 2, 2022 · 5 comments

Comments

@tonggne
Copy link

tonggne commented Mar 2, 2022

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 tonggne changed the title left_join for new variables left_join or mutate for new variables Mar 2, 2022
@ldbro0
Copy link

ldbro0 commented Mar 2, 2022

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)                              

@bguiastr
Copy link
Collaborator

bguiastr commented Mar 14, 2022

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

@tonggne
Copy link
Author

tonggne commented Mar 14, 2022 via email

@ldbro0
Copy link

ldbro0 commented Mar 14, 2022

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

@bguiastr
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants