-
Notifications
You must be signed in to change notification settings - Fork 16
/
Multinomial-Regression-With-R.html
338 lines (299 loc) · 19.2 KB
/
Multinomial-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
334
335
336
337
338
<!DOCTYPE html>
<html>
<head>
<title>Multinomial 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>Multinomial Regression</h1>
<blockquote>
<p>Multinomial regression is much similar to logistic regression but is applicable when the response variable is a nominal categorical variable with more than 2 levels.</p>
</blockquote>
<h2>Introduction</h2>
<p>Multinomial logistic regression can be implemented with <code>mlogit()</code> from mlogit package and <code>multinom()</code> from <code>nnet</code> package. We will use the latter for this example.</p>
<h2>Example: Predict Choice of Contraceptive Method</h2>
<p>In this example, we will try to predict the choice of contraceptive preferred by women <em>(1=No-use, 2=Long-term, 3=Short-term)</em>. We have the education, work, religion, number of children, media exposure and standard of living as variables available in the <a href="http://archive.ics.uci.edu/ml/datasets/Contraceptive+Method+Choice">cmc data</a>. In this example, we will model the choice of contraceptive method <code>cmc</code> as a function of all these variables.</p>
<h2>Import Data</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cmcData <-<span class="st"> </span><span class="kw">read.csv</span>(<span class="st">"http://archive.ics.uci.edu/ml/machine-learning-databases/cmc/cmc.data"</span>, <span class="dt">stringsAsFactors=</span><span class="ot">FALSE</span>, <span class="dt">header=</span>F)
<span class="kw">colnames</span>(cmcData) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"wife_age"</span>, <span class="st">"wife_edu"</span>, <span class="st">"hus_edu"</span>, <span class="st">"num_child"</span>, <span class="st">"wife_rel"</span>, <span class="st">"wife_work"</span>, <span class="st">"hus_occu"</span>, <span class="st">"sil"</span>, <span class="st">"media_exp"</span>, <span class="st">"cmc"</span>)
<span class="kw">head</span>(cmcData)
<span class="co">#> wife_age wife_edu hus_edu num_child wife_rel wife_work hus_occu sil media_exp cmc</span>
<span class="co">#> 1 24 2 3 3 1 1 2 3 0 1</span>
<span class="co">#> 2 45 1 3 10 1 1 3 4 0 1</span>
<span class="co">#> 3 43 2 3 7 1 1 3 4 0 1</span>
<span class="co">#> 4 42 3 2 9 1 1 3 3 0 1</span>
<span class="co">#> 5 36 3 3 8 1 1 3 2 0 1</span>
<span class="co">#> 6 19 4 4 0 1 1 3 3 0 1</span></code></pre></div>
<h2>Convert Numerics to Factors</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cmcData$wife_edu <-<span class="st"> </span><span class="kw">factor</span>(cmcData$wife_edu, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$wife_edu)))
cmcData$hus_edu <-<span class="st"> </span><span class="kw">factor</span>(cmcData$hus_edu, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$hus_edu)))
cmcData$wife_rel <-<span class="st"> </span><span class="kw">factor</span>(cmcData$wife_rel, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$wife_rel)))
cmcData$wife_work <-<span class="st"> </span><span class="kw">factor</span>(cmcData$wife_work, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$wife_work)))
cmcData$hus_occu <-<span class="st"> </span><span class="kw">factor</span>(cmcData$hus_occu, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$hus_occu)))
cmcData$sil <-<span class="st"> </span><span class="kw">factor</span>(cmcData$sil, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$sil)))
cmcData$media_exp <-<span class="st"> </span><span class="kw">factor</span>(cmcData$media_exp, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$media_exp)))
cmcData$cmc <-<span class="st"> </span><span class="kw">factor</span>(cmcData$cmc, <span class="dt">levels=</span><span class="kw">sort</span>(<span class="kw">unique</span>(cmcData$cmc)))</code></pre></div>
<h2>Create Training and Test Data</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Prepare Training and Test Data</span>
<span class="kw">set.seed</span>(<span class="dv">100</span>)
trainingRows <-<span class="st"> </span><span class="kw">sample</span>(<span class="dv">1</span>:<span class="kw">nrow</span>(cmcData), <span class="fl">0.7</span>*<span class="kw">nrow</span>(cmcData))
training <-<span class="st"> </span>cmcData[trainingRows, ]
test <-<span class="st"> </span>cmcData[-trainingRows, ]</code></pre></div>
<h2>Build Multinomial Model</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(nnet)
multinomModel <-<span class="st"> </span><span class="kw">multinom</span>(cmc ~<span class="st"> </span>., <span class="dt">data=</span>training) <span class="co"># multinom Model</span>
<span class="kw">summary</span> (multinomModel) <span class="co"># model summary</span>
<span class="co">#> Call:</span>
<span class="co">#> multinom(formula = cmc ~ ., data = training)</span>
<span class="co">#> </span>
<span class="co">#> Coefficients:</span>
<span class="co">#> (Intercept) wife_age wife_edu2 wife_edu3 wife_edu4 hus_edu2 hus_edu3</span>
<span class="co">#> 2 -1.5937363 -0.04360644 1.07871567 2.0445226 2.835641 -1.407238 -1.268765</span>
<span class="co">#> 3 0.4376064 -0.10923832 0.03095292 0.4308403 0.979347 1.073331 1.150374</span>
<span class="co">#> hus_edu4 num_child wife_rel1 wife_work1 hus_occu2 hus_occu3 hus_occu4</span>
<span class="co">#> 2 -1.3102661 0.3060657 -0.4455628 0.1165996 -0.4943500 -0.40723995 1.2664442</span>
<span class="co">#> 3 0.8607095 0.3376620 -0.2072181 0.3427517 -0.1950799 0.04609764 0.5596847</span>
<span class="co">#> sil2 sil3 sil4 media_exp1</span>
<span class="co">#> 2 0.81445361 1.2655842 1.3311827 -0.2440084</span>
<span class="co">#> 3 0.03657688 0.3155116 0.5562075 -0.9285685</span>
<span class="co">#> Std. Errors:</span>
<span class="co">#> (Intercept) wife_age wife_edu2 wife_edu3 wife_edu4 hus_edu2 hus_edu3</span>
<span class="co">#> 2 0.9964378 0.01485064 0.5520832 0.5649966 0.5834594 0.6270468 0.5823429</span>
<span class="co">#> 3 0.9225193 0.01400097 0.3181759 0.3368472 0.3629088 0.6885676 0.6837955</span>
<span class="co">#> hus_edu4 num_child wife_rel1 wife_work1 hus_occu2 hus_occu3 hus_occu4</span>
<span class="co">#> 2 0.5886178 0.05094430 0.2391401 0.2001434 0.2473945 0.2444405 0.6986301</span>
<span class="co">#> 3 0.6915629 0.04595659 0.2373718 0.1814554 0.2302729 0.2226137 0.6189151</span>
<span class="co">#> sil2 sil3 sil4 media_exp1</span>
<span class="co">#> 2 0.5462033 0.5229496 0.5268553 0.4951397</span>
<span class="co">#> 3 0.3106383 0.2907037 0.2943716 0.3819526</span>
<span class="co">#> </span>
<span class="co">#> Residual Deviance: 1930.658 </span>
<span class="co">#> AIC: 2002.658</span></code></pre></div>
<h2>Predict on Test Data</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">predicted_scores <-<span class="st"> </span><span class="kw">predict</span> (multinomModel, test, <span class="st">"probs"</span>) <span class="co"># predict on new data</span>
<span class="co">#> 1 2 3</span>
<span class="co">#> 6 0.2699230 0.18691129 0.54316572</span>
<span class="co">#> 9 0.3626476 0.08523814 0.55211422</span>
<span class="co">#> 10 0.7564912 0.19409005 0.04941879</span>
<span class="co">#> 12 0.7680439 0.05851352 0.17344257</span>
<span class="co">#> 14 0.8961808 0.04747638 0.05634281</span>
<span class="co">#> 17 0.6677357 0.23683800 0.09542632</span>
<span class="co">#> .</span>
<span class="co">#> .</span>
<span class="co">#> 1464 0.5523515 0.02851988 0.4191287</span>
<span class="co">#> 1471 0.1816340 0.41055467 0.4078114</span>
<span class="co">#> 1472 0.5369837 0.16864237 0.2943739</span>
predicted_class <-<span class="st"> </span><span class="kw">predict</span> (multinomModel, test)
<span class="co">#> [1] 3 3 1 1 1 1</span>
<span class="co">#> Levels: 1 2 3</span></code></pre></div>
<h2>Confusion Matrix and Misclassification Error</h2>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">table</span>(predicted_class, test$cmc)
<span class="co">#> predicted_class 1 2 3</span>
<span class="co">#> 1 112 26 58</span>
<span class="co">#> 2 19 37 21</span>
<span class="co">#> 3 55 39 75</span>
<span class="kw">mean</span>(<span class="kw">as.character</span>(predicted_class) !=<span class="st"> </span><span class="kw">as.character</span>(test$cmc))
<span class="co">#=> 0.4932127</span></code></pre></div>
<p>A misclassification error of 49.3% is probably too high. May be it can be improved by improving the model terms or may be the variables are not as good in explaining the contraceptive method used. Either ways, I would encourage the investigator to try other ML approaches as well for this problem.</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>