| 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 |
|
| 14 |
|
| 15 |
<title>Extended Model Formulas in R: Multiple Parts and Multiple Responses</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
html { -webkit-text-size-adjust: 100%; } |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
} |
| 79 |
pre.numberSource { margin-left: 3em; padding-left: 4px; } |
| 80 |
div.sourceCode |
| 81 |
{ } |
| 82 |
@media screen { |
| 83 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 84 |
} |
| 85 |
code span.al { font-weight: bold; } |
| 86 |
code span.an { font-style: italic; } |
| 87 |
code span.cf { font-weight: bold; } |
| 88 |
code span.co { font-style: italic; } |
| 89 |
code span.cv { font-style: italic; } |
| 90 |
code span.do { font-style: italic; } |
| 91 |
code span.dt { text-decoration: underline; } |
| 92 |
code span.er { font-weight: bold; } |
| 93 |
code span.in { font-style: italic; } |
| 94 |
code span.kw { font-weight: bold; } |
| 95 |
code span.pp { font-weight: bold; } |
| 96 |
code span.wa { font-style: italic; } |
| 97 |
</style> |
| 98 |
<script> |
| 99 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 100 |
(function() { |
| 101 |
var sheets = document.styleSheets; |
| 102 |
for (var i = 0; i < sheets.length; i++) { |
| 103 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 104 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 105 |
var j = 0; |
| 106 |
while (j < rules.length) { |
| 107 |
var rule = rules[j]; |
| 108 |
// check if there is a div.sourceCode rule |
| 109 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 110 |
j++; |
| 111 |
continue; |
| 112 |
} |
| 113 |
var style = rule.style.cssText; |
| 114 |
// check if color or background-color is set |
| 115 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 116 |
j++; |
| 117 |
continue; |
| 118 |
} |
| 119 |
// replace div.sourceCode by a pre.sourceCode rule |
| 120 |
sheets[i].deleteRule(j); |
| 121 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 122 |
} |
| 123 |
} |
| 124 |
})(); |
| 125 |
</script> |
| 126 |
|
| 127 |
<style type="text/css"> |
| 128 |
p.abstract{ |
| 129 |
text-align: center; |
| 130 |
font-weight: bold; |
| 131 |
} |
| 132 |
div.abstract{ |
| 133 |
margin: auto; |
| 134 |
width: 90%; |
| 135 |
} |
| 136 |
</style> |
| 137 |
|
| 138 |
|
| 139 |
<style type="text/css"> |
| 140 |
|
| 141 |
div.csl-bib-body { } |
| 142 |
div.csl-entry { |
| 143 |
clear: both; |
| 144 |
} |
| 145 |
.hanging div.csl-entry { |
| 146 |
margin-left:2em; |
| 147 |
text-indent:-2em; |
| 148 |
} |
| 149 |
div.csl-left-margin { |
| 150 |
min-width:2em; |
| 151 |
float:left; |
| 152 |
} |
| 153 |
div.csl-right-inline { |
| 154 |
margin-left:2em; |
| 155 |
padding-left:1em; |
| 156 |
} |
| 157 |
div.csl-indent { |
| 158 |
margin-left: 2em; |
| 159 |
} |
| 160 |
</style> |
| 161 |
|
| 162 |
<style type="text/css">body { |
| 163 |
background-color: #fff; |
| 164 |
margin: 1em auto; |
| 165 |
max-width: 700px; |
| 166 |
overflow: visible; |
| 167 |
padding-left: 2em; |
| 168 |
padding-right: 2em; |
| 169 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 170 |
font-size: 14px; |
| 171 |
line-height: 1.35; |
| 172 |
} |
| 173 |
#TOC { |
| 174 |
clear: both; |
| 175 |
margin: 0 0 10px 10px; |
| 176 |
padding: 4px; |
| 177 |
width: 400px; |
| 178 |
border: 1px solid #CCCCCC; |
| 179 |
border-radius: 5px; |
| 180 |
background-color: #f6f6f6; |
| 181 |
font-size: 13px; |
| 182 |
line-height: 1.3; |
| 183 |
} |
| 184 |
#TOC .toctitle { |
| 185 |
font-weight: bold; |
| 186 |
font-size: 15px; |
| 187 |
margin-left: 5px; |
| 188 |
} |
| 189 |
#TOC ul { |
| 190 |
padding-left: 40px; |
| 191 |
margin-left: -1.5em; |
| 192 |
margin-top: 5px; |
| 193 |
margin-bottom: 5px; |
| 194 |
} |
| 195 |
#TOC ul ul { |
| 196 |
margin-left: -2em; |
| 197 |
} |
| 198 |
#TOC li { |
| 199 |
line-height: 16px; |
| 200 |
} |
| 201 |
table { |
| 202 |
margin: 1em auto; |
| 203 |
border-width: 1px; |
| 204 |
border-color: #DDDDDD; |
| 205 |
border-style: outset; |
| 206 |
border-collapse: collapse; |
| 207 |
} |
| 208 |
table th { |
| 209 |
border-width: 2px; |
| 210 |
padding: 5px; |
| 211 |
border-style: inset; |
| 212 |
} |
| 213 |
table td { |
| 214 |
border-width: 1px; |
| 215 |
border-style: inset; |
| 216 |
line-height: 18px; |
| 217 |
padding: 5px 5px; |
| 218 |
} |
| 219 |
table, table th, table td { |
| 220 |
border-left-style: none; |
| 221 |
border-right-style: none; |
| 222 |
} |
| 223 |
table thead, table tr.even { |
| 224 |
background-color: #f7f7f7; |
| 225 |
} |
| 226 |
p { |
| 227 |
margin: 0.5em 0; |
| 228 |
} |
| 229 |
blockquote { |
| 230 |
background-color: #f6f6f6; |
| 231 |
padding: 0.25em 0.75em; |
| 232 |
} |
| 233 |
hr { |
| 234 |
border-style: solid; |
| 235 |
border: none; |
| 236 |
border-top: 1px solid #777; |
| 237 |
margin: 28px 0; |
| 238 |
} |
| 239 |
dl { |
| 240 |
margin-left: 0; |
| 241 |
} |
| 242 |
dl dd { |
| 243 |
margin-bottom: 13px; |
| 244 |
margin-left: 13px; |
| 245 |
} |
| 246 |
dl dt { |
| 247 |
font-weight: bold; |
| 248 |
} |
| 249 |
ul { |
| 250 |
margin-top: 0; |
| 251 |
} |
| 252 |
ul li { |
| 253 |
list-style: circle outside; |
| 254 |
} |
| 255 |
ul ul { |
| 256 |
margin-bottom: 0; |
| 257 |
} |
| 258 |
pre, code { |
| 259 |
background-color: #f7f7f7; |
| 260 |
border-radius: 3px; |
| 261 |
color: #333; |
| 262 |
white-space: pre-wrap; |
| 263 |
} |
| 264 |
pre { |
| 265 |
border-radius: 3px; |
| 266 |
margin: 5px 0px 10px 0px; |
| 267 |
padding: 10px; |
| 268 |
} |
| 269 |
pre:not([class]) { |
| 270 |
background-color: #f7f7f7; |
| 271 |
} |
| 272 |
code { |
| 273 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 274 |
font-size: 85%; |
| 275 |
} |
| 276 |
p > code, li > code { |
| 277 |
padding: 2px 0px; |
| 278 |
} |
| 279 |
div.figure { |
| 280 |
text-align: center; |
| 281 |
} |
| 282 |
img { |
| 283 |
background-color: #FFFFFF; |
| 284 |
padding: 2px; |
| 285 |
border: 1px solid #DDDDDD; |
| 286 |
border-radius: 3px; |
| 287 |
border: 1px solid #CCCCCC; |
| 288 |
margin: 0 5px; |
| 289 |
} |
| 290 |
h1 { |
| 291 |
margin-top: 0; |
| 292 |
font-size: 35px; |
| 293 |
line-height: 40px; |
| 294 |
} |
| 295 |
h2 { |
| 296 |
border-bottom: 4px solid #f7f7f7; |
| 297 |
padding-top: 10px; |
| 298 |
padding-bottom: 2px; |
| 299 |
font-size: 145%; |
| 300 |
} |
| 301 |
h3 { |
| 302 |
border-bottom: 2px solid #f7f7f7; |
| 303 |
padding-top: 10px; |
| 304 |
font-size: 120%; |
| 305 |
} |
| 306 |
h4 { |
| 307 |
border-bottom: 1px solid #f7f7f7; |
| 308 |
margin-left: 8px; |
| 309 |
font-size: 105%; |
| 310 |
} |
| 311 |
h5, h6 { |
| 312 |
border-bottom: 1px solid #ccc; |
| 313 |
font-size: 105%; |
| 314 |
} |
| 315 |
a { |
| 316 |
color: #0033dd; |
| 317 |
text-decoration: none; |
| 318 |
} |
| 319 |
a:hover { |
| 320 |
color: #6666ff; } |
| 321 |
a:visited { |
| 322 |
color: #800080; } |
| 323 |
a:visited:hover { |
| 324 |
color: #BB00BB; } |
| 325 |
a[href^="http:"] { |
| 326 |
text-decoration: underline; } |
| 327 |
a[href^="https:"] { |
| 328 |
text-decoration: underline; } |
| 329 |
|
| 330 |
code > span.kw { color: #555; font-weight: bold; } |
| 331 |
code > span.dt { color: #902000; } |
| 332 |
code > span.dv { color: #40a070; } |
| 333 |
code > span.bn { color: #d14; } |
| 334 |
code > span.fl { color: #d14; } |
| 335 |
code > span.ch { color: #d14; } |
| 336 |
code > span.st { color: #d14; } |
| 337 |
code > span.co { color: #888888; font-style: italic; } |
| 338 |
code > span.ot { color: #007020; } |
| 339 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 340 |
code > span.fu { color: #900; font-weight: bold; } |
| 341 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 342 |
</style> |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
</head> |
| 348 |
|
| 349 |
<body> |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
<h1 class="title toc-ignore">Extended Model Formulas in R: Multiple |
| 355 |
Parts and Multiple Responses</h1> |
| 356 |
<h4 class="author">Achim Zeileis</h4> |
| 357 |
<address class="author_afil"> |
| 358 |
Universität Innsbruck<br><a class="author_email" href="mailto:#"><a href="mailto:[email protected]g" class="email">[email protected]</a></a> |
| 359 |
</address> |
| 360 |
<h4 class="author">Yves Croissant</h4> |
| 361 |
<address class="author_afil"> |
| 362 |
Université de la Réunion<br><a class="author_email" href="mailto:#"><a href="mailto:Yves.Croissant@univ-reunion.fr" class="email">[email protected]</a></a> |
| 363 |
</address> |
| 364 |
<div class="abstract"> |
| 365 |
<p class="abstract">Abstract</p> |
| 366 |
<p>This introduction to the R package <strong>Formula</strong> is a |
| 367 |
(slightly) modified version of <span class="citation">Zeileis and |
| 368 |
Croissant (2010)</span>, published in the <em>Journal of Statistical |
| 369 |
Software</em>. Model formulas are the standard approach for specifying |
| 370 |
the variables in statistical models in the S language. Although being |
| 371 |
eminently useful in an extremely wide class of applications, they have |
| 372 |
certain limitations including being confined to single responses and not |
| 373 |
providing convenient support for processing formulas with multiple |
| 374 |
parts. The latter is relevant for models with two or more sets of |
| 375 |
variables, e.g., different equations for different model parameters |
| 376 |
(such as mean and dispersion), regressors and instruments in |
| 377 |
instrumental variable regressions, two-part models such as hurdle |
| 378 |
models, or alternative-specific and individual-specific variables in |
| 379 |
choice models among many others. The R package <strong>Formula</strong> |
| 380 |
addresses these two problems by providing a new class |
| 381 |
<code>"Formula"</code> (inheriting from <code>"formula"</code>) that |
| 382 |
accepts an additional formula operator <code>|</code> separating |
| 383 |
multiple parts and by allowing all formula operators (including the new |
| 384 |
<code>|</code>) on the left-hand side to support multiple responses.</p> |
| 385 |
</div> |
| 386 |
|
| 387 |
|
| 388 |
<div id="TOC"> |
| 389 |
<ul> |
| 390 |
<li><a href="#sec:intro" id="toc-sec:intro">Introduction</a></li> |
| 391 |
<li><a href="#sec:motivation" id="toc-sec:motivation">Motivating |
| 392 |
examples</a> |
| 393 |
<ul> |
| 394 |
<li><a href="#multiple-parts" id="toc-multiple-parts">Multiple |
| 395 |
parts</a></li> |
| 396 |
<li><a href="#multiple-responses" id="toc-multiple-responses">Multiple |
| 397 |
responses</a></li> |
| 398 |
</ul></li> |
| 399 |
<li><a href="#sec:implementation" id="toc-sec:implementation">Implementation</a> |
| 400 |
<ul> |
| 401 |
<li><a href="#working-with-classic-formula-objects" id="toc-working-with-classic-formula-objects">Working with classic |
| 402 |
<code>"formula"</code> objects</a></li> |
| 403 |
<li><a href="#constructing-formula-objects" id="toc-constructing-formula-objects">Constructing |
| 404 |
<code>"Formula"</code> objects</a></li> |
| 405 |
<li><a href="#extracting-formula-and-terms-objects" id="toc-extracting-formula-and-terms-objects">Extracting |
| 406 |
<code>"formula"</code> and <code>"terms"</code> objects</a></li> |
| 407 |
<li><a href="#computing-model-frames-matrices-and-responses" id="toc-computing-model-frames-matrices-and-responses">Computing model |
| 408 |
frames, matrices, and responses</a></li> |
| 409 |
<li><a href="#further-methods" id="toc-further-methods">Further |
| 410 |
methods</a></li> |
| 411 |
</ul></li> |
| 412 |
<li><a href="#sec:usage" id="toc-sec:usage">Usage in model fitting |
| 413 |
functions</a></li> |
| 414 |
<li><a href="#sec:summary" id="toc-sec:summary">Summary</a></li> |
| 415 |
<li><a href="#references" id="toc-references">References</a></li> |
| 416 |
</ul> |
| 417 |
</div> |
| 418 |
|
| 419 |
<div id="sec:intro" class="section level2"> |
| 420 |
<h2>Introduction</h2> |
| 421 |
<p>Since publication of the seminal “white book” <span class="citation">(Chambers and Hastie 1992)</span> the standard approach |
| 422 |
for fitting statistical models in the S language is to apply some |
| 423 |
model-fitting function (such as <code>lm()</code> or <code>glm()</code>) |
| 424 |
to a <code>"formula"</code> description of the variables involved in the |
| 425 |
model and typically stored in a <code>"data.frame"</code>. The semantics |
| 426 |
of formula processing are based on the ideas of the <span class="citation">Wilkinson and Rogers (1973)</span> notation which in |
| 427 |
turn was targeted at specification of analysis of variance models. |
| 428 |
Despite this emphasis on specification of terms in models with linear |
| 429 |
predictors, formula notation has always been used much more generally in |
| 430 |
S, e.g., for specifying variables in classification and regression |
| 431 |
trees, margins in contingency tables, or variables in graphical |
| 432 |
displays. In such applications, the precise meaning of a particular |
| 433 |
formula depends on the function that processes it. Typically, the |
| 434 |
standard formula processing approach would encompass extraction of the |
| 435 |
specified terms using <code>terms()</code>, preparation of a |
| 436 |
preprocessed data frame using <code>model.frame()</code>, and |
| 437 |
computation of a “design” or “regressor” matrix using |
| 438 |
<code>model.matrix()</code>. However, there are certain limitations to |
| 439 |
these standard formula processing tools in S that can be rather |
| 440 |
inconvenient in certain applications:</p> |
| 441 |
<ul> |
| 442 |
<li>The formula notation can just be used on the right-hand side (RHS) |
| 443 |
of a formula (to the right of the <code>~</code>) while it has its |
| 444 |
original arithmetic meaning on the left-hand side (LHS). This makes it |
| 445 |
difficult to specify multiple responses, especially if these are not |
| 446 |
numeric (e.g., factors). This feature would be useful for specifying |
| 447 |
multivariate outcomes of mixed types in independence tests <span class="citation">Hothorn et al. (2008)</span> or in models with |
| 448 |
multivariate responses <span class="citation">(e.g., supported in the |
| 449 |
<strong>party</strong> package, Zeileis, Hothorn, et al. |
| 450 |
2008)</span>.</li> |
| 451 |
<li>There is no simple construct in standard formula notation that |
| 452 |
allows one to separate several groups of variables from which separate |
| 453 |
model matrices can be derived. This task occurs in many types of models, |
| 454 |
e.g., when processing separate sets of variables for mean and dispersion |
| 455 |
<span class="citation">(e.g., in the <strong>betareg</strong> package, |
| 456 |
Cribari-Neto and Zeileis 2010)</span>, separate equations for location, |
| 457 |
scatter, and shape <span class="citation">(e.g., in the |
| 458 |
<strong>gamlss</strong> package, Stasinopoulos and Rigby 2007)</span>, |
| 459 |
regressors and instruments in instrumental variable regressions <span class="citation">Kleiber and Zeileis (2008)</span>, variables in |
| 460 |
two-part models such as hurdle models or zero-inflated regressions <span class="citation">(e.g., in the <strong>pscl</strong> package, Zeileis, |
| 461 |
Kleiber, et al. 2008)</span>, alternative-specific and |
| 462 |
individual-specific variables in choice models <span class="citation">(e.g., in the <strong>mlogit</strong> package, |
| 463 |
Croissant 2010)</span>, efficiency level variables in stochastic |
| 464 |
frontier analysis <span class="citation">(e.g., in the |
| 465 |
<strong>frontier</strong> package, Coelli and Henningsen 2010)</span>, |
| 466 |
or modeling variables and partitioning variables in model-based |
| 467 |
recursive partitioning techniques <span class="citation">(e.g., in the |
| 468 |
<strong>party</strong> package, Zeileis, Hothorn, et al. |
| 469 |
2008)</span>.</li> |
| 470 |
</ul> |
| 471 |
<p>In many of the aformentioned packages, standard |
| 472 |
<code>"formula"</code> objects are employed but their processing is |
| 473 |
generalized, e.g., by using multiple formulas, multiple terms, by adding |
| 474 |
new formula operators or even more elaborate solutions. However, in many |
| 475 |
situations it is not easy to reuse these generalizations outside the |
| 476 |
package/function they were designed for. Therefore, as we repeatedly |
| 477 |
needed such generalizations in our own packages and addressed this in |
| 478 |
the past by various different solutions, it seemed natural to provide a |
| 479 |
more general unified approach by means of our new |
| 480 |
<strong>Formula</strong> package. This has already been reused in some |
| 481 |
of our own packages (including <strong>AER</strong>, |
| 482 |
<strong>betareg</strong>, <strong>mlogit</strong>, and |
| 483 |
<strong>plm</strong>) but can also be easily employed by other package |
| 484 |
developers (e.g., as in the <strong>frontier</strong> package). More |
| 485 |
applications in our own and other packages will hopefully follow.</p> |
| 486 |
<p>In the remainder of this paper we discuss how multiple responses and |
| 487 |
multiple parts (both on the LHS and RHS) are enabled in the |
| 488 |
<strong>Formula</strong> package written in the R system for statistical |
| 489 |
computing <span class="citation">(R Core Team 2009)</span> and available |
| 490 |
from the Comprehensive R Archive Network (CRAN) at <a href="https://CRAN.R-project.org/package=Formula" class="uri">https://CRAN.R-project.org/package=Formula</a>. Built on top |
| 491 |
of basic <code>"formula"</code> objects, the <code>"Formula"</code> |
| 492 |
class just adds a thin additional layer which is based on a single |
| 493 |
additional operator, namely <code>|</code>, that can be used to separate |
| 494 |
different parts (or groups) of variables. <strong>Formula</strong> |
| 495 |
essentially just handles the different formula parts and leverages the |
| 496 |
existing methods for <code>"formula"</code> objects for all remaining |
| 497 |
operations. (Throughout the paper, no terminological distinction is made |
| 498 |
between classic <code>"formula"</code> objects as such and the way they |
| 499 |
are interpreted by standard processing tools (e.g., |
| 500 |
<code>terms()</code>, <code>model.frame()</code>, |
| 501 |
<code>model.matrix()</code>). The reasons for this are twofold: First, |
| 502 |
it reflects their use as described in <span class="citation">Chambers |
| 503 |
and Hastie (1992)</span> and, second, generalizations generally require |
| 504 |
extra effort from the user.) In the <a href="#sec:motivation">“Motivating examples”</a> we show the main ideas |
| 505 |
implemented in the package and how easily they can be employed. The |
| 506 |
details of the <code>"Formula"</code> class and its associated methods |
| 507 |
are discussed in the <a href="#sec:implementation">“Implementation”</a> |
| 508 |
section and the <a href="#sec:usage">“Usage in model fitting |
| 509 |
functions”</a> illustrates how <strong>Formula</strong> can be used in |
| 510 |
the development of new functions/packages. A short <a href="#sec:summary">“Summary”</a> concludes the paper.</p> |
| 511 |
</div> |
| 512 |
<div id="sec:motivation" class="section level2"> |
| 513 |
<h2>Motivating examples</h2> |
| 514 |
<p>To illustrate the basic ideas of the <strong>Formula</strong> |
| 515 |
package, we first generate a small artificial data set (with both |
| 516 |
numeric and categorical variables) and subsequently illustrate its usage |
| 517 |
with a multi-part and a multi-response <code>"Formula"</code>, |
| 518 |
respectively.</p> |
| 519 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1090</span>)</span> |
| 520 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">as.data.frame</span>(<span class="fu">matrix</span>(<span class="fu">round</span>(<span class="fu">runif</span>(<span class="dv">21</span>), <span class="at">digits =</span> <span class="dv">2</span>), <span class="at">ncol =</span> <span class="dv">7</span>))</span> |
| 521 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">colnames</span>(dat) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"y1"</span>, <span class="st">"y2"</span>, <span class="st">"y3"</span>, <span class="st">"x1"</span>, <span class="st">"x2"</span>, <span class="st">"x3"</span>, <span class="st">"x4"</span>)</span> |
| 522 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">6</span><span class="sc">:</span><span class="dv">7</span>)) dat[[i]] <span class="ot"><-</span> <span class="fu">factor</span>(dat[[i]] <span class="sc"><</span> <span class="fl">0.5</span>,</span> |
| 523 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a> <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>))</span> |
| 524 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a>dat<span class="sc">$</span>y2[<span class="dv">1</span>] <span class="ot"><-</span> <span class="cn">NA</span></span> |
| 525 |
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a>dat</span> |
| 526 |
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="do">## y1 y2 y3 x1 x2 x3 x4</span></span> |
| 527 |
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="do">## 1 0.82 <NA> 0.27 0.09 0.22 a b</span></span> |
| 528 |
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="do">## 2 0.70 a 0.17 0.26 0.46 b b</span></span> |
| 529 |
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="do">## 3 0.65 b 0.28 0.03 0.37 a a</span></span></code></pre></div> |
| 530 |
<div id="multiple-parts" class="section level3"> |
| 531 |
<h3>Multiple parts</h3> |
| 532 |
<p>We start out with a simple formula |
| 533 |
<code>log(y1) ~ x1 + x2 | I(x1^2)</code> which has a single response |
| 534 |
<code>log(y1)</code> on the LHS and two parts on the RHS, separated by |
| 535 |
<code>|</code>. The first part contains <code>x1</code> and |
| 536 |
<code>x2</code>, the second contains <code>I(x1^2)</code>, i.e., the |
| 537 |
squared values of <code>x1</code>. The initial <code>"formula"</code> |
| 538 |
can be transformed to a <code>"Formula"</code> using the constructor |
| 539 |
function <code>Formula()</code>:</p> |
| 540 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>F1 <span class="ot"><-</span> <span class="fu">Formula</span>(<span class="fu">log</span>(y1) <span class="sc">~</span> x1 <span class="sc">+</span> x2 <span class="sc">|</span> <span class="fu">I</span>(x1<span class="sc">^</span><span class="dv">2</span>))</span> |
| 541 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">length</span>(F1)</span> |
| 542 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="do">## [1] 1 2</span></span></code></pre></div> |
| 543 |
<p>The <code>length()</code> method indicates that there is one part on |
| 544 |
the LHS and two parts on the RHS. The first step of processing data |
| 545 |
using a formula is typically the construction of a so-called model frame |
| 546 |
containing only the variables required by the formula. As usual, this |
| 547 |
can be obtained with the <code>model.frame()</code> method.</p> |
| 548 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>mf1 <span class="ot"><-</span> <span class="fu">model.frame</span>(F1, <span class="at">data =</span> dat)</span> |
| 549 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>mf1</span> |
| 550 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="do">## log(y1) x1 x2 I(x1^2)</span></span> |
| 551 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="do">## 1 -0.1984509 0.09 0.22 0.0081</span></span> |
| 552 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="do">## 2 -0.3566749 0.26 0.46 0.0676</span></span> |
| 553 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="do">## 3 -0.4307829 0.03 0.37 9e-04</span></span></code></pre></div> |
| 554 |
<p>As this model just has a single response (as in base |
| 555 |
<code>"formula"</code> objects), the extractor function |
| 556 |
<code>model.response()</code> can be employed:</p> |
| 557 |
<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="fu">model.response</span>(mf1)</span> |
| 558 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="do">## 1 2 3 </span></span> |
| 559 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="do">## -0.1984509 -0.3566749 -0.4307829</span></span></code></pre></div> |
| 560 |
<p>For constructing separate model matrices for the two parts on the |
| 561 |
RHS, the <code>model.matrix()</code> can be employed and additionally |
| 562 |
specifying the argument <code>rhs</code>:</p> |
| 563 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">model.matrix</span>(F1, <span class="at">data =</span> mf1, <span class="at">rhs =</span> <span class="dv">1</span>)</span> |
| 564 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="do">## (Intercept) x1 x2</span></span> |
| 565 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="do">## 1 1 0.09 0.22</span></span> |
| 566 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="do">## 2 1 0.26 0.46</span></span> |
| 567 |
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="do">## 3 1 0.03 0.37</span></span> |
| 568 |
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="do">## attr(,"assign")</span></span> |
| 569 |
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="do">## [1] 0 1 2</span></span> |
| 570 |
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="fu">model.matrix</span>(F1, <span class="at">data =</span> mf1, <span class="at">rhs =</span> <span class="dv">2</span>)</span> |
| 571 |
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="do">## (Intercept) I(x1^2)</span></span> |
| 572 |
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="do">## 1 1 0.0081</span></span> |
| 573 |
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="do">## 2 1 0.0676</span></span> |
| 574 |
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="do">## 3 1 0.0009</span></span> |
| 575 |
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="do">## attr(,"assign")</span></span> |
| 576 |
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="do">## [1] 0 1</span></span></code></pre></div> |
| 577 |
</div> |
| 578 |
<div id="multiple-responses" class="section level3"> |
| 579 |
<h3>Multiple responses</h3> |
| 580 |
<p>To accommodate multiple responses, all formula operators can be |
| 581 |
employed on the LHS in <code>"Formula"</code> objects (whereas they |
| 582 |
would have their original arithmetic meaning in <code>"formula"</code> |
| 583 |
objects). This also includes the new <code>|</code> operator for |
| 584 |
separating different parts. Thus, one could specify a two-part response |
| 585 |
via <code>y1 | y2 ~ x3</code> or a single part with two variables via |
| 586 |
<code>y1 + y2 ~ x3</code>. We do the latter in the following |
| 587 |
illustration.</p> |
| 588 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>F2 <span class="ot"><-</span> <span class="fu">Formula</span>(y1 <span class="sc">+</span> y2 <span class="sc">~</span> x3)</span> |
| 589 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="fu">length</span>(F2)</span> |
| 590 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="do">## [1] 1 1</span></span></code></pre></div> |
| 591 |
<p>As usual, the model frame can be derived by</p> |
| 592 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>mf2 <span class="ot"><-</span> <span class="fu">model.frame</span>(F2, <span class="at">data =</span> dat)</span> |
| 593 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>mf2</span> |
| 594 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="do">## y1 y2 x3</span></span> |
| 595 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="do">## 2 0.70 a b</span></span> |
| 596 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="do">## 3 0.65 b a</span></span></code></pre></div> |
| 597 |
<p>However, there is an important difference to the model frame |
| 598 |
<code>mf1</code> derived in the previous example. As the (non-generic) |
| 599 |
<code>model.response()</code> function would only be able to extract a |
| 600 |
single response column from a model frame, multi-response model frames |
| 601 |
in <strong>Formula</strong> are implemented to have no response at all |
| 602 |
in the <code>model.response()</code> sense:</p> |
| 603 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">model.response</span>(mf2)</span> |
| 604 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="do">## NULL</span></span></code></pre></div> |
| 605 |
<p>As <code>model.response()</code> cannot be extended (without |
| 606 |
overloading the base R function), <strong>Formula</strong> provides a |
| 607 |
new generic function <code>model.part()</code> which can be used to |
| 608 |
extract all variables from a model frame pertaining to specific parts. |
| 609 |
This can also be used to extract multiple responses. Its syntax is |
| 610 |
modeled after <code>model.matrix()</code> taking a |
| 611 |
<code>"Formula"</code> as the first argument. For further details see |
| 612 |
the <a href="#sec:implementation">“Implementation”</a> section. Its |
| 613 |
application is straightforward and all LHS variables (in the first and |
| 614 |
only part of the LHS) can be extracted via</p> |
| 615 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">model.part</span>(F2, <span class="at">data =</span> mf2, <span class="at">lhs =</span> <span class="dv">1</span>)</span> |
| 616 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="do">## y1 y2</span></span> |
| 617 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="do">## 2 0.70 a</span></span> |
| 618 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="do">## 3 0.65 b</span></span></code></pre></div> |
| 619 |
<p>The same method also works for single response models as in the |
| 620 |
previous example:</p> |
| 621 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">model.part</span>(F1, <span class="at">data =</span> mf1, <span class="at">lhs =</span> <span class="dv">1</span>, <span class="at">drop =</span> <span class="cn">TRUE</span>)</span> |
| 622 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="do">## 1 2 3 </span></span> |
| 623 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="do">## -0.1984509 -0.3566749 -0.4307829</span></span></code></pre></div> |
| 624 |
</div> |
| 625 |
</div> |
| 626 |
<div id="sec:implementation" class="section level2"> |
| 627 |
<h2>Implementation</h2> |
| 628 |
<p>Below we discuss the ideas for the design of the |
| 629 |
<code>"Formula"</code> class and methods. As all tools are built on top |
| 630 |
of the <code>"formula"</code> class and its associated methods whose |
| 631 |
most important feature are briefly outlined as well.</p> |
| 632 |
<div id="working-with-classic-formula-objects" class="section level3"> |
| 633 |
<h3>Working with classic <code>"formula"</code> objects</h3> |
| 634 |
<p>Classic <code>"formula"</code> objects <span class="citation">(Chambers and Hastie 1992)</span> are constructed by |
| 635 |
using <code>~</code> to separate LHS and RHS, typically (but not |
| 636 |
necessarily) interpreted as “dependent” and “explanatory” variables. For |
| 637 |
describing relationships between variables on the RHS, several operators |
| 638 |
can be used: <code>+</code>, <code>-</code>, <code>*</code>, |
| 639 |
<code>/</code>, <code>:</code>, <code>%in%</code>, <code>^</code>. Thus, |
| 640 |
these do not have their original meaning in the top-level RHS while they |
| 641 |
keep their original arithmetic meaning on higher levels of the RHS |
| 642 |
(thus, within function calls such as <code>I(x1^2)</code>) and on the |
| 643 |
LHS in general. A first step in using <code>"formula"</code> objects is |
| 644 |
often to compute the associated <code>"terms"</code> using the function |
| 645 |
<code>terms()</code>. Based on the formula or the associated terms and a |
| 646 |
suitable set of variables (typically either in a data frame or in the |
| 647 |
global environment) <code>model.frame()</code> can build a so-called |
| 648 |
model frame that contains all variables in the formula/terms. This might |
| 649 |
include processing of missing values (<code>NA</code>s), carrying out |
| 650 |
variable transformations (such as logs, squares, or other functions of |
| 651 |
one or more variables) and providing further variables like weights, |
| 652 |
offset, etc. A model frame is simply a <code>"data.frame"</code> with |
| 653 |
additional attributes (including <code>"terms"</code>) but without a |
| 654 |
specific class. From this preprocessed model frame several components |
| 655 |
can be extracted using <code>model.extract()</code> or |
| 656 |
<code>model.response()</code>, <code>model.weights()</code>, and |
| 657 |
<code>model.offset()</code>, all of which are non-generic. Last not |
| 658 |
least, <code>model.matrix()</code> (which is generic) can compute |
| 659 |
“design” or “regressor” matrices based on the formula/terms and the |
| 660 |
associated model frame.</p> |
| 661 |
</div> |
| 662 |
<div id="constructing-formula-objects" class="section level3"> |
| 663 |
<h3>Constructing <code>"Formula"</code> objects</h3> |
| 664 |
<p>To accomplish the main objectives of the <code>"Formula"</code> |
| 665 |
class, the following design principles have been used: reuse of |
| 666 |
<code>"formula"</code> objects, introduction of a single new operator |
| 667 |
<code>|</code>, and support of all formula operators on the LHS. Thus, |
| 668 |
<code>|</code> loses its original meaning (logical “or”) on the first |
| 669 |
level of formulas but can still be used with its original meaning on |
| 670 |
higher levels, e.g., <code>factor(x1 > 0.5 | x3 == "a")</code> still |
| 671 |
works as before. For assigning a new class to formulas containing |
| 672 |
<code>|</code>, the constructor function <code>Formula()</code> is |
| 673 |
used:</p> |
| 674 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>F3 <span class="ot"><-</span> <span class="fu">Formula</span>(y1 <span class="sc">+</span> y2 <span class="sc">|</span> <span class="fu">log</span>(y3) <span class="sc">~</span> x1 <span class="sc">+</span> <span class="fu">I</span>(x2<span class="sc">^</span><span class="dv">2</span>) <span class="sc">|</span> <span class="dv">0</span> <span class="sc">+</span> <span class="fu">log</span>(x1) <span class="sc">|</span> x3 <span class="sc">/</span> x4)</span> |
| 675 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a>F3</span> |
| 676 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="do">## y1 + y2 | log(y3) ~ x1 + I(x2^2) | 0 + log(x1) | x3/x4</span></span> |
| 677 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="fu">length</span>(F3)</span> |
| 678 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="do">## [1] 2 3</span></span></code></pre></div> |
| 679 |
<p>In this example, <code>F3</code> is an artificially complex formula |
| 680 |
with two parts on the LHS and three parts on the RHS, both containing |
| 681 |
multiple terms, transformations or other formula operators. Apart from |
| 682 |
assigning the new class <code>"Formula"</code> (in addition to the old |
| 683 |
<code>"formula"</code> class), <code>Formula()</code> also splits up the |
| 684 |
formula into LHS and RHS parts which are stored as list attributes |
| 685 |
<code>"lhs"</code> and <code>"rhs"</code>, respectively, e.g.,</p> |
| 686 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="fu">attr</span>(F3, <span class="st">"lhs"</span>)</span> |
| 687 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="do">## [[1]]</span></span> |
| 688 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="do">## y1 + y2</span></span> |
| 689 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="do">## </span></span> |
| 690 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="do">## [[2]]</span></span> |
| 691 |
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="do">## log(y3)</span></span></code></pre></div> |
| 692 |
<p>and analogously <code>attr(F3, "rhs")</code>. The user never has to |
| 693 |
compute on these attributes directly, but many methods for |
| 694 |
<code>"Formula"</code> objects take <code>lhs</code> and/or |
| 695 |
<code>rhs</code> arguments. These always refer to index vectors for the |
| 696 |
two respective lists.</p> |
| 697 |
<p>It would have been conceivable to generalize not only the notion of |
| 698 |
formulas but also of terms or model frames. However, there are few |
| 699 |
generics with methods for <code>"terms"</code> objects and there is no |
| 700 |
particular class for model frames at all. Hence, computing with |
| 701 |
generalized versions of these concepts would have required much more |
| 702 |
overhead for users of <strong>Formula</strong>. Hence, it was decided |
| 703 |
not to do so and keep the package interface as simple as possible.</p> |
| 704 |
</div> |
| 705 |
<div id="extracting-formula-and-terms-objects" class="section level3"> |
| 706 |
<h3>Extracting <code>"formula"</code> and <code>"terms"</code> |
| 707 |
objects</h3> |
| 708 |
<p>As subsequent computations typically require a <code>"formula"</code> |
| 709 |
or a <code>"terms"</code> object, <strong>Formula</strong> provides |
| 710 |
suitable <code>formula()</code> and <code>terms()</code> extractors for |
| 711 |
<code>"Formula"</code> objects. For the former, the idea is to be able |
| 712 |
to switch back and forth between the <code>"Formula"</code> and |
| 713 |
<code>"formula"</code> representation, e.g., |
| 714 |
<code>formula(Formula(...))</code> should recover the original input |
| 715 |
formula. For the latter, the objective is somewhat different: |
| 716 |
<code>terms()</code> should always return a <code>"terms"</code> object |
| 717 |
that can be processed by <code>model.frame()</code> and similar |
| 718 |
functions. Thus, the terms must not contain multiple responses and/or |
| 719 |
the new <code>|</code> operator.</p> |
| 720 |
<p>The <code>formula()</code> method is straightforward. When no |
| 721 |
additional arguments are supplied it recovers the original |
| 722 |
<code>"formula"</code>. Furthermore, there are two optional additional |
| 723 |
arguments <code>lhs</code> and <code>rhs</code>. With these arguments |
| 724 |
subsets of formulas can be chosen, indexing the LHS and RHS parts. The |
| 725 |
default value for both is <code>NULL</code>, meaning that all parts are |
| 726 |
employed.</p> |
| 727 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="fu">formula</span>(F3)</span> |
| 728 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a><span class="do">## y1 + y2 | log(y3) ~ x1 + I(x2^2) | 0 + log(x1) | x3/x4</span></span> |
| 729 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="fu">formula</span>(F3, <span class="at">lhs =</span> <span class="dv">2</span>, <span class="at">rhs =</span> <span class="sc">-</span><span class="dv">2</span>)</span> |
| 730 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="do">## log(y3) ~ x1 + I(x2^2) | x3/x4</span></span> |
| 731 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="fu">formula</span>(F3, <span class="at">lhs =</span> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>), <span class="at">rhs =</span> <span class="dv">0</span>)</span> |
| 732 |
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="do">## y1 + y2 ~ 0</span></span></code></pre></div> |
| 733 |
<p>Similarly, <code>terms()</code> computes a <code>"terms"</code> |
| 734 |
object, by default using all parts in the formula, but <code>lhs</code> |
| 735 |
and <code>rhs</code> can be used as above. To remove the <code>|</code> |
| 736 |
operator, all parts are collapsed using the <code>+</code> operator. |
| 737 |
Furthermore, the LHS variables can only be kept on the LHS if they |
| 738 |
contain a single term. Otherwise, to stop subsequent computations from |
| 739 |
interpreting the formula operators as arithmetic operators, all LHS |
| 740 |
components are added on the RHS as well. Thus, for <code>F3</code> we |
| 741 |
obtain</p> |
| 742 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">terms</span>(F3)</span> |
| 743 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="do">## ~y1 + y2 + log(y3) + (x1 + I(x2^2)) + (0 + log(x1)) + x3/x4</span></span> |
| 744 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="do">## attr(,"variables")</span></span> |
| 745 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="do">## list(y1, y2, log(y3), x1, I(x2^2), log(x1), x3, x4)</span></span> |
| 746 |
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="do">## attr(,"factors")</span></span> |
| 747 |
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="do">## y1 y2 log(y3) x1 I(x2^2) log(x1) x3 x3:x4</span></span> |
| 748 |
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="do">## y1 1 0 0 0 0 0 0 0</span></span> |
| 749 |
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="do">## y2 0 1 0 0 0 0 0 0</span></span> |
| 750 |
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="do">## log(y3) 0 0 1 0 0 0 0 0</span></span> |
| 751 |
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="do">## x1 0 0 0 1 0 0 0 0</span></span> |
| 752 |
<span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="do">## I(x2^2) 0 0 0 0 1 0 0 0</span></span> |
| 753 |
<span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="do">## log(x1) 0 0 0 0 0 1 0 0</span></span> |
| 754 |
<span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="do">## x3 0 0 0 0 0 0 1 2</span></span> |
| 755 |
<span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="do">## x4 0 0 0 0 0 0 0 1</span></span> |
| 756 |
<span id="cb14-15"><a href="#cb14-15" tabindex="-1"></a><span class="do">## attr(,"term.labels")</span></span> |
| 757 |
<span id="cb14-16"><a href="#cb14-16" tabindex="-1"></a><span class="do">## [1] "y1" "y2" "log(y3)" "x1" "I(x2^2)" "log(x1)" "x3" </span></span> |
| 758 |
<span id="cb14-17"><a href="#cb14-17" tabindex="-1"></a><span class="do">## [8] "x3:x4" </span></span> |
| 759 |
<span id="cb14-18"><a href="#cb14-18" tabindex="-1"></a><span class="do">## attr(,"order")</span></span> |
| 760 |
<span id="cb14-19"><a href="#cb14-19" tabindex="-1"></a><span class="do">## [1] 1 1 1 1 1 1 1 2</span></span> |
| 761 |
<span id="cb14-20"><a href="#cb14-20" tabindex="-1"></a><span class="do">## attr(,"intercept")</span></span> |
| 762 |
<span id="cb14-21"><a href="#cb14-21" tabindex="-1"></a><span class="do">## [1] 0</span></span> |
| 763 |
<span id="cb14-22"><a href="#cb14-22" tabindex="-1"></a><span class="do">## attr(,"response")</span></span> |
| 764 |
<span id="cb14-23"><a href="#cb14-23" tabindex="-1"></a><span class="do">## [1] 0</span></span> |
| 765 |
<span id="cb14-24"><a href="#cb14-24" tabindex="-1"></a><span class="do">## attr(,".Environment")</span></span> |
| 766 |
<span id="cb14-25"><a href="#cb14-25" tabindex="-1"></a><span class="do">## <environment: R_GlobalEnv></span></span></code></pre></div> |
| 767 |
<p>Instead of using all parts, subsets can again be selected. We |
| 768 |
illustrate this below but only show the associated |
| 769 |
<code>"formula"</code> to save output space:</p> |
| 770 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="fu">formula</span>(<span class="fu">terms</span>(F3))</span> |
| 771 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="do">## ~y1 + y2 + log(y3) + (x1 + I(x2^2)) + (0 + log(x1)) + x3/x4</span></span> |
| 772 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="fu">formula</span>(<span class="fu">terms</span>(F3, <span class="at">lhs =</span> <span class="dv">2</span>, <span class="at">rhs =</span> <span class="sc">-</span><span class="dv">2</span>))</span> |
| 773 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="do">## log(y3) ~ x1 + I(x2^2) + x3/x4</span></span> |
| 774 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="fu">formula</span>(<span class="fu">terms</span>(F3, <span class="at">lhs =</span> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>), <span class="at">rhs =</span> <span class="dv">0</span>))</span> |
| 775 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="do">## ~y1 + y2</span></span></code></pre></div> |
| 776 |
</div> |
| 777 |
<div id="computing-model-frames-matrices-and-responses" class="section level3"> |
| 778 |
<h3>Computing model frames, matrices, and responses</h3> |
| 779 |
<p>Given that suitable <code>"terms"</code> can be extracted from |
| 780 |
<code>"Formula"</code> objects, it is straightforward to set up the |
| 781 |
corresponding model frame. The <code>model.frame()</code> method simply |
| 782 |
first calls the <code>terms()</code> method and then applies the default |
| 783 |
<code>model.frame()</code>. Hence, all further arguments are processed |
| 784 |
as usual, e.g.,</p> |
| 785 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>mf3 <span class="ot"><-</span> <span class="fu">model.frame</span>(F3, <span class="at">data =</span> dat, <span class="at">subset =</span> y1 <span class="sc"><</span> <span class="fl">0.75</span>, <span class="at">weights =</span> x1)</span> |
| 786 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>mf3</span> |
| 787 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="do">## y1 y2 log(y3) x1 I(x2^2) log(x1) x3 x4 (weights)</span></span> |
| 788 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="do">## 2 0.70 a -1.771957 0.26 0.2116 -1.347074 b b 0.26</span></span> |
| 789 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="do">## 3 0.65 b -1.272966 0.03 0.1369 -3.506558 a a 0.03</span></span></code></pre></div> |
| 790 |
<p>All subsequent computations are then based on this preprocessed model |
| 791 |
frame (and possibly the original <code>"Formula"</code>). Thus, the |
| 792 |
model matrices for each RHS part can be easily computed, again setting |
| 793 |
the <code>rhs</code> argument:</p> |
| 794 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">model.matrix</span>(F3, <span class="at">data =</span> mf3, <span class="at">rhs =</span> <span class="dv">2</span>)</span> |
| 795 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="do">## log(x1)</span></span> |
| 796 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="do">## 2 -1.347074</span></span> |
| 797 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="do">## 3 -3.506558</span></span> |
| 798 |
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="do">## attr(,"assign")</span></span> |
| 799 |
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="do">## [1] 1</span></span></code></pre></div> |
| 800 |
<p>Typically, just a single RHS will be selected and hence |
| 801 |
<code>rhs = 1</code> and not <code>rhs = NULL</code> is the default in |
| 802 |
this method. However, multiple RHS parts are also supported. Also, there |
| 803 |
is a <code>lhs</code> argument available (defaulting to |
| 804 |
<code>NULL</code>) which might seem unnecessary at first sight but it is |
| 805 |
important in case the selected RHS part(s) contain(s) a “<code>.</code>” |
| 806 |
that needs to be resolved (see <code>?model.matrix.Formula</code> for an |
| 807 |
example).</p> |
| 808 |
<p>The LHS parts can be extracted using the method for the new |
| 809 |
<code>model.part()</code> generic, employing a syntax similar to |
| 810 |
<code>model.matrix()</code>:</p> |
| 811 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">model.part</span>(F3, <span class="at">data =</span> mf3, <span class="at">lhs =</span> <span class="dv">1</span>)</span> |
| 812 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="do">## y1 y2</span></span> |
| 813 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="do">## 2 0.70 a</span></span> |
| 814 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="do">## 3 0.65 b</span></span> |
| 815 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="fu">model.part</span>(F3, <span class="at">data =</span> mf3, <span class="at">lhs =</span> <span class="dv">2</span>)</span> |
| 816 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="do">## log(y3)</span></span> |
| 817 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="do">## 2 -1.771957</span></span> |
| 818 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="do">## 3 -1.272966</span></span></code></pre></div> |
| 819 |
<p>As argued above, introduction of a new generic is necessary for |
| 820 |
supporting multi-response formulas because <code>model.response()</code> |
| 821 |
is non-generic. For model frames derived from single-response formulas, |
| 822 |
<code>model.response()</code> can be used as usual. The remaining |
| 823 |
extractors work as usual:</p> |
| 824 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="fu">model.weights</span>(mf3)</span> |
| 825 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="do">## [1] 0.26 0.03</span></span></code></pre></div> |
| 826 |
</div> |
| 827 |
<div id="further-methods" class="section level3"> |
| 828 |
<h3>Further methods</h3> |
| 829 |
<p>To conclude the suite of methods available for the new |
| 830 |
<code>"Formula"</code> class, <strong>Formula</strong> provides an |
| 831 |
<code>update()</code> method and a new <code>as.Formula()</code> generic |
| 832 |
with suitable methods. The former updates the formula part by part, |
| 833 |
adding new parts if necessary:</p> |
| 834 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="fu">update</span>(F1, . <span class="sc">~</span> . <span class="sc">-</span> x1 <span class="sc">|</span> . <span class="sc">+</span> x1)</span> |
| 835 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a><span class="do">## log(y1) ~ x2 | I(x1^2) + x1</span></span> |
| 836 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="fu">update</span>(F1, . <span class="sc">+</span> y2 <span class="sc">|</span> y3 <span class="sc">~</span> .)</span> |
| 837 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="do">## log(y1) + y2 | y3 ~ x1 + x2 | I(x1^2)</span></span></code></pre></div> |
| 838 |
<p>The <code>as.Formula()</code> method coerces to |
| 839 |
<code>"Formula"</code>, possibly also processing multiple arguments:</p> |
| 840 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="fu">as.Formula</span>(y1 <span class="sc">~</span> x1, y2 <span class="sc">~</span> x2, <span class="sc">~</span> x3)</span> |
| 841 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a><span class="do">## y1 | y2 ~ x1 | x2 | x3</span></span></code></pre></div> |
| 842 |
</div> |
| 843 |
</div> |
| 844 |
<div id="sec:usage" class="section level2"> |
| 845 |
<h2>Usage in model fitting functions</h2> |
| 846 |
<p>A typical application of <strong>Formula</strong> is to provide the |
| 847 |
workhorse for formula processing in model-fitting functions that require |
| 848 |
specification of multiple parts or multiple responses. To provide a very |
| 849 |
brief and simple example, we show how such a function can be set up. For |
| 850 |
illustration, we compute the coefficients in an instrumental variables |
| 851 |
regression using two-stage least squares.</p> |
| 852 |
<p>The <code>ivcoef()</code> function below takes the usual arguments |
| 853 |
<code>formula</code>, <code>data</code>, <code>subset</code>, and |
| 854 |
<code>na.action</code> (and further arguments <code>weights</code> and |
| 855 |
<code>offset</code> could be included in the same way). The |
| 856 |
<code>formula</code> should be a two-part formula like |
| 857 |
<code>y ~ x1 + x2 | z1 + z2 + z3</code>. There is a single response on |
| 858 |
the LHS, one RHS part with the regressors and a second RHS part with the |
| 859 |
instruments. The function <code>ivcoef()</code> uses the typical |
| 860 |
workflow of model-fitting functions and processes its arguments in the |
| 861 |
following four steps: (1) process the call, (2) set up the model frame |
| 862 |
(using the <code>"Formula"</code> method), (3) extract response and |
| 863 |
regressors from the model frame, (4) estimate the model (by calling |
| 864 |
<code>lm.fit()</code> twice to compute the two-stage least squares |
| 865 |
coefficients).</p> |
| 866 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a>ivcoef <span class="ot"><-</span> <span class="cf">function</span>(formula, data, subset, na.action, ...)</span> |
| 867 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a>{</span> |
| 868 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a> mf <span class="ot"><-</span> <span class="fu">match.call</span>(<span class="at">expand.dots =</span> <span class="cn">FALSE</span>)</span> |
| 869 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a> m <span class="ot"><-</span> <span class="fu">match</span>(<span class="fu">c</span>(<span class="st">"formula"</span>, <span class="st">"data"</span>, <span class="st">"subset"</span>, <span class="st">"na.action"</span>), <span class="fu">names</span>(mf), <span class="dv">0</span>)</span> |
| 870 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a> mf <span class="ot"><-</span> mf[<span class="fu">c</span>(<span class="dv">1</span>, m)]</span> |
| 871 |
<span id="cb22-6"><a href="#cb22-6" tabindex="-1"></a> </span> |
| 872 |
<span id="cb22-7"><a href="#cb22-7" tabindex="-1"></a> f <span class="ot"><-</span> <span class="fu">Formula</span>(formula)</span> |
| 873 |
<span id="cb22-8"><a href="#cb22-8" tabindex="-1"></a> mf[[<span class="dv">1</span>]] <span class="ot"><-</span> <span class="fu">as.name</span>(<span class="st">"model.frame"</span>)</span> |
| 874 |
<span id="cb22-9"><a href="#cb22-9" tabindex="-1"></a> mf<span class="sc">$</span>formula <span class="ot"><-</span> f</span> |
| 875 |
<span id="cb22-10"><a href="#cb22-10" tabindex="-1"></a> mf <span class="ot"><-</span> <span class="fu">eval</span>(mf, <span class="fu">parent.frame</span>())</span> |
| 876 |
<span id="cb22-11"><a href="#cb22-11" tabindex="-1"></a> </span> |
| 877 |
<span id="cb22-12"><a href="#cb22-12" tabindex="-1"></a> y <span class="ot"><-</span> <span class="fu">model.response</span>(mf)</span> |
| 878 |
<span id="cb22-13"><a href="#cb22-13" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">model.matrix</span>(f, <span class="at">data =</span> mf, <span class="at">rhs =</span> <span class="dv">1</span>)</span> |
| 879 |
<span id="cb22-14"><a href="#cb22-14" tabindex="-1"></a> z <span class="ot"><-</span> <span class="fu">model.matrix</span>(f, <span class="at">data =</span> mf, <span class="at">rhs =</span> <span class="dv">2</span>)</span> |
| 880 |
<span id="cb22-15"><a href="#cb22-15" tabindex="-1"></a></span> |
| 881 |
<span id="cb22-16"><a href="#cb22-16" tabindex="-1"></a> xz <span class="ot"><-</span> <span class="fu">as.matrix</span>(<span class="fu">lm.fit</span>(z, x)<span class="sc">$</span>fitted.values)</span> |
| 882 |
<span id="cb22-17"><a href="#cb22-17" tabindex="-1"></a> <span class="fu">lm.fit</span>(xz, y)<span class="sc">$</span>coefficients</span> |
| 883 |
<span id="cb22-18"><a href="#cb22-18" tabindex="-1"></a>}</span></code></pre></div> |
| 884 |
<p>The resulting function can then be applied easily (albeit not very |
| 885 |
meaningfully) to the <code>dat</code> data frame:</p> |
| 886 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a><span class="fu">ivcoef</span>(<span class="fu">log</span>(y1) <span class="sc">~</span> x1 <span class="sc">|</span> x2, <span class="at">data =</span> dat)</span> |
| 887 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="do">## (Intercept) x1 </span></span> |
| 888 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="do">## -0.169027 -1.260073</span></span></code></pre></div> |
| 889 |
<p>The same coefficients can be derived along with all the usual |
| 890 |
inference using the <code>ivreg()</code> function from the |
| 891 |
<strong>AER</strong> package <span class="citation">(Kleiber and Zeileis |
| 892 |
2008)</span>, which also uses the <strong>Formula</strong> tools in its |
| 893 |
latest release. Apart from providing inference and many other details, |
| 894 |
<code>ivreg()</code> also supports <code>weights</code>, |
| 895 |
<code>offsets</code> etc. Finally, for backward compatibility, the |
| 896 |
function also allows separate formulas for regressors and instruments |
| 897 |
(i.e., <code>formula = y ~ x1 + x2</code> and |
| 898 |
<code>instruments = ~ z1 + z2 + z3</code>) which can be easily |
| 899 |
incorporated using the <strong>Formula</strong> tools, e.g., replacing |
| 900 |
<code>f <- Formula(formula)</code> by</p> |
| 901 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a> f <span class="ot"><-</span> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">is.null</span>(instruments)) <span class="fu">as.Formula</span>(formula, instruments)</span> |
| 902 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a> <span class="cf">else</span> <span class="fu">as.Formula</span>(formula)</span> |
| 903 |
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">isTRUE</span>(<span class="fu">all.equal</span>(<span class="fu">length</span>(f), <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>))))</span></code></pre></div> |
| 904 |
<p>In summary, the usage of <strong>Formula</strong> should reduce the |
| 905 |
overhead for the developers of model-fitting functions in R with |
| 906 |
multiple responses and/or multiple parts and make the resulting programs |
| 907 |
more intelligible. Further R packages employing the |
| 908 |
<code>"Formula"</code> approach can be obtained from CRAN, including |
| 909 |
<strong>betareg</strong>, <strong>frontier</strong>, |
| 910 |
<strong>mlogit</strong>, and <strong>plm</strong>.</p> |
| 911 |
</div> |
| 912 |
<div id="sec:summary" class="section level2"> |
| 913 |
<h2>Summary</h2> |
| 914 |
<p>The <strong>Formula</strong> package provides tools for processing |
| 915 |
multi-response and multi-part formulas in the R system for statistical |
| 916 |
computing. The new class <code>"Formula"</code> inherits from the |
| 917 |
existing <code>"formula"</code> class, only adds a single new formula |
| 918 |
operator <code>|</code>, and enables the usage of formula operators on |
| 919 |
the left-hand side of formulas. The methods provided for |
| 920 |
<code>"Formula"</code> objects are as similar as possible to the classic |
| 921 |
methods, facilitating their usage in model-fitting functions that |
| 922 |
require support for multiple responses and/or multiple parts of |
| 923 |
variables.</p> |
| 924 |
</div> |
| 925 |
<div id="references" class="section level2 unnumbered"> |
| 926 |
<h2 class="unnumbered">References</h2> |
| 927 |
<div id="refs" class="references csl-bib-body hanging-indent"> |
| 928 |
<div id="ref-Formula:Chambers+Hastie:1992" class="csl-entry"> |
| 929 |
Chambers, John M., and Trevor J. Hastie, eds. 1992. <em>Statistical |
| 930 |
Models in <span>S</span></em>. Chapman & Hall. |
| 931 |
</div> |
| 932 |
<div id="ref-Formula:Coelli+Henningsen:2010" class="csl-entry"> |
| 933 |
Coelli, Tim, and Arne Henningsen. 2010. <em><span class="nocase">frontier</span>: Stochastic Frontier Analysis</em>. <a href="https://CRAN.R-project.org/package=frontier">https://CRAN.R-project.org/package=frontier</a>. |
| 934 |
</div> |
| 935 |
<div id="ref-Formula:Cribari-Neto+Zeileis:2010" class="csl-entry"> |
| 936 |
Cribari-Neto, Francisco, and Achim Zeileis. 2010. <span>“Beta Regression |
| 937 |
in <span>R</span>.”</span> <em>Journal of Statistical Software</em> 34 |
| 938 |
(2): 1–24. <a href="https://doi.org/10.18637/jss.v034.i02">https://doi.org/10.18637/jss.v034.i02</a>. |
| 939 |
</div> |
| 940 |
<div id="ref-Formula:Croissant:2010" class="csl-entry"> |
| 941 |
Croissant, Yves. 2010. <em><span class="nocase">mlogit</span>: |
| 942 |
Multinomial Logit Model</em>. <a href="https://CRAN.R-project.org/package=mlogit">https://CRAN.R-project.org/package=mlogit</a>. |
| 943 |
</div> |
| 944 |
<div id="ref-Formula:Croissant+Millo:2008" class="csl-entry"> |
| 945 |
Croissant, Yves, and Giovanni Millo. 2008. <span>“Panel Data |
| 946 |
Econometrics in <span>R</span>: The <span class="nocase">plm</span> |
| 947 |
Package.”</span> <em>Journal of Statistical Software</em> 27 (2): 1–43. |
| 948 |
<a href="https://doi.org/10.18637/jss.v027.i02">https://doi.org/10.18637/jss.v027.i02</a>. |
| 949 |
</div> |
| 950 |
<div id="ref-Formula:Hothorn+Hornik+VanDeWiel:2006" class="csl-entry"> |
| 951 |
Hothorn, Torsten, Kurt Hornik, Mark A. van de Wiel, and Achim Zeileis. |
| 952 |
2006. <span>“A <span>L</span>ego System for Conditional |
| 953 |
Inference.”</span> <em>The American Statistician</em> 60 (3): 257–63. |
| 954 |
</div> |
| 955 |
<div id="ref-Formula:Hothorn+Hornik+VanDeWiel:2008" class="csl-entry"> |
| 956 |
Hothorn, Torsten, Kurt Hornik, Mark A. van de Wiel, and Achim Zeileis. |
| 957 |
2008. <span>“Implementing a Class of Permutation Tests: The <span class="nocase">coin</span> Package.”</span> <em>Journal of Statistical |
| 958 |
Software</em> 28 (8): 1–23. <a href="https://doi.org/10.18637/jss.v028.i08">https://doi.org/10.18637/jss.v028.i08</a>. |
| 959 |
</div> |
| 960 |
<div id="ref-Formula:Kleiber+Zeileis:2008" class="csl-entry"> |
| 961 |
Kleiber, Christian, and Achim Zeileis. 2008. <em>Applied Econometrics |
| 962 |
with <span>R</span></em>. Springer-Verlag. |
| 963 |
</div> |
| 964 |
<div id="ref-Formula:R:2009" class="csl-entry"> |
| 965 |
R Core Team. 2009. <em><span>R</span>: <span>A</span> Language and |
| 966 |
Environment for Statistical Computing</em>. <span>R</span> Foundation |
| 967 |
for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>. |
| 968 |
</div> |
| 969 |
<div id="ref-Formula:Stasinopoulos+Rigby:2007" class="csl-entry"> |
| 970 |
Stasinopoulos, D. Mikis, and Robert A. Rigby. 2007. <span>“Generalized |
| 971 |
Additive Models for Location Scale and Shape (<span>GAMLSS</span>) in |
| 972 |
<span>R</span>.”</span> <em>Journal of Statistical Software</em> 23 (7): |
| 973 |
1–46. <a href="https://doi.org/10.18637/jss.v023.i07">https://doi.org/10.18637/jss.v023.i07</a>. |
| 974 |
</div> |
| 975 |
<div id="ref-Formula:Wilkinson+Rogers:1973" class="csl-entry"> |
| 976 |
Wilkinson, G. N., and C. E. Rogers. 1973. <span>“Symbolic Description of |
| 977 |
Factorial Models for Analysis of Variance.”</span> <em>Applied |
| 978 |
Statistics</em> 22: 392–99. |
| 979 |
</div> |
| 980 |
<div id="ref-Formula:Zeileis+Croissant:2010" class="csl-entry"> |
| 981 |
Zeileis, Achim, and Yves Croissant. 2010. <span>“Extended Model Formulas |
| 982 |
in <span>R</span>: Multiple Parts and Multiple Responses.”</span> |
| 983 |
<em>Journal of Statistical Software</em> 34 (1): 1–13. <a href="https://doi.org/10.18637/jss.v034.i01">https://doi.org/10.18637/jss.v034.i01</a>. |
| 984 |
</div> |
| 985 |
<div id="ref-Formula:Zeileis+Hothorn+Hornik:2008" class="csl-entry"> |
| 986 |
Zeileis, Achim, Torsten Hothorn, and Kurt Hornik. 2008. |
| 987 |
<span>“Model-Based Recursive Partitioning.”</span> <em>Journal of |
| 988 |
Computational and Graphical Statistics</em> 17 (2): 492–514. |
| 989 |
</div> |
| 990 |
<div id="ref-Formula:Zeileis+Kleiber+Jackman:2008" class="csl-entry"> |
| 991 |
Zeileis, Achim, Christian Kleiber, and Simon Jackman. 2008. |
| 992 |
<span>“Regression Models for Count Data in <span>R</span>.”</span> |
| 993 |
<em>Journal of Statistical Software</em> 27 (8): 1–25. <a href="https://doi.org/10.18637/jss.v027.i08">https://doi.org/10.18637/jss.v027.i08</a>. |
| 994 |
</div> |
| 995 |
</div> |
| 996 |
</div> |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
<script> |
| 1005 |
(function () { |
| 1006 |
var script = document.createElement("script"); |
| 1007 |
script.type = "text/javascript"; |
| 1008 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 1009 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 1010 |
})(); |
| 1011 |
</script> |
| 1012 |
|
| 1013 |
</body> |
| 1014 |
</html> |