-
Notifications
You must be signed in to change notification settings - Fork 16
/
Dirichlet-Regression-With-R.html
333 lines (298 loc) · 18.3 KB
/
Dirichlet-Regression-With-R.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html>
<head>
<title>Dirichlet Regression</title>
<meta charset="utf-8">
<meta name="Description" content="R Language Tutorials for Advanced Statistics">
<meta name="Keywords" content="R, Tutorial, Machine learning, Statistics, Data Mining, Analytics, Data science, Linear Regression, Logistic Regression, Time series, Forecasting">
<meta name="Distribution" content="Global">
<meta name="Author" content="Selva Prabhakaran">
<meta name="Robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/screenshots/iconb-64.png" type="image/x-icon" />
<link href="www/bootstrap.min.css" rel="stylesheet">
<link href="www/highlight.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Inconsolata:400,700'
rel='stylesheet' type='text/css'>
<!-- Color Script -->
<style type="text/css">
a {
color: #3675C5;
color: rgb(25, 145, 248);
color: #4582ec;
color: #3F73D8;
}
li {
line-height: 1.65;
}
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
</style>
<!-- Add Google search -->
<script language="Javascript" type="text/javascript">
function my_search_google()
{
var query = document.getElementById("my-google-search").value;
window.open("http://google.com/search?q=" + query
+ "%20site:" + "http://r-statistics.co");
}
</script>
</head>
<body>
<div class="container">
<div class="masthead">
<!--
<ul class="nav nav-pills pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Table of contents<b class="caret"></b>
</a>
<ul class="dropdown-menu pull-right" role="menu">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</ul>
-->
<ul class="nav nav-pills pull-right">
<div class="input-group">
<form onsubmit="my_search_google()">
<input type="text" class="form-control" id="my-google-search" placeholder="Search..">
<form>
</div><!-- /input-group -->
</ul><!-- /.col-lg-6 -->
<h3 class="muted"><a href="/">r-statistics.co</a><small> by Selva Prabhakaran</small></h3>
<hr>
</div>
<div class="row">
<div class="col-xs-12 col-sm-3" id="nav">
<div class="well">
<li>
<ul class="list-unstyled">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</div>
<div class="well">
<p>Stay up-to-date. <a href="https://docs.google.com/forms/d/1xkMYkLNFU9U39Dd8S_2JC0p8B5t6_Yq6zUQjanQQJpY/viewform">Subscribe!</a></p>
<p><a href="https://docs.google.com/forms/d/13GrkCFcNa-TOIllQghsz2SIEbc-YqY9eJX02B19l5Ow/viewform">Chat!</a></p>
</div>
<h4>Contents</h4>
<ul class="list-unstyled" id="toc"></ul>
<!--
<hr>
<p><a href="/contribute.html">How to contribute</a></p>
<p><a class="btn btn-primary" href="">Edit this page</a></p>
-->
</div>
<div id="content" class="col-xs-12 col-sm-8 pull-right">
<h1>Dirichlet Regression</h1>
<blockquote>
<p>Dirichlet regression can be used to model <em>compositional data</em>, when the dependent-Y variable is practically a sum total of contribution from multiple components.</p>
</blockquote>
<h2>Introduction</h2>
<p>Dirichlet regression can be used to predict the ratio in which the sum total X (demand/forecast/estimate) can be distributed among the component <span class="math inline"><em>Y</em><em>s</em></span>. It is practically a case where there are multiple dependent ‘Y’ variables and one predictor <span class="math inline"><em>X</em></span> variable, whose sum is distributed among the <span class="math inline"><em>Y</em><em>s</em></span> .</p>
<p>A couple of possible real-world examples could be as follows:</p>
<ol style="list-style-type: decimal">
<li><p>Total demand of a product A in a multi-facility manufacturing organization is actually a sum of demand of product A from <em>n</em> individual factories of the organization. Given the total demand, we are interested to know in what proportions the <em>n</em> factories contributed.</p></li>
<li><p>The total car sales in the US is a sum of car sales from 50+ individual car brands. In case we know the total projected car sales in the US, the proportional contribution from the individual brands can be predicted using <em>Dirichlet regression</em>.</p></li>
<li><p>The demand of a product is actually the sum total of demand of 4 different models (variants) of the same product.</p></li>
</ol>
<p>In either case, the dependent Y variables, which are the contributions from each component, should be converted to fractions summing up to 1. It is the job of <code>DirichReg()</code> to predict these fractions when the sum total <code>X</code> is known.</p>
<p>The code shown below can model, predict and visualize multiple Y Variables</p>
<h2>1. Data Preparation</h2>
<p>Prepare the test and training samples. Make the dirichlet Reg data on Y’s.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span> (DirichletReg)
inputData <-<span class="st"> </span>ArcticLake <span class="co"># plug-in your data here.</span>
<span class="kw">set.seed</span>(<span class="dv">100</span>)
train <-<span class="st"> </span><span class="kw">sample</span> (<span class="dv">1</span>:<span class="kw">nrow</span> (inputData), <span class="kw">round</span> (<span class="fl">0.7</span>*<span class="kw">nrow</span> (inputData))) <span class="co"># 70% training sample</span>
inputData_train <-<span class="st"> </span>inputData [train, ] <span class="co"># training Data</span>
inputData_test <-<span class="st"> </span>inputData [-train, ] <span class="co"># test Data</span>
inputData$Y <-<span class="st"> </span><span class="kw">DR_data</span> (inputData[,<span class="dv">1</span>:<span class="dv">3</span>]) <span class="co"># prepare the Y's</span>
inputData_train$Y <-<span class="st"> </span><span class="kw">DR_data</span> (inputData_train[,<span class="dv">1</span>:<span class="dv">3</span>])
inputData_test$Y <-<span class="st"> </span><span class="kw">DR_data</span> (inputData_test[,<span class="dv">1</span>:<span class="dv">3</span>])</code></pre></div>
<h2>2. Train the model</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Train the model. Modify the predictors as such.</span>
res1 <-<span class="st"> </span><span class="kw">DirichReg</span>(Y ~<span class="st"> </span>depth +<span class="st"> </span><span class="kw">I</span>(depth^<span class="dv">2</span>), inputData_train) <span class="co"># modify the predictors and input data here</span>
res2 <-<span class="st"> </span><span class="kw">DirichReg</span>(Y ~<span class="st"> </span>depth +<span class="st"> </span><span class="kw">I</span>(depth^<span class="dv">2</span>) |<span class="st"> </span>depth, inputData_train, <span class="dt">model=</span><span class="st">"alternative"</span>)
<span class="kw">summary</span>(res1)
<span class="co">#> Call:</span>
<span class="co">#> DirichReg(formula = Y ~ depth + I(depth^2), data = inputData_train)</span>
<span class="co">#> </span>
<span class="co">#> Standardized Residuals:</span>
<span class="co">#> Min 1Q Median 3Q Max</span>
<span class="co">#> sand -1.6372 -0.8499 -0.4344 1.0560 2.2233</span>
<span class="co">#> silt -1.0645 -0.5042 -0.0898 0.1858 1.5665</span>
<span class="co">#> clay -1.5058 -0.6494 0.0081 0.5867 1.7450</span>
<span class="co">#> </span>
<span class="co">#> ------------------------------------------------------------------</span>
<span class="co">#> Beta-Coefficients for variable no. 1: sand</span>
<span class="co">#> Estimate Std. Error z value Pr(>|z|) </span>
<span class="co">#> (Intercept) 1.8089738 1.0414098 1.737 0.0824 .</span>
<span class="co">#> depth -0.0220478 0.0458691 -0.481 0.6308 </span>
<span class="co">#> I(depth^2) 0.0002771 0.0004098 0.676 0.4988 </span>
<span class="co">#> ------------------------------------------------------------------</span>
<span class="co">#> Beta-Coefficients for variable no. 2: silt</span>
<span class="co">#> Estimate Std. Error z value Pr(>|z|)</span>
<span class="co">#> (Intercept) 4.641e-01 1.124e+00 0.413 0.680</span>
<span class="co">#> depth 4.355e-02 5.463e-02 0.797 0.425</span>
<span class="co">#> I(depth^2) 2.064e-05 5.078e-04 0.041 0.968</span>
<span class="co">#> ------------------------------------------------------------------</span>
<span class="co">#> Beta-Coefficients for variable no. 3: clay</span>
<span class="co">#> Estimate Std. Error z value Pr(>|z|)</span>
<span class="co">#> (Intercept) -1.5520413 1.1244396 -1.380 0.168</span>
<span class="co">#> depth 0.0874478 0.0578113 1.513 0.130</span>
<span class="co">#> I(depth^2) -0.0002161 0.0005433 -0.398 0.691</span>
<span class="co">#> ------------------------------------------------------------------</span>
<span class="co">#> Significance codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span>
<span class="co">#> </span>
<span class="co">#> Log-likelihood: 80.66 on 9 df (183 BFGS + 2 NR Iterations)</span>
<span class="co">#> AIC: -143.3, BIC: -131.7</span>
<span class="co">#> Number of Observations: 27</span>
<span class="co">#> Link: Log</span>
<span class="co">#> Parametrization: common</span></code></pre></div>
<p>As you can see from the summary results, the <span class="math inline"><em>β</em></span> coefficients for the <span class="math inline"><em>X</em><em>s</em></span> are computed to predict each of the <span class="math inline"><em>Y</em><em>s</em></span>.</p>
<h2>3. Fitted and Forecasts</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Predict On Training Data: Fitted Values</span>
<span class="kw">predict</span>(res1) <span class="co"># Model 1 fit</span>
<span class="co">#> sand silt clay</span>
<span class="co">#> [1,] 0.38244831 0.4564125 0.16113919</span>
<span class="co">#> [2,] 0.43736620 0.4285154 0.13411836</span>
<span class="co">#> [3,] 0.15978409 0.5177743 0.32244164</span>
<span class="co">#> [4,] 0.58529627 0.3386196 0.07608417</span>
<span class="co">#> [5,] 0.23630422 0.5094430 0.25425275</span>
<span class="co">#> .</span>
<span class="co">#> .</span>
<span class="kw">predict</span>(res2) <span class="co"># Model 2 fit</span>
<span class="kw">resid</span>(res1) <span class="co"># Residuals</span>
<span class="co"># Predict On Test Data or Forecast</span>
predicted_res1 <-<span class="st"> </span><span class="kw">predict</span>(res1, inputData_test) <span class="co"># Model 1</span>
predicted_res2 <-<span class="st"> </span><span class="kw">predict</span>(res2, inputData_test) <span class="co"># Model 2</span></code></pre></div>
<h2>4. Visualize</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Plot</span>
<span class="kw">plot</span>(<span class="kw">DR_data</span>(predicted_res2)) <span class="co"># plot test Data on model 2</span>
<span class="co"># additional plots</span>
<span class="kw">plot</span>(inputData$Y)</code></pre></div>
<p><img src='screenshots/Dirichlet-Plot.png' width='275' height='245' /></p>
<p>This page is based on the examples available in <a href="https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.pdf">Dirichlet regression vignette</a> and details about the implementation are available in <a href="http://epub.wu.ac.at/4077/1/Report125.pdf">here</a>.</p>
</div>
</div>
<div class="footer">
<hr>
<p>© 2016-17 Selva Prabhakaran. Powered by <a href="http://jekyllrb.com/">jekyll</a>,
<a href="http://yihui.name/knitr/">knitr</a>, and
<a href="http://johnmacfarlane.net/pandoc/">pandoc</a>.
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">Creative Commons License.</a>
</p>
</div>
</div> <!-- /container -->
<script src="//code.jquery.com/jquery.js"></script>
<script src="www/bootstrap.min.js"></script>
<script src="www/toc.js"></script>
<!-- MathJax Script -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Google Analytics Code -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-69351797-1', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
body {
font-family: 'Helvetica Neue', Roboto, Arial, sans-serif;
font-size: 16px;
line-height: 27px;
font-weight: 400;
}
blockquote p {
line-height: 1.75;
color: #717171;
}
.well li{
line-height: 28px;
}
li.dropdown-header {
display: block;
padding: 0px;
font-size: 14px;
}
</style>
</body>
</html>