Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a sort of answer; there is no way that I know of to formulate this model directly by the formula ...</p> <p>Construct data as above:</p> <pre><code>d &lt;- data.frame(A = rep(c("a1", "a2"), each = 50), B = c("b1", "b2"), value = rnorm(100)) options(contrasts=c('contr.sum','contr.poly')) </code></pre> <p>Confirm original finding that just subtracting the factor from the formula doesn't work:</p> <pre><code>m1 &lt;- lm(value ~ A * B, data = d) coef(m1) ## (Intercept) A1 B1 A1:B1 ## -0.23766309 0.04651298 -0.13019317 -0.06421580 m2 &lt;- update(m1, .~. - A) coef(m2) ## (Intercept) B1 Bb1:A1 Bb2:A1 ## -0.23766309 -0.13019317 -0.01770282 0.11072877 </code></pre> <p>Formulate the new model matrix:</p> <pre><code>X0 &lt;- model.matrix(m1) ## drop Intercept column *and* A from model matrix X1 &lt;- X0[,!colnames(X0) %in% "A1"] </code></pre> <p><code>lm.fit</code> allows direct specification of the model matrix:</p> <pre><code>m3 &lt;- lm.fit(x=X1,y=d$value) coef(m3) ## (Intercept) B1 A1:B1 ## -0.2376631 -0.1301932 -0.0642158 </code></pre> <p>This method only works for a few special cases that allow the model matrix to be specified explicitly (e.g. <code>lm.fit</code>, <code>glm.fit</code>).</p> <p>More generally:</p> <pre><code>## need to drop intercept column (or use -1 in the formula) X1 &lt;- X1[,!colnames(X1) %in% "(Intercept)"] ## : will confuse things -- substitute something inert colnames(X1) &lt;- gsub(":","_int_",colnames(X1)) newf &lt;- reformulate(colnames(X1),response="value") m4 &lt;- lm(newf,data=data.frame(value=d$value,X1)) coef(m4) ## (Intercept) B1 A1_int_B1 ## -0.2376631 -0.1301932 -0.0642158 </code></pre> <p>This approach has the disadvantage that it won't recognize multiple input variables as stemming from the same predictor (i.e., multiple factor levels from a more-than-2-level factor).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload