-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathProbit-Regression-With-R.html
329 lines (294 loc) · 18.1 KB
/
Probit-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
<!DOCTYPE html>
<html>
<head>
<title>Probit 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>Probit Regression</h1>
<blockquote>
<p>Probit regression can used to solve binary classification problems, just like logistic regression.</p>
</blockquote>
<p>While logistic regression used a cumulative logistic function, probit regression uses a normal cumulative density function for the estimation model. Specifying a probit model is similar to logistic regression, i.e. using the <code>glm()</code> function but with <code>family</code> argument set to <code>binomial(link="probit")</code>.</p>
<h2>Example Problem</h2>
<p>Lets use the same example from <a href="Logistic-Regression-With-R.html">logistic regression</a> and try to predict if an individual will earn more than $50K. So before running the code below that builds the probit model, you will have to follow the steps from <a href="http://r-statistics.co/Logistic-Regression-With-R.html#Create%20Training%20and%20Test%20Samples">logistic regression</a> to import and prepare the training and test data.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">probitMod <-<span class="st"> </span><span class="kw">glm</span>(ABOVE50K ~<span class="st"> </span>RELATIONSHIP +<span class="st"> </span>AGE +<span class="st"> </span>CAPITALGAIN +<span class="st"> </span>OCCUPATION +<span class="st"> </span>EDUCATIONNUM, <span class="dt">data=</span>trainingData, <span class="dt">family=</span><span class="kw">binomial</span>(<span class="dt">link=</span><span class="st">"probit"</span>)) <span class="co"># build the logit model</span>
predicted <-<span class="st"> </span><span class="kw">predict</span>(probitMod, testData, <span class="dt">type=</span><span class="st">"response"</span>) <span class="co"># predict the probability scores</span>
<span class="kw">summary</span>(probitMod) <span class="co"># model summary</span>
<span class="co">#> Call:</span>
<span class="co">#> glm(formula = ABOVE50K ~ RELATIONSHIP + AGE + CAPITALGAIN + OCCUPATION + </span>
<span class="co">#> EDUCATIONNUM, family = binomial(link = "probit"), data = trainingData)</span>
<span class="co">#> </span>
<span class="co">#> Deviance Residuals: </span>
<span class="co">#> Min 1Q Median 3Q Max </span>
<span class="co">#> -4.5104 -0.5506 -0.0005 0.6515 3.6745 </span>
<span class="co">#> </span>
<span class="co">#> Coefficients:</span>
<span class="co">#> Estimate Std. Error z value Pr(>|z|) </span>
<span class="co">#> (Intercept) -2.590e+00 1.401e-01 -18.492 < 2e-16 ***</span>
<span class="co">#> RELATIONSHIP Not-in-family -1.330e+00 4.039e-02 -32.929 < 2e-16 ***</span>
<span class="co">#> RELATIONSHIP Other-relative -1.565e+00 1.406e-01 -11.135 < 2e-16 ***</span>
<span class="co">#> RELATIONSHIP Own-child -1.986e+00 8.785e-02 -22.611 < 2e-16 ***</span>
<span class="co">#> RELATIONSHIP Unmarried -1.466e+00 6.308e-02 -23.241 < 2e-16 ***</span>
<span class="co">#> RELATIONSHIP Wife 1.758e-01 6.387e-02 2.753 0.005910 ** </span>
<span class="co">#> AGE 1.523e-02 1.358e-03 11.214 < 2e-16 ***</span>
<span class="co">#> CAPITALGAIN 1.744e-04 9.158e-06 19.045 < 2e-16 ***</span>
<span class="co">#> OCCUPATION Adm-clerical 4.955e-01 9.583e-02 5.170 2.34e-07 ***</span>
<span class="co">#> OCCUPATION Armed-Forces 7.364e-01 1.034e+00 0.712 0.476343 </span>
<span class="co">#> OCCUPATION Craft-repair 7.247e-01 9.246e-02 7.838 4.59e-15 ***</span>
<span class="co">#> OCCUPATION Exec-managerial 1.141e+00 9.117e-02 12.520 < 2e-16 ***</span>
<span class="co">#> OCCUPATION Farming-fishing 7.620e-02 1.237e-01 0.616 0.537808 </span>
<span class="co">#> OCCUPATION Handlers-cleaners 3.048e-01 1.295e-01 2.353 0.018599 * </span>
<span class="co">#> OCCUPATION Machine-op-inspct 4.044e-01 1.060e-01 3.816 0.000136 ***</span>
<span class="co">#> OCCUPATION Other-service 8.727e-02 1.098e-01 0.795 0.426548 </span>
<span class="co">#> OCCUPATION Priv-house-serv -1.605e+00 1.245e+00 -1.289 0.197400 </span>
<span class="co">#> OCCUPATION Prof-specialty 9.801e-01 9.354e-02 10.477 < 2e-16 ***</span>
<span class="co">#> OCCUPATION Protective-serv 8.934e-01 1.271e-01 7.031 2.06e-12 ***</span>
<span class="co">#> OCCUPATION Sales 8.024e-01 9.325e-02 8.605 < 2e-16 ***</span>
<span class="co">#> OCCUPATION Tech-support 9.705e-01 1.157e-01 8.387 < 2e-16 ***</span>
<span class="co">#> OCCUPATION Transport-moving 6.133e-01 1.054e-01 5.821 5.86e-09 ***</span>
<span class="co">#> EDUCATIONNUM 1.575e-01 7.696e-03 20.464 < 2e-16 ***</span>
<span class="co">#> ---</span>
<span class="co">#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span>
<span class="co">#> </span>
<span class="co">#> (Dispersion parameter for binomial family taken to be 1)</span>
<span class="co">#> </span>
<span class="co">#> Null deviance: 15216.0 on 10975 degrees of freedom</span>
<span class="co">#> Residual deviance: 8753.6 on 10953 degrees of freedom</span>
<span class="co">#> AIC: 8799.6</span>
<span class="co">#> </span>
<span class="co">#> Number of Fisher Scoring iterations: 8</span></code></pre></div>
<p>Almost all the <span class="math inline"><em>X</em><em>s</em></span> have p values less than 0.05. So the significance of predictors should be of any concern here. It is okay to ignore few category levels of <code>OCCUPATION</code> not being significant, as most of the other levels have acceptable p values.</p>
<h2>Model Diagnostics</h2>
<p>The model diagnostics for probit models are the same as seen in case of <a href="http://r-statistics.co/Logistic-Regression-With-R.html#Model%20Diagnostics">logistic regression</a>.</p>
<h4>Determine Optimal Cutoff</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(InformationValue)
optCutOff <-<span class="st"> </span><span class="kw">optimalCutoff</span>(testData$ABOVE50K, predicted_scores)[<span class="dv">1</span>]
<span class="co">#=> [1] 0.88</span></code></pre></div>
<h4>ROC Curve</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plotROC</span>(testData$ABOVE50K, predicted_scores)</code></pre></div>
<p><img src='screenshots/ROC-Curve-Probit.png' width='609' height='513' /></p>
<p>The area under ROC curve is slightly higher than that of the logit model’s. Therefore this could be the preferred model.</p>
<h4>Concordance</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">Concordance</span>(testData$ABOVE50K, predicted)
<span class="co">#> $Concordance</span>
<span class="co">#> [1] 0.891276</span></code></pre></div>
<p>Concordance is numerially similar to that of the logit model’s.</p>
<h4>Misclassification Error</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">misClassError</span>(testData$ABOVE50K, predicted_scores, <span class="dt">threshold =</span> optCutOff)
<span class="co">#=> 0.0894</span></code></pre></div>
<p>The misclassification error is marginally higher than that of <a href="http://r-statistics.co/Logistic-Regression-With-R.html#Model%20Diagnostics">logit model</a>.</p>
<h4>Specificity and Sensitivity</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">sensitivity</span>(testData$ABOVE50K, predicted_scores, <span class="dt">threshold =</span> optCutOff)
<span class="co">#> 0.359541</span>
<span class="kw">specificity</span>(testData$ABOVE50K, predicted_scores, <span class="dt">threshold =</span> optCutOff)
<span class="co">#> 0.9780574</span></code></pre></div>
<p>The ability to detect the events (i.e. persons earning more than $50K), is 5% higher than that of logit model with same formula. But the specificity that is pretty high is marginally lower compared to logit.</p>
<h4>Confusion Matrix</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">confusionMatrix</span>(testData$ABOVE50K, predicted_scores, <span class="dt">threshold =</span> optCutOff)
<span class="co"># The columns are actuals, while rows are predicteds.</span>
<span class="co">#> 0 1</span>
<span class="co">#> 0 18810 1507</span>
<span class="co">#> 1 422 846</span></code></pre></div>
</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>