Re: Referenced to the basal median within group

33 views
Skip to first unread message

Brandon Hurr

unread,
Jul 14, 2012, 12:44:59 PM7/14/12
to doctorate, ggp...@googlegroups.com
 I could get the medians of each group and add this a new column by using the ddply and transform, but couldn't make it to have divisions of each marker reading by the corresponding V1 median. Any help? please try to find a flexible and short code for this purpose.
df<-ddply(df, c("group","site","visit"), transform,marker.median=median(as.numeric(marker),na.rm=TRUE),na.rm=T) 

#The following seems to work to divide the marker value by the median of V1
#but I don't get the same values as your final dataset so I don't know if it's right

df<- ddply(df, c("group","site"), transform, marker.based=marker/marker.median[visit=="V1"])

 
Another question would be to combine the four medians together to establish a start point common to ALL four groups(in the graph you can see 4 different starting points for each group) so how can one make it only one start point for all?

If the data are different values why do you want them to all start in the same place? 
 
Other question how to anchor this starting point to the lower left corner of the four plots, any idea? 

I think expand... 

scale_y_continuous(name="Marker normalized to V1",trans = 'log10', expand= c(0,0))

I've never used it before though so you'll have to tinker to get it right. 

 

plot<-ggplot(df,aes(x=visit, y = as.numeric(marker.based), alpha=subject, shape=bacteria,group = subject),na.rm=TRUE) +
    geom_point(size=4)+
    geom_line(size=1) +
    facet_grid(facets = site~group) +
    scale_y_continuous(name="Marker normalized to V1",trans = 'log10')+
    scale_shape_manual(name="Bacterial status",guide = guide_legend(),values = c(19,15))+     
    scale_alpha(guide = c(pos = 'none'))+   
     theme_bw()+   
   opts(axis.title.x = theme_blank(),axis.title.y = theme_text(size = 14.0,angle = 90.0),strip.text.x = theme_text(face = 'bold',size = 16.0),strip.text.y = theme_text(face = 'bold',size = 16.0,angle = 270.0))
    plot

doctorate

unread,
Jul 14, 2012, 3:51:44 PM7/14/12
to ggp...@googlegroups.com
Dear Brandon,
Thank you for the notion about expand. I will try to adjust that. I attached the dataset file again to avoid any numerical issues, last time I struggled to retain the precision point of marker readings esp. when you do the median. Now I attached a cleaner dataset file.

On Saturday, July 14, 2012 8:42:46 AM UTC+2, doctorate wrote:
The attached dataset is a classical vaccine/placebo trial on 34 subjects. There are five visits (V1 >to V5) and I want to stratify readings of the marker measured in lab by site (A and C), and >bacteria (Positiv and Negative) at least for this example. I want to have my visit >readings(V2,V3,V4,V5) REFERENCED to the MEDIAN of the V1 readings within the stratified groups. So >the data if stratified should look like this. Let's go back to the problem: we agree that this line:
df<-ddply(df, c("group","site","visit"), transform,marker.median=median(as.numeric(marker),na.rm=TRUE),na.rm=T) 
will give us the corresponding medians for the groups, but the second line is really good to have the reading REFERENCED to the medians BUT with one thing still missing:
the output for V1 are not correct, for all other visits the readings are now normalized but one more thing to make it right, is to find a way to keep the median in the column once we are in the V1 reading. This will make the marker.based column perfect ! otherwise it will not give the same result as in the graph. I attached the "trial_dataset_primary_shouldbe.csv" so that to check whether marker.based column has achieved the goal. Fast check for that, we look for the first row which is subject 4 in this case, if the column marker.based has a value of 0.004500707 in visit1 and 0.146842279 in V2 (referenced), THEN everything is in order. I don't know how can we keep the medians in the column if visit =="V1"? any idea?

   #but I don't get the same values as your final dataset so I don't know if it's right
i think this because of the numbers issue mentioned above, I am sure you would've had the same numbers but try the new dataset now and see.

#If the data are different values why do you want them to all start in the same place?
good, this is because the starting point is with the same subjects ALL having the same condition set by the trial. So there is no point against not unifying them by the first visit.

Thanks a lot and best regards



 
Vaccine/Placebo Site  Bacteria         Visits              Marker
Vaccine          A    Positive    V1, V2, V3, V4, V5
Vaccine          A    Negative    V1, V2, V3, V4, V5
Vaccine          C    Positive    V1, V2, V3, V4, V5
Vaccine          C    Negative    V1, V2, V3, V4, V5

Placebo          A    Positive    V1, V2, V3, V4, V5
Placebo          A    Negative    V1, V2, V3, V4, V5
Placebo          C    Positive    V1, V2, V3, V4, V5
Placebo          C    Negative    V1, V2, V3, V4, V5

>So the first task is to stratify the dataset, then find the medians of V1 within each group >stratified (there can be different ways to stratify so the code better to take this into account >and be flexible), then find a way to divide the marker readings of other visits to that of V1 >median of the CORRESPONDING subgroup.
>I like to work with the R, plyr package, reshape or reshape2, and graphics with ggplot2.
>The first attahced file is dataset only, the second attached file is what I have done but manually >(time consuming to find out the reference V1 of the corresponding group for each reading). The graph is what the result should look like( see attached). I could get the medians of each group and add this a new column by using the ddply and transform, but couldn't make it to have divisions of each marker reading by the corresponding V1 median. Any help? please try to find a flexible and short code for this purpose. Another question would be to combine the four medians together to establish a start point common to ALL four groups(in the graph you can see 4 different starting points for each group) so how can one make it only one start point for all? Other question how to anchor this starting point to the lower left corner of the four plots, any idea? many thanks in advance.
and the trial_dataset_shouldbe.csv dataset where the last two columns were not in the first dataset, but done manually.
df<-ddply(df, c("group","site","visit"), transform,marker.median=median(as.numeric(marker),na.rm=TRUE),na.rm=T) # only this was done automatically the other column marker.based was done manually. I want to find a code for its output.
trial_dataset_primary.csv
trial_dataset_primary_shouldbe.csv

doctorate

unread,
Jul 14, 2012, 7:12:56 PM7/14/12
to ggp...@googlegroups.com
the two lines can be now done in one line using the mutate() of plyr, mutate while transform cannot do that !
df<- ddply(df, c("group","site"), mutate, marker.median=median(marker[visit=="V1"],na.rm=T), marker.norm=marker/marker.median[visit=="V1"])

BUT still I am looking for a way to keep the value of medians for V1 withing group if visit =="V1". How can we enter a condition to put the value of median as such when visist is V1 otherwise do the normalization by dividing the marker over the median of V1?


 



On Saturday, July 14, 2012 8:42:46 AM UTC+2, doctorate wrote:
The attached dataset is a classical vaccine/placebo trial on 34 subjects. There are five visits (V1 >to V5) and I want to stratify readings of the marker measured in lab by site (A and C), and >bacteria (Positiv and Negative) at least for this example. I want to have my visit >readings(V2,V3,V4,V5) REFERENCED to the MEDIAN of the V1 readings within the stratified groups. So >the data if stratified should look like this:
Reply all
Reply to author
Forward
0 new messages