Referenced to the basal median within group
===

The following study 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 (Positive and Negative). I want to have my visit readings(V2,V3,V4,V5) each REFERENCED to the MEDIAN of the V1 readings within the stratified groups. So the data if stratified should look like this:

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 is better off 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 dataset pls find below in .txt format (just copy paste and write.table). The graph below is what the result should look like:![scatterplot norm to median][1].

What I succeeded to do is to get the medians of each group and add this a new column(df$marker.median) by using the ddply and mutate (transform can be also) and add another columns (df$marker.norm) with each marker reading in all visits divided by the corresponding median of that subgroup (by the way you will not be able to have the second column with transform in only ONE-line code, it should be by mutate which is the only advantage I think over transform), here is the line:

    df<- ddply(df, c("group","site"), mutate, marker.median=median(marker[visit=="V1"],na.rm=T), marker.norm=marker/marker.median[visit=="V1"]) #to get medians, and based readings BUT not keeping the V1 medians in the marker.norm column which is my intention from this question.

to maker it short for you, you can check shortly the result of the above line it is like this:  

marker.median 0.004500706 marker.norm 1.010557 #is not correct for subject 4 by V1  
marker.median 0.004500706 marker.norm 0.1468423 # is correct for the same subject 4 by V2  

the expected result for this subject as an example and rapid control whether the code is doing what is expected, it should look like this:  

marker.median 0.004500706 marker.norm 0.004500706 #should keep the median for subject 4 by V1  
marker.median 0.004500706 marker.norm 0.1468423 # is correct for the same subject 4 by V2 


so in order to achieve this goal, there should be some way to do the above trnasformation with mutate but THEN to keep those values of the df$marker.median just copied in the new column(df$marker.norm) without dividing them. THIS IS MY QUESION all about. Of course, I could have the correct result but manually and it is tiresome to locate the correspoding median to divide by and esp. if you have more than one marker for this trial to investigate. So please can anyone help to have this DONE with a few letter-code and make use of R powerful potential. The other two requests would be:

1. I get a warning message after ddply line, what can I do to suppress that?  
2. Since we are going to have the readings for other visits as fold increase as they would be normalized to that of V1 visit, so I want to recode all the readings of V1 to the value of 1, and show that in the graph instead of having the real median value for that subgroup, as you can see in the graph each subgroup has its own V1 value which is in fact is like 1 and the other visits are factor increase of that value? Can we do the recoding with the mutate line also?  

   

  [1]: http://i.stack.imgur.com/wkjbk.png



>this is the data set in .txt format

id,subject,group,visit,bacteria,site,marker
1,4,Placebo,V1,Negative,A,0.00454822
2,4,Placebo,V1,Negative,C,0.007704816
3,4,Placebo,V2,Negative,A,0.000660894
4,4,Placebo,V2,Negative,C,0.002953378
5,4,Placebo,V4,Negative,A,0.001021363
6,4,Placebo,V4,Negative,C,0.015539791
7,4,Placebo,V5,Negative,A,
8,4,Placebo,V5,Negative,C,0.004294848
9,2,Vaccine,V1,Negative,A,0.037833545
10,2,Vaccine,V1,Negative,C,0.001916592
11,2,Vaccine,V2,Negative,A,0.012112331
12,2,Vaccine,V2,Negative,C,0.00103842
13,2,Vaccine,V4,Negative,A,0.001532585
14,2,Vaccine,V4,Negative,C,0.00401743
15,3,Vaccine,V1,Negative,A,0.000435784
16,3,Vaccine,V1,Negative,C,0.000799435
17,3,Vaccine,V2,Negative,A,0.000289486
18,3,Vaccine,V2,Negative,C,0.001076575
19,3,Vaccine,V4,Negative,A,0.005827298
20,3,Vaccine,V4,Negative,C,0.000551333
21,3,Vaccine,V5,Negative,A,
22,3,Vaccine,V5,Negative,C,0.00577512
23,6,Vaccine,V1,Negative,A,0.003421863
24,6,Vaccine,V1,Negative,C,0.002210371
25,6,Vaccine,V2,Negative,A,0.000343935
26,6,Vaccine,V2,Negative,C,0.018920369
27,6,Vaccine,V4,Negative,A,0.001238113
28,6,Vaccine,V4,Negative,C,0.000349031
29,6,Vaccine,V5,Negative,A,0.000472605
30,6,Vaccine,V5,Negative,C,0.012695968
31,9,Vaccine,V1,Negative,A,0.001011269
32,9,Vaccine,V1,Negative,C,0.001143067
33,9,Vaccine,V2,Negative,A,0.003449035
34,9,Vaccine,V2,Negative,C,
35,9,Vaccine,V4,Negative,A,0.000646908
36,9,Vaccine,V4,Negative,C,0.015565749
37,9,Vaccine,V5,Negative,A,0.002759373
38,9,Vaccine,V5,Negative,C,0.003189613
39,20,Placebo,V1,Negative,A,
40,20,Placebo,V1,Negative,C,
41,20,Placebo,V2,Negative,A,0.004740703
42,20,Placebo,V2,Negative,C,0.017212368
43,20,Placebo,V4,Negative,A,0.020980705
44,20,Placebo,V4,Negative,C,0.00193199
45,11,Vaccine,V1,Negative,A,
46,11,Vaccine,V1,Negative,C,6.19E-05
47,11,Vaccine,V2,Negative,A,0.003142827
48,11,Vaccine,V2,Negative,C,0.02441898
49,11,Vaccine,V4,Negative,A,0.002247413
50,11,Vaccine,V4,Negative,C,0.005227935
51,26,Placebo,V1,Negative,A,
52,26,Placebo,V2,Negative,A,0.000244831
53,26,Placebo,V2,Negative,C,0.000323903
54,26,Placebo,V4,Negative,A,0.008039642
55,26,Placebo,V4,Negative,C,0.002652432
56,26,Placebo,V5,Negative,A,0.001157015
57,26,Placebo,V5,Negative,C,0.022535763
58,28,Placebo,V1,Negative,A,0.005881369
59,28,Placebo,V1,Negative,C,0.01682053
60,28,Placebo,V2,Negative,A,0.003253584
61,28,Placebo,V2,Negative,C,0.001349307
62,12,Placebo,V1,Negative,A,0.001437399
63,12,Placebo,V1,Negative,C,0.000475865
64,12,Placebo,V2,Negative,A,0.000492308
65,12,Placebo,V2,Negative,C,0.034909877
66,12,Placebo,V4,Negative,A,0.000697526
67,12,Placebo,V4,Negative,C,0.002825684
68,12,Placebo,V5,Negative,A,
69,12,Placebo,V5,Negative,C,0.003051677
70,8,Placebo,V1,Negative,A,0.012425069
71,8,Placebo,V1,Negative,C,0.002001363
72,8,Placebo,V2,Negative,A,0.002729388
73,8,Placebo,V2,Negative,C,0.013623564
74,8,Placebo,V4,Negative,A,0.001367748
75,8,Placebo,V4,Negative,C,
76,25,Vaccine,V1,Negative,A,
77,25,Vaccine,V1,Negative,C,0.016314392
78,25,Vaccine,V2,Negative,A,0.001340877
79,25,Vaccine,V2,Negative,C,
80,25,Vaccine,V4,Negative,A,0.002466446
81,25,Vaccine,V4,Negative,C,0.003682454
82,13,Placebo,V1,Negative,A,0.001344014
83,13,Placebo,V1,Negative,C,0.000511695
84,13,Placebo,V2,Negative,A,0.005299382
85,13,Placebo,V2,Negative,C,
86,13,Placebo,V4,Negative,A,0.00225334
87,13,Placebo,V4,Negative,C,
88,13,Placebo,V5,Negative,A,0.000905534
89,13,Placebo,V5,Negative,C,0.002301685
90,21,Vaccine,V1,Negative,A,0.002184514
91,21,Vaccine,V1,Negative,C,0.06274818
92,21,Vaccine,V2,Negative,A,0.020707623
93,21,Vaccine,V2,Negative,C,0.0005449
94,21,Vaccine,V4,Negative,A,
95,21,Vaccine,V4,Negative,C,
96,15,Vaccine,V2,Negative,A,
97,15,Vaccine,V2,Negative,C,
98,15,Vaccine,V4,Negative,A,0.004059997
99,15,Vaccine,V4,Negative,C,
100,29,Vaccine,V1,Negative,A,
101,29,Vaccine,V1,Negative,C,0.026162959
102,29,Vaccine,V2,Negative,A,0.010420663
103,29,Vaccine,V2,Negative,C,0.004853931
104,29,Vaccine,V4,Negative,A,0.001752707
105,29,Vaccine,V4,Negative,C,0.00690867
106,31,Vaccine,V1,Negative,A,0.011395959
107,31,Vaccine,V1,Negative,C,0.00280216
108,31,Vaccine,V4,Negative,A,0.000480986
109,31,Vaccine,V4,Negative,C,0.000880069
110,31,Vaccine,V5,Negative,A,
111,31,Vaccine,V5,Negative,C,
112,33,Vaccine,V1,Negative,A,
113,33,Vaccine,V1,Negative,C,
114,33,Vaccine,V4,Negative,A,
115,33,Vaccine,V4,Negative,C,
116,7,Vaccine,V1,Positive,A,0.000453794
117,7,Vaccine,V1,Positive,C,0.000646046
118,7,Vaccine,V2,Positive,A,0.006927991
119,7,Vaccine,V2,Positive,C,0.05435448
120,7,Vaccine,V3,Positive,A,0.001844738
121,7,Vaccine,V3,Positive,C,
122,17,Vaccine,V1,Positive,A,0.000797981
123,17,Vaccine,V1,Positive,C,0.000137597
124,17,Vaccine,V2,Positive,A,
125,17,Vaccine,V2,Positive,C,0.001597157
126,17,Vaccine,V3,Positive,A,0.000825156
127,17,Vaccine,V3,Positive,C,0.006101024
128,1,Placebo,V1,Positive,A,0.004453193
129,1,Placebo,V1,Positive,C,
130,1,Placebo,V2,Positive,A,0.0262974
131,1,Placebo,V2,Positive,C,0.000154048
132,1,Placebo,V3,Positive,A,
133,1,Placebo,V3,Positive,C,
134,18,Placebo,V1,Positive,A,0.008257987
135,18,Placebo,V1,Positive,C,0.002609319
136,18,Placebo,V2,Positive,A,
137,18,Placebo,V2,Positive,C,0.003597362
138,18,Placebo,V3,Positive,A,
139,18,Placebo,V3,Positive,C,0.001350303
140,19,Placebo,V1,Positive,A,0.022519897
141,19,Placebo,V1,Positive,C,0.000462146
142,19,Placebo,V2,Positive,A,
143,19,Placebo,V2,Positive,C,0.011201878
144,19,Placebo,V3,Positive,A,0.001490843
145,19,Placebo,V3,Positive,C,7.76E-05
146,5,Placebo,V1,Positive,A,0.003756308
147,5,Placebo,V1,Positive,C,0.008868555
148,5,Placebo,V2,Positive,A,0.001652853
149,5,Placebo,V2,Positive,C,0.00178776
150,5,Placebo,V3,Positive,A,0.00555549
151,5,Placebo,V3,Positive,C,0.001909622
152,10,Vaccine,V1,Positive,A,0.000651004
153,10,Vaccine,V1,Positive,C,
154,10,Vaccine,V2,Positive,A,0.012009644
155,10,Vaccine,V2,Positive,C,0.000571848
156,10,Vaccine,V3,Positive,A,
157,10,Vaccine,V3,Positive,C,
158,22,Vaccine,V1,Positive,A,0.002841921
159,22,Vaccine,V1,Positive,C,0.001280671
160,22,Vaccine,V2,Positive,A,
161,22,Vaccine,V2,Positive,C,0.000693125
162,22,Vaccine,V3,Positive,A,0.001077318
163,22,Vaccine,V3,Positive,C,0.000553438
164,24,Placebo,V1,Positive,A,
165,24,Placebo,V1,Positive,C,0.001007185
166,24,Placebo,V2,Positive,A,
167,24,Placebo,V2,Positive,C,0.105733046
168,24,Placebo,V3,Positive,A,0.000249513
169,24,Placebo,V3,Positive,C,0.0184666
170,23,Placebo,V1,Positive,A,
171,23,Placebo,V1,Positive,C,0.02161664
172,23,Placebo,V2,Positive,A,0.002131145
173,23,Placebo,V2,Positive,C,0.000646699
174,23,Placebo,V3,Positive,A,0.000424124
175,23,Placebo,V3,Positive,C,0.004607125
176,14,Vaccine,V1,Positive,A,0.002786908
177,14,Vaccine,V1,Positive,C,
178,14,Vaccine,V2,Positive,A,0.007854095
179,14,Vaccine,V2,Positive,C,0.002140268
180,14,Vaccine,V3,Positive,A,0.000141185
181,14,Vaccine,V3,Positive,C,0.01064868
182,27,Placebo,V1,Positive,A,0.000398721
183,27,Placebo,V1,Positive,C,
184,27,Placebo,V2,Positive,A,0.000192298
185,27,Placebo,V2,Positive,C,
186,27,Placebo,V3,Positive,A,0.000251784
187,27,Placebo,V3,Positive,C,0.013739101
188,16,Placebo,V1,Positive,A,
189,16,Placebo,V1,Positive,C,0.000466456
190,16,Placebo,V2,Positive,A,0.001142002
191,16,Placebo,V2,Positive,C,0.016214244
192,16,Placebo,V3,Positive,A,0.00094668
193,16,Placebo,V3,Positive,C,0.00538915
194,30,Vaccine,V1,Positive,A,0.004470786
195,30,Vaccine,V1,Positive,C,0.002342689
196,30,Vaccine,V2,Positive,A,
197,30,Vaccine,V2,Positive,C,0.016629
198,30,Vaccine,V3,Positive,A,0.025021527
199,30,Vaccine,V3,Positive,C,0.005998177
200,32,Vaccine,V1,Positive,A,0.011789812
201,32,Vaccine,V1,Positive,C,0.001339222
202,32,Vaccine,V2,Positive,A,0.000324325
203,32,Vaccine,V2,Positive,C,0.004005592
204,32,Vaccine,V3,Positive,A,0.001280665
205,32,Vaccine,V3,Positive,C,0.00649033
206,34,Vaccine,V1,Positive,A,
207,34,Vaccine,V1,Positive,C,0.006240052
208,34,Vaccine,V2,Positive,A,0.002102898
209,34,Vaccine,V2,Positive,C,
210,34,Vaccine,V3,Positive,A,0.002200992
211,34,Vaccine,V3,Positive,C,0.000792496
212,31,Vaccine,V2,Negative,A,
213,31,Vaccine,V2,Negative,C,0.003643946
214,33,Vaccine,V2,Negative,A,
215,33,Vaccine,V2,Negative,C,0.000208909
216,26,Placebo,V1,Negative,C,0.005642675

    df<-read.table(file.choose(), header=TRUE, sep=",") #this is the RAW primary data
    library(plyr)  #for ddply
    library(reshape) # may be not needed there is also reshape2 now
    library(ggplot2) # for the graph
    df<- ddply(df, c("group","site"), mutate, marker.median=median(marker[visit=="V1"],na.rm=T), marker.norm=marker/marker.median[visit=="V1"]) #this will give a different graph as the one shown above
    #to get the graph above: use the below script of ggplot2 after modifying the ddply line in order to have the correct marker.norm column which I did MANUALLY (tedious task!)
    plot<-ggplot(df,aes(x=visit, y = marker.norm, 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  

I look forward at seeing the same graph above with a powerful code line. Thank you in advance.
