title: R, simulating effects of treatments including the effects of interaction between treatments
body: <p>Think about an experiment with mice. We split the mice into 2 groups and we give to these groups different dose of cafein. We might also split each group receiving a given dose of cafein into 2 groups for which we give different dose of testosteron. We would have a so-called 2x2 design. The following matrix represents a 2x2x2 design.</p>
  
  <pre><code>&gt; groups
             [,1] [,2] [,3] [,4] [,5]    [,6]
      [1,]    1    1    1    1    2 -0.811962000
      [2,]    2    2    1    1    9  1.142115906
      [3,]    3    1    2    1    3 -0.999317454
      [4,]    4    2    2    1    7 -1.984324334
      [5,]    5    1    1    2   10  1.816095153
      [6,]    6    2    1    2    1  1.567284537
      [7,]    7    1    2    2    6  0.009253221
      [8,]    8    2    2    2    5 -1.764737015
  </code></pre>
  
  <p>Rows: individuals </p>
  
  <p>column 1 : just an ID, don't pay much attention to it !</p>
  
  <p>column 2 : groups for the first factor (cafein)</p>
  
  <p>column 3 : groups for the second factor (testosteron)</p>
  
  <p>column 3 : groups for the third factor (food quantity)</p>
  
  <p>column 5 : just an ID, don't pay much attention to it !</p>
  
  <p>column 6 : trait value (it could the difference to the mean weight of these mice after experiment)</p>
  
  <p>In this particular matrix, we have 1 individual per group. Of course the design vary and the number of individuals per group also vary. We might have a 2x3x5 design (3 factors, 2,3 and 5 groups within the 1st, 2nd and 3rd factors) with 12 inds per group.</p>
  
  <hr>
  
  <p><strong>My aim : Simulate some effects of these treatments.</strong></p>
  
  <p>There are basically two types of effects:</p>
  
  <p><strong>effect of on treatment (factors.effect object)</strong></p>
  
  <p>and</p>
  
  <p><strong>effect of interactions between treatments (interactions.effects object)</strong>.</p>
  
  <p>I have two objects:</p>
  
  <pre><code>&gt; factors.effect
  [1] 1 2 3
  
  &gt; interactions.effect
  [[1]]
  [1] 1 3
  [[2]]
  [1] 1 2 3
  </code></pre>
  
  <p>Those objects are "half-randomly" created.</p>
  
  <p>I already solved how to apply the <strong>factors.effect</strong>. Here is the code I'm using.</p>
  
  <pre><code>                    for (factor.effect in factors.effect){
                          for (group.effect in unique(groups[,factor.effect+1])[-1]){
                              groups[which(groups[,factor.effect+1]==group.effect),ncol(groups)] = groups[which(groups[,factor.effect+1]==group.effect),ncol(groups)] + seq(from=0,to=treatment.factors.inloop,length.out=length(unique(groups[,factor.effect+1])))[group.effect]
                          }
                      }
  </code></pre>
  
  <p>treatment.factor.inloop contains a single integer which represents the maximum possible effect of a treatment. The code allows to give increasing effect as the group number increase.</p>
  
  <p><strong>My question is: How can I compute a similar thing for interactions.effect ?</strong></p>
  
  <p>So if we take interactions.effect [<a href="https://i.stack.imgur.com/ln6E0.png" rel="nofollow noreferrer">1</a>] (which is c(1,3)), the code should increase the trait value if factor1 and factor3 are high. But if any of these factors is equal to 1 (group 1), no effect should be added. I think it is not as simple as just taking the mean of the two factors. It is important this interaction effect is gradual as the factors.effect is.</p>
  
  <p>If I then plot my data I should end up with something like that (apologize if it's not a beautiful plot !) :
  <img src="https://i.stack.imgur.com/ln6E0.png" alt="enter image description here"></p>
  
  <p>Thanks a lot</p>