-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1617 lines (1563 loc) · 141 KB
/
index.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 xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Mario Camacho">
<meta name="keywords" content="respuesta múltiple, convergenia modelos, respuesta nominal, efectos aleatorios, medidas repetidas">
<title>Choice of Brand for Crackers</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
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;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="index_files/libs/clipboard/clipboard.min.js"></script>
<script src="index_files/libs/quarto-html/quarto.js"></script>
<script src="index_files/libs/quarto-html/popper.min.js"></script>
<script src="index_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="index_files/libs/quarto-html/anchor.min.js"></script>
<link href="index_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="index_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="index_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="index_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="index_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-full toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active" data-toc-expanded="99">
<h2 id="toc-title">Índice</h2>
<ul>
<li><a href="#database" id="toc-database" class="nav-link active" data-scroll-target="#database">Database</a>
<ul class="collapse">
<li><a href="#descripción" id="toc-descripción" class="nav-link" data-scroll-target="#descripción">Descripción</a></li>
<li><a href="#datos-por-individuo" id="toc-datos-por-individuo" class="nav-link" data-scroll-target="#datos-por-individuo">Datos por individuo</a></li>
<li><a href="#distribución-de-las-preferencias" id="toc-distribución-de-las-preferencias" class="nav-link" data-scroll-target="#distribución-de-las-preferencias">Distribución de las preferencias</a></li>
<li><a href="#escala" id="toc-escala" class="nav-link" data-scroll-target="#escala">Escala</a></li>
</ul></li>
<li><a href="#análisis-exploratorio" id="toc-análisis-exploratorio" class="nav-link" data-scroll-target="#análisis-exploratorio">Análisis exploratorio</a>
<ul class="collapse">
<li><a href="#eliminación-datos-confusos" id="toc-eliminación-datos-confusos" class="nav-link" data-scroll-target="#eliminación-datos-confusos">Eliminación datos confusos</a></li>
<li><a href="#distribución-del-precio" id="toc-distribución-del-precio" class="nav-link" data-scroll-target="#distribución-del-precio">Distribución del precio</a></li>
</ul></li>
<li><a href="#modelo-predictivo" id="toc-modelo-predictivo" class="nav-link" data-scroll-target="#modelo-predictivo">Modelo predictivo</a>
<ul class="collapse">
<li><a href="#objetivo" id="toc-objetivo" class="nav-link" data-scroll-target="#objetivo">Objetivo</a></li>
<li><a href="#enfoque-del-modelo" id="toc-enfoque-del-modelo" class="nav-link" data-scroll-target="#enfoque-del-modelo">Enfoque del modelo</a></li>
<li><a href="#train-y-test" id="toc-train-y-test" class="nav-link" data-scroll-target="#train-y-test">Train y test</a></li>
<li><a href="#modelo-respuesta-múltiple-nominal" id="toc-modelo-respuesta-múltiple-nominal" class="nav-link" data-scroll-target="#modelo-respuesta-múltiple-nominal">Modelo respuesta múltiple nominal</a>
<ul class="collapse">
<li><a href="#test-modelo-2" id="toc-test-modelo-2" class="nav-link" data-scroll-target="#test-modelo-2">Test modelo 2</a></li>
</ul></li>
<li><a href="#modelo-respuesta-múltiple-nominal-medidas-repetidas" id="toc-modelo-respuesta-múltiple-nominal-medidas-repetidas" class="nav-link" data-scroll-target="#modelo-respuesta-múltiple-nominal-medidas-repetidas">Modelo respuesta múltiple nominal medidas repetidas</a>
<ul class="collapse">
<li><a href="#vif" id="toc-vif" class="nav-link" data-scroll-target="#vif">VIF</a></li>
<li><a href="#escalado-de-variables" id="toc-escalado-de-variables" class="nav-link" data-scroll-target="#escalado-de-variables">Escalado de variables</a></li>
<li><a href="#test-modelo3" id="toc-test-modelo3" class="nav-link" data-scroll-target="#test-modelo3">Test modelo3</a></li>
<li><a href="#modelo-final" id="toc-modelo-final" class="nav-link" data-scroll-target="#modelo-final">Modelo final</a></li>
</ul></li>
<li><a href="#efectos-aleatorios" id="toc-efectos-aleatorios" class="nav-link" data-scroll-target="#efectos-aleatorios">Efectos aleatorios</a>
<ul class="collapse">
<li><a href="#comparación-individuos" id="toc-comparación-individuos" class="nav-link" data-scroll-target="#comparación-individuos">Comparación individuos</a></li>
</ul></li>
</ul></li>
<li><a href="#bibliografía" id="toc-bibliografía" class="nav-link" data-scroll-target="#bibliografía">Bibliografía</a></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar zindex-bottom">
</div>
<main class="content column-page-right" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Choice of Brand for Crackers</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Autor/a</div>
<div class="quarto-title-meta-contents">
<p>Mario Camacho </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Fecha de publicación</div>
<div class="quarto-title-meta-contents">
<p class="date">10 agosto 2024</p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Fecha de modificación</div>
<div class="quarto-title-meta-contents">
<p class="date-modified">12 agosto 2024</p>
</div>
</div>
</div>
<div>
<div class="abstract">
<div class="abstract-title">Resumen</div>
<p>Nos adentramos en los modelos de respuesta múltiple nominal con medidas repetidas. Se demuestra que incluir efectos aleatorios es necesario para tener un buen ajuste ya que esta correlación entre observaciones es importante recogerla dentro del modelo.</p>
</div>
</div>
</header>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(RColorBrewer)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggridges)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggstance)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(Ecdat)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rpivotTable)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(VGAM)</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mclogit)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(car)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="database" class="level1">
<h1>Database</h1>
<section id="descripción" class="level2">
<h2 class="anchored" data-anchor-id="descripción">Descripción</h2>
<p>En este caso de uso utilizamos la base de datos Cracker del paquete Ecdat sobre la elección de un individuo de una marca de galletas saladas entre cuatro opciones posibles. (Sunshine, Kleebler, Nabisco y Private)</p>
<p>La base de datos contiene 3.292 registros y dentro de cada fila tenemos:</p>
<ul>
<li><strong>id</strong>: individuals identifiers<br>
</li>
<li><strong>choice</strong>: one of sunshine, kleebler, nabisco, private<br>
</li>
<li><strong>disp.z</strong>: is there a display for brand z ?<br>
</li>
<li><strong>feat.z</strong>: is there a newspaper feature advertisement for brand z ?<br>
</li>
<li><strong>price.z</strong>: price of brand z</li>
</ul>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(Cracker)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> id disp.sunshine disp.kleebler disp.nabisco disp.private feat.sunshine
Min. : 1.00 Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
1st Qu.: 35.00 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
Median : 67.00 Median :0.0000 Median :0.0000 Median :0.0000 Median :0.00000 Median :0.00000
Mean : 67.49 Mean :0.1288 Mean :0.1063 Mean :0.3402 Mean :0.09872 Mean :0.03767
3rd Qu.: 99.00 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
Max. :136.00 Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
feat.kleebler feat.nabisco feat.private price.sunshine price.kleebler price.nabisco
Min. :0.00000 Min. :0.00000 Min. :0.00000 Min. : 49.0 Min. : 88.0 Min. : 0.0
1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.: 89.0 1st Qu.:105.0 1st Qu.: 99.0
Median :0.00000 Median :0.00000 Median :0.00000 Median : 97.0 Median :109.0 Median :109.0
Mean :0.04253 Mean :0.08657 Mean :0.04708 Mean : 95.7 Mean :112.6 Mean :107.9
3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:105.0 3rd Qu.:121.0 3rd Qu.:119.0
Max. :1.00000 Max. :1.00000 Max. :1.00000 Max. :129.0 Max. :139.0 Max. :169.0
price.private choice
Min. : 38.00 sunshine: 239
1st Qu.: 59.00 kleebler: 226
Median : 65.00 nabisco :1792
Mean : 68.07 private :1035
3rd Qu.: 78.00
Max. :115.00 </code></pre>
</div>
</div>
</section>
<section id="datos-por-individuo" class="level2">
<h2 class="anchored" data-anchor-id="datos-por-individuo">Datos por individuo</h2>
<p>Se han recogido datos de 136 individuos. Cada individuo tiene de media 21 registros.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>Cracker <span class="sc">|></span> <span class="fu">select</span>(id) <span class="sc">|></span> <span class="fu">table</span>() <span class="sc">|></span> <span class="fu">as.data.frame</span>() <span class="sc">|></span> <span class="fu">select</span>(Freq) <span class="sc">|></span> <span class="fu">summary</span>()</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> Freq
Min. :14.00
1st Qu.:16.75
Median :21.00
Mean :24.21
3rd Qu.:29.00
Max. :77.00 </code></pre>
</div>
</div>
<p>Tomamos de ejemplo algunos de los registros del id 1.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>Cracker <span class="sc">|></span> <span class="fu">filter</span>(id<span class="sc">==</span><span class="dv">1</span>) <span class="sc">|></span> <span class="fu">head</span>(<span class="dv">5</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> id disp.sunshine disp.kleebler disp.nabisco disp.private feat.sunshine feat.kleebler feat.nabisco
1 1 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0
4 1 0 0 0 0 0 0 0
5 1 0 0 0 0 0 0 0
feat.private price.sunshine price.kleebler price.nabisco price.private choice
1 0 98 88 120 71 nabisco
2 0 99 109 99 71 nabisco
3 0 49 109 109 78 sunshine
4 0 103 109 89 78 nabisco
5 0 109 109 119 64 nabisco</code></pre>
</div>
</div>
</section>
<section id="distribución-de-las-preferencias" class="level2">
<h2 class="anchored" data-anchor-id="distribución-de-las-preferencias">Distribución de las preferencias</h2>
<p>Para cada usuario contamos el número de veces que ha elegido cada una de las marcas.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>data_0 <span class="ot"><-</span> </span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>Cracker <span class="sc">|></span> <span class="fu">select</span>(id, choice) <span class="sc">|></span> <span class="fu">group_by_all</span>() <span class="sc">|></span> <span class="fu">count</span>(,<span class="at">.drop =</span> <span class="cn">FALSE</span>) <span class="sc">|></span> <span class="fu">ungroup</span>() <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">p =</span> n<span class="sc">/</span><span class="fu">sum</span>(n), <span class="at">.by=</span>id) <span class="sc">|></span> </span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="co">#mutate(scale = scale(p, scale = FALSE), .by=id) |> </span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(id,<span class="sc">-</span>n) <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">preference =</span> <span class="fu">row_number</span>(), <span class="at">.by=</span>id)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>El individuo 1 ha elegido la marca Nabisco en el 87.5 % de las veces.</p>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 × 5
id choice n p preference
<dbl> <fct> <int> <dbl> <int>
1 1 nabisco 14 0.875 1
2 1 sunshine 2 0.125 2
3 1 kleebler 0 0 3
4 1 private 0 0 4
5 2 sunshine 10 0.625 1
6 2 nabisco 4 0.25 2
7 2 kleebler 2 0.125 3
8 2 private 0 0 4</code></pre>
</div>
</div>
<p>La distribución de la preferencia de cada marca:</p>
<div>
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>data_0 <span class="sc">|></span> <span class="fu">select</span>(choice, preference) <span class="sc">|></span> <span class="fu">table</span>() <span class="sc">|></span> <span class="fu">addmargins</span>()</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">round</span>(</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> data_0 <span class="sc">|></span> <span class="fu">select</span>(choice, preference) <span class="sc">|></span> <span class="fu">table</span>() <span class="sc">|></span> <span class="fu">prop.table</span>(<span class="dv">1</span>)<span class="sc">*</span><span class="dv">100</span>, <span class="dv">2</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|></span> <span class="fu">addmargins</span>()</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell quarto-layout-panel">
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="cell-output cell-output-stdout quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<pre><code> preference
choice 1 2 3 4 Sum
sunshine 8 60 59 9 136
kleebler 6 18 63 49 136
nabisco 82 30 9 15 136
private 40 28 5 63 136
Sum 136 136 136 136 544</code></pre>
</div>
<div class="cell-output cell-output-stdout quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<pre><code> preference
choice 1 2 3 4 Sum
sunshine 5.88 44.12 43.38 6.62 100.00
kleebler 4.41 13.24 46.32 36.03 100.00
nabisco 60.29 22.06 6.62 11.03 100.00
private 29.41 20.59 3.68 46.32 100.00
Sum 99.99 100.01 100.00 100.00 400.00</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="escala" class="level2">
<h2 class="anchored" data-anchor-id="escala">Escala</h2>
<p>Creamos una escala teniendo en cuenta las preferencias del individuo e incluyendo el porcentaje de veces que elige cada una de las marcas sobre el total de sus elecciones.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>data_1 <span class="ot"><-</span> </span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>data_0 <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">scale =</span> <span class="fu">scale</span>(p, <span class="at">scale =</span> <span class="cn">FALSE</span>), <span class="at">.by=</span>id)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>Esta escala tiene una relación directa con la proporción de cada una de las elecciones. Se mueve entre los valores -0.25 y 0.75.</p>
<ul>
<li>El valor -0.25 si esa marca nunca ha sido elegida por el individuo (p=0).</li>
<li>El valor 0.75 si es la única marca elegida por el usuario (p=1).</li>
</ul>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>data_1 <span class="sc">|></span> <span class="fu">select</span>(choice, p, scale) <span class="sc">|></span> <span class="fu">summary</span>()</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> choice p scale.V1
sunshine:136 Min. :0.00000 Min. :-0.2500000
kleebler:136 1st Qu.:0.00000 1st Qu.:-0.2500000
nabisco :136 Median :0.05556 Median :-0.1944444
private :136 Mean :0.25000 Mean : 0.0000000
3rd Qu.:0.43902 3rd Qu.: 0.1890244
Max. :1.00000 Max. : 0.7500000 </code></pre>
</div>
</div>
<p>Si las elecciones fueran fruto del azar, cada marca sería elegida un 25 % por cada individuo. Con la escala creado cuantificamos cuánto se desvía la proporción de cada elección sobre este porcentaje esperado.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>data_1 <span class="sc">|></span> <span class="fu">filter</span>(id<span class="sc">==</span><span class="dv">1</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 6
id choice n p preference scale[,1]
<dbl> <fct> <int> <dbl> <int> <dbl>
1 1 nabisco 14 0.875 1 0.625
2 1 sunshine 2 0.125 2 -0.125
3 1 kleebler 0 0 3 -0.25
4 1 private 0 0 4 -0.25 </code></pre>
</div>
</div>
<p>Los individuos 7 y 123 han hecho un total de 17 elecciones siendo su marca preferida Nabisco.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>data_1 <span class="sc">|></span> <span class="fu">filter</span>(id <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">7</span>,<span class="dv">123</span>))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 × 6
id choice n p preference scale[,1]
<dbl> <fct> <int> <dbl> <int> <dbl>
1 7 nabisco 12 0.706 1 0.456
2 7 kleebler 4 0.235 2 -0.0147
3 7 sunshine 1 0.0588 3 -0.191
4 7 private 0 0 4 -0.25
5 123 nabisco 14 0.824 1 0.574
6 123 sunshine 1 0.0588 2 -0.191
7 123 kleebler 1 0.0588 3 -0.191
8 123 private 1 0.0588 4 -0.191 </code></pre>
</div>
</div>
</section>
</section>
<section id="análisis-exploratorio" class="level1">
<h1>Análisis exploratorio</h1>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>paleta_choice <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"lightsalmon"</span>, <span class="st">"darkolivegreen3"</span>, <span class="st">"lightblue"</span>, <span class="st">"mediumorchid"</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<section id="eliminación-datos-confusos" class="level2">
<h2 class="anchored" data-anchor-id="eliminación-datos-confusos">Eliminación datos confusos</h2>
<p>Existen tres registros donde el precio de la marca Nabisco es cero. Puede deberse a alguna oferta que no tengamos recogida o error en los datos. Eliminamos estos registros</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>Cracker <span class="sc">|></span> <span class="fu">filter</span>(price.nabisco<span class="sc">==</span><span class="dv">0</span>) <span class="sc">|></span> <span class="fu">select</span>(<span class="fu">c</span>(<span class="fu">contains</span>(<span class="st">'nabisco'</span>),<span class="st">'choice'</span>))</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>Cracker_clean <span class="ot"><-</span> Cracker <span class="sc">|></span> <span class="fu">filter</span>(price.nabisco<span class="sc">></span><span class="dv">0</span>) </span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> disp.nabisco feat.nabisco price.nabisco choice
319 1 0 0 nabisco
321 0 0 0 nabisco
1051 0 0 0 nabisco</code></pre>
</div>
</div>
</section>
<section id="distribución-del-precio" class="level2">
<h2 class="anchored" data-anchor-id="distribución-del-precio">Distribución del precio</h2>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(Cracker_clean <span class="sc">|></span> <span class="fu">select</span>(<span class="fu">contains</span>(<span class="st">'price'</span>)))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> price.sunshine price.kleebler price.nabisco price.private
Min. : 49.0 Min. : 88.0 Min. : 49 Min. : 38.00
1st Qu.: 89.0 1st Qu.:105.0 1st Qu.: 99 1st Qu.: 59.00
Median : 97.0 Median :109.0 Median :109 Median : 65.00
Mean : 95.7 Mean :112.6 Mean :108 Mean : 68.08
3rd Qu.:105.0 3rd Qu.:121.0 3rd Qu.:119 3rd Qu.: 78.00
Max. :129.0 Max. :139.0 Max. :169 Max. :115.00 </code></pre>
</div>
</div>
<p>Definimos el precio de la marca elegida por el usuario y lo enfrentamos al precio medio de las elecciones que tenía el individuo.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>data_2 <span class="ot"><-</span> Cracker_clean <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">precio_medio =</span> (price.sunshine <span class="sc">+</span> price.kleebler <span class="sc">+</span> price.nabisco <span class="sc">+</span> price.private) <span class="sc">/</span> <span class="dv">4</span>,</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="at">precio_preferencia =</span> <span class="fu">case_when</span>(choice <span class="sc">==</span> <span class="st">"sunshine"</span> <span class="sc">~</span> price.sunshine,</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> choice <span class="sc">==</span> <span class="st">"kleebler"</span> <span class="sc">~</span> price.kleebler,</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> choice <span class="sc">==</span> <span class="st">"nabisco"</span> <span class="sc">~</span> price.nabisco,</span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> choice <span class="sc">==</span> <span class="st">"private"</span> <span class="sc">~</span> price.private),</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a> <span class="at">id=</span><span class="fu">factor</span>(id))</span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># |></span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> <span class="co">#select(precio_preferencia, precio_medio, id, choice)</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>La marca Private casi siempre tiene un precio superior al precio medio de las cuatro marcas cuando es la marca que el individuo prefiere. Aquellos casos donde la marca elegida es la marca Nabisco, el precio de esta elección casi siempre es inferior al precio medio de las cuatro marcas.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>data_2 <span class="sc">|></span> <span class="fu">ggplot</span>() <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x=</span>precio_preferencia, <span class="at">y=</span>precio_medio, <span class="at">colour =</span> <span class="fu">factor</span>(choice))) <span class="sc">+</span> </span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_abline</span>(<span class="at">intercept =</span> <span class="dv">0</span>) <span class="sc">+</span> <span class="fu">xlim</span>(<span class="dv">30</span>, <span class="dv">150</span>) <span class="sc">+</span> <span class="fu">ylim</span>(<span class="dv">30</span>, <span class="dv">150</span>) <span class="sc">+</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">"Precio de la marca escogida (choice)"</span>) <span class="sc">+</span> </span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Precio medio de las cuatro marcas"</span>)<span class="sc">+</span></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> paleta_choice) <span class="sc">+</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">colour=</span><span class="st">"choice"</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" style="width:80.0%"></p>
</div>
</div>
<p>Tenemos la distribución del precio para cada una de las marcas y marcamos la media del precio de cada una de ellas. Las marcas Sunshine y Klebbe son bimodales.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>Cracker_clean <span class="sc">|></span> <span class="fu">select</span>(<span class="fu">contains</span>(<span class="st">"price"</span>)) <span class="sc">|></span> <span class="fu">head</span>() <span class="sc">|></span> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> <span class="fu">c</span>(price.sunshine,price.kleebler ,price.nabisco ,price.private), <span class="at">names_to =</span> <span class="st">"Marca"</span>, <span class="at">values_to =</span> <span class="st">"Precio"</span>) <span class="sc">|></span> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Precio, <span class="at">y =</span> Marca, <span class="at">fill=</span>Marca)) <span class="sc">+</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density_ridges</span>(<span class="at">quantile_lines=</span><span class="cn">TRUE</span>, <span class="at">quantile_fun=</span><span class="cf">function</span>(Precio,...)<span class="fu">mean</span>(Precio),</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="at">jittered_points =</span> <span class="cn">TRUE</span>, <span class="at">position =</span> <span class="st">"raincloud"</span>, <span class="at">scale =</span> <span class="fl">0.9</span>,</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">point_color =</span> Marca, <span class="at">point_fill =</span> Marca, <span class="at">point_shape =</span> Marca)) <span class="sc">+</span></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_summaryh</span>(<span class="at">fun.x=</span>median,</span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a> <span class="at">geom=</span><span class="st">"text"</span>,</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">label=</span><span class="fu">sprintf</span>(<span class="st">"%1.1f"</span>, ..x..)),</span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a> <span class="at">position=</span><span class="fu">position_nudge</span>(<span class="at">y=</span><span class="sc">-</span><span class="fl">0.1</span>),</span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a> <span class="at">colour=</span><span class="st">"black"</span>,</span>
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span><span class="fl">3.5</span>) <span class="sc">+</span></span>
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> paleta_choice) <span class="sc">+</span></span>
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">fill=</span><span class="st">""</span>,<span class="at">color=</span><span class="st">""</span>,</span>
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Density Ridges del precio"</span>,</span>
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"se incluyen las medianas"</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid" style="width:80.0%"></p>
</div>
</div>
</section>
</section>
<section id="modelo-predictivo" class="level1">
<h1>Modelo predictivo</h1>
<section id="objetivo" class="level2">
<h2 class="anchored" data-anchor-id="objetivo">Objetivo</h2>
<p>Queremos predecir la preferencia de un individuo entre cuatro posibles marcas de galletas saladas en función de las características de venta de cada una de las marcas.</p>
</section>
<section id="enfoque-del-modelo" class="level2">
<h2 class="anchored" data-anchor-id="enfoque-del-modelo">Enfoque del modelo</h2>
<p>La variable respuesta es la variable <em>choice</em>. Una variable nominal con cuatro categorías que identifica la marca preferida de cada individuo.</p>
<p>Tenemos dos variables dicotómicas para cada marca. La variable <em>display</em> indica si la marca tenía un display o no. Y la variable <em>feat</em> indica si había un anuncio en el periódico o no.</p>
<p>También la variable continua <em>price</em> sobre el precio de la marca.</p>
</section>
<section id="train-y-test" class="level2">
<h2 class="anchored" data-anchor-id="train-y-test">Train y test</h2>
<p>Separamos mediante un muestreo estratificado (estrato: usuario) el 10 % de los datos para testear el modelo.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1234</span>)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>Cracker_clean <span class="ot"><-</span> Cracker_clean <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">r=</span><span class="fu">row_number</span>())</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a>test_0 <span class="ot"><-</span> Cracker_clean <span class="sc">|></span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice_sample</span>(<span class="at">prop =</span> <span class="fl">0.10</span>, <span class="at">by =</span> <span class="fu">c</span>(id))</span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a>train_0 <span class="ot"><-</span> <span class="fu">anti_join</span>(Cracker_clean, test_0, <span class="at">by=</span><span class="st">'r'</span>) <span class="sc">|></span> <span class="fu">select</span>(<span class="sc">-</span>r)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="modelo-respuesta-múltiple-nominal" class="level2">
<h2 class="anchored" data-anchor-id="modelo-respuesta-múltiple-nominal">Modelo respuesta múltiple nominal</h2>
<p>Usamos la librería VGAM para un modelo repuesta múltiple nominal.</p>
<p>Previamente debemos elegir una categoría de referencia. El resultado del modelo indicará cómo de probable es la elección de cada una de las tres marcas en relación a la categoría de referencia para cada individuo en función.</p>
<p><em>Se elige como referencia el nivel “Private”.</em></p>
<p><u><strong>Modelo 1</strong></u></p>
<p>Modelo saturado.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>modelo1 <span class="ot"><-</span> <span class="fu">vglm</span>((choice) <span class="sc">~</span> disp.sunshine <span class="sc">+</span> disp.kleebler <span class="sc">+</span> disp.nabisco <span class="sc">+</span> disp.private <span class="sc">+</span> feat.sunshine <span class="sc">+</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>feat.kleebler <span class="sc">+</span> feat.nabisco <span class="sc">+</span> feat.private <span class="sc">+</span> price.sunshine <span class="sc">+</span> price.kleebler <span class="sc">+</span> price.nabisco <span class="sc">+</span> price.private,</span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a><span class="at">family=</span><span class="fu">multinomial</span>(<span class="at">refLevel=</span><span class="st">'private'</span>),</span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> train_0)</span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(modelo1)</span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a><span class="do">## Call:</span></span>
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a><span class="do">## vglm(formula = (choice) ~ disp.sunshine + disp.kleebler + disp.nabisco + </span></span>
<span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private + feat.sunshine + feat.kleebler + feat.nabisco + </span></span>
<span id="cb29-10"><a href="#cb29-10" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private + price.sunshine + price.kleebler + price.nabisco + </span></span>
<span id="cb29-11"><a href="#cb29-11" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private, family = multinomial(refLevel = "private"), </span></span>
<span id="cb29-12"><a href="#cb29-12" aria-hidden="true" tabindex="-1"></a><span class="do">## data = train_0)</span></span>
<span id="cb29-13"><a href="#cb29-13" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-14"><a href="#cb29-14" aria-hidden="true" tabindex="-1"></a><span class="do">## Coefficients: </span></span>
<span id="cb29-15"><a href="#cb29-15" aria-hidden="true" tabindex="-1"></a><span class="do">## Estimate Std. Error z value Pr(>|z|) </span></span>
<span id="cb29-16"><a href="#cb29-16" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):1 -0.371567 1.100520 -0.338 0.73564 </span></span>
<span id="cb29-17"><a href="#cb29-17" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):2 2.549861 1.038657 2.455 0.01409 * </span></span>
<span id="cb29-18"><a href="#cb29-18" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):3 2.371634 0.589147 4.026 5.68e-05 ***</span></span>
<span id="cb29-19"><a href="#cb29-19" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.sunshine:1 0.132609 0.229104 0.579 0.56271 </span></span>
<span id="cb29-20"><a href="#cb29-20" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.sunshine:2 -0.069164 0.288310 -0.240 0.81041 </span></span>
<span id="cb29-21"><a href="#cb29-21" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.sunshine:3 -0.010579 0.150467 -0.070 0.94395 </span></span>
<span id="cb29-22"><a href="#cb29-22" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:1 -0.393682 0.343247 -1.147 0.25141 </span></span>
<span id="cb29-23"><a href="#cb29-23" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:2 0.490660 0.245832 1.996 0.04594 * </span></span>
<span id="cb29-24"><a href="#cb29-24" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:3 0.352822 0.153248 2.302 0.02132 * </span></span>
<span id="cb29-25"><a href="#cb29-25" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:1 0.602452 0.171697 3.509 0.00045 ***</span></span>
<span id="cb29-26"><a href="#cb29-26" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:2 -0.237287 0.187923 -1.263 0.20671 </span></span>
<span id="cb29-27"><a href="#cb29-27" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:3 0.138710 0.091933 1.509 0.13135 </span></span>
<span id="cb29-28"><a href="#cb29-28" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:1 -0.291666 0.333351 -0.875 0.38160 </span></span>
<span id="cb29-29"><a href="#cb29-29" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:2 0.547604 0.300788 1.821 0.06867 . </span></span>
<span id="cb29-30"><a href="#cb29-30" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:3 0.292045 0.164870 1.771 0.07650 . </span></span>
<span id="cb29-31"><a href="#cb29-31" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.sunshine:1 0.424514 0.320745 1.324 0.18566 </span></span>
<span id="cb29-32"><a href="#cb29-32" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.sunshine:2 -0.318405 0.575457 -0.553 0.58005 </span></span>
<span id="cb29-33"><a href="#cb29-33" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.sunshine:3 -0.002787 0.263883 -0.011 0.99157 </span></span>
<span id="cb29-34"><a href="#cb29-34" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:1 1.105727 0.426805 2.591 0.00958 ** </span></span>
<span id="cb29-35"><a href="#cb29-35" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:2 0.968945 0.343167 2.824 0.00475 ** </span></span>
<span id="cb29-36"><a href="#cb29-36" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:3 0.814436 0.257045 3.168 0.00153 ** </span></span>
<span id="cb29-37"><a href="#cb29-37" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:1 0.850133 0.306648 2.772 0.00557 ** </span></span>
<span id="cb29-38"><a href="#cb29-38" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:2 0.673887 0.362386 1.860 0.06294 . </span></span>
<span id="cb29-39"><a href="#cb29-39" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:3 0.835435 0.182237 4.584 4.55e-06 ***</span></span>
<span id="cb29-40"><a href="#cb29-40" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:1 0.503238 0.413168 1.218 0.22322 </span></span>
<span id="cb29-41"><a href="#cb29-41" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:2 -1.117189 0.553613 -2.018 0.04359 * </span></span>
<span id="cb29-42"><a href="#cb29-42" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:3 -0.453002 0.236248 -1.917 0.05518 . </span></span>
<span id="cb29-43"><a href="#cb29-43" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:1 -0.057617 0.007008 -8.222 < 2e-16 ***</span></span>
<span id="cb29-44"><a href="#cb29-44" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:2 0.001373 0.007364 0.186 0.85211 </span></span>
<span id="cb29-45"><a href="#cb29-45" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:3 -0.005830 0.004097 -1.423 0.15469 </span></span>
<span id="cb29-46"><a href="#cb29-46" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:1 0.015718 0.008700 1.807 0.07082 . </span></span>
<span id="cb29-47"><a href="#cb29-47" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:2 -0.049030 0.008632 -5.680 1.35e-08 ***</span></span>
<span id="cb29-48"><a href="#cb29-48" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:3 0.007070 0.004442 1.591 0.11150 </span></span>
<span id="cb29-49"><a href="#cb29-49" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:1 0.008991 0.006987 1.287 0.19816 </span></span>
<span id="cb29-50"><a href="#cb29-50" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:2 -0.000778 0.006570 -0.118 0.90573 </span></span>
<span id="cb29-51"><a href="#cb29-51" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:3 -0.031939 0.003389 -9.424 < 2e-16 ***</span></span>
<span id="cb29-52"><a href="#cb29-52" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:1 0.015571 0.007137 2.182 0.02913 * </span></span>
<span id="cb29-53"><a href="#cb29-53" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:2 0.017209 0.006668 2.581 0.00986 ** </span></span>
<span id="cb29-54"><a href="#cb29-54" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:3 0.018100 0.003719 4.866 1.14e-06 ***</span></span>
<span id="cb29-55"><a href="#cb29-55" aria-hidden="true" tabindex="-1"></a><span class="do">## ---</span></span>
<span id="cb29-56"><a href="#cb29-56" aria-hidden="true" tabindex="-1"></a><span class="do">## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span>
<span id="cb29-57"><a href="#cb29-57" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-58"><a href="#cb29-58" aria-hidden="true" tabindex="-1"></a><span class="do">## Names of linear predictors: log(mu[,1]/mu[,4]), log(mu[,2]/mu[,4]), log(mu[,3]/mu[,4])</span></span>
<span id="cb29-59"><a href="#cb29-59" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-60"><a href="#cb29-60" aria-hidden="true" tabindex="-1"></a><span class="do">## Residual deviance: 5982.475 on 9027 degrees of freedom</span></span>
<span id="cb29-61"><a href="#cb29-61" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-62"><a href="#cb29-62" aria-hidden="true" tabindex="-1"></a><span class="do">## Log-likelihood: -2991.238 on 9027 degrees of freedom</span></span>
<span id="cb29-63"><a href="#cb29-63" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-64"><a href="#cb29-64" aria-hidden="true" tabindex="-1"></a><span class="do">## Number of Fisher scoring iterations: 5 </span></span>
<span id="cb29-65"><a href="#cb29-65" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-66"><a href="#cb29-66" aria-hidden="true" tabindex="-1"></a><span class="do">## Warning: Hauck-Donner effect detected in the following estimate(s):</span></span>
<span id="cb29-67"><a href="#cb29-67" aria-hidden="true" tabindex="-1"></a><span class="do">## 'price.sunshine:1', 'price.kleebler:2'</span></span>
<span id="cb29-68"><a href="#cb29-68" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-69"><a href="#cb29-69" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb29-70"><a href="#cb29-70" aria-hidden="true" tabindex="-1"></a><span class="do">## Reference group is level 4 of the response</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p><u><strong>Modelo 2</strong></u></p>
<p>Sin las variables disp.sunshine y feat.sunshine, ya que son las únicas variables no significativas para las tres marcas. (“los tres modelos”)</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>modelo2 <span class="ot"><-</span> <span class="fu">vglm</span>((choice) <span class="sc">~</span> disp.kleebler <span class="sc">+</span> disp.nabisco <span class="sc">+</span> disp.private <span class="sc">+</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> feat.kleebler <span class="sc">+</span> feat.nabisco <span class="sc">+</span> feat.private <span class="sc">+</span> price.sunshine <span class="sc">+</span> price.kleebler <span class="sc">+</span> price.nabisco <span class="sc">+</span> price.private,</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="at">family=</span><span class="fu">multinomial</span>(<span class="at">refLevel=</span><span class="st">'private'</span>),</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> train_0)</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(modelo2)</span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a><span class="do">## Call:</span></span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a><span class="do">## vglm(formula = (choice) ~ disp.kleebler + disp.nabisco + disp.private + </span></span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler + feat.nabisco + feat.private + price.sunshine + </span></span>
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler + price.nabisco + price.private, family = multinomial(refLevel = "private"), </span></span>
<span id="cb30-11"><a href="#cb30-11" aria-hidden="true" tabindex="-1"></a><span class="do">## data = train_0)</span></span>
<span id="cb30-12"><a href="#cb30-12" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-13"><a href="#cb30-13" aria-hidden="true" tabindex="-1"></a><span class="do">## Coefficients: </span></span>
<span id="cb30-14"><a href="#cb30-14" aria-hidden="true" tabindex="-1"></a><span class="do">## Estimate Std. Error z value Pr(>|z|) </span></span>
<span id="cb30-15"><a href="#cb30-15" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):1 0.0151529 1.0657969 0.014 0.988656 </span></span>
<span id="cb30-16"><a href="#cb30-16" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):2 2.4433041 1.0162492 2.404 0.016206 * </span></span>
<span id="cb30-17"><a href="#cb30-17" aria-hidden="true" tabindex="-1"></a><span class="do">## (Intercept):3 2.3584486 0.5685170 4.148 3.35e-05 ***</span></span>
<span id="cb30-18"><a href="#cb30-18" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:1 -0.4109334 0.3456079 -1.189 0.234433 </span></span>
<span id="cb30-19"><a href="#cb30-19" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:2 0.4986944 0.2448975 2.036 0.041716 * </span></span>
<span id="cb30-20"><a href="#cb30-20" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.kleebler:3 0.3533531 0.1531462 2.307 0.021038 * </span></span>
<span id="cb30-21"><a href="#cb30-21" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:1 0.6265560 0.1710428 3.663 0.000249 ***</span></span>
<span id="cb30-22"><a href="#cb30-22" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:2 -0.2421768 0.1876353 -1.291 0.196815 </span></span>
<span id="cb30-23"><a href="#cb30-23" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.nabisco:3 0.1380648 0.0918016 1.504 0.132595 </span></span>
<span id="cb30-24"><a href="#cb30-24" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:1 -0.3080303 0.3326407 -0.926 0.354438 </span></span>
<span id="cb30-25"><a href="#cb30-25" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:2 0.5436649 0.3004812 1.809 0.070402 . </span></span>
<span id="cb30-26"><a href="#cb30-26" aria-hidden="true" tabindex="-1"></a><span class="do">## disp.private:3 0.2920867 0.1647212 1.773 0.076192 . </span></span>
<span id="cb30-27"><a href="#cb30-27" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:1 1.1671297 0.4266385 2.736 0.006226 ** </span></span>
<span id="cb30-28"><a href="#cb30-28" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:2 0.9557485 0.3415322 2.798 0.005135 ** </span></span>
<span id="cb30-29"><a href="#cb30-29" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.kleebler:3 0.8146497 0.2565710 3.175 0.001498 ** </span></span>
<span id="cb30-30"><a href="#cb30-30" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:1 0.8362242 0.3064157 2.729 0.006352 ** </span></span>
<span id="cb30-31"><a href="#cb30-31" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:2 0.6706660 0.3623797 1.851 0.064209 . </span></span>
<span id="cb30-32"><a href="#cb30-32" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.nabisco:3 0.8358562 0.1822010 4.588 4.48e-06 ***</span></span>
<span id="cb30-33"><a href="#cb30-33" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:1 0.4709882 0.4126084 1.141 0.253666 </span></span>
<span id="cb30-34"><a href="#cb30-34" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:2 -1.1065705 0.5533831 -2.000 0.045538 * </span></span>
<span id="cb30-35"><a href="#cb30-35" aria-hidden="true" tabindex="-1"></a><span class="do">## feat.private:3 -0.4531545 0.2362288 -1.918 0.055075 . </span></span>
<span id="cb30-36"><a href="#cb30-36" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:1 -0.0632866 0.0057637 -10.980 < 2e-16 ***</span></span>
<span id="cb30-37"><a href="#cb30-37" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:2 0.0030933 0.0065791 0.470 0.638230 </span></span>
<span id="cb30-38"><a href="#cb30-38" aria-hidden="true" tabindex="-1"></a><span class="do">## price.sunshine:3 -0.0057063 0.0035560 -1.605 0.108562 </span></span>
<span id="cb30-39"><a href="#cb30-39" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:1 0.0172282 0.0085896 2.006 0.044888 * </span></span>
<span id="cb30-40"><a href="#cb30-40" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:2 -0.0494362 0.0086086 -5.743 9.32e-09 ***</span></span>
<span id="cb30-41"><a href="#cb30-41" aria-hidden="true" tabindex="-1"></a><span class="do">## price.kleebler:3 0.0070815 0.0044332 1.597 0.110183 </span></span>
<span id="cb30-42"><a href="#cb30-42" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:1 0.0090329 0.0069624 1.297 0.194499 </span></span>
<span id="cb30-43"><a href="#cb30-43" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:2 -0.0008594 0.0065740 -0.131 0.895997 </span></span>
<span id="cb30-44"><a href="#cb30-44" aria-hidden="true" tabindex="-1"></a><span class="do">## price.nabisco:3 -0.0319371 0.0033881 -9.426 < 2e-16 ***</span></span>
<span id="cb30-45"><a href="#cb30-45" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:1 0.0156465 0.0071157 2.199 0.027887 * </span></span>
<span id="cb30-46"><a href="#cb30-46" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:2 0.0169181 0.0066223 2.555 0.010628 * </span></span>
<span id="cb30-47"><a href="#cb30-47" aria-hidden="true" tabindex="-1"></a><span class="do">## price.private:3 0.0180773 0.0036959 4.891 1.00e-06 ***</span></span>
<span id="cb30-48"><a href="#cb30-48" aria-hidden="true" tabindex="-1"></a><span class="do">## ---</span></span>
<span id="cb30-49"><a href="#cb30-49" aria-hidden="true" tabindex="-1"></a><span class="do">## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span>
<span id="cb30-50"><a href="#cb30-50" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-51"><a href="#cb30-51" aria-hidden="true" tabindex="-1"></a><span class="do">## Names of linear predictors: log(mu[,1]/mu[,4]), log(mu[,2]/mu[,4]), log(mu[,3]/mu[,4])</span></span>
<span id="cb30-52"><a href="#cb30-52" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-53"><a href="#cb30-53" aria-hidden="true" tabindex="-1"></a><span class="do">## Residual deviance: 5986.242 on 9033 degrees of freedom</span></span>
<span id="cb30-54"><a href="#cb30-54" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-55"><a href="#cb30-55" aria-hidden="true" tabindex="-1"></a><span class="do">## Log-likelihood: -2993.121 on 9033 degrees of freedom</span></span>
<span id="cb30-56"><a href="#cb30-56" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-57"><a href="#cb30-57" aria-hidden="true" tabindex="-1"></a><span class="do">## Number of Fisher scoring iterations: 5 </span></span>
<span id="cb30-58"><a href="#cb30-58" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-59"><a href="#cb30-59" aria-hidden="true" tabindex="-1"></a><span class="do">## Warning: Hauck-Donner effect detected in the following estimate(s):</span></span>
<span id="cb30-60"><a href="#cb30-60" aria-hidden="true" tabindex="-1"></a><span class="do">## 'price.sunshine:1', 'price.kleebler:2'</span></span>
<span id="cb30-61"><a href="#cb30-61" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-62"><a href="#cb30-62" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb30-63"><a href="#cb30-63" aria-hidden="true" tabindex="-1"></a><span class="do">## Reference group is level 4 of the response</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p><u><strong>Coeficientes modelo 2</strong></u></p>
<p>No existen más variables que no sean significativas a la hora de comparar la probabilidad de preferencias de cada una de las tres marcas contra la marca de referencia.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">coef</span>(modelo2, <span class="at">matrix =</span> <span class="cn">TRUE</span>)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> log(mu[,1]/mu[,4]) log(mu[,2]/mu[,4]) log(mu[,3]/mu[,4])
(Intercept) 0.015152926 2.4433041462 2.358448597
disp.kleebler -0.410933438 0.4986943571 0.353353124
disp.nabisco 0.626556037 -0.2421768133 0.138064808
disp.private -0.308030320 0.5436648975 0.292086742
feat.kleebler 1.167129719 0.9557485448 0.814649652
feat.nabisco 0.836224200 0.6706659893 0.835856223
feat.private 0.470988159 -1.1065705217 -0.453154538
price.sunshine -0.063286638 0.0030933268 -0.005706344
price.kleebler 0.017228172 -0.0494362244 0.007081537
price.nabisco 0.009032948 -0.0008593535 -0.031937054
price.private 0.015646453 0.0169181035 0.018077284</code></pre>
</div>
</div>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>mycol <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"red"</span>,<span class="st">"darkgreen"</span>,<span class="st">"blue"</span>)</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" 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">3</span>))</span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plotvgam</span>(modelo2, <span class="at">se=</span><span class="cn">TRUE</span>, <span class="at">scale=</span><span class="dv">12</span>,</span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a> <span class="at">lcol=</span>mycol[<span class="dv">1</span>], <span class="at">scol=</span>mycol[<span class="dv">1</span>], <span class="at">which.term=</span><span class="dv">1</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">1</span>))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" style="width:100.0%"></p>
</div>
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plotvgam</span>(modelo2, <span class="at">se=</span><span class="cn">TRUE</span>, <span class="at">scale=</span><span class="dv">12</span>,</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> <span class="at">lcol=</span>mycol[<span class="dv">2</span>], <span class="at">scol=</span>mycol[<span class="dv">2</span>], <span class="at">which.term=</span><span class="dv">2</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">1</span>))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-22-2.png" class="img-fluid" style="width:100.0%"></p>
</div>
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plotvgam</span>(modelo2, <span class="at">se=</span><span class="cn">TRUE</span>, <span class="at">scale=</span><span class="dv">12</span>,</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a> <span class="at">lcol=</span>mycol[<span class="dv">3</span>], <span class="at">scol=</span>mycol[<span class="dv">3</span>], <span class="at">which.term=</span><span class="dv">3</span>, <span class="at">ylim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">1</span>))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-22-3.png" class="img-fluid" style="width:100.0%"></p>
</div>
</div>
<section id="test-modelo-2" class="level3 underline">
<h3 class="underline anchored" data-anchor-id="test-modelo-2">Test modelo 2</h3>
<p>La predicción del modelo nos devuelte el valor <em>response</em> o el valor <em>link</em>.</p>
<p>El valor <em>link</em> compara la probabilidad de elección de cada marca en comparación con la marca de referencia. Es decir, para cada nivel de la variable respuesta la predicción nos indica si es menos probable que la marca de referencia (valor menor a 0), igual de probable (valor igual a 0) o más probable (valor mayor a 0).</p>
<p>El valor <em>response</em> nos devuelve la probabilidad de elección de cada marca.</p>
<div>
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">predictvglm</span>(modelo2, <span class="at">newdata =</span> test_0, <span class="at">type =</span> <span class="st">"link"</span>) <span class="sc">|></span> <span class="fu">head</span>()</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="fu">predictvglm</span>(modelo2, <span class="at">newdata =</span> test_0, <span class="at">type =</span> <span class="st">"response"</span>) <span class="sc">|></span> <span class="fu">head</span>()</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell quarto-layout-panel">
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="cell-output cell-output-stdout quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<pre><code> log(mu[,1]/mu[,4]) log(mu[,2]/mu[,4]) log(mu[,3]/mu[,4])
12 -2.5586249 -1.831425 0.9862012
32 -2.2252401 -2.620349 -0.2720557
42 -0.5227571 -1.882065 0.8922528
68 -2.8907497 -2.296808 0.1787439
51 -2.9610127 -1.772618 0.6944356
87 -1.9756270 -2.121116 0.1297697</code></pre>
</div>
<div class="cell-output cell-output-stdout quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<pre><code> sunshine kleebler nabisco private
12 0.01975466 0.04087786 0.6841760 0.2551914
32 0.05561607 0.03746334 0.3921547 0.5147659
42 0.14164231 0.03637921 0.5830744 0.2389041
68 0.02361336 0.04276647 0.5084191 0.4252011
51 0.01605544 0.05269086 0.6211024 0.3101513
87 0.05784995 0.05001702 0.4749689 0.4171641</code></pre>
</div>
</div>
</div>
</div>
<p>En el primer caso, como log(mu[,1]/mu[,4]) es menor a 0, sabemos que la probabilidad de que el usuario escoja la marca sunshine es menor que la probabilidad de escoger la marca private. <span class="math inline">\(\exp(-2.9901208)\)</span> = 0.0502814<br>
<span class="math inline">\(\frac{0.01433895}{0.2851743}\)</span> = 0.0502814</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>pred <span class="ot"><-</span> </span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="fu">predictvglm</span>(modelo2, <span class="at">newdata =</span> train_0, </span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"response"</span>, </span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <span class="at">se.fit =</span> <span class="cn">FALSE</span>, <span class="at">deriv =</span> <span class="dv">0</span>, <span class="at">dispersion =</span> <span class="cn">NULL</span>,</span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a> <span class="at">untransform =</span> <span class="cn">FALSE</span>)</span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-7"><a href="#cb39-7" aria-hidden="true" tabindex="-1"></a>pred <span class="ot"><-</span> </span>
<span id="cb39-8"><a href="#cb39-8" aria-hidden="true" tabindex="-1"></a><span class="fu">cbind</span>(</span>
<span id="cb39-9"><a href="#cb39-9" aria-hidden="true" tabindex="-1"></a>pred <span class="sc">|></span> <span class="fu">as.data.frame</span>() <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">id=</span><span class="fu">row_number</span>()) <span class="sc">|></span> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"category"</span>, <span class="at">cols =</span> <span class="fu">c</span>(sunshine,kleebler,nabisco,private)) <span class="sc">|></span> <span class="fu">slice_max</span>(value, <span class="at">n=</span><span class="dv">1</span>, <span class="at">by =</span> (id)),</span>
<span id="cb39-10"><a href="#cb39-10" aria-hidden="true" tabindex="-1"></a><span class="at">choice =</span> train_0<span class="sc">$</span>choice) <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">category=</span><span class="fu">as.factor</span>(category))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>La tasa de acierto es muy baja.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>caret<span class="sc">::</span><span class="fu">confusionMatrix</span>(<span class="at">data=</span>pred<span class="sc">$</span>category, <span class="at">reference =</span> pred<span class="sc">$</span>choice)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction sunshine kleebler nabisco private
sunshine 18 3 10 13
kleebler 0 0 0 0
nabisco 163 161 1418 703
private 29 39 225 240
Overall Statistics
Accuracy : 0.5546
95% CI : (0.5367, 0.5724)
No Information Rate : 0.547
P-Value [Acc > NIR] : 0.2055
Kappa : 0.1103
Mcnemar's Test P-Value : <2e-16
Statistics by Class:
Class: sunshine Class: kleebler Class: nabisco Class: private
Sensitivity 0.085714 0.00000 0.8578 0.25105
Specificity 0.990754 1.00000 0.2498 0.85818
Pos Pred Value 0.409091 NaN 0.5800 0.45028
Neg Pred Value 0.935527 0.93283 0.5927 0.71233
Prevalence 0.069490 0.06717 0.5470 0.31635
Detection Rate 0.005956 0.00000 0.4692 0.07942
Detection Prevalence 0.014560 0.00000 0.8091 0.17637
Balanced Accuracy 0.538234 0.50000 0.5538 0.55461</code></pre>
</div>
</div>
</section>
</section>
<section id="modelo-respuesta-múltiple-nominal-medidas-repetidas" class="level2">
<h2 class="anchored" data-anchor-id="modelo-respuesta-múltiple-nominal-medidas-repetidas">Modelo respuesta múltiple nominal medidas repetidas</h2>
<p>Hasta el momento no hemos tenido en cuenta en el modelo predictivo que estamos usando información con medidas repetidas, ya que para cada usuario tenemos más de un evento donde ha elegido entre las cuatro marcas.</p>
<p>El modelo saturado inicial cuenta con una parte aleatoria. * Intersección aleatorioa. Cada usuario tiene su valor base en la relación entre la variable respuesta y las variables independientes. * Pendiente aleatoria. La relación entre cada una de las variables dependientes y la variable independiente será diferente para cada usuario. La relación entre cada variable dependiente y la dependiente puede ser diferente para cada usuario.</p>
<p><u><strong>Modelo 3</strong></u></p>
<p>Modelo saturado.</p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>modelo3 <span class="ot"><-</span> <span class="fu">mblogit</span>(<span class="fu">relevel</span>(choice, <span class="at">ref =</span> <span class="st">"private"</span>) <span class="sc">~</span> disp.sunshine <span class="sc">+</span> disp.kleebler <span class="sc">+</span> disp.nabisco <span class="sc">+</span> disp.private <span class="sc">+</span> feat.sunshine <span class="sc">+</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a>feat.kleebler <span class="sc">+</span> feat.nabisco <span class="sc">+</span> feat.private <span class="sc">+</span> price.sunshine <span class="sc">+</span> price.kleebler <span class="sc">+</span> price.nabisco <span class="sc">+</span> price.private, </span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> <span class="at">random =</span> <span class="sc">~</span> <span class="dv">1</span> <span class="sc">|</span> id, </span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> train_0,</span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a><span class="at">control =</span> <span class="fu">mmclogit.control</span>(<span class="at">epsilon =</span> <span class="fl">1e-10</span>,</span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="at">maxit =</span> <span class="dv">50</span>, <span class="at">trace=</span><span class="cn">TRUE</span>, <span class="co"># Intento aumentar número de iteraciones para que converga</span></span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.inner=</span><span class="cn">FALSE</span>,</span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a> <span class="at">avoid.increase =</span> <span class="cn">FALSE</span>,</span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.increase =</span> <span class="cn">FALSE</span>,</span>
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.infinite =</span> <span class="cn">FALSE</span>,</span>
<span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.negative =</span> <span class="cn">FALSE</span>))</span>
<span id="cb42-12"><a href="#cb42-12" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb42-13"><a href="#cb42-13" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 1 - deviance = 3827.34 - criterion = 0.759349</span></span>
<span id="cb42-14"><a href="#cb42-14" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 2 - deviance = 3367.426 - criterion = 0.06472519</span></span>
<span id="cb42-15"><a href="#cb42-15" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 3 - deviance = 3219.024 - criterion = 0.02117881</span></span>
<span id="cb42-16"><a href="#cb42-16" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 4 - deviance = 3146.159 - criterion = 0.005016585</span></span>
<span id="cb42-17"><a href="#cb42-17" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 5 - deviance = 3117.599 - criterion = 0.0007487687</span></span>
<span id="cb42-18"><a href="#cb42-18" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 6 - deviance = 3107.389 - criterion = 1.998695e-05</span></span>
<span id="cb42-19"><a href="#cb42-19" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 7 - deviance = 3106.231 - criterion = 7.095098e-07</span></span>
<span id="cb42-20"><a href="#cb42-20" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 8 - deviance = 3106.079 - criterion = 2.944966e-13</span></span>
<span id="cb42-21"><a href="#cb42-21" aria-hidden="true" tabindex="-1"></a><span class="do">## converged</span></span>
<span id="cb42-22"><a href="#cb42-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-23"><a href="#cb42-23" aria-hidden="true" tabindex="-1"></a><span class="co">#summary(modelo3)</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>pred <span class="ot"><-</span> </span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="fu">predict</span>(modelo3, test_0, <span class="at">type=</span><span class="st">"response"</span>)</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a>pred <span class="ot"><-</span> </span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="fu">cbind</span>(</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a>pred <span class="sc">|></span> <span class="fu">as.data.frame</span>() <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">id=</span><span class="fu">row_number</span>()) <span class="sc">|></span> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"category"</span>, <span class="at">cols =</span> <span class="fu">c</span>(sunshine,kleebler,nabisco,private)) <span class="sc">|></span> <span class="fu">slice_max</span>(value, <span class="at">n=</span><span class="dv">1</span>, <span class="at">by =</span> (id)),</span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a><span class="at">choice =</span> test_0<span class="sc">$</span>choice) <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">category=</span><span class="fu">as.factor</span>(category))</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>El modelo mblogit usa un proceso iterativo para ajustar los parámetros del modelo. El algoritmo busca minimizar o maximizar una función objetivo (verosimilitud del modelo).</p>
<p>La convergencia se alcanza en el punto donde los cambios en los parámetros de una iteración a la siguiente son lo suficientemente pequeños como para considerar que el proceso ha encontrado una solución óptima.</p>
<p>Podemos tener casos donde el algortimo nos advierte de que no ha encontrado esta convergencia. Posibles causas:</p>
<ul>
<li><p><strong>Multicolinealidad.</strong> Una alta correlación entre las variables independientes puede hacer que el modelo tenga dificultades para encontrar una solución estable.</p></li>
<li><p><strong>Datos desbalanceados.</strong> Si los datos están muy desbalanceados puede provocar que el modelo tenga problemas para converger.</p></li>
<li><p><strong>Valores atípicos.</strong> El ruido en los datos pueden interferir con el proceso de convergencia.</p></li>
<li><p><strong>Modelo demasiado complejo.</strong> Un modelo demasiado complejo o mal especificado puede dificultar la convergencia.</p></li>
<li><p><strong>Escalado de variables.</strong> Variables en escalas muy diferentes pueden causar problemas de convergencia.</p></li>
</ul>
<p>En este punto tenemos tres posibles formas de resolver el problema:</p>
<ul>
<li>Verificar la multicolinealidad. Usar el VIF para verificar si las variables independientes están correlacionadas.</li>
<li>Escalar las variables. Tenemos variables dicotómicas y variables contínuas de precio.</li>
<li>Aumenta el número de iteraciones o ajusta la tolerancia. (En realidad hemos bajado el número de iteraciones en el modelo3 para encontrarnos con este problema)</li>
</ul>
<section id="vif" class="level3">
<h3 class="anchored" data-anchor-id="vif">VIF</h3>
<p>El VIF está basado en la suposición de un modelo lineal.</p>
<p>Cuando la variable respuesta es categórica, no podemos calcular directamente el VIF. Sin embargo, podemos evaluar la multicolinealidad entre las variables independientes antes de ajustar el modelo usando un modelo lineal auxiliar donde se trata una de las variables independientes como la variable respuesta.</p>
<ol type="1">
<li>Ignoramos la variable categórica respuesta. La multicolinealidad es un problema que afecta a las relaciones entre las variables independientes. La variable respuesta categórica no influye en la multicolinealidad entre las predictoras.</li>
<li>Ajustamos un modelo de regresión lineal donde cada una de las variables independientes es la variable dependiente del modelo lineal.</li>
<li>Calcular el VIF para cada variable independiente.</li>
</ol>
</section>
<section id="escalado-de-variables" class="level3">
<h3 class="anchored" data-anchor-id="escalado-de-variables">Escalado de variables</h3>
<p><u><strong>Escalado:</strong></u></p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Escalado del conjunto de entrenamiento</span></span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a>train_1 <span class="ot"><-</span> train_0 <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">price.sunshine =</span> <span class="fu">scale</span>(price.sunshine),</span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a> <span class="at">price.kleebler =</span> <span class="fu">scale</span>(price.kleebler),</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a> <span class="at">price.nabisco =</span> <span class="fu">scale</span>(price.nabisco),</span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a> <span class="at">price.private =</span> <span class="fu">scale</span>(price.private))</span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(train_1)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> id disp.sunshine disp.kleebler disp.nabisco disp.private feat.sunshine
Min. : 1.00 Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
1st Qu.: 35.00 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
Median : 67.00 Median :0.0000 Median :0.0000 Median :0.0000 Median :0.00000 Median :0.00000
Mean : 67.46 Mean :0.1281 Mean :0.1056 Mean :0.3388 Mean :0.09696 Mean :0.03805
3rd Qu.: 99.00 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
Max. :136.00 Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
feat.kleebler feat.nabisco feat.private price.sunshine.V1 price.kleebler.V1
Min. :0.00000 Min. :0.00000 Min. :0.00000 Min. :-3.504420 Min. :-2.3197739
1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:-0.493700 1st Qu.:-0.7184402
Median :0.00000 Median :0.00000 Median :0.00000 Median : 0.108444 Median :-0.3416558
Mean :0.04269 Mean :0.08637 Mean :0.04533 Mean : 0.000000 Mean : 0.0000000
3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.: 0.710588 3rd Qu.: 0.7886974
Max. :1.00000 Max. :1.00000 Max. :1.00000 Max. : 2.517020 Max. : 2.4842272
price.nabisco.V1 price.private.V1 choice
Min. :-4.187234 Min. :-2.440051 sunshine: 210
1st Qu.:-0.640322 1st Qu.:-0.736038 kleebler: 203
Median : 0.069060 Median :-0.249177 nabisco :1653
Mean : 0.000000 Mean : 0.000000 private : 956
3rd Qu.: 0.778443 3rd Qu.: 0.805689
Max. : 4.325355 Max. : 3.807999 </code></pre>
</div>
</div>
<p><u><strong>Normalización:</strong></u></p>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Normalizado del conjunto de entrenamiento</span></span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a>normalizar <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> min_values <span class="ot"><-</span> <span class="fu">min</span>(x)</span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> max_values <span class="ot"><-</span> <span class="fu">max</span>(x)</span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a> (x <span class="sc">-</span> min_values) <span class="sc">/</span> (max_values <span class="sc">-</span> min_values)</span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a>train_2 <span class="ot"><-</span> train_0 <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">price.sunshine =</span> <span class="fu">normalizar</span>(price.sunshine),</span>
<span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a> <span class="at">price.kleebler =</span> <span class="fu">normalizar</span>(price.kleebler),</span>
<span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a> <span class="at">price.nabisco =</span> <span class="fu">normalizar</span>(price.nabisco),</span>
<span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a> <span class="at">price.private =</span> <span class="fu">normalizar</span>(price.private))</span>
<span id="cb46-13"><a href="#cb46-13" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(train_2)</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> id disp.sunshine disp.kleebler disp.nabisco disp.private feat.sunshine
Min. : 1.00 Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
1st Qu.: 35.00 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
Median : 67.00 Median :0.0000 Median :0.0000 Median :0.0000 Median :0.00000 Median :0.00000
Mean : 67.46 Mean :0.1281 Mean :0.1056 Mean :0.3388 Mean :0.09696 Mean :0.03805
3rd Qu.: 99.00 3rd Qu.:0.0000 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
Max. :136.00 Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
feat.kleebler feat.nabisco feat.private price.sunshine price.kleebler price.nabisco
Min. :0.00000 Min. :0.00000 Min. :0.00000 Min. :0.000 Min. :0.0000 Min. :0.0000
1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.500 1st Qu.:0.3333 1st Qu.:0.4167
Median :0.00000 Median :0.00000 Median :0.00000 Median :0.600 Median :0.4118 Median :0.5000
Mean :0.04269 Mean :0.08637 Mean :0.04533 Mean :0.582 Mean :0.4829 Mean :0.4919
3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.700 3rd Qu.:0.6471 3rd Qu.:0.5833
Max. :1.00000 Max. :1.00000 Max. :1.00000 Max. :1.000 Max. :1.0000 Max. :1.0000
price.private choice
Min. :0.0000 sunshine: 210
1st Qu.:0.2727 kleebler: 203
Median :0.3506 nabisco :1653
Mean :0.3905 private : 956
3rd Qu.:0.5195
Max. :1.0000 </code></pre>
</div>
</div>
<div class="cell">
<details>
<summary>Código</summary>
<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>modelo3_escalado <span class="ot"><-</span> <span class="fu">mblogit</span>(<span class="fu">relevel</span>(choice, <span class="at">ref =</span> <span class="st">"private"</span>) <span class="sc">~</span> disp.sunshine <span class="sc">+</span> disp.kleebler <span class="sc">+</span> disp.nabisco <span class="sc">+</span> disp.private <span class="sc">+</span> feat.sunshine <span class="sc">+</span></span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a>feat.kleebler <span class="sc">+</span> feat.nabisco <span class="sc">+</span> feat.private <span class="sc">+</span> price.sunshine <span class="sc">+</span> price.kleebler <span class="sc">+</span> price.nabisco <span class="sc">+</span> price.private, </span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a> <span class="at">random =</span> <span class="sc">~</span> <span class="dv">1</span> <span class="sc">|</span> id, </span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> train_2,</span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a><span class="at">control =</span> <span class="fu">mmclogit.control</span>(<span class="at">epsilon =</span> <span class="fl">1e-10</span>,</span>
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a> <span class="at">maxit =</span> <span class="dv">50</span>, <span class="at">trace=</span><span class="cn">TRUE</span>, <span class="co"># Intento aumentar número de iteraciones para que converga</span></span>
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a> <span class="at">trace.inner=</span><span class="cn">FALSE</span>,</span>
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a> <span class="at">avoid.increase =</span> <span class="cn">FALSE</span>,</span>
<span id="cb48-9"><a href="#cb48-9" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.increase =</span> <span class="cn">FALSE</span>,</span>
<span id="cb48-10"><a href="#cb48-10" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.infinite =</span> <span class="cn">FALSE</span>,</span>
<span id="cb48-11"><a href="#cb48-11" aria-hidden="true" tabindex="-1"></a> <span class="at">break.on.negative =</span> <span class="cn">FALSE</span>))</span>
<span id="cb48-12"><a href="#cb48-12" aria-hidden="true" tabindex="-1"></a><span class="do">## </span></span>
<span id="cb48-13"><a href="#cb48-13" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 1 - deviance = 3827.34 - criterion = 0.759349</span></span>
<span id="cb48-14"><a href="#cb48-14" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 2 - deviance = 3367.426 - criterion = 0.06472519</span></span>
<span id="cb48-15"><a href="#cb48-15" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 3 - deviance = 3219.024 - criterion = 0.02117881</span></span>
<span id="cb48-16"><a href="#cb48-16" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 4 - deviance = 3146.159 - criterion = 0.005016585</span></span>
<span id="cb48-17"><a href="#cb48-17" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 5 - deviance = 3117.599 - criterion = 0.0007487687</span></span>
<span id="cb48-18"><a href="#cb48-18" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 6 - deviance = 3107.389 - criterion = 1.998695e-05</span></span>
<span id="cb48-19"><a href="#cb48-19" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 7 - deviance = 3106.231 - criterion = 7.095098e-07</span></span>
<span id="cb48-20"><a href="#cb48-20" aria-hidden="true" tabindex="-1"></a><span class="do">## Iteration 8 - deviance = 3106.079 - criterion = 2.944971e-13</span></span>
<span id="cb48-21"><a href="#cb48-21" aria-hidden="true" tabindex="-1"></a><span class="do">## converged</span></span>
<span id="cb48-22"><a href="#cb48-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-23"><a href="#cb48-23" aria-hidden="true" tabindex="-1"></a><span class="co">#summary(modelo3)</span></span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="test-modelo3" class="level3 underline">
<h3 class="underline anchored" data-anchor-id="test-modelo3">Test modelo3</h3>
<p>En este caso los resultados del modelo3 y el modelo3_escalado son los mismos y solo cambia el valor de los parámetros.</p>
<div class="cell">
<details>
<summary>Código</summary>