forked from FukamiLab/BIO202
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-A-mixed-models.html
1046 lines (955 loc) · 48.4 KB
/
04-A-mixed-models.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">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Tad & Beth" />
<title>Mixed-effects models with nested data</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = false;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="00-computer-setup.html">Computer Setup</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W1
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="01-A-R-intro.html">Intro to R</a>
</li>
<li>
<a href="01-B-Rmarkdown-intro.html">R markdown</a>
</li>
<li>
<a href="01-C-R-workshop.html">R workshop</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W2
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="02-A-tidyr.html">ggplot2 and tidyr</a>
</li>
<li>
<a href="02-B-git-intro.html">Intro to git</a>
</li>
<li>
<a href="02-C-student-projects.html">Project introductions</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W3
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="03-A-data-exploration.html">Data exploration</a>
</li>
<li>
<a href="03-B-linear-models.html">Linear models</a>
</li>
<li>
<a href="03-C-heterogeneity.html">Heterogeneity</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W4
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="04-A-mixed-models.html">Mixed effects models</a>
</li>
<li>
<a href="04-B-binary-data.html">Binary & proportional data</a>
</li>
<li>
<a href="04-C-zero-data.html">Zero-inflated data</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W5
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="05-A-Bayesian-linear-models.html">Bayesian linear models</a>
</li>
<li>
<a href="05-B-Bayesian-priors.html">Bayesian inference with prior information</a>
</li>
<li>
<a href="05-C-Advanced-bayesian-example.html">Advanced Bayesian model example</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W6
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="06-A-unconstrained-ordination.html">Unconstrained ordination</a>
</li>
<li>
<a href="06-B-constrained-ordination.html">Constrained ordination</a>
</li>
<li>
<a href="06-C-matrix-comparison.html">Comparing multivariate data</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W8
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="08-A-mapping.html">Visualizing spatial data</a>
</li>
<li>
<a href="08-B-spatial-regression.html">Spatial regression</a>
</li>
<li>
<a href="08-C-spatial-ordination.html">Ordination approach to spatial analysis</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
W9
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="09-A-time-series.html">Time series</a>
</li>
<li>
<a href="09-B-networks.html">Network analysis</a>
</li>
<li>
<a href="09-C-occupancy-models.html">Occupancy models</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Mixed-effects models with nested data</h1>
<h4 class="author"><em>Tad & Beth</em></h4>
</div>
<p><strong>Assigned Reading:</strong></p>
<blockquote>
<p>Chapter 5 from: Zuur, A. F., Ieno, E. N., Walker, N., Saveliev, A. A. and Smith, G. M. 2009. <em>Mixed Effects Models and Extensions in Ecology with R.</em> Springer. <a href="https://link-springer-com.stanford.idm.oclc.org/book/10.1007%2F978-0-387-87458-6">link</a></p>
</blockquote>
<div id="key-points" class="section level3">
<h3>Key Points</h3>
<div id="fixed-vs.random-effects" class="section level4">
<h4>Fixed vs. random effects</h4>
<ul>
<li><p>Fixed and random effects affect mean and variance of y, respectively.</p></li>
<li><p>Examples</p>
<pre><code>Fixed: Nutrient added or not, male or female, upland or lowland, wet versus dry, light versus shade, one age versus another
Random: genotype, block within a field, individuals with repeated measures, family, parent</code></pre></li>
<li><p>Mixed-effects models maximize use of info in the data, compared to 2-stage analysis.</p></li>
</ul>
</div>
<div id="linear-mixed-effects-models" class="section level4">
<h4>Linear mixed-effects models</h4>
<ul>
<li><p>Random intercept model</p></li>
<li><p>Random intercept and slope model</p></li>
<li><p>Random effects model</p></li>
</ul>
<div class="figure">
<img src="images/random-effects-plot.png" style="width:50.0%" />
</div>
</div>
<div id="model-selection-and-validation" class="section level4">
<h4>Model selection and validation</h4>
<ul>
<li><p>Step 1: fit linear regression</p></li>
<li><p>Step 2: fit model with gls (so linear regression model can be compared with mixed-effects models)</p></li>
<li><p>Step 3: choose variance strcuture</p></li>
<li>Introduce random effects, and/or</li>
<li><p>Adjust variance structure to take care of heterogeneity</p></li>
<li><p>Step 4: fit the model</p></li>
<li><p>Make sure <code>method="REML"</code></p>
<p><code>M1.lme=lme(Form, random=~1|Nest, method="REML", data=Owls)</code></p></li>
<li><p>Step 5: compare new mixed-effects model with old</p>
<p><code>Form <- formula(LogNeg~SexParent*FoodTreatment+SexParent*ArrivalTime)</code></p>
<p><code>M.gls = gls(Form, data=Owls)</code></p>
<p><code>M1.lme=lme(Form, random=~1|Nest, method="REML", data=Owls)</code></p>
<p><code>anova(M.gls, M1.lme)</code></p></li>
<li><p>Stept 6: validate model and, if necessary, repeat steps 4 and 5 until good model is found</p></li>
<li><p>Steps 7 and 8: find optimal fixed effects structure</p>
<p>First, interaction terms; drop one at time and compare models</p>
<p>Use <code>methods="ML"</code></p></li>
<li><p>Step 9: refit with REML and validate model</p></li>
<li><p>Step 10: interpret</p></li>
<li><p>Step 11: additive model</p></li>
</ul>
</div>
<div id="additional-reading" class="section level4">
<h4>Additional reading</h4>
<blockquote>
<p>Chapter 19. Mixed-Effercts Models, in Crawley 2012 The R Book, 2nd ed. <a href="http://onlinelibrary.wiley.com/doi/10.1002/9780470515075.ch19/summary">link</a></p>
</blockquote>
<ul>
<li><p>Fixed vs. random effects</p></li>
<li><p>Pseudoreplication</p></li>
<li><p>More examples of mixed-effects models for different sampling and experimental designs</p></li>
</ul>
<hr />
</div>
</div>
<div id="analysis-example" class="section level3">
<h3>Analysis Example</h3>
<p>The data:</p>
<ul>
<li>19 plant-hummingbird networks - hummingbirds using plants as nectar resources<br />
</li>
<li>19 sites with increasing proportion of forest cover in 100m radius around sampling site (forest is replaced with coffee agriculture in the study region)<br />
</li>
<li>13 hummingbird species<br />
</li>
<li>Plant-hummingbird networks used to calculate hummingbird species’ specialization</li>
</ul>
<p>Hummingbird species specialization measured using z-score standardized d’ values</p>
<ul>
<li>One d’ value for each species at each site<br />
</li>
<li>The greater the d’ value, the more specialized the species</li>
</ul>
<div id="question-is-there-a-relationship-between-hummingbird-species-specialization-and-forest-cover" class="section level4">
<h4>Question: Is there a relationship between hummingbird species specialization and forest cover?</h4>
<p>Load the data</p>
<p>This is a dataset of 3 columns:</p>
<ul>
<li>data[ ,1] = proportion of forest cover at 100m radius around sampling site where hummingbird species was caught<br />
</li>
<li>data[ ,2] = each hummingbird species as a factor at each site it was caught<br />
</li>
<li>data[ ,3] = the specialization (d’) value for each species at each site</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Beth's data are not availale on our class GitHub repository. </span>
<span class="co"># To use this script, download the data from our class Canvas site and place it in the data folder of your working directory.</span>
<span class="co"># Use this line if you downloaded the data file to the data directory on your computer</span>
spec <-<span class="st"> </span><span class="kw">read.csv</span>(<span class="dt">file =</span> <span class="st">"data/Specialization_Data_BIO202.csv"</span>, <span class="dt">header =</span> <span class="ot">TRUE</span>)
<span class="kw">head</span>(spec)</code></pre></div>
<pre><code>## Polygon.Area.100 fSpecies Specialization
## 1 1 Charming.Hummingbird -0.574054
## 2 1 Green.Hermit 8.986848
## 3 1 Rufous.tailed.Hummingbird 1.360791
## 4 1 Snowy.bellied.Hummingbird 1.980888
## 5 1 Stripe.throated.Hermit 2.902282
## 6 1 Violet.crowned.Woodnymph 1.662080</code></pre>
<p>Originally when I did this analysis I followed Zuur et al.’s “10-step process” in the book starting on pg. 130. I do not outline all the steps I took here, but I found it useful in case any of you are interested for future reference.</p>
<p>All analayses are done using the package <code>{nlme}</code> but can also be done in <code>{lme4}</code></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(nlme)</code></pre></div>
<p>Since I want to know if hummingbird specialization changes with increasing forest cover, I can start with a simple linear regression</p>
<p>Side note: instead of using the <code>lm()</code> function for my linear model, I use the <code>gls()</code> function in case I need to add a variance later on, this way I can compare the model with no variance structure to the model with the variance structure.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">Form <-<span class="st"> </span>Specialization ~<span class="st"> </span>Polygon.Area<span class="fl">.100</span>
M.lm <-<span class="st"> </span><span class="kw">gls</span>(Form, <span class="dt">data =</span> spec)
<span class="kw">plot</span>(Form, <span class="dt">data =</span> spec, <span class="dt">xlab =</span> <span class="st">"Forest Cover (100m)"</span>, <span class="dt">ylab =</span> <span class="st">"Specialization (d')"</span>)
<span class="kw">abline</span>(M.lm, <span class="dt">lwd =</span> <span class="dv">4</span>)</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-4-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(M.lm)</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-5-1.png" width="672" /></p>
<p>The cone shape in my residuals illustrates that my data are violating the assumption of homogeneity, so I will apply a variance structure to hopefully take care of this.</p>
<p>The best variance structure I found was the varPower structure</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">vf3 <-<span class="st"> </span><span class="kw">varPower</span>(<span class="dt">form =</span> ~Polygon.Area<span class="fl">.100</span>) ## power of the covariate variance structure = the variance of the residuals multiplied with the power of the absolute value of the variance covariate forest cover
## nested within linear model so can be compared to linear model fit using log likelihood ratio
## Variance covariate can not have values equal to zero
M.gls3 <-<span class="st"> </span><span class="kw">gls</span>(Specialization ~<span class="st"> </span>Polygon.Area<span class="fl">.100</span>, <span class="dt">weights =</span> vf3, <span class="dt">data =</span> spec)
<span class="kw">AIC</span>(M.lm, M.gls3) ## we used the gls function earlier instead of the lm function so we can do this anova</code></pre></div>
<pre><code>## df AIC
## M.lm 3 908.2300
## M.gls3 4 851.0649</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(M.gls3) ## you can see the residuals are now less cone-y</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-7-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">summary</span>(M.gls3)</code></pre></div>
<pre><code>## Generalized least squares fit by REML
## Model: Specialization ~ Polygon.Area.100
## Data: spec
## AIC BIC logLik
## 851.0649 863.4399 -421.5325
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.7317331
##
## Coefficients:
## Value Std.Error t-value p-value
## (Intercept) 0.193740 0.3090522 0.626885 0.5316
## Polygon.Area.100 4.830138 0.8255718 5.850657 0.0000
##
## Correlation:
## (Intr)
## Polygon.Area.100 -0.791
##
## Standardized residuals:
## Min Q1 Med Q3 Max
## -1.5689357 -0.6917083 -0.3274916 0.5277962 2.7803202
##
## Residual standard error: 5.450152
## Degrees of freedom: 165 total; 163 residual</code></pre>
<p>From the model with the variance structure we can tell that it looks like there is a relationship between hummingbird species specialization and amount of forest cover, so that’s neat.</p>
<p>Biologically, however, it’s totally possible that each of the 13 hummingbird species has its own relationship between specialization and forest cover. For example, a morphologically specialized hummingbird species that has a really long, curved beak may not be able to display the same degree of change in its specialization as a hummingbird with a medium-length, straight beak.</p>
<p>In addition, as with the example of the RIKZ data from 5 beaches in the Zuur book Chp. 5, the specialization values of each hummingbird species across the sites are likely to be more related to each other than to the specialization values of other hummingbird species. This makes the data nested.</p>
<p>Thus, a mixed effects model for nested data is applicable in this case!</p>
</div>
<div id="random-intercept-model" class="section level4">
<h4>Random Intercept Model</h4>
<p>We can model specialization as a linear function of forest cover where the intercept is allowed to change per hummingbird species. This biologically makes sense because it is expected that each hummingbird species will have its own unique specialization behavior at each site.</p>
<p>Allowing the intercept to vary for each species is a random intercept model</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">Mlme1 <-<span class="st"> </span><span class="kw">lme</span>(Form, <span class="dt">random =</span> ~<span class="dv">1</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3)
## lme stands for linear mixed effects. In this function you must specify a "random" argument
## ~1|fSpecies specifies the random intercept model. Remember | in this case means "by" so you are allowing the intercept to vary by species.
<span class="kw">summary</span>(Mlme1)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 807.9522 823.4209 -398.9761
##
## Random effects:
## Formula: ~1 | fSpecies
## (Intercept) Residual
## StdDev: 1.425693 4.349495
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.7097861
## Fixed effects: list(Form)
## Value Std.Error DF t-value p-value
## (Intercept) 0.077894 0.4869339 151 0.159969 0.8731
## Polygon.Area.100 4.678450 0.6992141 151 6.691012 0.0000
## Correlation:
## (Intr)
## Polygon.Area.100 -0.467
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -3.1087088 -0.6534845 -0.2104922 0.6095179 3.2441853
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
<p>The fixed effects part of the output shows that the general equation for the relationship between forest cover (FC) and specialization is <span class="math inline">\(0.08 + 4.68*FC\)</span>.</p>
<p>This is the average relationship for all the species considered together. However, we are letting each hummingbird species get its own intercept, so each hummingbird species can get its own equation as well. To incorporate the contribution of each species to the equation, we incorporate the random effect of each species into the intercept portion of the equation.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">F0 <-<span class="st"> </span><span class="kw">fitted</span>(Mlme1, <span class="dt">level =</span> <span class="dv">0</span>) ## the fitted, fixed values for the population model. These values are the same for every hummingbird within a site -- this will plot our overall trend line
F1 <-<span class="st"> </span><span class="kw">fitted</span>(Mlme1, <span class="dt">level =</span> <span class="dv">1</span>) ## the fitted, random values for each hummingbird at each site (within-species fitted values). These vary for every hummingbird within a site -- this will plot the trend line for each individual species
I <-<span class="st"> </span><span class="kw">order</span>(spec$Polygon.Area<span class="fl">.100</span>); Polygon.Area.100s <-<span class="st"> </span><span class="kw">sort</span>(spec$Polygon.Area<span class="fl">.100</span>) ## order() gives the sorted row numbers corresponding to increasing forest cover, these numbers will be used to index the correct corresponding values of F0 (see plot function below), sort() gives the actual values of increasing forest cover to plot
thirteen2 <-<span class="st"> </span><span class="kw">unique</span>(spec$fSpecies) ## I use this for indexing in my for-loop, it is simply a list of names of every species in the data set (there are thirteen species)
<span class="kw">plot</span>(Polygon.Area.100s, F0[I], <span class="dt">lwd =</span> <span class="dv">4</span>, <span class="dt">type =</span> <span class="st">"l"</span>, <span class="dt">ylab =</span> <span class="st">"Specialization (d')"</span>, <span class="dt">xlab =</span> <span class="st">"Forest Cover (100m)"</span>, <span class="dt">ylim =</span> <span class="kw">c</span>(-<span class="dv">5</span>, <span class="dv">10</span>)) ## draws average trend line
## following for-loop draws thirteen individual species trend lines
for(i in <span class="dv">1</span>:<span class="kw">length</span>(thirteen2)) {
x1 <-<span class="st"> </span>spec$Polygon.Area<span class="fl">.100</span>[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
y1 <-<span class="st"> </span>F1[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
K <-<span class="st"> </span><span class="kw">order</span>(x1)
<span class="kw">lines</span>(<span class="kw">sort</span>(x1), y1[K])
}</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-10-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">anova</span>(M.gls3, Mlme1) ## compares the non-random effects model to the random-intercept model - which one fits better? </code></pre></div>
<pre><code>## Model df AIC BIC logLik Test L.Ratio p-value
## M.gls3 1 4 851.0649 863.4399 -421.5325
## Mlme1 2 5 807.9522 823.4209 -398.9761 1 vs 2 45.11275 <.0001</code></pre>
<p>The AIC is lower for the random-intercept model and the L.Ratio is quite high, so the p-value is low (p<0.0001) which is good evidence that the random-effects model is a better fit for the data, and that each species does in fact have its own specialization relationship with forest cover.</p>
<p>However, we have good reason to think, biologically, that the relationship between specialization and forest cover is different for each hummingbird species (and thus not all species will have a relationship parallel to the population curve).</p>
<p>Thus, we will now fit a random intercept AND random slope model</p>
</div>
<div id="random-intercept-and-slope-model" class="section level4">
<h4>Random Intercept and Slope Model</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">Mlme2 <-<span class="st"> </span><span class="kw">lme</span>(Form, <span class="dt">random =</span> ~<span class="dv">1</span> +<span class="st"> </span>Polygon.Area<span class="fl">.100</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3) ## the "~1" specifies the random intecerpt and "Polygon.Area.100|fSpecies" allows the slope to vary by hummingbird species
<span class="kw">summary</span>(Mlme2)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 763.8287 785.485 -374.9144
##
## Random effects:
## Formula: ~1 + Polygon.Area.100 | fSpecies
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## (Intercept) 0.5185718 (Intr)
## Polygon.Area.100 4.6121499 -0.079
## Residual 3.4333447
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.6460465
## Fixed effects: list(Form)
## Value Std.Error DF t-value p-value
## (Intercept) 0.273699 0.2775939 151 0.9859696 0.3257
## Polygon.Area.100 3.919556 1.4091936 151 2.7814175 0.0061
## Correlation:
## (Intr)
## Polygon.Area.100 -0.324
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.7228479 -0.5905337 -0.0581716 0.5215418 3.5139731
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
<p>The amount of variation around the intercept is 0.52<sup>2</sup> and the random vairation around the slope is 4.61<sup>2</sup> indicating that there is more variation in slopes than in intercepts between the hummingbird species (which is evident when you look at the plot below). There is a correlation between random intercepts and slopes of only -0.079 which is quite low (good).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">AIC</span>(Mlme1, Mlme2) ## does the random intercept model or the random intercept and slope model have a lower AIC? </code></pre></div>
<pre><code>## df AIC
## Mlme1 5 807.9522
## Mlme2 7 763.8287</code></pre>
<p>The random intercept and slope model has a lower AIC than the random intercept model, which is a good sign.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## plotting the random intercept and slope model
F0 <-<span class="st"> </span><span class="kw">fitted</span> (Mlme2, <span class="dt">level =</span> <span class="dv">0</span>)
F1 <-<span class="st"> </span><span class="kw">fitted</span>(Mlme2, <span class="dt">level =</span> <span class="dv">1</span>)
I <-<span class="st"> </span><span class="kw">order</span>(spec$Polygon.Area<span class="fl">.100</span>); Polygon.Area.100s <-<span class="st"> </span><span class="kw">sort</span>(spec$Polygon.Area<span class="fl">.100</span>)
h.cols.rainbow =<span class="st"> </span><span class="kw">rainbow</span>(<span class="dv">13</span>) ## because this time it would be nice to give a different color to each species
<span class="kw">plot</span>(Polygon.Area.100s, F0[I], <span class="dt">lwd =</span> <span class="dv">6</span>, <span class="dt">type =</span> <span class="st">"l"</span>, <span class="dt">ylab =</span> <span class="st">"Specialization (d')"</span>, <span class="dt">xlab =</span> <span class="st">"Forest Cover (100m)"</span>, <span class="dt">ylim =</span> <span class="kw">c</span>(-<span class="dv">3</span>, <span class="dv">15</span>))
for(i in <span class="dv">1</span>:<span class="kw">length</span>(thirteen2)) {
x1 <-<span class="st"> </span>spec$Polygon.Area<span class="fl">.100</span>[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
y1 <-<span class="st"> </span>F1[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
K <-<span class="st"> </span><span class="kw">order</span>(x1)
<span class="kw">lines</span>(<span class="kw">sort</span>(x1), y1[K], <span class="dt">lwd =</span> <span class="dv">2</span>, <span class="dt">lty =</span> <span class="dv">4</span>, <span class="dt">col =</span> h.cols.rainbow[i])
}</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-14-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">fixed.effects</span>(Mlme2)</code></pre></div>
<pre><code>## (Intercept) Polygon.Area.100
## 0.2736991 3.9195557</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">random.effects</span>(Mlme2) ## gives the intercept and slope adjustment to the population curve for each hummingbird species. To get the intercept and slope for each individual species you add each species' values below to the fixed effects values (i.e. the population curve values) given above. </code></pre></div>
<pre><code>## (Intercept) Polygon.Area.100
## Charming.Hummingbird -0.371743752 0.1196069
## Garden.Emerald 0.138848695 -2.7304097
## Green.crowned.Brilliant -0.343739802 -1.6064965
## Green.Hermit 0.148005627 10.4221947
## Long.billed.Starthroat -0.132222769 -4.3955088
## Rufous.tailed.Hummingbird 0.041034119 3.6722203
## Scaly.breasted.Hummingbird -0.343415166 -4.7436235
## Snowy.bellied.Hummingbird 0.552190446 -2.8180533
## Stripe.throated.Hermit 0.261321687 0.1362129
## Violet.crowned.Woodnymph -0.204904787 -0.9737176
## Violet.Sabrewing 0.189618494 -3.2726526
## White.tailed.Emerald 0.056395222 0.2902166
## White.tipped.Sicklebill 0.008611986 5.9000106</code></pre>
<p>In practical terms, what these numbers mean is that if you were to catch a hummingbird in a site with no forest cover whatsoever (the intercept) and you didn’t know what species it was, you would predict that its specialization would be 0.27 because that’s the population estimate for all hummingbird species in general. However, if you caught, say, a Garden Emerald hummingbird in a site with no forest cover, because you know the species, you predict that its specialization is 0.27 + 0.14 = 0.41, which is a higher specialization than the population estimate.</p>
</div>
<div id="random-effects-model" class="section level4">
<h4>Random Effects Model</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">Mlme3 <-<span class="st"> </span><span class="kw">lme</span>(Specialization ~<span class="st"> </span><span class="dv">1</span>, <span class="dt">random =</span> ~<span class="dv">1</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3)
<span class="kw">summary</span>(Mlme3)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 846.6653 859.0648 -419.3327
##
## Random effects:
## Formula: ~1 | fSpecies
## (Intercept) Residual
## StdDev: 1.443682 5.070841
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.7504239
## Fixed effects: Specialization ~ 1
## Value Std.Error DF t-value p-value
## (Intercept) 1.549056 0.4422072 152 3.50301 6e-04
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -3.11140157 -0.27351685 0.07591166 0.63584959 3.40459610
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
<p>Comparing our models with AIC</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">AIC</span>(M.gls3, Mlme1, Mlme2, Mlme3)</code></pre></div>
<pre><code>## df AIC
## M.gls3 4 851.0649
## Mlme1 5 807.9522
## Mlme2 7 763.8287
## Mlme3 4 846.6653</code></pre>
<p>Using this approach we would select our random intercept and random slope model (Mlme2) because it has the lowest AIC</p>
<p>Fitting the final model with Restricted Maximum Liklihood</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">M1 <-<span class="st"> </span><span class="kw">lme</span>(Form, <span class="dt">random =</span> ~<span class="dv">1</span> +<span class="st"> </span>Polygon.Area<span class="fl">.100</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3, <span class="dt">method =</span> <span class="st">"REML"</span>)
<span class="kw">summary</span>(M1)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 763.8287 785.485 -374.9144
##
## Random effects:
## Formula: ~1 + Polygon.Area.100 | fSpecies
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## (Intercept) 0.5185718 (Intr)
## Polygon.Area.100 4.6121499 -0.079
## Residual 3.4333447
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.6460465
## Fixed effects: list(Form)
## Value Std.Error DF t-value p-value
## (Intercept) 0.273699 0.2775939 151 0.9859696 0.3257
## Polygon.Area.100 3.919556 1.4091936 151 2.7814175 0.0061
## Correlation:
## (Intr)
## Polygon.Area.100 -0.324
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.7228479 -0.5905337 -0.0581716 0.5215418 3.5139731
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">fixed.effects</span>(Mlme2)</code></pre></div>
<pre><code>## (Intercept) Polygon.Area.100
## 0.2736991 3.9195557</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">random.effects</span>(Mlme2) ## gives the intercept and slope adjustment to the population curve for each hummingbird species. To get the intercept and slope for each individual species you add each species' values below to the fixed effects values (i.e. the population curve values) given above. </code></pre></div>
<pre><code>## (Intercept) Polygon.Area.100
## Charming.Hummingbird -0.371743752 0.1196069
## Garden.Emerald 0.138848695 -2.7304097
## Green.crowned.Brilliant -0.343739802 -1.6064965
## Green.Hermit 0.148005627 10.4221947
## Long.billed.Starthroat -0.132222769 -4.3955088
## Rufous.tailed.Hummingbird 0.041034119 3.6722203
## Scaly.breasted.Hummingbird -0.343415166 -4.7436235
## Snowy.bellied.Hummingbird 0.552190446 -2.8180533
## Stripe.throated.Hermit 0.261321687 0.1362129
## Violet.crowned.Woodnymph -0.204904787 -0.9737176
## Violet.Sabrewing 0.189618494 -3.2726526
## White.tailed.Emerald 0.056395222 0.2902166
## White.tipped.Sicklebill 0.008611986 5.9000106</code></pre>
<p>In practical terms, what these numbers mean is that if you were to catch a hummingbird in a site with no forest cover whatsoever (the intercept) and you didn’t know what species it was, you would predict that its specialization would be 0.27 because that’s the population estimate for all hummingbird species in general. However, if you caught, say, a Garden Emerald hummingbird in a site with no forest cover, because you know the species, you predict that its specialization is <span class="math inline">\(0.27 + 0.14 = 0.41\)</span>, which is a higher specialization than the population estimate.</p>
<p>Another (easier) way to see the parameter estimates for each species is simply using the function <code>coef()</code></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">coef</span>(Mlme2)</code></pre></div>
<pre><code>## (Intercept) Polygon.Area.100
## Charming.Hummingbird -0.09804466 4.0391626
## Garden.Emerald 0.41254779 1.1891461
## Green.crowned.Brilliant -0.07004071 2.3130593
## Green.Hermit 0.42170472 14.3417505
## Long.billed.Starthroat 0.14147632 -0.4759531
## Rufous.tailed.Hummingbird 0.31473321 7.5917760
## Scaly.breasted.Hummingbird -0.06971607 -0.8240677
## Snowy.bellied.Hummingbird 0.82588954 1.1015025
## Stripe.throated.Hermit 0.53502078 4.0557686
## Violet.crowned.Woodnymph 0.06879431 2.9458381
## Violet.Sabrewing 0.46331759 0.6469032
## White.tailed.Emerald 0.33009432 4.2097723
## White.tipped.Sicklebill 0.28231108 9.8195663</code></pre>
</div>
<div id="random-effects-model-1" class="section level4">
<h4>Random Effects Model</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">Mlme3 <-<span class="st"> </span><span class="kw">lme</span>(Specialization ~<span class="st"> </span><span class="dv">1</span>, <span class="dt">random =</span> ~<span class="dv">1</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3)
<span class="kw">summary</span>(Mlme3)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 846.6653 859.0648 -419.3327
##
## Random effects:
## Formula: ~1 | fSpecies
## (Intercept) Residual
## StdDev: 1.443682 5.070841
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.7504239
## Fixed effects: Specialization ~ 1
## Value Std.Error DF t-value p-value
## (Intercept) 1.549056 0.4422072 152 3.50301 6e-04
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -3.11140157 -0.27351685 0.07591166 0.63584959 3.40459610
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
<p>Random effects only graph</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## plotting the random intercept and slope model
F0 <-<span class="st"> </span><span class="kw">fitted</span> (Mlme3, <span class="dt">level =</span> <span class="dv">0</span>)
F1 <-<span class="st"> </span><span class="kw">fitted</span>(Mlme3, <span class="dt">level =</span> <span class="dv">1</span>)
I <-<span class="st"> </span><span class="kw">order</span>(spec$Polygon.Area<span class="fl">.100</span>); Polygon.Area.100s <-<span class="st"> </span><span class="kw">sort</span>(spec$Polygon.Area<span class="fl">.100</span>)
h.cols.rainbow =<span class="st"> </span><span class="kw">rainbow</span>(<span class="dv">13</span>) ## because this time it would be nice to give a different color to each species
<span class="kw">plot</span>(Polygon.Area.100s, F0[I], <span class="dt">lwd =</span> <span class="dv">6</span>, <span class="dt">type =</span> <span class="st">"l"</span>, <span class="dt">ylab =</span> <span class="st">"Specialization (d')"</span>, <span class="dt">xlab =</span> <span class="st">"Forest Cover (100m)"</span>, <span class="dt">ylim =</span> <span class="kw">c</span>(-<span class="dv">3</span>, <span class="dv">15</span>))
for(i in <span class="dv">1</span>:<span class="kw">length</span>(thirteen2)) {
x1 <-<span class="st"> </span>spec$Polygon.Area<span class="fl">.100</span>[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
y1 <-<span class="st"> </span>F1[spec$fSpecies ==<span class="st"> </span>thirteen2[i]]
K <-<span class="st"> </span><span class="kw">order</span>(x1)
<span class="kw">lines</span>(<span class="kw">sort</span>(x1), y1[K], <span class="dt">lwd =</span> <span class="dv">2</span>, <span class="dt">lty =</span> <span class="dv">4</span>, <span class="dt">col =</span> h.cols.rainbow[i])
}</code></pre></div>
<p><img src="images/04-A/unnamed-chunk-24-1.png" width="672" /></p>
<p>Comparing our models with AIC</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">AIC</span>(M.gls3, Mlme1, Mlme2, Mlme3)</code></pre></div>
<pre><code>## df AIC
## M.gls3 4 851.0649
## Mlme1 5 807.9522
## Mlme2 7 763.8287
## Mlme3 4 846.6653</code></pre>
<p>Using this approach we would select our random intercept and random slope model (Mlme2) because it has the lowest AIC</p>
<p>Fitting the final model with Restricted Maximum Liklihood</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">M1 <-<span class="st"> </span><span class="kw">lme</span>(Form, <span class="dt">random =</span> ~<span class="dv">1</span> +<span class="st"> </span>Polygon.Area<span class="fl">.100</span>|fSpecies, <span class="dt">data =</span> spec, <span class="dt">weights =</span> vf3, <span class="dt">method =</span> <span class="st">"REML"</span>)
<span class="kw">summary</span>(M1)</code></pre></div>
<pre><code>## Linear mixed-effects model fit by REML
## Data: spec
## AIC BIC logLik
## 763.8287 785.485 -374.9144
##
## Random effects:
## Formula: ~1 + Polygon.Area.100 | fSpecies
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## (Intercept) 0.5185718 (Intr)
## Polygon.Area.100 4.6121499 -0.079
## Residual 3.4333447
##
## Variance function:
## Structure: Power of variance covariate
## Formula: ~Polygon.Area.100
## Parameter estimates:
## power
## 0.6460465
## Fixed effects: list(Form)
## Value Std.Error DF t-value p-value
## (Intercept) 0.273699 0.2775939 151 0.9859696 0.3257
## Polygon.Area.100 3.919556 1.4091936 151 2.7814175 0.0061
## Correlation:
## (Intr)
## Polygon.Area.100 -0.324
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.7228479 -0.5905337 -0.0581716 0.5215418 3.5139731
##
## Number of Observations: 165
## Number of Groups: 13</code></pre>
</div>
<div id="conclusions" class="section level4">
<h4>Conclusions</h4>
<p>We now have evidence that hummingbird species’ specialization, in general, increases with increasing forest cover. However, not all hummingbird species show the same relationship; some species show a much greter increase in specialization with forest cover than others. Species becoming less specialized in less forested areas suggests that changes in resource availability as forest cover is lost are potentially forcing species to be less ‘picky’ in disturbed, agricultural areas.</p>
</div>
</div>
<div id="discussion-questions" class="section level3">
<h3>Discussion Questions</h3>
<ul>