Debian Codegraph @

inst/doc/add-est-check.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13 <meta name="author" content="estimability package, Version 2.0.0" />
14
15
16 <title>How to add estimability checking to your model’s predict method</title>
17
18 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
19 // be compatible with the behavior of Pandoc < 2.8).
20 document.addEventListener('DOMContentLoaded', function(e) {
21 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
22 var i, h, a;
23 for (i = 0; i < hs.length; i++) {
24 h = hs[i];
25 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
26 a = h.attributes;
27 while (a.length > 0) h.removeAttribute(a[0].name);
28 }
29 });
30 </script>
31
32 <style type="text/css">
33 code{white-space: pre-wrap;}
34 span.smallcaps{font-variant: small-caps;}
35 span.underline{text-decoration: underline;}
36 div.column{display: inline-block; vertical-align: top; width: 50%;}
37 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
38 ul.task-list{list-style: none;}
39 </style>
40
41
42
43 <style type="text/css">
44 code {
45 white-space: pre;
46 }
47 .sourceCode {
48 overflow: visible;
49 }
50 </style>
51 <style type="text/css" data-origin="pandoc">
52 html { -webkit-text-size-adjust: 100%; }
53 pre > code.sourceCode { white-space: pre; position: relative; }
54 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
55 pre > code.sourceCode > span:empty { height: 1.2em; }
56 .sourceCode { overflow: visible; }
57 code.sourceCode > span { color: inherit; text-decoration: inherit; }
58 div.sourceCode { margin: 1em 0; }
59 pre.sourceCode { margin: 0; }
60 @media screen {
61 div.sourceCode { overflow: auto; }
62 }
63 @media print {
64 pre > code.sourceCode { white-space: pre-wrap; }
65 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
66 }
67 pre.numberSource code
68 { counter-reset: source-line 0; }
69 pre.numberSource code > span
70 { position: relative; left: -4em; counter-increment: source-line; }
71 pre.numberSource code > span > a:first-child::before
72 { content: counter(source-line);
73 position: relative; left: -1em; text-align: right; vertical-align: baseline;
74 border: none; display: inline-block;
75 -webkit-touch-callout: none; -webkit-user-select: none;
76 -khtml-user-select: none; -moz-user-select: none;
77 -ms-user-select: none; user-select: none;
78 padding: 0 4px; width: 4em;
79 color: #aaaaaa;
80 }
81 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
82 div.sourceCode
83 { }
84 @media screen {
85 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
86 }
87 code span.al { color: #ff0000; font-weight: bold; }
88 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
89 code span.at { color: #7d9029; }
90 code span.bn { color: #40a070; }
91 code span.bu { color: #008000; }
92 code span.cf { color: #007020; font-weight: bold; }
93 code span.ch { color: #4070a0; }
94 code span.cn { color: #880000; }
95 code span.co { color: #60a0b0; font-style: italic; }
96 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
97 code span.do { color: #ba2121; font-style: italic; }
98 code span.dt { color: #902000; }
99 code span.dv { color: #40a070; }
100 code span.er { color: #ff0000; font-weight: bold; }
101 code span.ex { }
102 code span.fl { color: #40a070; }
103 code span.fu { color: #06287e; }
104 code span.im { color: #008000; font-weight: bold; }
105 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
106 code span.kw { color: #007020; font-weight: bold; }
107 code span.op { color: #666666; }
108 code span.ot { color: #007020; }
109 code span.pp { color: #bc7a00; }
110 code span.sc { color: #4070a0; }
111 code span.ss { color: #bb6688; }
112 code span.st { color: #4070a0; }
113 code span.va { color: #19177c; }
114 code span.vs { color: #4070a0; }
115 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
116 </style>
117 <script>
118 // apply pandoc div.sourceCode style to pre.sourceCode instead
119 (function() {
120 var sheets = document.styleSheets;
121 for (var i = 0; i < sheets.length; i++) {
122 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
123 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
124 var j = 0;
125 while (j < rules.length) {
126 var rule = rules[j];
127 // check if there is a div.sourceCode rule
128 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
129 j++;
130 continue;
131 }
132 var style = rule.style.cssText;
133 // check if color or background-color is set
134 if (rule.style.color === '' && rule.style.backgroundColor === '') {
135 j++;
136 continue;
137 }
138 // replace div.sourceCode by a pre.sourceCode rule
139 sheets[i].deleteRule(j);
140 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
141 }
142 }
143 })();
144 </script>
145
146
147
148
149 <style type="text/css">body {
150 background-color: #fff;
151 margin: 1em auto;
152 max-width: 700px;
153 overflow: visible;
154 padding-left: 2em;
155 padding-right: 2em;
156 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
157 font-size: 14px;
158 line-height: 1.35;
159 }
160 #TOC {
161 clear: both;
162 margin: 0 0 10px 10px;
163 padding: 4px;
164 width: 400px;
165 border: 1px solid #CCCCCC;
166 border-radius: 5px;
167 background-color: #f6f6f6;
168 font-size: 13px;
169 line-height: 1.3;
170 }
171 #TOC .toctitle {
172 font-weight: bold;
173 font-size: 15px;
174 margin-left: 5px;
175 }
176 #TOC ul {
177 padding-left: 40px;
178 margin-left: -1.5em;
179 margin-top: 5px;
180 margin-bottom: 5px;
181 }
182 #TOC ul ul {
183 margin-left: -2em;
184 }
185 #TOC li {
186 line-height: 16px;
187 }
188 table {
189 margin: 1em auto;
190 border-width: 1px;
191 border-color: #DDDDDD;
192 border-style: outset;
193 border-collapse: collapse;
194 }
195 table th {
196 border-width: 2px;
197 padding: 5px;
198 border-style: inset;
199 }
200 table td {
201 border-width: 1px;
202 border-style: inset;
203 line-height: 18px;
204 padding: 5px 5px;
205 }
206 table, table th, table td {
207 border-left-style: none;
208 border-right-style: none;
209 }
210 table thead, table tr.even {
211 background-color: #f7f7f7;
212 }
213 p {
214 margin: 0.5em 0;
215 }
216 blockquote {
217 background-color: #f6f6f6;
218 padding: 0.25em 0.75em;
219 }
220 hr {
221 border-style: solid;
222 border: none;
223 border-top: 1px solid #777;
224 margin: 28px 0;
225 }
226 dl {
227 margin-left: 0;
228 }
229 dl dd {
230 margin-bottom: 13px;
231 margin-left: 13px;
232 }
233 dl dt {
234 font-weight: bold;
235 }
236 ul {
237 margin-top: 0;
238 }
239 ul li {
240 list-style: circle outside;
241 }
242 ul ul {
243 margin-bottom: 0;
244 }
245 pre, code {
246 background-color: #f7f7f7;
247 border-radius: 3px;
248 color: #333;
249 white-space: pre-wrap;
250 }
251 pre {
252 border-radius: 3px;
253 margin: 5px 0px 10px 0px;
254 padding: 10px;
255 }
256 pre:not([class]) {
257 background-color: #f7f7f7;
258 }
259 code {
260 font-family: Consolas, Monaco, 'Courier New', monospace;
261 font-size: 85%;
262 }
263 p > code, li > code {
264 padding: 2px 0px;
265 }
266 div.figure {
267 text-align: center;
268 }
269 img {
270 background-color: #FFFFFF;
271 padding: 2px;
272 border: 1px solid #DDDDDD;
273 border-radius: 3px;
274 border: 1px solid #CCCCCC;
275 margin: 0 5px;
276 }
277 h1 {
278 margin-top: 0;
279 font-size: 35px;
280 line-height: 40px;
281 }
282 h2 {
283 border-bottom: 4px solid #f7f7f7;
284 padding-top: 10px;
285 padding-bottom: 2px;
286 font-size: 145%;
287 }
288 h3 {
289 border-bottom: 2px solid #f7f7f7;
290 padding-top: 10px;
291 font-size: 120%;
292 }
293 h4 {
294 border-bottom: 1px solid #f7f7f7;
295 margin-left: 8px;
296 font-size: 105%;
297 }
298 h5, h6 {
299 border-bottom: 1px solid #ccc;
300 font-size: 105%;
301 }
302 a {
303 color: #0033dd;
304 text-decoration: none;
305 }
306 a:hover {
307 color: #6666ff; }
308 a:visited {
309 color: #800080; }
310 a:visited:hover {
311 color: #BB00BB; }
312 a[href^="http:"] {
313 text-decoration: underline; }
314 a[href^="https:"] {
315 text-decoration: underline; }
316
317 code > span.kw { color: #555; font-weight: bold; }
318 code > span.dt { color: #902000; }
319 code > span.dv { color: #40a070; }
320 code > span.bn { color: #d14; }
321 code > span.fl { color: #d14; }
322 code > span.ch { color: #d14; }
323 code > span.st { color: #d14; }
324 code > span.co { color: #888888; font-style: italic; }
325 code > span.ot { color: #007020; }
326 code > span.al { color: #ff0000; font-weight: bold; }
327 code > span.fu { color: #900; font-weight: bold; }
328 code > span.er { color: #a61717; background-color: #e3d2d2; }
329 </style>
330
331
332
333
334 </head>
335
336 <body>
337
338
339
340
341 <h1 class="title toc-ignore">How to add estimability checking to your
342 model’s <code>predict</code> method</h1>
343 <h4 class="author">estimability package, Version 2.0.0</h4>
344
345
346
347 <p>The goal of this short vignette is to show how you can easily add
348 estimability checking to your package’s <code>predict()</code> methods.
349 Suppose that you have developed a model class that has elements
350 <code>$coefficients</code>, <code>$formula</code>, etc. Suppose it also
351 has an <code>$env</code> element, an environment that can hold
352 miscellaneous information. This is not absolutely necessary, but handy
353 if it exists. Your model class involves some kind of linear
354 predictor.</p>
355 <p>We are concerned with models that:</p>
356 <ul>
357 <li>Allow rank deficiencies (where some predictors may be excluded)</li>
358 <li>Allow predictions for new data</li>
359 </ul>
360 <p>For any such model, it is important to add estimability checking to
361 your predict method, because the regression coefficients are not unique
362 – and hence that predictions may not be unique. It can be shown that
363 predictions on new data are unique only for cases that fall within the
364 row space of the model matrix. The <strong>estimability</strong> package
365 is designed to check for this.</p>
366 <p>The recommended design for accommodating rank-deficient models is to
367 follow the example of <code>stats::lm</code> objects, where any
368 predictors that are excluded have a corresponding regression coefficient
369 of <code>NA</code>. Please note that this <code>NA</code> code actually
370 doesn’t actually means the coefficient is missing; it is a code that
371 means that that coefficient has been constrained to be zero. In what
372 follows, we assume that this convention is used.</p>
373 <p>First note that estimability checking is not needed unless you are
374 predicting for new data. So that’s where you need to incorporate
375 estimability checking. The <code>predict</code> method should be coded
376 something like this:</p>
377 <pre><code>predict.mymod &lt;- function(object, newdata, ...) {
378 # ... some setup code ...
379 if (!missing(newdata)) {
380 X &lt;- # ... code to set up the model matrix for newdata ...
381
382 b &lt;- coef(object)
383 if (any(is.na(b))) { # we have rank deficiency so test estimability
384 if (is.null (nbasis &lt;- object$env$nbasis))
385 nbasis &lt;- object$nbasis &lt;-
386 estimability::nonest.basis(model.matrix(object))
387 b[is.na(b)] &lt;- 0
388 pred &lt;- X %*% b
389 pred[!estimability::is.estble(X, nbasis)] &lt;- NA
390 }
391 else
392 pred &lt;- X %*% coef(object)
393 }
394 # ... perhaps more code ...
395 pred
396 }</code></pre>
397 <p>That’s it – and this is the fancy version, where we can save
398 <code>nbasis</code> for use with possible future predictions. Any
399 non-estimable cases are flagged as <code>NA</code> in the
400 <code>pred</code> vector.</p>
401 <p>An alternative way to code this would be to exclude the columns of
402 <code>X</code> and elements of <code>b</code> that correspond to
403 <code>NA</code>s in <code>b</code>. But be careful, because you need
404 <em>all</em> the columns in <code>X</code> in order to check
405 estimability.</p>
406 <p>The only other thing you need to do is add <code>estimability</code>
407 to the <code>Imports</code> list in your `Description file.</p>
408 <div id="a-complete-example" class="section level2">
409 <h2>A complete example</h2>
410 <p>Below is a complete model-fitting function that illustrates how to
411 navigate the various practical aspects of incorporating estimability
412 checking in your code. This code emphasises readability rather than
413 computational efficiency, and lacks many aspects of robustness or
414 checking of inputs. The function returns information about the model
415 terms and factor levels, the computed coefficients <code>coef</code> and
416 their variance-covariance matrix <code>vcov</code>, and the basis
417 <code>nbasis</code> of the null space of the model matrix. It utilizes
418 the Cholesky decomposition with pivoting. Pivoting involves rearranging
419 the order of predictors, managing rank-deficient cases by moving some
420 linearly dependent predictors to the end of the line. We can the ignore
421 or “discard” those dependent predictors, which in estimability parlance
422 is obtaining a solution by constraining the coefficients of those
423 predictors to be zero.</p>
424 <p>Much of what is shown here is how to deal with the <code>pivot</code>
425 attribute, which comprises the indices of the reordered predictors. The
426 <code>rank</code> attribute tells us how many nonzero coefficients there
427 are. We compute <code>coef</code> using the first <code>rank</code>
428 pivoted predictors, and by putting <code>NA</code>s for the coefficients
429 constrained to zero, while <code>vcov</code> is only for the
430 non-<code>NA</code> elements of <code>coef</code>. As is also true in
431 the standard <code>lm</code> function, the <code>NA</code> elements are
432 just used to signal which coefficients were constrained to zero. The
433 <code>nonest.basis()</code> function sees the <code>pivot</code>
434 attribute and re-orders the predictors accordingly, so we don’t un-pivot
435 that result. The last few lines of code are necessary for passing needed
436 information to the <code>predict</code> method.</p>
437 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>mylm <span class="ot">&lt;-</span> <span class="cf">function</span>(formula, data) {</span>
438 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> y <span class="ot">&lt;-</span> data[[<span class="fu">all.vars</span>(formula)[<span class="dv">1</span>]]]</span>
439 <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> X <span class="ot">&lt;-</span> <span class="fu">model.matrix</span>(formula[<span class="sc">-</span><span class="dv">2</span>], <span class="at">data =</span> data)</span>
440 <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> ch <span class="ot">&lt;-</span> <span class="fu">chol</span>(<span class="fu">t</span>(X) <span class="sc">%*%</span> X, <span class="at">pivot =</span> <span class="cn">TRUE</span>) <span class="sc">|&gt;</span> <span class="fu">suppressWarnings</span>()</span>
441 <span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> rank <span class="ot">=</span> <span class="fu">attr</span>(ch, <span class="st">&quot;rank&quot;</span>)</span>
442 <span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> pivot <span class="ot">&lt;-</span> <span class="fu">attr</span>(ch, <span class="st">&quot;pivot&quot;</span>)</span>
443 <span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a> XpXinv <span class="ot">&lt;-</span> <span class="fu">chol2inv</span>(ch, <span class="at">size =</span> rank)</span>
444 <span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> coef <span class="ot">&lt;-</span> <span class="fu">rep</span>(<span class="cn">NA</span>, <span class="fu">ncol</span>(X))</span>
445 <span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a> <span class="fu">names</span>(coef) <span class="ot">&lt;-</span> <span class="fu">colnames</span>(X)</span>
446 <span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a> coef[pivot[<span class="dv">1</span><span class="sc">:</span>rank]] <span class="ot">&lt;-</span> XpXinv <span class="sc">%*%</span> (<span class="fu">t</span>(X[, pivot[<span class="dv">1</span><span class="sc">:</span>rank], <span class="at">drop =</span> <span class="cn">FALSE</span>]) <span class="sc">%*%</span> y)</span>
447 <span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a> nonNA <span class="ot">&lt;-</span> <span class="fu">which</span>(<span class="sc">!</span><span class="fu">is.na</span>(coef))</span>
448 <span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a> fit <span class="ot">&lt;-</span> X[ , nonNA, drop <span class="ot">=</span> <span class="cn">FALSE</span>] <span class="sc">%*%</span> coef[nonNA]</span>
449 <span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a> mse <span class="ot">&lt;-</span> <span class="fu">sum</span>((y <span class="sc">-</span> fit)<span class="sc">^</span><span class="dv">2</span>) <span class="sc">/</span> (<span class="fu">length</span>(y) <span class="sc">-</span> rank)</span>
450 <span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a> ord <span class="ot">&lt;-</span> <span class="fu">order</span>(pivot[<span class="dv">1</span><span class="sc">:</span>rank])</span>
451 <span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a> vcov <span class="ot">&lt;-</span> mse <span class="sc">*</span> XpXinv[ord, ord, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
452 <span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a> <span class="fu">dimnames</span>(vcov) <span class="ot">&lt;-</span> <span class="fu">list</span>(<span class="fu">names</span>(coef)[nonNA], <span class="fu">names</span>(coef)[nonNA])</span>
453 <span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a> nbasis <span class="ot">&lt;-</span> estimability<span class="sc">::</span><span class="fu">nonest.basis</span>(ch)</span>
454 <span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a> terms <span class="ot">&lt;-</span> <span class="fu">terms</span>(formula) <span class="sc">|&gt;</span> <span class="fu">delete.response</span>()</span>
455 <span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a> xlev <span class="ot">=</span> <span class="fu">list</span>()</span>
456 <span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a> <span class="cf">for</span> (v <span class="cf">in</span> <span class="fu">all.vars</span>(terms))</span>
457 <span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">is.null</span>(lv <span class="ot">&lt;-</span> <span class="fu">levels</span>(data[[v]])))</span>
458 <span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a> xlev[[v]] <span class="ot">&lt;-</span> lv</span>
459 <span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a> obj <span class="ot">&lt;-</span> <span class="fu">list</span>(<span class="at">terms =</span> terms, <span class="at">xlev =</span> xlev, <span class="at">coef =</span> coef, <span class="at">vcov =</span> vcov, <span class="at">nbasis =</span> nbasis)</span>
460 <span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a> <span class="fu">class</span>(obj) <span class="ot">&lt;-</span> <span class="st">&quot;mylm&quot;</span></span>
461 <span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a> obj</span>
462 <span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a>}</span></code></pre></div>
463 <p>The following code is a corresponding S3 method to provide
464 predictions and standard errors for new data, with estimability
465 checking. The <code>model.frame</code> call ensures that we incorporate
466 the original factor coding in the new data. We then construct the
467 corresponding model matrix <code>X</code> for the new data, and test the
468 rows for estimability. Once that is determined, we obtain the matrix
469 <code>XX</code> with only the estimable rows and corresponding to
470 non-<code>NA</code> elements of <code>coef</code>. The predictions and
471 SEs of any non-estimable rows are set to <code>NA</code>.</p>
472 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>predict.mylm <span class="ot">&lt;-</span> <span class="cf">function</span>(mod, newdata) {</span>
473 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a> mf <span class="ot">&lt;-</span> <span class="fu">model.frame</span>(mod<span class="sc">$</span>terms, <span class="at">data =</span> newdata, <span class="at">xlev =</span> mod<span class="sc">$</span>xlev)</span>
474 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a> X <span class="ot">&lt;-</span> <span class="fu">model.matrix</span>(mod<span class="sc">$</span>terms, <span class="at">data =</span> mf)</span>
475 <span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a> pred <span class="ot">&lt;-</span> se <span class="ot">&lt;-</span> <span class="fu">rep</span>(<span class="cn">NA</span>, <span class="fu">nrow</span>(X))</span>
476 <span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a> estble <span class="ot">&lt;-</span> estimability<span class="sc">::</span><span class="fu">is.estble</span>(X, mod<span class="sc">$</span>nbasis)</span>
477 <span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a> XX <span class="ot">&lt;-</span> X[estble, <span class="sc">!</span><span class="fu">is.na</span>(mod<span class="sc">$</span>coef), drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
478 <span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a> pred[estble] <span class="ot">&lt;-</span> XX <span class="sc">%*%</span> mod<span class="sc">$</span>coef[<span class="sc">!</span><span class="fu">is.na</span>(mod<span class="sc">$</span>coef)]</span>
479 <span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a> se[estble] <span class="ot">&lt;-</span> <span class="fu">sqrt</span>(<span class="fu">diag</span>(XX <span class="sc">%*%</span> mod<span class="sc">$</span>vcov <span class="sc">%*%</span> <span class="fu">t</span>(XX)))</span>
480 <span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a> <span class="fu">list</span>(<span class="at">pred =</span> pred, <span class="at">se =</span> se)</span>
481 <span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a>}</span></code></pre></div>
482 <p>Finally, here is a test of these functions, using a subset of the
483 <code>warpbreaks</code> data with two complete cells excluded along with
484 a few more observations. Then we obtain predictions for all factor
485 combinations. We find that the two excluded cells are non-estimable.</p>
486 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="co"># test code</span></span>
487 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>warp <span class="ot">=</span> warpbreaks[<span class="dv">11</span><span class="sc">:</span><span class="dv">40</span>, ]</span>
488 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>warp.mylm <span class="ot">=</span> <span class="fu">mylm</span>(breaks <span class="sc">~</span> wool<span class="sc">*</span>tension, warp)</span>
489 <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a></span>
490 <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a>new <span class="ot">=</span> <span class="fu">do.call</span>(expand.grid, warp.mylm<span class="sc">$</span>xlev)</span>
491 <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="fu">cbind</span>(new, <span class="fu">predict</span>(warp.mylm, <span class="at">newdata =</span> new))</span></code></pre></div>
492 <pre class="ro"><code>## wool tension pred se
493 ## 1 A L NA NA
494 ## 2 B L 28.22222 3.324365
495 ## 3 A M 24.75000 3.526021
496 ## 4 B M 25.75000 4.986547
497 ## 5 A H 24.55556 3.324365
498 ## 6 B H NA NA</code></pre>
499 </div>
500
501
502
503 <!-- code folding -->
504
505
506 <!-- dynamically load mathjax for compatibility with self-contained -->
507 <script>
508 (function () {
509 var script = document.createElement("script");
510 script.type = "text/javascript";
511 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
512 document.getElementsByTagName("head")[0].appendChild(script);
513 })();
514 </script>
515
516 </body>
517 </html>