xpose icon indicating copy to clipboard operation
xpose copied to clipboard

order of stratified vpc plots

Open oskarrgh opened this issue 7 years ago • 2 comments

First of all, thanks for a great package! When generating vpc data based on non-PsN generated simulations the order of elements in column "strat" and the columns containing the stratification variables (both in data frame "vpc_dat" and "aggr_obs") seems to be based on data class character. This although the stratification variable in problem 1 and 2 are data class factor, with appropriate levels and ordering. This can results in unexpected ordering of vpc plots when stratification variable is for example 2, 5 and 10, where 10 then will be ordered before 2 and 5. If my understanding is correct, is it possible to not do this intermediate transformation of the stratification variables during vpc data generation or is there any obvious solution/feature that I have missed?

oskarrgh avatar Aug 20 '18 14:08 oskarrgh

Hi @oskarrgh,

Thanks a lot for the feedback. Would you be able to PM me a reproducible example so I can run some tests on it?

In the meantime they are some yet not so well documented features of xpose to modify the vpc (i.e. special) data.

library(xpose)

# Create the VPC data
xpdb <- vpc_data(xpdb_ex_pk, stratify = 'SEX')

# You can extract the vpc data to take a look at its structure
vpc_special <- get_special(xpdb)

# Then you can modifiy the xpdb as you wish (e.g. remove/rename strata, etc).
xpdb %>% 
  
  # Change the sim shadded areas dataset
  mutate(strat = as.numeric(strat), 
         .source = 'special', 
         .where = 'vpc_dat') %>% 
  
  # Change the obs lines dataset
  mutate(strat = as.numeric(strat), 
         .source = 'special', 
         .where = 'aggr_obs') %>% 
  
  # Change the obs points dataset
  mutate(SEX = as.numeric(SEX), 
         .source = 'special', 
         .where = 'obs') %>% 
  
  # Draw the plot
  vpc()

bguiastr avatar Aug 20 '18 15:08 bguiastr

Thanks for the fast reply @guiastrennec, I will get back to you asap with regards to a reproducible example. Meanwhile I can confirm that the suggested features did solved my issue by enabling transformation of the variable into a factor and setting the appropriate levels.

xpdb %>% 
vpc_data(stratify=c("DOSE") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
         .source = "special", 
         .where = "vpc_dat") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
         .source = "special", 
         .where = "aggr_obs") %>%
mutate(variable = factor(variable, levels=c("2", "5", "10"), ordered=TRUE),
         .source = "special", 
         .where = "obs") %>%
vpc(facets=c("variable"))

Would be good to have the feature documented as I failed to figure out how to do this prior to my first post. Still curious if it would be possible to retain the data class set in problem 1 and 2 when generating data for problem 3. I think that would convenient not to have to modify variables more than once. Keep up the good work!

oskarrgh avatar Aug 21 '18 08:08 oskarrgh

@bguiastr, how to re-order of stratified vpc plots with 2 stratifiers (e.g., DAY and DOSE)? DAY has two values (1, 15), and DOSE has three values (60, 80, 120). I used the code below; however, the 120 dose is still shown in front of the 60 and 80 dose.

vpc_data=vpc_data(xpdb,psn_folder="@dir/@run_TAD_DAY_DOSE",psn_bins=TRUE,stratify=DAY~DOSE)

vpc_special <- get_special(vpc_data)

vpc_data=vpc_data%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'vpc_dat')%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'aggr_obs')%>% mutate(strat=factor(strat,levels=c('1, 60','1, 80','1, 120','15, 60','15, 80','15, 120'),ordered=TRUE), .source = 'special', .where = 'obs')

JoannaPeng avatar May 21 '24 21:05 JoannaPeng

@JoannaPeng, the approach you used to relabel the factors should work you are probably close to the answer ! Two suggestions from my end:

  1. You might find it easier to create a new variable in your dataset which would be taking values based on your DAY and DOSE variables. This allows you to stratify on 2 or more variable without difficulty and apply the code I provided in my earlier example.

  2. Beware that you need to edit the variable strat in vpc_dat (summary of simulations), and aggr_obs (summary of observations) as well as your stratification variable (which might not be called strat as I see in your example code) in obs (raw observations).

Hope it helps!

bguiastr avatar May 26 '24 20:05 bguiastr

@bguiastr, thanks for your help. I figured out how to fix this problem. For my code above, I should mutate "DOSE", not "strat", as an ordered factor. It is because the plotting uses "DOSE" (and "DAY"), not "strat".

JoannaPeng avatar May 30 '24 05:05 JoannaPeng

Glad it solved your problem !

bguiastr avatar Jun 02 '24 19:06 bguiastr