-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulacao.html
1440 lines (1349 loc) · 180 KB
/
Simulacao.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Gabriel Agostini Orso" />
<title>Geração de Números Aleatórios</title>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script src="site_libs/navigation-1.1/codefolding.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ background-color: #f8f8f8; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #204a87; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #8f5902; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #204a87; font-weight: bold; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #ce5c00; font-weight: bold; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
.sourceCode .row {
width: 100%;
}
.sourceCode {
overflow-x: auto;
}
.code-folding-btn {
margin-right: -30px;
}
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark the anchor link active (and if it's in a dropdown, also mark that active)
var dropdown = menuAnchor.closest('li.dropdown');
if (window.bootstrap) { // Bootstrap 4+
menuAnchor.addClass('active');
dropdown.find('> .dropdown-toggle').addClass('active');
} else { // Bootstrap 3
menuAnchor.parent().addClass('active');
dropdown.addClass('active');
}
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="ggplot.html">ggplot</a>
</li>
<li>
<a href="MQO.html">MQO</a>
</li>
<li>
<a href="dplyr.html">dplyr</a>
</li>
<li>
<a href="Simulacao.html">Simulação</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/gabrielorso/gabrielorso.github.io">
<span class="fa fa-github fa-lg"></span>
GitHub
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<div class="btn-group pull-right float-right">
<button type="button" class="btn btn-default btn-xs btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>
<h1 class="title toc-ignore">Geração de Números Aleatórios</h1>
<h4 class="author">Gabriel Agostini Orso</h4>
</div>
<div id="introdução" class="section level1" number="1">
<h1><span class="header-section-number">1</span> INTRODUÇÃO</h1>
<p>Esse trabalho foi requisito para aprovação na disciplina <strong>CE089 - Estatística Computacional II</strong>, ofertada para o curso de Estatística da Universidade Federal do Paraná, e ministrada pelos professores Paulo Justiniano Ribeiro Júnior e Walmes Zeviani. O objetivo do trabalho foi gerar amostras aleatórias de uma certa distribuição de probabilidade com um parâmetro <span class="math inline">\(\theta\)</span>, e a partir dessas amostras realizar a estimativa pontual e intervalar de <span class="math inline">\(\hat{\theta}\)</span> via máxima verossimilhança, jackknife e bootstrap.</p>
<hr />
<p>Em linhas gerais, esses são os principais pontos do trabalho, que eu gostaria que o leitor se atentasse para os seguintes pontos:</p>
<ul>
<li><p>A geração de números aleatórios a partir do algoritmo de Metropolis-Hastings</p></li>
<li><p>O tamanho do intervalo de confiança é diferente para diferentes valores de <span class="math inline">\(\theta\)</span></p></li>
<li><p>O intervalo de confiança diminui à medida que aumenta o tamanho da amostra</p></li>
</ul>
<hr />
<p>A distribuição de probabilidade escolhida foi</p>
<span class="math display" id="eq:f">\[\begin{equation}
F(x) = 1-e^{-x^2 \over 2\theta^2} \quad , \quad x > 0 , \enspace \theta > 0
\tag{1.1}
\end{equation}\]</span>
<p>Veja que <a href="#eq:f">(1.1)</a> já é a distribuição acumulada de <span class="math inline">\(x\)</span>. Já a densidade pode ser obtida derivando a função em relação a <span class="math inline">\(x\)</span>:</p>
<span class="math display" id="eq:fder">\[\begin{equation}
f(x) = {dF(x) \over dx} = {d \over dx} \Big[1-e^{-x^2 \over 2\theta^2} \Big] = {x e^{ {-x^2 \over 2\theta^2}} \over \theta^2}
\tag{1.2}
\end{equation}\]</span>
<p>Na <a href="#fig:f2">1.1</a> estão apresentadas as curvas de densidade e acumuladas da distribuição escolhida para diferentes valores do parâmetro <span class="math inline">\(\theta\)</span>.</p>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:f2"></span>
<img src="Simulacao_files/figure-html/f2-1.png" alt="Possíveis parametrizações da distribuição de probabilidade." width="768" />
<p class="caption">
Figure 1.1: Possíveis parametrizações da distribuição de probabilidade.
</p>
</div>
<p>Veja que nosso objetivo é gerar valores de <span class="math inline">\(x\)</span> pertencentes à essa distribuição com um determinado valor de <span class="math inline">\(\theta\)</span> fixado, e a partir dessa amostra de <span class="math inline">\(x\)</span> <em>encontrar o valor de <span class="math inline">\(\theta\)</span> que foi usado para gerar esses dados</em>, denotado por <span class="math inline">\(\hat{\theta}\)</span>. Portanto <span class="math inline">\(\theta\)</span> se refere ao valor paramétrico, verdadeiro, aquele que gerou os dados, e <span class="math inline">\(\hat{\theta}\)</span> é a nossa estimativa do valor verdadeiro a partir dos dados. Como é uma estimativa, há uma incerteza associada à essa estimativa. Essa incerteza é a base dos intervalos de confiança.</p>
<p>Vamos tentar estimar o parâmetro <span class="math inline">\(\theta\)</span> e sua incerteza a partir de três métodos. O primeiro é a partir da máxima verossimilhança, o segundo a partir de jackknife, e o terceiro a partir de bootstrap. Também vamos repetir esse experimento inúmeras vezes e avaliar o comportamento da estimativa para um grande número de repetições, o que também caracteriza esse trabalho como um estudo de Monte Carlo.</p>
<hr />
<div id="gerador-de-números-aleatórios" class="section level2" number="1.1">
<h2><span class="header-section-number">1.1</span> Gerador de números aleatórios</h2>
<p>Como gerar números aleatórios dessa distribuição? no R, diversas distribuições de probabilidade possuem geradores de números aleatórios já implementados no pacote base: <code>rnorm()</code>, <code>rexp()</code>, <code>rchisq()</code>, etc. A distribuição desse trabalho, no entanto, não possui tal função implementada, e vamos utilizar um algoritmo chamado Metropolis-Hastings para amostrar dessa distribuição. Tudo que precisamos é a função densidade <a href="#eq:fder">(1.2)</a>.</p>
<p>De uma maneira resumida, o algoritmo de Metropolis-Hastings usa uma outra distribuição de probabilidade (chamada de <em>proposal</em>) para gerar amostras da distribuição desejada. Se certas condições forem satisfeitas, <a href="http://cursos.leg.ufpr.br/ce089/08_MCMC.html">com o tempo esse processo irá convergir para a distribuição estacionária de uma cadeia de Markov</a>, que não por acaso será a própria distribuição desejada. Veja que devemos ser capazes de gerar números aleatórios da <em>proposal</em> para que isso funcione. Aqui, utilizei uma distribuição qui-quadrado (<span class="math inline">\(\chi^2\)</span>) como <em>proposal</em>.</p>
<p>Como é uma cadeia de Markov que precisa entrar em estacionariedade, é comum no método “jogar fora” as amostras iniciais (burnin), de forma a só amostrar da distribuição quando o processo entrar em estacionariedade. Além disso, por ser um processo markoviano, cada amostra gerada não é independente da anterior, por isso também é comum aproveitar somente 1 a cada x amostras geradas (thinning) para amenizar essa dependência entre as observações.</p>
<p>Abaixo está minha implementação do algoritmo de Metropolis-Hastings para o problema.</p>
<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="co"># Proposal</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>g <span class="ot"><-</span> <span class="cf">function</span>(x, df, ...) {<span class="fu">dchisq</span>(x, df, ...)}</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a>rfun <span class="ot"><-</span> <span class="cf">function</span>(n, theta, dprop, <span class="at">burnin =</span> <span class="dv">1000</span>, <span class="at">thinning =</span> <span class="dv">10</span>, <span class="at">x0 =</span> <span class="fu">rchisq</span>(<span class="dv">1</span>,<span class="dv">3</span>), ...) {</span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a> <span class="co"># n: número de amostras geradas da distribuição</span></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a> </span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a> <span class="co"># theta: parâmetro da distribuição</span></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a> </span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a> <span class="co"># dprop: função densidade da distribuição proposta</span></span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a> </span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a> <span class="co"># burnin: número de amostras de aquecimento. O padrão é 1000</span></span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a> </span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a> <span class="co"># thinning: passo de seleção de amostras para amenizar a autocorrelação. O padrão é 10</span></span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a> </span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a> <span class="co"># x0: valor inicial da distribuição proposta. O padrão é rchisq(n = 1, df = 3)</span></span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a> </span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a> <span class="co"># ...: demais argumentos passados para a função dprop</span></span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a></span>
<span id="cb1-19"><a href="#cb1-19" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">numeric</span>(n)</span>
<span id="cb1-20"><a href="#cb1-20" tabindex="-1"></a> x[<span class="dv">1</span>] <span class="ot"><-</span> x0</span>
<span id="cb1-21"><a href="#cb1-21" tabindex="-1"></a> k <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb1-22"><a href="#cb1-22" tabindex="-1"></a> </span>
<span id="cb1-23"><a href="#cb1-23" tabindex="-1"></a> <span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">2</span><span class="sc">:</span>(n<span class="sc">*</span>thinning<span class="sc">+</span>burnin)) {</span>
<span id="cb1-24"><a href="#cb1-24" tabindex="-1"></a> </span>
<span id="cb1-25"><a href="#cb1-25" tabindex="-1"></a> y <span class="ot"><-</span> <span class="fu">rchisq</span>(<span class="dv">1</span>,x[i<span class="dv">-1</span>])</span>
<span id="cb1-26"><a href="#cb1-26" tabindex="-1"></a> </span>
<span id="cb1-27"><a href="#cb1-27" tabindex="-1"></a> alpha <span class="ot"><-</span> <span class="fu">exp</span>((<span class="fu">log</span>(<span class="fu">dfun</span>(y, theta))<span class="sc">+</span><span class="fu">log</span>(<span class="fu">g</span>(x[i<span class="dv">-1</span>], y))) <span class="sc">-</span></span>
<span id="cb1-28"><a href="#cb1-28" tabindex="-1"></a> (<span class="fu">log</span>(<span class="fu">dfun</span>(x[i<span class="dv">-1</span>], theta))<span class="sc">+</span><span class="fu">log</span>(<span class="fu">g</span>(y, x[i<span class="dv">-1</span>]))))</span>
<span id="cb1-29"><a href="#cb1-29" tabindex="-1"></a> </span>
<span id="cb1-30"><a href="#cb1-30" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">runif</span>(<span class="dv">1</span>) <span class="sc"><=</span> alpha) {</span>
<span id="cb1-31"><a href="#cb1-31" tabindex="-1"></a> x[i] <span class="ot"><-</span> y</span>
<span id="cb1-32"><a href="#cb1-32" tabindex="-1"></a> k <span class="ot"><-</span> k<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb1-33"><a href="#cb1-33" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb1-34"><a href="#cb1-34" tabindex="-1"></a> x[i] <span class="ot"><-</span> x[i<span class="dv">-1</span>]</span>
<span id="cb1-35"><a href="#cb1-35" tabindex="-1"></a> }</span>
<span id="cb1-36"><a href="#cb1-36" tabindex="-1"></a> </span>
<span id="cb1-37"><a href="#cb1-37" tabindex="-1"></a> }</span>
<span id="cb1-38"><a href="#cb1-38" tabindex="-1"></a> </span>
<span id="cb1-39"><a href="#cb1-39" tabindex="-1"></a> xn <span class="ot"><-</span> x[<span class="sc">-</span><span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span>burnin)]</span>
<span id="cb1-40"><a href="#cb1-40" tabindex="-1"></a> xn <span class="ot"><-</span> xn[<span class="fu">seq</span>(<span class="dv">0</span>,<span class="fu">length</span>(xn), thinning)]</span>
<span id="cb1-41"><a href="#cb1-41" tabindex="-1"></a> </span>
<span id="cb1-42"><a href="#cb1-42" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">list</span>(<span class="at">mcmc =</span> xn, <span class="at">k =</span> k<span class="sc">/</span>(n<span class="sc">*</span>thinning<span class="sc">+</span>burnin)))</span>
<span id="cb1-43"><a href="#cb1-43" tabindex="-1"></a></span>
<span id="cb1-44"><a href="#cb1-44" tabindex="-1"></a>}</span></code></pre></div>
<p>Na Figura <a href="#fig:gna">1.2</a> há um exemplo comparando a distribuição teórica de parâmetro <span class="math inline">\(\theta = 40\)</span> com a distribuição empírica construída a partir de 10000 amostras geradas pelo algoritmo de Metropolis-Hastings. As curvas estão bem parecidas, então aparentemente o algoritmo funciona bem. O painel inferior direito da figura mostra a função de autocorrelação, e aparentemente um valor de raleio de 50 elimina a dependência entre observações.</p>
<p>Foi utilizado um burnin de 1000 amostras, e um thinning de 50. Isso quer dizer que as primeiras 1000 amostras geradas pelo algoritmo foram descartadas, e após isso, apenas uma amostra a cada 50 foi de fato utilizada. Em outras palavras, para ter uma amostra de tamanho 10000, precisamos gerar <span class="math inline">\(10000*50 + 1000 = 501000\)</span> valores da distribuição desejada.</p>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:gna"></span>
<img src="Simulacao_files/figure-html/gna-1.png" alt="Comparação entre curvas empírica e teórica da distribuição para uma amostra de tamanho 10000." width="960" />
<p class="caption">
Figure 1.2: Comparação entre curvas empírica e teórica da distribuição para uma amostra de tamanho 10000.
</p>
</div>
<hr />
</div>
</div>
<div id="estimando-theta" class="section level1" number="2">
<h1><span class="header-section-number">2</span> ESTIMANDO <span class="math inline">\(\theta\)</span></h1>
<div id="estimador-de-máxima-verossimilhança" class="section level2" number="2.1">
<h2><span class="header-section-number">2.1</span> Estimador de máxima verossimilhança</h2>
<p>Para estimar <span class="math inline">\(\hat{\theta}\)</span> a partir dos dados, usarei o estimador de máxima verossimilhança de <span class="math inline">\(\theta\)</span>. O método de máxima verossimilhança basicamente nos responde a seguinte questão: dado que esses são os dados, qual seria o valor de <span class="math inline">\(\theta\)</span> <em>que torna mais provável a geração desses dados</em>? Mais informações podem ser obtidas <a href="http://www.leg.ufpr.br/~wagner/MCIE/Tutorial/TutorialII.html">aqui</a>.</p>
<p>As funções de verissimilhança <span class="math inline">\(L(x)\)</span> e log-verossimilhança <span class="math inline">\(lL(x)\)</span> são escritas como</p>
<span class="math display">\[\begin{equation}
L(x|\theta) = \displaystyle\prod_{i=1}^{n} { x_i e^{-x^2_i \over 2\theta^2} \over \theta^2} = {\displaystyle \prod_{i=1}^n x_i e^{-x^2_i \over 2\theta^2} \over \theta^{2n}}
\end{equation}\]</span>
<span class="math display">\[\begin{equation}
\label{eq:lL}
lL(\theta | x) = log(L(x|\theta)) = \sum_{i=1}^n log(x_i) - {\displaystyle \sum_{i=1}^n x^2_i \over 2\theta^2} - 2n log(\theta)
\end{equation}\]</span>
<p>O estimador de máxima verossimilhança de <span class="math inline">\(\theta\)</span> é obtido a partir da derivada de <span class="math inline">\(lL(x)\)</span> em relação à <span class="math inline">\(\theta\)</span> e igualando a derivada a 0.</p>
<p><span class="math display">\[
U(\theta) = {d \over d\theta} [lL(\theta | x)] = {\displaystyle\sum_{i=1}^n x^2_i \over \theta^3} - {2n \over \theta}
\]</span></p>
<p><span class="math display">\[
{\displaystyle\sum_{i=1}^n x^2_i \over \hat{\theta}^3} - {2n \over \hat{\theta}} = 0
\]</span></p>
<span class="math display" id="eq:esttheta">\[\begin{equation}
\hat{\theta} = \Bigg( {\displaystyle\sum_{i=1}^n x^2_i \over 2n } \Bigg)^{1 \over 2}
\tag{2.1}
\end{equation}\]</span>
<p>Ou seja, dado meu conjunto de dados <span class="math inline">\(x\)</span>, <a href="#eq:esttheta">(2.1)</a> é o estimador de máxima verossimilhança de <span class="math inline">\(\theta\)</span>. Abaixo está sua implementação em R. Usarei essa função para estimar <span class="math inline">\(\hat{\theta}\)</span> ao longo do trabalho.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>esttheta <span class="ot"><-</span> <span class="cf">function</span>(amostra, ...) (<span class="fu">sum</span>(amostra<span class="sc">^</span><span class="dv">2</span>)<span class="sc">/</span>(<span class="dv">2</span><span class="sc">*</span><span class="fu">length</span>(amostra)))<span class="sc">^</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>)</span></code></pre></div>
</div>
<div id="variância-do-estimador-de-máxima-verossimilhança" class="section level2" number="2.2">
<h2><span class="header-section-number">2.2</span> Variância do estimador de máxima verossimilhança</h2>
<p>A Variância do estimador é definida como <span class="math inline">\(V(\theta) = -I(\theta)^{-1}\)</span>. A informação observada de Fisher para essa distribuição é dada por</p>
<p><span class="math display">\[
I(\theta) = {d \over d\theta} [U(\theta)] = {-3\displaystyle \sum_{i=1}^n x^2_i \over \theta^4} + {2n \over \theta^2} = {-3\displaystyle\sum_{i=1}^n x^2_i + 2n\theta^2 \over \theta^4}
\]</span></p>
<span class="math display" id="eq:var">\[\begin{equation}
V(\theta) = -I(\theta)^{-1} = -\Bigg( {-3\displaystyle\sum_{i=1}^n x^2_i + 2n\theta^2 \over \theta^4} \Bigg)^{-1} = { \theta^4 \over 3\displaystyle\sum_{i=1}^n x^2_i - 2n\theta^2 }
\tag{2.2}
\end{equation}\]</span>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>vartheta <span class="ot"><-</span> <span class="cf">function</span>(amostra, that, ...) {</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a> that<span class="sc">^</span><span class="dv">4</span><span class="sc">/</span>(<span class="dv">3</span><span class="sc">*</span><span class="fu">sum</span>(amostra<span class="sc">^</span><span class="dv">2</span>)<span class="sc">-</span><span class="dv">2</span><span class="sc">*</span><span class="fu">length</span>(amostra)<span class="sc">*</span>that<span class="sc">^</span><span class="dv">2</span>) </span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a>}</span></code></pre></div>
<p>Aqui utilizei a informação observada, e não esperada, de Fisher. De qualquer forma, a variância do estimador não foi utilizada, pois o intervalo de confiança foi construído a partir da Deviance, apresentada abaixo.</p>
</div>
<div id="intevalo-de-confiança-baseado-na-razão-de-verossimilhança" class="section level2" number="2.3">
<h2><span class="header-section-number">2.3</span> Intevalo de confiança baseado na razão de verossimilhança</h2>
<p>A deviance pode ser escrita como <span class="math inline">\(D(\theta) = -2log\big({L(\theta|x) \over L(\hat{\theta}|x)}\big)\)</span>, tal que <span class="math inline">\(D(\theta) \sim \chi^2_1\)</span>.</p>
<p><span class="math display">\[
D(\theta) = -2log\Bigg({L(\theta|x) \over L(\hat{\theta}|x)}\Bigg) = -2\Big[ lL(\theta) - lL(\hat{\theta}) \Big] =
\]</span></p>
<p><span class="math display">\[
-2 \Bigg[ \displaystyle \sum_{i=1}^n log(x_i) - {\displaystyle \sum_{i=1}^n x^2_i \over 2\theta^2} - 2nlog(\theta) - \Bigg( \displaystyle \sum_{i=1}^n log(x_i) - {\displaystyle \sum_{i=1}^n x^2_i \over 2\hat{\theta}^2} - 2nlog(\hat{\theta}) \Bigg) \Bigg] =
\]</span></p>
<p><span class="math display">\[
-2 \Bigg[ {\displaystyle\sum_{i=1}^n x^2_i \over 2\hat{\theta}^2} - {\displaystyle\sum_{i=1}^n x^2_i \over 2\theta^2} + 2nlog(\hat{\theta}) - 2nlog(\theta) \Bigg] = -2\Bigg[ {\displaystyle\sum_{i=1}^n x^2_i \over 2} \Bigg({1 \over \hat{\theta}^2} - {1 \over \theta^2} \Bigg) + 2nlog\Bigg({\hat{\theta} \over \theta} \Bigg) \Bigg]
\]</span></p>
<span class="math display" id="eq:deviance">\[\begin{equation}
D(\theta) = \displaystyle\sum_{i=1}^n x^2_i \Bigg( {1 \over \theta^2} - {1 \over \hat{\theta}^2} \Bigg) - 4nlog\Bigg( {\hat{\theta} \over \theta} \Bigg)
\tag{2.3}
\end{equation}\]</span>
<p>Portanto os valores de <span class="math inline">\(\theta\)</span> que resultem em um valor extremo de <span class="math inline">\(D(\theta)\)</span> (sabendo que <span class="math inline">\(D(\theta) \sim \chi^2_1\)</span>), serão valores não contidos pelo intervalo de confiança (Figura <a href="#fig:deviance">2.1</a>).</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>devtheta <span class="ot"><-</span> <span class="cf">function</span>(t, that, amostra, ...) {</span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> <span class="fu">sum</span>(amostra<span class="sc">^</span><span class="dv">2</span>)<span class="sc">*</span>(<span class="dv">1</span><span class="sc">/</span>t<span class="sc">^</span><span class="dv">2</span> <span class="sc">-</span> <span class="dv">1</span><span class="sc">/</span>that<span class="sc">^</span><span class="dv">2</span>) <span class="sc">-</span> <span class="dv">4</span><span class="sc">*</span><span class="fu">length</span>(amostra)<span class="sc">*</span><span class="fu">log</span>(that<span class="sc">/</span>t)</span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>}</span></code></pre></div>
<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">set.seed</span>(<span class="dv">50</span>)</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a></span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">1</span>))</span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">devtheta</span>(x, <span class="at">that =</span> <span class="dv">40</span>, <span class="fu">rfun</span>(<span class="dv">30</span>, <span class="dv">40</span>, g)<span class="sc">$</span>mcmc), <span class="at">xlim =</span> <span class="fu">c</span>(<span class="dv">30</span>, <span class="dv">50</span>),</span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a> <span class="at">xlab =</span> <span class="fu">expression</span>(theta), <span class="at">ylab =</span> <span class="st">'Deviance'</span>,</span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a> <span class="at">main =</span> <span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 40'</span>))</span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> <span class="fu">qchisq</span>(<span class="fl">0.95</span>, <span class="dv">1</span>), <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">devtheta</span>(x, <span class="at">that =</span> <span class="dv">1</span>, <span class="fu">rfun</span>(<span class="dv">30</span>, <span class="dv">1</span>, g)<span class="sc">$</span>mcmc), <span class="at">xlim =</span> <span class="fu">c</span>(<span class="fl">0.7</span>, <span class="fl">1.5</span>),</span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a> <span class="at">xlab =</span> <span class="fu">expression</span>(theta), <span class="at">ylab =</span> <span class="st">'Deviance'</span>,</span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a> <span class="at">main =</span> <span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 1'</span>))</span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> <span class="fu">qchisq</span>(<span class="fl">0.95</span>, <span class="dv">1</span>), <span class="at">col =</span> <span class="dv">2</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:deviance"></span>
<img src="Simulacao_files/figure-html/deviance-1.png" alt="Deviances para amostras de tamanho 30 utilizando valores de parâmetro 40 e 1. Os valores de theta cuja deviance esteja abaixo da linha vermelha estão contidos no intervalo de confiança." width="768" />
<p class="caption">
Figure 2.1: Deviances para amostras de tamanho 30 utilizando valores de parâmetro 40 e 1. Os valores de theta cuja deviance esteja abaixo da linha vermelha estão contidos no intervalo de confiança.
</p>
</div>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
<hr />
</div>
</div>
<div id="estudo-do-viés-e-cobertura-de-ic-para-theta." class="section level1" number="3">
<h1><span class="header-section-number">3</span> ESTUDO DO VIÉS E COBERTURA DE IC PARA <span class="math inline">\(\theta\)</span>.</h1>
<p>Foram considerados 2 cenários para a simulação:</p>
<ol style="list-style-type: decimal">
<li><p><span class="math inline">\(\theta = 40\)</span>;</p></li>
<li><p><span class="math inline">\(\theta = 1\)</span>.</p></li>
</ol>
<p>No entanto, em cada cenário foram geradas amostras aleatórias da distribuição escolhida de tamanhos 2 a 30, e depois de 50 a 1000 ao passo de 20, o que totaliza 77 tamanhos de amostra diferentes. De cada uma dessas amostras foram feitas a estimativa pontual do parâmetro (Equação <a href="#eq:esttheta">(2.1)</a>) e delimitação do intervalo de confiança utilizando máxima verossimilhança, jackknife e bootstrap. Além disso, cada uma dessas etapas foi repetida 100 vezes, a fim de avaliar a taxa real de cobertura dos intervalos de confiança por cada método para cada cenário.</p>
<p>A Figura <a href="#fig:densidades">3.1</a> apresenta a curva da densidade de probabilidade da função para os dois parâmetros escolhidos. Veja que quando <span class="math inline">\(\theta = 1\)</span>, a curva é muito mais assimétrica e com menor dispersão.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>))</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">dfun</span>(x, <span class="at">theta =</span> <span class="dv">1</span>), <span class="at">col =</span> <span class="dv">4</span>, <span class="at">xlim =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">40</span>),</span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="st">'Densidade'</span>)</span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">dfun</span>(x, <span class="at">theta =</span> <span class="dv">40</span>), <span class="at">col =</span> <span class="dv">2</span>, <span class="at">add =</span> T)</span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 1'</span>), <span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 40'</span>)),</span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>), <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>, <span class="at">text.col =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>))</span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">dfun</span>(x, <span class="at">theta =</span> <span class="dv">40</span>), <span class="at">col =</span> <span class="dv">2</span>, <span class="at">xlim =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">120</span>), <span class="at">xlab =</span> <span class="cn">NA</span>, </span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a> <span class="at">ylab =</span> <span class="st">'Densidade'</span>)</span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="fu">curve</span>(<span class="fu">dfun</span>(x, <span class="at">theta =</span> <span class="dv">1</span>), <span class="at">col =</span> <span class="dv">4</span>, <span class="at">add =</span> T)</span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 1'</span>), <span class="fu">expression</span>(theta<span class="sc">*</span><span class="st">' = 40'</span>)),</span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a> <span class="at">text.col =</span> <span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">2</span>), <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:densidades"></span>
<img src="Simulacao_files/figure-html/densidades-1.png" alt="Densidade da distribuição para os dois parâmetros testados." width="768" />
<p class="caption">
Figure 3.1: Densidade da distribuição para os dois parâmetros testados.
</p>
</div>
<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">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
<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">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>C1 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) <span class="fu">lapply</span>(n, <span class="cf">function</span>(x) <span class="fu">rfun</span>(<span class="at">n=</span>x, <span class="at">theta =</span> par_C1, <span class="at">dprop =</span> g, <span class="at">thinning =</span> <span class="dv">50</span>)<span class="sc">$</span>mcmc))</span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a>C2 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) <span class="fu">lapply</span>(n, <span class="cf">function</span>(x) <span class="fu">rfun</span>(<span class="at">n=</span>x, <span class="at">theta =</span> par_C2, <span class="at">dprop =</span> g, <span class="at">thinning =</span> <span class="dv">50</span>)<span class="sc">$</span>mcmc))</span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a></span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co"># Estimativas de theta</span></span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a>that_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(C1, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, esttheta))</span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a>that_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(C2, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, esttheta))</span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co"># médias de theta estimado para cada tamanho de amostra</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a>media_C1 <span class="ot"><-</span> <span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">mean</span>(<span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) that_C1[[y]][[x]])))</span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a>media_C2 <span class="ot"><-</span> <span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">mean</span>(<span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) that_C2[[y]][[x]])))</span></code></pre></div>
<p>Na Figura <a href="#fig:simulacoes">3.2</a> estão os valores estimados de <span class="math inline">\(\hat{\theta}\)</span> para cada tamanho de amostra e cada uma das 100 realizações de monte carlo. Os paineis à esquerda são um recorte dos da direita, limitando a um tamanho de amostra entre 2 e 30. Veja que conforme aumenta o tamanho de amostra, há uma redução da variabilidade de <span class="math inline">\(\hat{\theta}\)</span>. Ou seja, passamos a ser mais <em>confiantes</em> da nossa estimativa conforme aumenta o tamanho da amostra.</p>
<p>Veja que também que a dispersão das linhas para <span class="math inline">\(\theta = 40\)</span> é maior que quando <span class="math inline">\(\theta = 1\)</span>. Veja também que de acordo com a equação <a href="#eq:var">(2.2)</a>, a variância do estimador depende de seu valor. Portanto maiores valores de <span class="math inline">\(\theta\)</span> incorrem em uma maior variabilidade em torno de si.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">2</span>))</span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a></span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C1[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)))</span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], that_C1[[x]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span>par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C1[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="fu">plot</span>(n, media_C1, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n, that_C1[[x]], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span>par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a><span class="fu">lines</span>(n, media_C1, <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">'Média'</span>, <span class="st">'Parâmetro'</span>, <span class="st">'Simulações'</span>),</span>
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a> <span class="at">lty =</span> <span class="dv">1</span>, <span class="at">lwd =</span> <span class="dv">1</span>, <span class="at">col =</span> <span class="fu">c</span>(<span class="st">'red'</span>, <span class="st">'blue'</span>, <span class="st">'lightgrey'</span>),</span>
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a> <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>)</span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a></span>
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a></span>
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C2[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">3</span>),</span>
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)))</span>
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], that_C2[[x]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span>par_C2, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a><span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C2[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a></span>
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a><span class="fu">plot</span>(n, media_C2, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">3</span>), </span>
<span id="cb10-26"><a href="#cb10-26" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb10-27"><a href="#cb10-27" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n, that_C2[[x]], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb10-28"><a href="#cb10-28" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span>par_C2, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb10-29"><a href="#cb10-29" tabindex="-1"></a><span class="fu">lines</span>(n, media_C2, <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:simulacoes"></span>
<img src="Simulacao_files/figure-html/simulacoes-1.png" alt="Estimativas de theta para cada tamanho de amostra e simulação de monte carlo." width="768" />
<p class="caption">
Figure 3.2: Estimativas de theta para cada tamanho de amostra e simulação de monte carlo.
</p>
</div>
<div class="sourceCode" id="cb11"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
<div id="via-máxima-verossimilhança" class="section level2" number="3.1">
<h2><span class="header-section-number">3.1</span> Via Máxima Verossimilhança</h2>
<p>A partir da função Deviance (Equação <a href="#eq:deviance">(2.3)</a>), podemos calcular o intervalo de confiança da nossa estimativa <span class="math inline">\(\hat{\theta}\)</span> para cada uma das 100 realizações de Monte Carlo. A Figura <a href="#fig:ICLL">3.3</a> mostra esses intervalos para a primeira realização de Monte Carlo. As barras vermelhas representam intervalos que não contiveram o valor verdadeiro (40 e 1, linha azul).</p>
<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="co"># Utilizando máxima verossimilhança -----------</span></span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a></span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co"># Variância de theta</span></span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a>var_LL_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">vartheta</span>(<span class="at">amostra =</span> C1[[y]][[x]], <span class="at">that =</span> that_C1[[y]][[x]])))</span>
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a></span>
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a>var_LL_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">vartheta</span>(<span class="at">amostra =</span> C2[[y]][[x]], <span class="at">that =</span> that_C2[[y]][[x]])))</span>
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a></span>
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a></span>
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co"># Intervalos de confiança de 95%</span></span>
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="fu">require</span>(rootSolve)</span>
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a></span>
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a>IC_LL_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(j) {<span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), </span>
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a> <span class="cf">function</span>(y) {</span>
<span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a> </span>
<span id="cb12-15"><a href="#cb12-15" tabindex="-1"></a> LI <span class="ot"><-</span> <span class="fu">uniroot</span>(<span class="at">f =</span> <span class="cf">function</span>(x) {</span>
<span id="cb12-16"><a href="#cb12-16" tabindex="-1"></a> <span class="fu">devtheta</span>(<span class="at">t =</span> x, <span class="at">that =</span> that_C1[[j]][[y]],</span>
<span id="cb12-17"><a href="#cb12-17" tabindex="-1"></a> <span class="at">amostra =</span> C1[[j]][[y]]) <span class="sc">-</span> <span class="fu">qchisq</span>(<span class="fl">0.95</span>,<span class="dv">1</span>)},</span>
<span id="cb12-18"><a href="#cb12-18" tabindex="-1"></a> <span class="at">lower =</span> <span class="fl">0.001</span>, <span class="at">upper =</span> that_C1[[j]][[y]])<span class="sc">$</span>root</span>
<span id="cb12-19"><a href="#cb12-19" tabindex="-1"></a> </span>
<span id="cb12-20"><a href="#cb12-20" tabindex="-1"></a> LS <span class="ot"><-</span> <span class="fu">uniroot</span>(<span class="at">f =</span> <span class="cf">function</span>(x) {</span>
<span id="cb12-21"><a href="#cb12-21" tabindex="-1"></a> <span class="fu">devtheta</span>(<span class="at">t =</span> x, <span class="at">that =</span> that_C1[[j]][[y]],</span>
<span id="cb12-22"><a href="#cb12-22" tabindex="-1"></a> <span class="at">amostra =</span> C1[[j]][[y]]) <span class="sc">-</span> <span class="fu">qchisq</span>(<span class="fl">0.95</span>,<span class="dv">1</span>)},</span>
<span id="cb12-23"><a href="#cb12-23" tabindex="-1"></a> <span class="at">lower =</span> that_C1[[j]][[y]], <span class="at">upper =</span> <span class="dv">200</span>)<span class="sc">$</span>root</span>
<span id="cb12-24"><a href="#cb12-24" tabindex="-1"></a> </span>
<span id="cb12-25"><a href="#cb12-25" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">c</span>(LI, LS))</span>
<span id="cb12-26"><a href="#cb12-26" tabindex="-1"></a> }</span>
<span id="cb12-27"><a href="#cb12-27" tabindex="-1"></a> )</span>
<span id="cb12-28"><a href="#cb12-28" tabindex="-1"></a> }</span>
<span id="cb12-29"><a href="#cb12-29" tabindex="-1"></a>)</span>
<span id="cb12-30"><a href="#cb12-30" tabindex="-1"></a></span>
<span id="cb12-31"><a href="#cb12-31" tabindex="-1"></a></span>
<span id="cb12-32"><a href="#cb12-32" tabindex="-1"></a></span>
<span id="cb12-33"><a href="#cb12-33" tabindex="-1"></a>IC_LL_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(j) {<span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(y) {</span>
<span id="cb12-34"><a href="#cb12-34" tabindex="-1"></a> </span>
<span id="cb12-35"><a href="#cb12-35" tabindex="-1"></a> <span class="fu">uniroot.all</span>(<span class="at">f =</span> <span class="cf">function</span>(x) {</span>
<span id="cb12-36"><a href="#cb12-36" tabindex="-1"></a> <span class="fu">devtheta</span>(<span class="at">t =</span> x, <span class="at">that =</span> that_C2[[j]][[y]], <span class="at">amostra =</span> C2[[j]][[y]]) <span class="sc">-</span> <span class="fu">qchisq</span>(<span class="fl">0.95</span>,<span class="dv">1</span>)},</span>
<span id="cb12-37"><a href="#cb12-37" tabindex="-1"></a> <span class="at">interval=</span><span class="fu">c</span>(<span class="fl">0.0001</span>, <span class="dv">5</span>))</span>
<span id="cb12-38"><a href="#cb12-38" tabindex="-1"></a> </span>
<span id="cb12-39"><a href="#cb12-39" tabindex="-1"></a> }</span>
<span id="cb12-40"><a href="#cb12-40" tabindex="-1"></a>)}</span>
<span id="cb12-41"><a href="#cb12-41" tabindex="-1"></a>)</span></code></pre></div>
<div class="sourceCode" id="cb13"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>contem_C1 <span class="ot"><-</span> <span class="fu">sapply</span>(IC_LL_C1[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C1 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C1)</span>
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>contem_C2 <span class="ot"><-</span> <span class="fu">sapply</span>(IC_LL_C2[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C2 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C2)</span>
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a></span>
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">2</span>))</span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C1[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)),</span>
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a> <span class="at">bg =</span> <span class="st">'black'</span>)</span>
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">x1=</span>n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">y0 =</span> <span class="fu">sapply</span>(IC_LL_C1[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>])[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], </span>
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a> <span class="at">y1 =</span> <span class="fu">sapply</span>(IC_LL_C1[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">2</span>])[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>],</span>
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C1, <span class="st">'green'</span>, <span class="st">'red'</span>)[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>])</span>
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a></span>
<span id="cb13-14"><a href="#cb13-14" tabindex="-1"></a></span>
<span id="cb13-15"><a href="#cb13-15" tabindex="-1"></a><span class="fu">plot</span>(n, media_C1, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb13-16"><a href="#cb13-16" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb13-17"><a href="#cb13-17" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n, <span class="at">x1=</span>n, <span class="at">y0 =</span> <span class="fu">sapply</span>(IC_LL_C1[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>]), </span>
<span id="cb13-18"><a href="#cb13-18" tabindex="-1"></a> <span class="at">y1 =</span> <span class="fu">sapply</span>(IC_LL_C1[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">2</span>]),</span>
<span id="cb13-19"><a href="#cb13-19" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C1, <span class="st">'green'</span>, <span class="st">'red'</span>))</span>
<span id="cb13-20"><a href="#cb13-20" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb13-21"><a href="#cb13-21" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">'Média'</span>, <span class="st">'Parâmetro'</span>), <span class="at">lwd =</span> <span class="dv">1</span>, <span class="at">lty =</span> <span class="dv">1</span>, </span>
<span id="cb13-22"><a href="#cb13-22" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">c</span>(<span class="st">'black'</span>, <span class="st">'blue'</span>), <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>)</span>
<span id="cb13-23"><a href="#cb13-23" tabindex="-1"></a></span>
<span id="cb13-24"><a href="#cb13-24" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], media_C2[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">2</span>),</span>
<span id="cb13-25"><a href="#cb13-25" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)))</span>
<span id="cb13-26"><a href="#cb13-26" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">x1=</span>n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">y0 =</span> <span class="fu">sapply</span>(IC_LL_C2[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>])[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], </span>
<span id="cb13-27"><a href="#cb13-27" tabindex="-1"></a> <span class="at">y1 =</span> <span class="fu">sapply</span>(IC_LL_C2[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">2</span>])[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>],</span>
<span id="cb13-28"><a href="#cb13-28" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C2, <span class="st">'green'</span>, <span class="st">'red'</span>)[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>])</span>
<span id="cb13-29"><a href="#cb13-29" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C2, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb13-30"><a href="#cb13-30" tabindex="-1"></a></span>
<span id="cb13-31"><a href="#cb13-31" tabindex="-1"></a><span class="fu">plot</span>(n, media_C2, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">2</span>),</span>
<span id="cb13-32"><a href="#cb13-32" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb13-33"><a href="#cb13-33" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n, <span class="at">x1=</span>n, <span class="at">y0 =</span> <span class="fu">sapply</span>(IC_LL_C2[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">1</span>]), </span>
<span id="cb13-34"><a href="#cb13-34" tabindex="-1"></a> <span class="at">y1 =</span> <span class="fu">sapply</span>(IC_LL_C2[[<span class="dv">1</span>]], <span class="cf">function</span>(x) x[<span class="dv">2</span>]),</span>
<span id="cb13-35"><a href="#cb13-35" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C2, <span class="st">'green'</span>, <span class="st">'red'</span>))</span>
<span id="cb13-36"><a href="#cb13-36" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C2, <span class="at">col =</span> <span class="st">'blue'</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:ICLL"></span>
<img src="Simulacao_files/figure-html/ICLL-1.png" alt="Intervalos de confiança 95% para a primeira realização da simulação de monte carlo utilizando máxima verossimilhança." width="768" />
<p class="caption">
Figure 3.3: Intervalos de confiança 95% para a primeira realização da simulação de monte carlo utilizando máxima verossimilhança.
</p>
</div>
<div class="sourceCode" id="cb14"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
<p>Para cada intervalo de confiança dessas figuras, existem outros 99 intervalos construídos via simulação. Como os intervalos são de 95% de confiança, espera-se que 95% dessas 100 realizações contenham o valor verdadeiro. Isso se chama taxa de cobertura do intervalo de confiança (quanto ele realmente é confiável).</p>
<p>A Figura <a href="#fig:coberturaLL">3.4</a> apresenta a taxa de cobertura dos intervalos de confiança para os diferentes tamanhos de amostra. Aparentemente os intervalos possuem uma cobertura similar para todo os tamanhos de amostra, mas esse resultado não está muito de acordo com o que se vê na literatura, portanto deve ser verificado com mais calma.</p>
<div class="alert alert-danger" role="alert">
<p>
<strong>ATENÇÃO:</strong> Essa taxa de cobertura está estranha. a taxa de cobertura utilizando bootstrap é o comportamento esperado.
</p>
</div>
<div class="sourceCode" id="cb15"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>cobertura_LL_C1 <span class="ot"><-</span> <span class="fu">numeric</span>(<span class="at">length=</span><span class="fu">length</span>(n))</span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a>cobertura_LL_C2 <span class="ot"><-</span> <span class="fu">numeric</span>(<span class="fu">length</span>(<span class="fu">length</span>(n)))</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a></span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n)) {</span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> a <span class="ot"><-</span> <span class="fu">lapply</span>(IC_LL_C1, <span class="st">'[['</span>, i)</span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> b <span class="ot"><-</span> <span class="fu">lapply</span>(IC_LL_C2, <span class="st">'[['</span>, i)</span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a> </span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a> </span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a> cobertura_LL_C1[i] <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">sapply</span>(a, <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C1 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C1), </span>
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a> <span class="at">na.rm =</span> T)<span class="sc">/</span><span class="fu">length</span>(a)</span>
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a> cobertura_LL_C2[i] <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">sapply</span>(b[validos_C2<span class="sc">==</span><span class="dv">2</span>], <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C2 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C2), </span>
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a> <span class="at">na.rm =</span> T)<span class="sc">/</span><span class="fu">length</span>(b)</span>
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a>}</span>
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a></span>
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a></span>
<span id="cb15-17"><a href="#cb15-17" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>))</span>
<span id="cb15-18"><a href="#cb15-18" tabindex="-1"></a><span class="fu">plot</span>(n, cobertura_LL_C1, <span class="at">main =</span> <span class="fu">c</span>(<span class="st">'Taxa de cobertura para</span><span class="sc">\n</span><span class="st">theta = 40'</span>),</span>
<span id="cb15-19"><a href="#cb15-19" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">'Tamanho de amostra'</span>, <span class="at">ylab =</span> <span class="st">'Taxa de Cobertura'</span>)</span>
<span id="cb15-20"><a href="#cb15-20" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> <span class="fl">0.95</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb15-21"><a href="#cb15-21" tabindex="-1"></a></span>
<span id="cb15-22"><a href="#cb15-22" tabindex="-1"></a><span class="fu">plot</span>(n, cobertura_LL_C2, <span class="at">main =</span> <span class="fu">c</span>(<span class="st">'Taxa de cobertura para</span><span class="sc">\n</span><span class="st">theta = 1'</span>),</span>
<span id="cb15-23"><a href="#cb15-23" tabindex="-1"></a> <span class="at">xlab =</span> <span class="st">'Tamanho de amostra'</span>, <span class="at">ylab =</span> <span class="st">'Taxa de Cobertura'</span>)</span>
<span id="cb15-24"><a href="#cb15-24" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> <span class="fl">0.95</span>, <span class="at">col =</span> <span class="dv">2</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:coberturaLL"></span>
<img src="Simulacao_files/figure-html/coberturaLL-1.png" alt="Taxa de cobertura dos Intervalos de confiança utilizando máxima verossimilhança para cada tamanho de amostra após 1000 simulações de monte carlo." width="768" />
<p class="caption">
Figure 3.4: Taxa de cobertura dos Intervalos de confiança utilizando máxima verossimilhança para cada tamanho de amostra após 1000 simulações de monte carlo.
</p>
</div>
<div class="sourceCode" id="cb16"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="fu">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
<p>Já ná Figura <a href="#fig:ViesLL">3.5</a> está o viés de estimativa dos parâmetros para os diferentes tamanhos de amostra. Aparentemente não foi identificado nenhum viés no estimador (lembrando que o estimador de máxima verossimilhança é assintoticamente não viesado).</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>vies_LL_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(that_C1, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) y <span class="sc">-</span> par_C1))</span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a>vies_medio_LL_C1 <span class="ot"><-</span> <span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">mean</span>(<span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) vies_LL_C1[[y]][[x]])))</span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a></span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a>vies_LL_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(that_C2, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) y <span class="sc">-</span> par_C2))</span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a>vies_medio_LL_C2 <span class="ot"><-</span> <span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(n), <span class="cf">function</span>(x) <span class="fu">mean</span>(<span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(y) vies_LL_C2[[y]][[x]])))</span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a></span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">2</span>))</span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_medio_LL_C1[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">40</span>, <span class="dv">40</span>),</span>
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)<span class="sc">-</span>theta))</span>
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_LL_C1[[x]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="dv">0</span>, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb17-15"><a href="#cb17-15" tabindex="-1"></a><span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_medio_LL_C1[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb17-16"><a href="#cb17-16" tabindex="-1"></a></span>
<span id="cb17-17"><a href="#cb17-17" tabindex="-1"></a><span class="fu">plot</span>(n, vies_medio_LL_C1, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">40</span>, <span class="dv">40</span>),</span>
<span id="cb17-18"><a href="#cb17-18" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb17-19"><a href="#cb17-19" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n, vies_LL_C1[[x]], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb17-20"><a href="#cb17-20" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="dv">0</span>, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb17-21"><a href="#cb17-21" tabindex="-1"></a><span class="fu">lines</span>(n, vies_medio_LL_C1, <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb17-22"><a href="#cb17-22" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">'Viés Médio'</span>, <span class="st">'0'</span>, <span class="st">'Simulações'</span>),</span>
<span id="cb17-23"><a href="#cb17-23" tabindex="-1"></a> <span class="at">lty =</span> <span class="dv">1</span>, <span class="at">lwd =</span> <span class="dv">1</span>, <span class="at">col =</span> <span class="fu">c</span>(<span class="st">'red'</span>, <span class="st">'blue'</span>, <span class="st">'lightgrey'</span>),</span>
<span id="cb17-24"><a href="#cb17-24" tabindex="-1"></a> <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>)</span>
<span id="cb17-25"><a href="#cb17-25" tabindex="-1"></a></span>
<span id="cb17-26"><a href="#cb17-26" tabindex="-1"></a></span>
<span id="cb17-27"><a href="#cb17-27" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_medio_LL_C2[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="fl">1.1</span>, <span class="fl">1.1</span>),</span>
<span id="cb17-28"><a href="#cb17-28" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)<span class="sc">-</span>theta))</span>
<span id="cb17-29"><a href="#cb17-29" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_LL_C2[[x]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb17-30"><a href="#cb17-30" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="dv">0</span>, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb17-31"><a href="#cb17-31" tabindex="-1"></a><span class="fu">lines</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], vies_medio_LL_C2[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span>
<span id="cb17-32"><a href="#cb17-32" tabindex="-1"></a></span>
<span id="cb17-33"><a href="#cb17-33" tabindex="-1"></a><span class="fu">plot</span>(n, vies_medio_LL_C2, <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>, <span class="at">ylim=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="fl">1.1</span>, <span class="fl">1.1</span>),</span>
<span id="cb17-34"><a href="#cb17-34" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb17-35"><a href="#cb17-35" tabindex="-1"></a>foo <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) <span class="fu">lines</span>(n, vies_LL_C2[[x]], <span class="at">col=</span><span class="st">'lightgrey'</span>))</span>
<span id="cb17-36"><a href="#cb17-36" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h=</span><span class="dv">0</span>, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb17-37"><a href="#cb17-37" tabindex="-1"></a><span class="fu">lines</span>(n, vies_medio_LL_C2, <span class="at">type=</span><span class="st">'l'</span>, <span class="at">col =</span> <span class="dv">2</span>)</span></code></pre></div>
<div class="figure" style="text-align: c"><span style="display:block;" id="fig:ViesLL"></span>
<img src="Simulacao_files/figure-html/ViesLL-1.png" alt="Viés médio e de cada simulação de monte carlo a partir do estimador de máxima verossimilhança." width="768" />
<p class="caption">
Figure 3.5: Viés médio e de cada simulação de monte carlo a partir do estimador de máxima verossimilhança.
</p>
</div>
<div class="sourceCode" id="cb18"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">layout</span>(<span class="dv">1</span>)</span></code></pre></div>
</div>
<div id="via-jackknife" class="section level2" number="3.2">
<h2><span class="header-section-number">3.2</span> Via Jackknife</h2>
<p>Seja uma amostra de tamanho <span class="math inline">\(n = 20\)</span> (<span class="math inline">\(x_1, x_2, ..., x_n\)</span>), e seja um parâmetro <span class="math inline">\(\hat{\theta}\)</span> estimado a partir dessas n amostras. A estimativa pontual jackknife consiste em retirar uma observação (a i-ésima) e estimar novamente o parâmetro com as 19 observações restantes, denotado por <span class="math inline">\(\hat{\theta}^{(i)}\)</span>. Repita esse passo retirando uma a uma todas as 20 observações.</p>
<p><span class="math display">\[
\hat{\theta} = f(x_1, x_2, ..., x_n)
\]</span></p>
<p><span class="math display">\[
\hat{\theta}^{(i)} = f(x_1, x_2, ..., x_{i-1}, x_{i+1}, ..., x_n)
\]</span></p>
<p>A seguir, estime os chamados pseudo valores, denotados por <span class="math inline">\(\hat{\theta}^{*(i)}\)</span>, da seguinte forma</p>
<p><span class="math display">\[
\hat{\theta}^{*(i)} = n . \hat{\theta} - (n-1) . \hat{\theta}^{(i)}
\]</span></p>
<p>O estimador pontual jackknife <span class="math inline">\(\hat{\theta}^*\)</span> será a média dos pseudo valores</p>
<p><span class="math display">\[
\hat{\theta}^* = {\displaystyle\sum_{i=1}^n \hat{\theta}^{*(i)} \over n}
\]</span></p>
<p>O erro padrao do estimador jackknife é dado por</p>
<p><span class="math display">\[
EP[\hat{\theta}^*] = \sqrt{Var[\hat{\theta}^*] \over n}
\]</span></p>
<p>e seu intervalo de confinça pode ser aproximado por uma distribuição t de student se considerarmos que os pseudo valores são independentes. Mais informações sobre jackknife podem ser encontradas <a href="http://cursos.leg.ufpr.br/ce089/11_jackknife.html">aqui</a>.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>esttheta_jack <span class="ot"><-</span> <span class="cf">function</span>(amostra, f, <span class="at">pseudo =</span> F, ...) {</span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="co"># amostra: vetor numérico com o conjunto de observações;</span></span>
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a> </span>
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a> <span class="co"># f: função da estatística desejada para cálculo;</span></span>
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a> </span>
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a> <span class="co"># pseudo: lógico que indica se os pseudo valores devem ser retornados;</span></span>
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a> </span>
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a> <span class="co"># ...: outros argumentos passados para f;</span></span>
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a> </span>
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a> <span class="co"># ---</span></span>
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a> </span>
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a> <span class="co"># value: se pseudo = F, uma lista contendo a estimativa pontual de Jackknife e a variância estimada do estimador de Jackknife. Se pseudo = T, uma lista igual à anterior mas acrescida dos pseudo valores.</span></span>
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a> </span>
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">length</span>(amostra)</span>
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a> i <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span>n</span>
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a> </span>
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a> par <span class="ot"><-</span> <span class="fu">f</span>(amostra, ...)</span>
<span id="cb19-18"><a href="#cb19-18" tabindex="-1"></a> </span>
<span id="cb19-19"><a href="#cb19-19" tabindex="-1"></a> val <span class="ot"><-</span> <span class="fu">sapply</span>(i, <span class="cf">function</span>(x) <span class="fu">f</span>(amostra[<span class="sc">-</span>x], ...), <span class="at">simplify =</span> T)</span>
<span id="cb19-20"><a href="#cb19-20" tabindex="-1"></a> </span>
<span id="cb19-21"><a href="#cb19-21" tabindex="-1"></a> pseudovalores <span class="ot"><-</span> n<span class="sc">*</span>par <span class="sc">-</span> (n<span class="dv">-1</span>)<span class="sc">*</span>val</span>
<span id="cb19-22"><a href="#cb19-22" tabindex="-1"></a> </span>
<span id="cb19-23"><a href="#cb19-23" tabindex="-1"></a> est <span class="ot"><-</span> <span class="fu">mean</span>(pseudovalores)</span>
<span id="cb19-24"><a href="#cb19-24" tabindex="-1"></a> </span>
<span id="cb19-25"><a href="#cb19-25" tabindex="-1"></a> var <span class="ot"><-</span> <span class="fu">var</span>(pseudovalores)<span class="sc">/</span><span class="fu">length</span>(pseudovalores)</span>
<span id="cb19-26"><a href="#cb19-26" tabindex="-1"></a> </span>
<span id="cb19-27"><a href="#cb19-27" tabindex="-1"></a> <span class="cf">if</span> (pseudo) {</span>
<span id="cb19-28"><a href="#cb19-28" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">list</span>(<span class="st">'est'</span> <span class="ot">=</span> est, <span class="st">'var'</span> <span class="ot">=</span> var, <span class="st">'pseudo'</span> <span class="ot">=</span> pseudovalores))</span>
<span id="cb19-29"><a href="#cb19-29" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb19-30"><a href="#cb19-30" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">list</span>(<span class="st">'est'</span> <span class="ot">=</span> est, <span class="st">'var'</span> <span class="ot">=</span> var))</span>
<span id="cb19-31"><a href="#cb19-31" tabindex="-1"></a> }</span>
<span id="cb19-32"><a href="#cb19-32" tabindex="-1"></a> </span>
<span id="cb19-33"><a href="#cb19-33" tabindex="-1"></a>}</span></code></pre></div>
<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="co"># Utilizando Jackknife --------------</span></span>
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a></span>
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co"># estimativa pontual</span></span>
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a>est_jack_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(C1, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) <span class="fu">esttheta_jack</span>(y, <span class="at">f =</span> esttheta)<span class="sc">$</span>est))</span>
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a>est_jack_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(C2, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) <span class="fu">esttheta_jack</span>(y, <span class="at">f =</span> esttheta)<span class="sc">$</span>est))</span>
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a></span>
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a><span class="co"># Variância de theta</span></span>
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a>var_jack_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(C1, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) <span class="fu">esttheta_jack</span>(y, <span class="at">f =</span> esttheta)<span class="sc">$</span>var))</span>
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a>var_jack_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(C2, <span class="cf">function</span>(x) <span class="fu">sapply</span>(x, <span class="cf">function</span>(y) <span class="fu">esttheta_jack</span>(y, <span class="at">f =</span> esttheta)<span class="sc">$</span>var))</span>
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a></span>
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co"># Intervalos de confiança de 95%</span></span>
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a>IC_jack_C1 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) {</span>
<span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a> <span class="fu">mapply</span>(<span class="cf">function</span>(est, am, var) est <span class="sc">+</span> <span class="fu">c</span>(<span class="at">LI =</span> <span class="sc">-</span><span class="dv">1</span>, <span class="at">LS =</span> <span class="dv">1</span>)<span class="sc">*</span><span class="fu">qt</span>(<span class="fl">0.975</span>, <span class="fu">length</span>(am)<span class="sc">-</span><span class="dv">1</span>)<span class="sc">*</span><span class="fu">sqrt</span>(var),</span>
<span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a> <span class="at">est =</span> est_jack_C1[[x]], <span class="at">am =</span> C1[[x]], <span class="at">var =</span> var_jack_C1[[x]])</span>
<span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a>}</span>
<span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a>)</span>
<span id="cb20-17"><a href="#cb20-17" tabindex="-1"></a></span>
<span id="cb20-18"><a href="#cb20-18" tabindex="-1"></a>IC_jack_C2 <span class="ot"><-</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>N, <span class="cf">function</span>(x) {</span>
<span id="cb20-19"><a href="#cb20-19" tabindex="-1"></a> <span class="fu">mapply</span>(<span class="cf">function</span>(est, am, var) est <span class="sc">+</span> <span class="fu">c</span>(<span class="at">LI =</span> <span class="sc">-</span><span class="dv">1</span>, <span class="at">LS =</span> <span class="dv">1</span>)<span class="sc">*</span><span class="fu">qt</span>(<span class="fl">0.975</span>, <span class="fu">length</span>(am)<span class="sc">-</span><span class="dv">1</span>)<span class="sc">*</span><span class="fu">sqrt</span>(var),</span>
<span id="cb20-20"><a href="#cb20-20" tabindex="-1"></a> <span class="at">est =</span> est_jack_C2[[x]], <span class="at">am =</span> C2[[x]], <span class="at">var =</span> var_jack_C2[[x]])</span>
<span id="cb20-21"><a href="#cb20-21" tabindex="-1"></a>}</span>
<span id="cb20-22"><a href="#cb20-22" tabindex="-1"></a>)</span></code></pre></div>
<p>A <a href="#fig:estjack">3.6</a> apresenta os intervalos de confiança para uma das 100 realizações de Monte Carlo. A taxa de cobertura (Figura <a href="#fig:coberturajack">3.7</a>) mostra um padrão também não muito condizente com o que se vê na literatura.</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r fold-hide"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>contem_C1_jack <span class="ot"><-</span> <span class="fu">apply</span>(IC_jack_C1[[<span class="dv">1</span>]], <span class="dv">2</span>, <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C1 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C1)</span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a>contem_C2_jack <span class="ot"><-</span> <span class="fu">apply</span>(IC_jack_C2[[<span class="dv">1</span>]], <span class="dv">2</span>, <span class="cf">function</span>(x) x[<span class="dv">1</span>] <span class="sc"><</span> par_C2 <span class="sc">&</span> x[<span class="dv">2</span>] <span class="sc">></span> par_C2)</span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a></span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a></span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="dv">2</span>))</span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], est_jack_C1[[<span class="dv">1</span>]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)))</span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">x1 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], </span>
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a> <span class="at">y0 =</span> IC_jack_C1[[<span class="dv">1</span>]][<span class="dv">1</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">y1 =</span> IC_jack_C1[[<span class="dv">1</span>]][<span class="dv">2</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>],</span>
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C1_jack[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="st">'green'</span>, <span class="st">'red'</span>))</span>
<span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb21-13"><a href="#cb21-13" tabindex="-1"></a></span>
<span id="cb21-14"><a href="#cb21-14" tabindex="-1"></a><span class="fu">plot</span>(n, est_jack_C1[[<span class="dv">1</span>]], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">80</span>),</span>
<span id="cb21-15"><a href="#cb21-15" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 40</span><span class="sc">\n</span><span class="st">n ~ 2:1000'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="cn">NA</span>)</span>
<span id="cb21-16"><a href="#cb21-16" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n, <span class="at">x1 =</span> n, <span class="at">y0 =</span> IC_jack_C1[[<span class="dv">1</span>]][<span class="dv">1</span>, ], <span class="at">y1 =</span> IC_jack_C1[[<span class="dv">1</span>]][<span class="dv">2</span>, ],</span>
<span id="cb21-17"><a href="#cb21-17" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">ifelse</span>(contem_C1_jack, <span class="st">'green'</span>, <span class="st">'red'</span>))</span>
<span id="cb21-18"><a href="#cb21-18" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">h =</span> par_C1, <span class="at">col =</span> <span class="st">'blue'</span>)</span>
<span id="cb21-19"><a href="#cb21-19" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">'topright'</span>, <span class="at">legend =</span> <span class="fu">c</span>(<span class="st">'Média'</span>, <span class="st">'Parâmetro'</span>), <span class="at">lty =</span> <span class="dv">1</span>, <span class="at">lwd =</span> <span class="dv">1</span>,</span>
<span id="cb21-20"><a href="#cb21-20" tabindex="-1"></a> <span class="at">col =</span> <span class="fu">c</span>(<span class="st">'black'</span>, <span class="st">'blue'</span>), <span class="at">bg =</span> <span class="st">'transparent'</span>, <span class="at">bty =</span> <span class="st">'n'</span>)</span>
<span id="cb21-21"><a href="#cb21-21" tabindex="-1"></a></span>
<span id="cb21-22"><a href="#cb21-22" tabindex="-1"></a></span>
<span id="cb21-23"><a href="#cb21-23" tabindex="-1"></a><span class="fu">plot</span>(n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], est_jack_C2[[<span class="dv">1</span>]][<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">type =</span> <span class="st">'l'</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">3</span>),</span>
<span id="cb21-24"><a href="#cb21-24" tabindex="-1"></a> <span class="at">main =</span> <span class="st">'theta = 1</span><span class="sc">\n</span><span class="st">n = 2:30'</span>, <span class="at">xlab =</span> <span class="cn">NA</span>, <span class="at">ylab =</span> <span class="fu">expression</span>(<span class="fu">widehat</span>(theta)))</span>
<span id="cb21-25"><a href="#cb21-25" tabindex="-1"></a><span class="fu">segments</span>(<span class="at">x0 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">x1 =</span> n[<span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], </span>
<span id="cb21-26"><a href="#cb21-26" tabindex="-1"></a> <span class="at">y0 =</span> IC_jack_C2[[<span class="dv">1</span>]][<span class="dv">1</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>], <span class="at">y1 =</span> IC_jack_C2[[<span class="dv">1</span>]][<span class="dv">2</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">29</span>],</span>