Gaussian function model in R - r

I have a dataset of barnacle density and coral cover by photo from two coral reef locations. I want to see if there is a pattern in barnacle density with depth or coral cover.
I have tried linear models and a negative binomial with the formula
m2 <- glm.nb(dens.cm ~ depth + coral.cover+location+depth:location, data =data)
However, after looking at a distribution of the density data with depth, I think a Gaussian function may better explain the patterns.
Density of barnacles per m2 by depth (m) and location
I am looking for advice on how to design a Gaussian model for my data in R. Any advice is appreciated!
> dput(dat)
structure(list(photo = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 50L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L,
90L, 91L, 92L, 93L, 94L, 114L, 115L, 116L, 117L, 118L, 119L,
120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L,
131L, 132L, 133L, 134L), .Label = c("101", "102", "103", "104",
"105", "106", "107", "108", "201", "202", "203", "204", "205",
"206", "207", "208", "209", "210", "211", "212", "301", "302",
"303", "304", "305", "306", "307", "501", "502", "503", "504",
"505", "506", "507", "508", "509", "510", "511", "512", "513",
"601", "602", "603", "604", "605", "606", "607", "608", "609",
"6157", "6173", "6177", "6178", "6181", "6182", "6199", "6201",
"6202", "6203", "6210", "6211", "6214", "6222", "6237", "6241",
"6245", "6256", "6260", "6261", "6296", "6297", "6299", "6302",
"6304", "6308", "6309", "6311", "6312", "6313", "6314", "6315",
"6320", "6322", "6323", "6324", "6325", "6326", "6327", "6328",
"6329", "6424", "6426", "6428", "6431", "701", "702", "703",
"704", "705", "706", "707", "708", "709", "801", "802", "803",
"804", "805", "806", "807", "808", "809", "810", "D01", "D02",
"D03", "D04", "D05", "D06", "D07", "D08", "D10", "D11", "D12",
"D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21",
"D22"), class = "factor"), location = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("fgb", "usvi"), class = "factor"), depth = c(19.5072,
19.812, 21.5, 20.7264, 21.336, 19.5072, 19.812, 20.0312, 21.9456,
23.4696, 23.4696, 24.0792, 23.1648, 23.4696, 21.336, 19.5072,
20.1168, 20.7264, 21.0312, 21.0312, 21.9456, 20.4216, 19.5072,
21.0312, 22.2504, 21.9456, 20.4216, 20.4216, 20.4216, 21.336,
20.7264, 20.7264, 20.4216, 20.4216, 19.812, 20.1168, 20.1168,
20.7264, 19.812, 21.9456, 22.86, 22.2504, 21.9456, 22.5552, 22.2504,
21.0312, 21.336, 21.336, 21.6408, 23.4696, 23.7744, 21.9456,
22.2504, 22.2504, 21.6408, 22.2504, 22.2504, 21.5, 23.1648, 22.5552,
22.2504, 22.5552, 22.2504, 21.9456, 21.85, 22.2504, 24.0792,
22.2504, 15, 15, 15, 15, 15, 15, 13, 13, 13, 13, 13, 13, 13,
21, 21, 21, 21, 7, 7, 7, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 38, 38, 38, 38,
32.6992, 29.5656, 31.0896, 31.0896, 32.6136, 33.8328, 35.3568,
35.3568, 31.0896, 37.7952, 29.5656, 31.0896, 31.0896, 32.6136,
33.8328, 35.3568, 35.3568, 36.8808, 37.7952, 37.7952, 38.1),
dens.m = c(267.86719, 350.47852, 431.81125, 622.71004, 599.24271,
1420.18674, 193.38521, 161.44909, 910.49021, 110.35386, 479.12616,
408.42407, 315.60503, 74.8805, 104.48846, 137.99029, 469.71577,
356.37609, 950.49046, 272.49611, 528.00183, 269.93556, 480.50256,
118.2897, 185.00516, 438.49583, 276.08897, 227.43988, 86.33476,
185.46051, 84.80511, 451.02732, 400.5159, 163.67933, 90.92022,
137.38598, 202.10666, 159.44588, 197.77431, 453.77111, 101.17702,
134.19122, 122.93134, 429.97449, 430.17319, 1153.40396, 214.65884,
1342.54685, 578.08208, 578.44438, 252.6739, 2174.60653, 354.51124,
340.84014, 390.41988, 244.08631, 806.81267, 651.94004, 57.84774,
303.84401, 411.5247, 555.01574, 118.71732, 94.01832, 572.41467,
444.28938, 123.78678, 320.6036361, 0, 0, 49.41053235, 0,
125.6693464, 0, 93.84212658, 198.2007337, 327.6507767, 907.6881184,
0, 239.4739237, 0, 0, 443.5415909, 0, 51.88753895, 401.7879564,
0, 428.9613238, 0, 17.05628117, 0, 0, 0, 62.93519689, 0,
14.42007124, 0, 0, 0, 52.11494159, 0, 0, 0, 0, 0, 0, 0, 10.83275387,
141.8632389, 0, 0, 0, 0, 446.919281, 132.8611692, 143.198051,
33.05694578, 167.1561242, 51.78159277, 99.97872, 75.88997,
502.1027409, 354.7612359, 18.01753245, 59.73474983, 101.6708376,
192.2764503, 279.5383788, 138.1696187, 289.6458105, 166.5402349,
65.25117077, 649.1753683, 346.42269), coral.cover = c(28.52606,
11.05908, 31.28802, 28.91658, 3.54822, 12.18002, 16.72137,
1.92059, 23.42574, 64.22509, 37.25867, 48.04682, 58.10703,
36.08555, 45.99744, 67.4129, 41.21151, 53.32379, 14.54049,
40.63984, 57.09064, 42.2561, 39.77932, 23.7793, 35.67588,
28.4876, 35.53832, 21.61865, 35.1461, 14.45028, 45.70443,
52.544, 53.58537, 27.60442, 16.56497, 6.12609, 31.23248,
48.8958, 25.30934, 40.41436, 28.02014, 36.47627, 28.28651,
13.44436, 25.07424, 38.02122, 49.11345, 7.12683, 24.52069,
15.27754, 35.67601, 8.35171, 1.87428, 6.0433, 20.08231, 13.70174,
39.39322, 9.61437, 10.3376, 50.15105, 37.62041, 39.14767,
41.23067, 38.1632, 46.12196, 16.10196, 36.32152, 44.90422,
2.0575, 12.13155, 5.20272, 5.34756, 4.0912, 0.60427, 5.47876,
1.29702, 0.78458, 0.56643, 0.75587, 2.14695, 8.99664, 0.73209,
1.15917, 1.40533, 4.95436, 0.63981, 1.03059, 1.19857, 0.38732,
60.28733, 25.67675, 10.33979, 13.07546, 4.08467, 6.10119,
35.65439, 5.54589, 15.93534, 6.06176, 9.86548, 7.00005, 21.27449,
12.13181, 26.65331, 5.83493, 14.69534, 6.87034, 23.73075,
7.24837, 1.58201, 2.56882, 0.35245, 20.23897, 42.96672, 44.67648,
28.76856, 37.52041, 40.01538, 4.705, 29.9067, 30.06042, 7.45481,
14.35932, 8.60488, 16.68506, 23.30932, 14.51399, 33.59438,
38.95256, 43.35688, 2.65983, 9.84355, 37.1201, 50.76407)), .Names = c("photo",
"location", "depth", "dens.m", "coral.cover"), class = "data.frame", row.names = c(NA,
-134L))

Related

If-Else Statement in R with 3 conditions

I am trying to use an if-else statement to create a column in my data set. I want this if-else statement to create a column called "Surgical" in the df "option1" that displays the value of the column "Duration" subtracted by 20 ONLY IF the value in Duration is above 625, AND the factor "Single" is indicated in the column "Variability".
I have tried the following code:
option1$Surgical <- ifelse(option1$Variability == "Single", option1$Duration - 20, option1$Duration)
Any insight into how to specify the "only if the value is greater than 625" portion is appreciated!!
Df "option 1" for reference.
dput(option1)
structure(list(Stimulus = structure(c(36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 67L, 73L, 74L, 75L, 76L, 77L, 78L,
79L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
69L, 70L, 71L, 72L, 73L, 74L, 75L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 60L, 61L,
62L, 63L, 64L, 65L, 66L, 67L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 69L,
70L, 71L, 72L, 73L, 74L, 75L, 7L, 9L, 12L, 18L, 28L, 26L, 51L,
37L, 3L, 2L, 19L, 14L, 27L, 23L, 65L, 77L, 7L, 9L, 12L, 18L,
28L, 26L, 51L, 37L, 3L, 2L, 19L, 14L, 27L, 23L, 65L, 77L, 5L,
11L, 20L, 16L, 30L, 25L, 35L, 33L, 7L, 9L, 12L, 18L, 28L, 26L,
51L, 37L, 5L, 11L, 20L, 16L, 30L, 25L, 35L, 33L, 7L, 9L, 12L,
18L, 28L, 26L, 51L, 37L), .Label = c("t1_block2_hoed3.mp3", "t1_block2_whod3.mp3",
"t1_block2_whod4.mp3", "t1_block2_whod5.mp3", "t1_block3_heed2.mp3",
"t1_block3_heed5.mp3", "t1_block3_hoed1.mp3", "t1_block3_hoed2.mp3",
"t1_block3_hoed4.mp3", "t1_block3_whod3.mp3", "t1_block4_heed5.mp3",
"t2_block1_hoed3.mp3", "t2_block1_whod1.mp3", "t2_block1_whod2.mp3",
"t2_block1_whod4.mp3", "t2_block2_heed3.mp3", "t2_block2_hoed5.mp3",
"t2_block3_hoed1.mp3", "t2_block3_whod1.mp3", "t2_block4_heed2.mp3",
"t2_block4_heed5.mp3", "t3_block1_heed1.mp3", "t3_block1_whod2.mp3",
"t3_block1_whod5.mp3", "t3_block2_heed5.mp3", "t3_block2_hoed2.mp3",
"t3_block2_whod5.mp3", "t3_block3_hoed1.mp3", "t3_block3_hoed4.mp3",
"t3_block4_heed3.mp3", "t4_block1_heed1.mp3", "t4_block1_heed2.mp3",
"t4_block1_heed3.mp3", "t4_block1_heed4.mp3", "t4_block1_heed5.mp3",
"t4_block1_hoed1.mp3", "t4_block1_hoed2.mp3", "t4_block1_hoed3.mp3",
"t4_block1_hoed4.mp3", "t4_block1_hoed5.mp3", "t4_block1_whod1.mp3",
"t4_block1_whod2.mp3", "t4_block1_whod3.mp3", "t4_block1_whod4.mp3",
"t4_block1_whod5.mp3", "t4_block2_heed1.mp3", "t4_block2_heed2.mp3",
"t4_block2_heed4.mp3", "t4_block2_heed5.mp3", "t4_block2_hoed1.mp3",
"t4_block2_hoed3.mp3", "t4_block2_hoed4.mp3", "t4_block2_hoed5.mp3",
"t4_block2_whod2.mp3", "t4_block2_whod4.mp3", "t4_block2_whod5.mp3",
"t4_block3_heed1.mp3", "t4_block3_heed4.mp3", "t4_block3_heed5.mp3",
"t4_block3_hoed1.mp3", "t4_block3_hoed2.mp3", "t4_block3_hoed4.mp3",
"t4_block3_hoed5.mp3", "t4_block3_whod1.mp3", "t4_block3_whod2.mp3",
"t4_block3_whod3.mp3", "t4_block3_whod5.mp3", "t4_block4_heed1.mp3",
"t4_block4_heed2.mp3", "t4_block4_heed3.mp3", "t4_block4_heed4.mp3",
"t4_block4_heed5.mp3", "t4_block4_hoed1.mp3", "t4_block4_hoed2.mp3",
"t4_block4_hoed3.mp3", "t4_block4_whod1.mp3", "t4_block4_whod2.mp3",
"t4_block4_whod3.mp3", "t4_block4_whod5.mp3"), class = "factor"),
Duration = c(497L, 517L, 580L, 563L, 569L, 486L, 506L, 536L,
545L, 554L, 516L, 600L, 607L, 577L, 537L, 583L, 544L, 566L,
567L, 616L, 652L, 564L, 517L, 612L, 564L, 632L, 662L, 565L,
594L, 622L, 552L, 542L, 539L, 554L, 600L, 607L, 577L, 497L,
517L, 580L, 563L, 569L, 594L, 563L, 623L, 602L, 516L, 600L,
607L, 577L, 531L, 642L, 624L, 566L, 567L, 616L, 652L, 654L,
576L, 556L, 608L, 632L, 662L, 565L, 497L, 517L, 580L, 563L,
569L, 486L, 506L, 536L, 545L, 554L, 516L, 600L, 607L, 577L,
537L, 583L, 544L, 566L, 567L, 616L, 652L, 564L, 517L, 612L,
564L, 632L, 662L, 565L, 594L, 622L, 552L, 542L, 539L, 554L,
600L, 607L, 577L, 497L, 517L, 580L, 563L, 569L, 594L, 563L,
623L, 602L, 516L, 600L, 607L, 577L, 531L, 642L, 624L, 566L,
567L, 616L, 652L, 654L, 576L, 556L, 608L, 632L, 662L, 565L,
452L, 547L, 510L, 663L, 470L, 503L, 600L, 517L, 491L, 505L,
641L, 581L, 520L, 485L, 517L, 622L, 452L, 547L, 510L, 663L,
470L, 503L, 600L, 517L, 491L, 505L, 641L, 581L, 520L, 485L,
517L, 622L, 510L, 458L, 558L, 638L, 483L, 538L, 577L, 600L,
452L, 547L, 510L, 663L, 470L, 503L, 600L, 517L, 510L, 458L,
558L, 638L, 483L, 538L, 577L, 600L, 452L, 547L, 510L, 663L,
470L, 503L, 600L, 517L), F0 = c(196L, 204L, 204L, 197L, 203L,
216L, 208L, 223L, 213L, 219L, 196L, 202L, 205L, 202L, 208L,
205L, 206L, 197L, 202L, 195L, 200L, 201L, 210L, 202L, 208L,
195L, 196L, 195L, 205L, 208L, 203L, 203L, 212L, 213L, 210L,
206L, 204L, 196L, 204L, 204L, 197L, 203L, 201L, 198L, 199L,
203L, 196L, 202L, 205L, 202L, 193L, 195L, 208L, 197L, 202L,
195L, 200L, 201L, 195L, 205L, 202L, 195L, 196L, 195L, 196L,
204L, 204L, 197L, 203L, 216L, 208L, 223L, 213L, 219L, 196L,
202L, 205L, 202L, 208L, 205L, 206L, 197L, 202L, 195L, 200L,
201L, 210L, 202L, 208L, 195L, 196L, 195L, 205L, 208L, 203L,
203L, 212L, 213L, 210L, 206L, 204L, 196L, 204L, 204L, 197L,
203L, 201L, 198L, 199L, 203L, 196L, 202L, 205L, 202L, 193L,
195L, 208L, 197L, 202L, 195L, 200L, 201L, 195L, 205L, 202L,
195L, 196L, 195L, 215L, 219L, 219L, 220L, 199L, 202L, 202L,
204L, 224L, 231L, 238L, 240L, 217L, 212L, 210L, 208L, 215L,
219L, 219L, 220L, 199L, 202L, 202L, 204L, 224L, 231L, 238L,
240L, 217L, 212L, 210L, 208L, 230L, 223L, 219L, 221L, 199L,
200L, 204L, 210L, 215L, 219L, 219L, 220L, 199L, 202L, 202L,
204L, 230L, 223L, 219L, 221L, 199L, 200L, 204L, 210L, 215L,
219L, 219L, 220L, 199L, 202L, 202L, 204L), F1 = c(576L, 553L,
579L, 586L, 601L, 398L, 390L, 398L, 389L, 404L, 587L, 560L,
562L, 553L, 393L, 397L, 382L, 553L, 592L, 556L, 571L, 387L,
392L, 398L, 400L, 580L, 580L, 554L, 403L, 391L, 388L, 393L,
382L, 375L, 384L, 392L, 388L, 576L, 553L, 579L, 586L, 601L,
387L, 393L, 402L, 406L, 587L, 560L, 562L, 553L, 388L, 391L,
412L, 553L, 592L, 556L, 571L, 410L, 404L, 401L, 420L, 580L,
580L, 554L, 576L, 553L, 579L, 586L, 601L, 398L, 390L, 398L,
389L, 404L, 587L, 560L, 562L, 553L, 393L, 397L, 382L, 553L,
592L, 556L, 571L, 387L, 392L, 398L, 400L, 580L, 580L, 554L,
403L, 391L, 388L, 393L, 382L, 375L, 384L, 392L, 388L, 576L,
553L, 579L, 586L, 601L, 387L, 393L, 402L, 406L, 587L, 560L,
562L, 553L, 388L, 391L, 412L, 553L, 592L, 556L, 571L, 410L,
404L, 401L, 420L, 580L, 580L, 554L, 620L, 630L, 602L, 605L,
571L, 573L, 560L, 553L, 434L, 417L, 306L, 319L, 414L, 419L,
392L, 391L, 620L, 630L, 602L, 605L, 571L, 573L, 560L, 553L,
434L, 417L, 306L, 319L, 414L, 419L, 392L, 391L, 448L, 441L,
293L, 291L, 420L, 420L, 388L, 384L, 620L, 630L, 602L, 605L,
571L, 573L, 560L, 553L, 448L, 441L, 293L, 291L, 420L, 420L,
388L, 384L, 620L, 630L, 602L, 605L, 571L, 573L, 560L, 553L
), F2 = c(1339L, 1381L, 1381L, 1347L, 1394L, 1484L, 1521L,
1539L, 1430L, 1454L, 1353L, 1378L, 1325L, 1357L, 1424L, 1563L,
1578L, 1350L, 1397L, 1273L, 1319L, 1548L, 1452L, 1499L, 1515L,
1358L, 1347L, 1248L, 1575L, 1438L, 1414L, 1548L, 3001L, 2916L,
2948L, 2973L, 2947L, 1339L, 1381L, 1381L, 1347L, 1394L, 2943L,
2913L, 2987L, 2940L, 1353L, 1378L, 1325L, 1357L, 3010L, 3008L,
2972L, 1350L, 1397L, 1273L, 1319L, 2963L, 2991L, 3007L, 2989L,
1358L, 1347L, 1248L, 1339L, 1381L, 1381L, 1347L, 1394L, 1484L,
1521L, 1539L, 1430L, 1454L, 1353L, 1378L, 1325L, 1357L, 1424L,
1563L, 1578L, 1350L, 1397L, 1273L, 1319L, 1548L, 1452L, 1499L,
1515L, 1358L, 1347L, 1248L, 1575L, 1438L, 1414L, 1548L, 3001L,
2916L, 2948L, 2973L, 2947L, 1339L, 1381L, 1381L, 1347L, 1394L,
2943L, 2913L, 2987L, 2940L, 1353L, 1378L, 1325L, 1357L, 3010L,
3008L, 2972L, 1350L, 1397L, 1273L, 1319L, 2963L, 2991L, 3007L,
2989L, 1358L, 1347L, 1248L, 1247L, 1244L, 1293L, 1264L, 1348L,
1354L, 1378L, 1381L, 1314L, 1233L, 1190L, 1208L, 1643L, 1659L,
1452L, 1438L, 1247L, 1244L, 1293L, 1264L, 1348L, 1354L, 1378L,
1381L, 1314L, 1233L, 1190L, 1208L, 1643L, 1659L, 1452L, 1438L,
2837L, 2816L, 2780L, 2776L, 2684L, 2718L, 2947L, 2948L, 1247L,
1244L, 1293L, 1264L, 1348L, 1354L, 1378L, 1381L, 2837L, 2816L,
2780L, 2776L, 2684L, 2718L, 2947L, 2948L, 1247L, 1244L, 1293L,
1264L, 1348L, 1354L, 1378L, 1381L), F3 = c(2831L, 2779L,
2915L, 2875L, 2712L, 2730L, 2793L, 2779L, 2772L, 2692L, 2718L,
2856L, 2674L, 2659L, 2717L, 2584L, 2829L, 2726L, 2685L, 2866L,
2793L, 2614L, 2636L, 2907L, 2822L, 2932L, 2882L, 2882L, 2650L,
2929L, 2809L, 2737L, 3623L, 3607L, 3584L, 3576L, 3680L, 2831L,
2779L, 2915L, 2875L, 2712L, 3641L, 3590L, 3556L, 3584L, 2718L,
2856L, 2674L, 2659L, 3640L, 3656L, 3686L, 2726L, 2685L, 2866L,
2793L, 3516L, 3552L, 3513L, 3579L, 2932L, 2882L, 2882L, 2831L,
2779L, 2915L, 2875L, 2712L, 2730L, 2793L, 2779L, 2772L, 2692L,
2718L, 2856L, 2674L, 2659L, 2717L, 2584L, 2829L, 2726L, 2685L,
2866L, 2793L, 2614L, 2636L, 2907L, 2822L, 2932L, 2882L, 2882L,
2650L, 2929L, 2809L, 2737L, 3623L, 3607L, 3584L, 3576L, 3680L,
2831L, 2779L, 2915L, 2875L, 2712L, 3641L, 3590L, 3556L, 3584L,
2718L, 2856L, 2674L, 2659L, 3640L, 3656L, 3686L, 2726L, 2685L,
2866L, 2793L, 3516L, 3552L, 3513L, 3579L, 2932L, 2882L, 2882L,
2711L, 3129L, 2786L, 2833L, 2754L, 2771L, 2856L, 2779L, 2909L,
2750L, 2866L, 2863L, 2804L, 2704L, 2636L, 2929L, 2711L, 3129L,
2786L, 2833L, 2754L, 2771L, 2856L, 2779L, 2909L, 2750L, 2866L,
2863L, 2804L, 2704L, 2636L, 2929L, 3226L, 3121L, 3867L, 3319L,
3426L, 3269L, 3680L, 3357L, 2711L, 3129L, 2786L, 2833L, 2754L,
2771L, 2856L, 2779L, 3226L, 3121L, 3867L, 3319L, 3426L, 3269L,
3680L, 3357L, 2711L, 3129L, 2786L, 2833L, 2754L, 2771L, 2856L,
2779L), Word = structure(c(2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L,
4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L,
4L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 2L,
2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4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394L, 394L, 394L, 564L, 564L, 564L, 564L,
394L, 394L, 394L, 564L, 564L, 564L, 564L, 394L, 394L, 394L,
394L, 564L, 564L, 564L, 394L, 394L, 394L, 394L, 398L, 398L,
398L, 398L, 398L, 564L, 564L, 564L, 564L, 564L, 398L, 398L,
398L, 398L, 564L, 564L, 564L, 564L, 398L, 398L, 398L, 564L,
564L, 564L, 564L, 398L, 398L, 398L, 398L, 564L, 564L, 564L,
564L, 564L, 564L, 564L, 564L, 394L, 394L, 394L, 394L, 394L,
564L, 564L, 564L, 564L, 394L, 394L, 394L, 564L, 564L, 564L,
564L, 394L, 394L, 394L, 394L, 564L, 564L, 564L, 394L, 394L,
394L, 394L, 398L, 398L, 398L, 398L, 398L, 564L, 564L, 564L,
564L, 564L, 398L, 398L, 398L, 398L, 564L, 564L, 564L, 564L,
398L, 398L, 398L, 564L, 564L, 564L, 564L, 398L, 398L, 398L,
398L, 564L, 564L, 564L, 627L, 627L, 614L, 614L, 614L, 614L,
566L, 566L, 432L, 432L, 327L, 327L, 415L, 415L, 393L, 393L,
627L, 627L, 614L, 614L, 614L, 614L, 566L, 566L, 432L, 432L,
327L, 327L, 415L, 415L, 393L, 393L, 397L, 397L, 292L, 292L,
417L, 417L, 398L, 398L, 627L, 627L, 614L, 614L, 614L, 614L,
566L, 566L, 397L, 397L, 292L, 292L, 417L, 417L, 398L, 398L,
627L, 627L, 614L, 614L, 614L, 614L, 566L, 566L), F2.Mean = c(1328L,
1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1496L,
1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1328L, 1328L,
1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1328L, 1328L, 1328L,
1496L, 1496L, 1496L, 1496L, 2969L, 2969L, 2969L, 2969L, 2969L,
1328L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 2969L,
1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 1328L, 1328L,
1328L, 1328L, 2969L, 2969L, 2969L, 2969L, 1328L, 1328L, 1328L,
1328L, 1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L,
1496L, 1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1328L,
1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1328L, 1328L,
1328L, 1496L, 1496L, 1496L, 1496L, 2969L, 2969L, 2969L, 2969L,
2969L, 1328L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L,
2969L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 1328L,
1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 2969L, 1328L, 1328L,
1328L, 1250L, 1250L, 1247L, 1247L, 1247L, 1247L, 1357L, 1357L,
1292L, 1292L, 1157L, 1157L, 1746L, 1746L, 1455L, 1455L, 1250L,
1250L, 1247L, 1247L, 1247L, 1247L, 1357L, 1357L, 1292L, 1292L,
1157L, 1157L, 1746L, 1746L, 1455L, 1455L, 2828L, 2828L, 2763L,
2763L, 2721L, 2721L, 2969L, 2969L, 1250L, 1250L, 1247L, 1247L,
1247L, 1247L, 1357L, 1357L, 2828L, 2828L, 2763L, 2763L, 2721L,
2721L, 2969L, 2969L, 1250L, 1250L, 1247L, 1247L, 1247L, 1247L,
1357L, 1357L), Distance = c(16L, 54L, 55L, 29L, 76L, 13L,
25L, 43L, 66L, 43L, 34L, 50L, 4L, 31L, 72L, 67L, 83L, 25L,
74L, 56L, 11L, 52L, 44L, 5L, 20L, 34L, 25L, 81L, 80L, 58L,
82L, 52L, 36L, 58L, 25L, 7L, 24L, 16L, 54L, 55L, 29L, 76L,
28L, 56L, 18L, 30L, 34L, 50L, 4L, 31L, 42L, 40L, 14L, 25L,
74L, 56L, 11L, 13L, 23L, 38L, 30L, 34L, 25L, 81L, 16L, 54L,
55L, 29L, 76L, 13L, 25L, 43L, 66L, 43L, 34L, 50L, 4L, 31L,
72L, 67L, 83L, 25L, 74L, 56L, 11L, 52L, 44L, 5L, 20L, 34L,
25L, 81L, 80L, 58L, 82L, 52L, 36L, 58L, 25L, 7L, 24L, 16L,
54L, 55L, 29L, 76L, 28L, 56L, 18L, 30L, 34L, 50L, 4L, 31L,
42L, 40L, 14L, 25L, 74L, 56L, 11L, 13L, 23L, 38L, 30L, 34L,
25L, 81L, 8L, 7L, 48L, 19L, 110L, 115L, 22L, 27L, 22L, 61L,
39L, 52L, 103L, 87L, 3L, 17L, 8L, 7L, 48L, 19L, 110L, 115L,
22L, 27L, 22L, 61L, 39L, 52L, 103L, 87L, 3L, 17L, 52L, 46L,
17L, 13L, 37L, 4L, 24L, 25L, 8L, 7L, 48L, 19L, 110L, 115L,
22L, 27L, 52L, 46L, 17L, 13L, 37L, 4L, 24L, 25L, 8L, 7L,
48L, 19L, 110L, 115L, 22L, 27L), Included = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("Consistent", "Inconsistent"), class = "factor"),
Fake = c(477, 497, 560, 543, 549, 466, 486, 516, 525, 534,
496, 580, 587, 557, 517, 563, 524, 546, 547, 596, 632, 544,
497, 592, 544, 612, 642, 545, 574, 602, 532, 522, 519, 534,
580, 587, 557, 477, 497, 560, 543, 549, 574, 543, 603, 582,
496, 580, 587, 557, 511, 622, 604, 546, 547, 596, 632, 634,
556, 536, 588, 612, 642, 545, 477, 497, 560, 543, 549, 466,
486, 516, 525, 534, 496, 580, 587, 557, 517, 563, 524, 546,
547, 596, 632, 544, 497, 592, 544, 612, 642, 545, 574, 602,
532, 522, 519, 534, 580, 587, 557, 477, 497, 560, 543, 549,
574, 543, 603, 582, 496, 580, 587, 557, 511, 622, 604, 546,
547, 596, 632, 634, 556, 536, 588, 612, 642, 545, 452, 547,
510, 663, 470, 503, 600, 517, 491, 505, 641, 581, 520, 485,
517, 622, 452, 547, 510, 663, 470, 503, 600, 517, 491, 505,
641, 581, 520, 485, 517, 622, 510, 458, 558, 638, 483, 538,
577, 600, 452, 547, 510, 663, 470, 503, 600, 517, 510, 458,
558, 638, 483, 538, 577, 600, 452, 547, 510, 663, 470, 503,
600, 517), Check = c(20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0)), row.names = c(NA, -192L), class = "data.frame")
We can check multiple conditions with & or |. Here, we would need & because both the conditions should be satisfied
option1$Surgical <- ifelse(option1$Variability == "Single" &
option1$Duration > 625, option1$Duration - 20, option1$Duration)
You can do this directly without using ifelse :
option1$Surgical <- with(option1, Duration - (20 *
(Variability == 'Single' & Duration > 625)))
Here, we take advantage of the fact that
20 * TRUE #gives
#[1] 20
and
20 * FALSE #gives
#[1] 0
So whenever the condition (Variability == 'Single' & Duration > 625) is TRUE it subtracts 20 from Duration or else 0.

Identify function is not accurate in R

Here is the problem:
When I use cook's distance to check influential points in SLR, I used two methods.
First one:
plot(mortality.model, which = 4)
This one gives me the correct answer.
Second one:
plot(cooks.distance(mortality.model), type = 'p')
identify(cooks.distance(mortality.model))
This one gives me the wrong answer, but very close to the correct answer.
Read the data set:
df.mortality <- read.csv("mortality.csv", header = TRUE)
Build the model:
mortality.model <- lm(log(infant) ~ log(income))
By the way, the dataset has NA values. If you would like to see the dataset, I could email it to you.
The dput result:
structure(list(X = structure(c(4L, 5L, 7L, 15L, 23L, 29L, 30L, 101L,
41L,43L, 46L, 61L, 62L, 66L, 73L, 79L, 86L, 87L, 10L, 97L, 2L, 25L, 38L,
39L, 40L, 52L, 65L, 75L, 100L, 3L, 9L, 18L, 19L, 21L, 24L, 32L, 33L, 42L,
45L, 50L, 55L, 58L, 63L, 68L, 71L, 77L, 83L, 89L, 93L, 94L, 99L, 103L,
105L, 8L, 14L, 20L, 26L, 27L, 31L, 36L, 44L, 47L, 80L, 51L, 59L, 69L, 70L,
72L, 88L, 91L, 95L, 81L, 1L, 6L,11L, 12L, 13L, 16L, 17L, 22L, 28L, 34L,
35L, 37L, 48L, 49L, 53L, 54L, 56L, 57L, 60L, 64L, 67L, 74L, 76L, 78L, 84L,
85L, 90L, 92L, 96L, 98L, 82L, 102L, 104L), .Label = c("Afganistan",
"Algeria", "Argentina", "Australia", "Austria", "Bangladesh","Belgium",
"Bolivia", "Brazil", "Britain", "Burma","Burundi","Cambodia","Cameroon",
"Canada", "Central.African.Republic", "Chad","Chile", "Colombia","Congo",
"Costa.Rica", "Dahomey", "Denmark", "Dominican.Republic", "Ecuador",
"Egypt", "El.Salvador", "Ethiopia", "Finland", "France", "Ghana",
"Greece", "Guatemala", "Guinea", "Haiti", "Honduras", "India",
"Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Ivory.Coast",
"Jamaica", "Japan", "Jordan", "Kenya", "Laos", "Lebanon", "Liberia",
"Libya", "Madagascar", "Malawi", "Malaysia", "Mali", "Mauritania",
"Mexico", "Moroco", "Nepal", "Netherlands", "New.Zealand", "Nicaragua",
"Niger", "Nigeria", "Norway", "Pakistan", "Panama", "Papua.New.Guinea",
"Paraguay", "Peru", "Philippines", "Portugal", "Rwanda", "Saudi.Arabia",
"Sierra.Leone", "Singapore", "Somalia", "South.Africa", "South.Korea",
"South.Vietnam", "Southern.Yemen", "Spain", "Sri.Lanka", "Sudan",
"Sweden", "Switzerland", "Syria", "Taiwan", "Tanzania", "Thailand",
"Togo", "Trinidad.and.Tobago", "Tunisia", "Turkey", "Uganda",
"United.States", "Upper.Volta", "Uruguay", "Venezuela", "West.Germany",
"Yemen", "Yugoslavia", "Zaire", "Zambia"), class = "factor"),
income = c(3426L, 3350L, 3346L, 4751L, 5029L, 3312L, 3403L,
5040L, 2009L, 2298L, 3292L, 4103L, 3723L, 4102L, 956L, 1000L,
5596L, 2963L, 2503L, 5523L, 400L, 250L, 110L, 1280L, 560L,
3010L, 220L, 1530L, 1240L, 1191L, 425L, 590L, 426L, 725L,
406L, 1760L, 302L, 2526L, 727L, 631L, 295L, 684L, 507L, 754L,
335L, 1268L, 1256L, 261L, 732L, 434L, 799L, 406L, 310L, 200L,
100L, 281L, 210L, 319L, 217L, 284L, 387L, 334L, 344L, 197L,
279L, 477L, 347L, 230L, 334L, 210L, 435L, 130L, 75L, 100L,
73L, 68L, 123L, 122L, 70L, 81L, 79L, 79L, 100L, 93L, 169L,
71L, 120L, 130L, 50L, 174L, 90L, 70L, 102L, 61L, 148L, 85L,
162L, 125L, 120L, 160L, 134L, 82L, 96L, 77L, 118L), infant = c(26.7,
23.7, 17, 16.8, 13.5, 10.1, 12.9, 20.4, 17.8, 25.7, 11.7,
11.6, 16.2, 11.3, 44.8, 71.5, 9.6, 12.8, 17.5, 17.6, 86.3,
78.5, 125, NA, 28.1, 300, 58, 650, 51.7, 59.6, 170, 78, 62.8,
54.4, 48.8, 27.8, 79.1, 22.1, 26.2, 13.6, 32, 60.9, 46, 34.1,
65.1, 20.4, 15.1, 19.1, 26.2, 76.3, 40.4, 43.3, 259, 60.4,
137, 180, 114, 58.2, 63.7, 39.3, 138, 21.3, 58, 159.2, 149,
10.2, 38.6, 67.9, 21.7, 27, 153, 100, 400, 124.3, 200, 150,
100, 190, 160, 109.6, 84.2, 216, NA, 60.6, 55, NA, 102, 148.3,
120, 187, NA, 200, 124.3, 132.9, 170, 158, 45.1, 129.4, 162.5,
127, 160, 180, 80, 50, 104), region = structure(c(3L, 4L,
4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 1L, 4L,
4L, 4L, 2L, 1L, 2L, 3L, 3L, 3L, 1L, 1L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 4L,
3L, 2L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 3L,
3L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L,
1L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L,
3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L), .Label = c("Africa",
"Americas", "Asia", "Europe"), class = "factor"), oil = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("no",
"yes"), class = "factor")), class = "data.frame", row.names = c(NA,
-105L))
Thanks!
Here are results:The correct answer The wrong answer
Could anyone explain why it happened?

How to plot different coloured GPS points (latitude and longitude) on a map for different categories of a key parameter using geom_point() in R

Overview:
I am undertaking a study regarding how latitude influences leaf senescence (leaf loss) in deciduous oak trees Q. Robur.
I am completely new to producing maps in R and I have been trying to produce the desired results for a couple of days without success.
If anyone can help, I would be deeply appreciative.
Problems:
I produced a map of the UK using my_map() (see diagram 1) and I have a data frame called lonlat_df containing the longitude and latitude coordinates for all oak trees recorded.
I am trying to incorporate the tree data points onto the map of the UK using geom_point(). However, I am unsure how to integrate the map, the GPS points for the tree species, and the key parameter objects together.
My goal
To produce 3 separate maps of the UK showing the GPS points (see desired output below) for each oak tree species recorded in the study, but I would like the points to be 4 different colours to correlate with each key parameter category (see below), in conjunction with a legend for each parameter category.
Key Parameters:
Urbanisation Index: 1=Urban, 2=Suburban, 3=village, 4=rural
Stand Density Index: 1=standing alone, 2=within a few trees or close proximity to other trees, 3=within a stand of 10-30 trees, and 4=large or woodland
Phenological Index: 1=no indication of autumn timing, 2=first autumn tinting, 3=partial autumn tinting (>25% of leaves), and 4=advanced autumn tinting (>75% of leaves)
R-code
##Import Packages
library(ggplot2)
library(maps)
library(mapdata)
library(tidyverse)
##Create objects for the key parameters from the data frame below called QuercusRobur1 to use as point data
latitude<-QuercusRobur1$Latitude
longitude<-QuercusRobur1$Longitude
PhenologyIndex<-QuercusRobur1$Phenological_Index
StandDensityIndex<-QuercusRobur1$Stand_density_index
UrbanisationIndex<-QuercusRobur1$Urbanisation_index
Species<-QuercusRobur1$Species
##Produce new data frame
lonlat_df<-as.data.frame(cbind(longitude, latitude, PhenologyIndex))
head(lonlat_df)
##Produce a map of the UK from maps:
UK <- map_data(map = "world", region = "UK")
head(UK)
dim(UK)
##Visualise the map of the UK using ggplot()
dev.new()
UK.Map<-ggplot(data = UK, aes(x = long, y = lat, group = group)) +
geom_point(colour="red", size=3, alpha=0.2)+
geom_polygon() +
coord_map()
##Produce Point Data
MapPoints<- MapUK + geom_point(data=lonlat_df, aes(x=long, y=lat, group=PhenologyIndex), colour="red", shape=21, fill="red", size=0.5)
Diagram 1
Desired output:
I would like to overlay the type of points shown in the desired output below on the map of the UK produced from the R-code above.
Dataframe
structure(list(Obs_.no = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 44L, 45L, 46L, 47L, 57L, 58L, 59L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 74L,
75L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 93L,
102L, 103L, 104L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L,
120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L,
131L, 135L, 136L, 137L, 138L, 143L, 144L, 145L, 146L, 147L, 148L,
149L, 150L, 151L, 152L, 153L, 154L, 155L, 158L, 159L, 160L, 161L,
162L, 163L, 164L, 165L, 169L, 170L, 171L, 172L, 177L, 178L, 179L,
180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L,
191L, 192L, 193L, 194L, 195L, 196L, 200L, 201L, 202L, 203L, 204L,
205L, 206L, 207L, 208L, 210L, 212L, 214L, 215L, 216L, 217L, 218L,
219L, 220L, 221L, 233L, 234L, 235L, 237L, 239L, 246L, 255L, 256L,
257L, 258L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 277L, 278L,
279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L,
290L, 291L, 292L, 293L, 294L, 295L, 296L), Date_observed = structure(c(4L,
15L, 6L, 6L, 6L, 6L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 6L, 6L,
6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L, 12L, 7L, 7L, 9L, 9L, 9L,
9L, 5L, 5L, 5L, 5L, 14L, 14L, 14L, 14L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 6L, 6L, 5L, 5L, 9L, 9L, 9L, 9L, 3L, 3L, 3L, 3L, 4L, 4L,
1L, 1L, 11L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 11L,
11L, 11L, 4L, 4L, 4L, 4L, 8L, 8L, 10L, 10L, 10L, 10L, 9L, 9L,
9L, 9L, 3L, 3L, 3L, 3L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 13L,
13L, 13L, 13L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 3L, 3L, 3L,
3L, 13L, 13L, 13L, 13L, 9L, 9L, 10L, 10L, 10L, 2L, 2L, 3L, 3L,
3L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 5L, 5L, 11L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 11L, 11L, 11L, 11L, 6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L), .Label = c("10/1/18",
"10/19/18", "10/20/18", "10/21/18", "10/22/18", "10/23/18", "10/24/18",
"10/25/18", "10/26/18", "10/27/18", "10/28/18", "10/28/19", "10/29/18",
"12/9/18", "8/20/18"), class = "factor"), Latitude = c(51.4175,
52.12087, 52.0269, 52.0269, 52.0269, 52.0269, 52.947709, 52.947709,
51.491811, 51.491811, 52.59925, 52.59925, 52.59925, 52.59925,
51.60157, 51.60157, 52.6888, 52.6888, 52.6888, 52.6888, 50.697802,
50.697802, 50.697802, 50.697802, 53.62417, 50.446841, 50.446841,
53.959679, 53.959679, 53.959679, 53.959679, 51.78375, 51.78375,
51.78375, 51.78375, 51.456965, 51.456965, 51.456965, 51.456965,
51.3651, 51.3651, 51.3651, 51.3651, 52.01182, 52.01182, 52.01182,
52.01182, 50.114277, 50.114277, 51.43474, 51.43474, 51.10676,
51.10676, 51.10676, 51.10676, 50.435984, 50.435984, 50.435984,
50.435984, 51.78666, 51.78666, 52.441088, 52.441088, 52.552344,
49.259471, 49.259471, 49.259471, 49.259471, 50.461625, 50.461625,
50.461625, 50.461625, 51.746642, 51.746642, 51.746642, 51.746642,
52.2501, 52.2501, 52.2501, 52.2501, 52.423336, 52.423336, 52.423336,
52.423336, 53.615575, 53.615575, 53.615575, 53.615575, 51.08474,
51.08474, 51.08474, 53.19329, 53.19329, 53.19329, 53.19329, 55.96785,
55.96785, 56.52664, 56.52664, 56.52664, 56.52664, 51.8113, 51.8113,
51.8113, 51.8113, 52.580157, 52.580157, 52.580157, 52.580157,
50.52008, 50.52008, 50.52008, 50.52008, 51.48417, 51.48417, 51.48417,
51.48417, 54.58243, 54.58243, 54.58243, 54.58243, 52.58839, 52.58839,
52.58839, 52.58839, 52.717283, 52.717283, 52.717283, 52.717283,
50.740764, 50.740764, 50.740764, 50.740764, 52.57937, 52.57937,
52.57937, 52.57937, 50.736531, 50.736531, 50.79926, 50.79926,
50.79926, 53.675996, 53.675996, 48.35079, 48.35079, 48.35079,
48.35079, 51.36445, 51.36445, 51.36445, 51.36445, 52.122402,
52.122402, 52.122402, 52.16104, 52.16104, 55.91913, 51.6528,
51.6528, 51.6528, 51.6528, 51.88485, 51.88485, 51.88485, 51.88485,
52.34015, 52.34015, 52.34015, 52.026042, 52.026042, 52.026042,
52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 51.51357,
51.51357, 51.51357, 51.51357, 53.43202, 53.43202, 53.43202, 53.43202,
51.50823, 51.50823, 51.50823, 51.50823), Longitude = c(-0.32118,
-0.29293, -0.7078, -0.7078, -0.7078, -0.7078, -1.435407, -1.435407,
-3.210324, -3.210324, 1.33011, 1.33011, 1.33011, 1.33011, -3.67111,
-3.67111, -3.30909, -3.30909, -3.30909, -3.30909, -2.11692, -2.11692,
-2.11692, -2.11692, -2.43155, -3.706923, -3.706923, -1.061008,
-1.061008, -1.061008, -1.061008, -0.65046, -0.65046, -0.65046,
-0.65046, -2.624917, -2.624917, -2.624917, -2.624917, 0.70706,
0.70706, 0.70706, 0.70706, -0.70082, -0.70082, -0.70082, -0.70082,
-5.541128, -5.541128, 0.45981, 0.45981, -2.32071, -2.32071, -2.32071,
-2.32071, -4.105617, -4.105617, -4.105617, -4.105617, -0.71433,
-0.71433, -0.176158, -0.176158, -1.337177, -123.107788, -123.107788,
-123.107788, -123.107788, 3.560973, 3.560973, 3.560973, 3.560973,
0.486416, 0.486416, 0.486416, 0.486416, -0.8825, -0.8825, -0.8825,
-0.8825, -1.787563, -1.787563, -1.787563, -1.787563, -2.432959,
-2.432959, -2.432959, -2.432959, -0.73645, -0.73645, -0.73645,
-0.63793, -0.63793, -0.63793, -0.63793, -3.18084, -3.18084, -3.40313,
-3.40313, -3.40313, -3.40313, -0.22894, -0.22894, -0.22894, -0.22894,
-1.948571, -1.948571, -1.948571, -1.948571, -4.20756, -4.20756,
-4.20756, -4.20756, -0.34854, -0.34854, -0.34854, -0.34854, -5.93229,
-5.93229, -5.93229, -5.93229, -1.96843, -1.96843, -1.96843, -1.96843,
-2.410575, -2.410575, -2.410575, -2.410575, -2.361234, -2.361234,
-2.361234, -2.361234, -1.89325, -1.89325, -1.89325, -1.89325,
-2.011143, -2.011143, -3.19446, -3.19446, -3.19446, -1.272824,
-1.272824, 10.91812, 10.91812, 10.91812, 10.91812, -0.23106,
-0.23106, -0.23106, -0.23106, -0.487443, -0.487443, -0.487443,
0.18702, 0.18702, -3.20987, -1.57361, -1.57361, -1.57361, -1.57361,
-0.17844, -0.17844, -0.17844, -0.17844, -1.27795, -1.27795, -1.27795,
-0.503114, -0.503114, -0.503114, -0.503114, -0.472994, -0.472994,
-0.472994, -0.472994, -3.18738, -3.18738, -3.18738, -3.18738,
-2.27968, -2.27968, -2.27968, -2.27968, -0.25847, -0.25847, -0.25847,
-0.25847), Altitude = c(5L, 0L, 68L, 68L, 68L, 68L, 104L, 104L,
15L, 15L, 23L, 23L, 23L, 23L, 184L, 184L, 176L, 176L, 176L, 176L,
12L, 12L, 12L, 12L, 178L, 36L, 36L, 11L, 11L, 11L, 11L, 210L,
210L, 210L, 210L, 97L, 97L, 97L, 97L, 23L, 23L, 23L, 23L, 0L,
0L, 0L, 0L, 9L, 9L, 4L, 4L, 200L, 200L, 200L, 200L, 160L, 160L,
160L, 160L, 166L, 166L, 0L, 0L, 0L, 47L, 47L, 47L, 47L, 58L,
58L, 58L, 58L, 43L, 43L, 43L, 43L, 97L, 97L, 97L, 97L, 133L,
133L, 133L, 133L, 123L, 123L, 123L, 123L, 128L, 128L, 128L, 15L,
15L, 15L, 15L, 14L, 14L, 65L, 65L, 65L, 65L, 129L, 129L, 129L,
129L, 140L, 140L, 140L, 140L, 18L, 18L, 18L, 18L, 30L, 30L, 30L,
30L, 19L, 19L, 19L, 19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 96L,
96L, 96L, 96L, 169L, 169L, 169L, 169L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 43L, 43L, 43L, 75L, 75L,
109L, 110L, 110L, 110L, 110L, 95L, 95L, 95L, 95L, 112L, 112L,
112L, 0L, 0L, 0L, 0L, 24L, 24L, 24L, 24L, 38L, 38L, 38L, 38L,
29L, 29L, 29L, 29L, 20L, 20L, 20L, 20L), Species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Quercus robur", class = "factor"),
Tree_diameter = c(68.8, 10, 98.5, 97, 32.5, 45.1, 847, 817,
62, 71, 140, 111.4, 114.6, 167.1, 29, 40.1, 68, 45, 60, 54,
104, 122, 85, 71, 81, 39.8, 43.6, 20.1, 17.8, 15.6, 12.1,
81.8, 102.5, 75.5, 57.3, 0.3, 0.2, 0.3, 0.3, 70, 36, 53,
44, 31.5, 27.1, 23.3, 22, 69.4, 37.3, 19.9, 14.6, 196, 122,
118, 180, 58.6, 54.1, 58, 61.5, 58.4, 61, 134, 64, 52.2,
170, 114, 127, 158, 147.4, 135.3, 122.9, 104.1, 263, 237,
322, 302, 175, 182, 141, 155, 89, 41, 70, 83, 141, 86.5,
82, 114.5, 129, 127, 143, 125, 92, 68, 90, 24.5, 20.1, 63.7,
39.8, 66.2, 112.4, 124.5, 94.1, 68.6, 74.4, 23.6, 27.7, 22.9,
25.2, 24.2, 54.7, 43, 33.1, 306, 274, 56, 60, 72.5, 128.5,
22, 16, 143, 103, 53, 130, 48.4, 69.8, 6.4, 18.6, 129.2,
41.7, 57.6, 14, 41.7, 30.2, 39.5, 24.2, 320, 352, 120.9,
108.3, 53.2, 274, 85, 52, 43, 38, 37, 219, 215, 216, 175,
85.9, 49.7, 97.1, 40.8, 62.4, 80.3, 43, 50.3, 28.7, 31.9,
181.5, 149.7, 122, 143.6, 148, 145, 99, 28, 32, 54, 54, 169,
152, 160, 138, 90.8, 87.9, 77.4, 81.2, 91.7, 62.7, 50, 72.9,
23.7, 58, 80.7, 73.7), Urbanisation_index = structure(c(2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 1L, 1L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L,
2L, 4L, 4L, 2L, 2L, 2L, 3L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 4L, 4L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3",
"4"), class = "factor"), Stand_density_index = structure(c(3L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 3L,
4L, 4L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L,
2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 2L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L), .Label = c("1", "2", "3",
"4"), class = "factor"), Canopy_Index = c(85L, 85L, 85L,
75L, 45L, 25L, 75L, 65L, 75L, 75L, 95L, 95L, 95L, 95L, 95L,
65L, 85L, 65L, 95L, 85L, 85L, 85L, 75L, 75L, 65L, 85L, 85L,
75L, 75L, 85L, 65L, 95L, 85L, 95L, 95L, 75L, 75L, 85L, 85L,
85L, 85L, 85L, 75L, 85L, 85L, 85L, 85L, 75L, 75L, 85L, 85L,
65L, 75L, 85L, 75L, 95L, 95L, 95L, 95L, 75L, 65L, 95L, 95L,
55L, 75L, 65L, 75L, 65L, 85L, 95L, 95L, 75L, 95L, 75L, 95L,
65L, 75L, 75L, 85L, 85L, 65L, 95L, 65L, 65L, 65L, 65L, 65L,
65L, 85L, 85L, 75L, 95L, 85L, 85L, 75L, 45L, 55L, 35L, 35L,
25L, 25L, 95L, 85L, 75L, 85L, 85L, 75L, 75L, 65L, 75L, 85L,
65L, 45L, 95L, 95L, 95L, 95L, 65L, 75L, 45L, 35L, 75L, 95L,
95L, 85L, 75L, 65L, 85L, 95L, 75L, 85L, 85L, 95L, 65L, 65L,
45L, 65L, 85L, 35L, 95L, 85L, 85L, 85L, 85L, 75L, 65L, 65L,
65L, 65L, 55L, 75L, 85L, 85L, 95L, 85L, 75L, 75L, 85L, 65L,
45L, 75L, 75L, 65L, 65L, 75L, 65L, 95L, 95L, 95L, 85L, 65L,
75L, 75L, 75L, 65L, 75L, 35L, 75L, 75L, 75L, 75L, 25L, 45L,
45L, 35L, 85L, 95L, 85L, 95L), Phenological_Index = c(2L,
4L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L,
3L, 3L, 4L, 3L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 4L, 2L, 4L,
3L, 3L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L)), row.names = c(NA, -189L
), class = "data.frame")
There's no need to create an additional data frame, especially if you don't include the key parameter variables there.
You can try this:
p <- ggplot(QuercusRobur1,
aes(x = Longitude, y = Latitude)) +
geom_polygon(data = UK,
aes(x = long, y = lat, group = group),
inherit.aes = FALSE) +
geom_point() +
coord_map(xlim = c(-10, 5)) + #limits added as there are some points really far away
theme_classic()
p +
aes(color = Urbanisation_index) +
scale_color_discrete(name = "Urbanisation Index",
labels = c("Urban", "Suburban", "Village", "Rural"))
p +
aes(color = Stand_density_index) +
scale_color_discrete(name = "Stand Density Index",
labels = c("Standing alone",
"Within a few trees or close proximity to other trees",
"Within a stand of 10-30 trees",
"Large or woodland"))
p +
aes(color = factor(Phenological_Index)) +
scale_color_discrete(name = "Phenological Index",
labels = c("No indication of autumn timing",
"First autumn tinting",
"Partial autumn tinting (>25% of leaves)",
"Advanced autumn tinting (>75% of leaves)"))

Linear Regression - NA's inserted for each category of an independent variable

Overview:
I have one dependent variable called 'Tree_diameter', and one independent variable called 'Stand_density_index' (see data frame 1 and 2dbelow).
Stand_density_index contains four categories:
Standing alone
A few trees in close proximity to other trees
Within a stand of 10-20 trees
large stand or woodland
If anyone could please advise which is the correct linear regression approach here:
Method 1
Method 2
Method 3
I would be deeply appreciative.
Overall Aim of the Question:
Using the data from the full database (see data frame 2 below) and the results from an appropriate statistical test, accept or reject the following hypothesis at the 5 % level of significance.
Hypothesis:
H(0): There is no difference in stem diameter of Quercus robur between the different categories of stand density index
From the whole database STATE
The statistical test used - linear regression
The independent (Tree_diameter) and the dependent variable (Stand_density_index)
Justify your conclusion based on this test
Method 1 - constructed with data frame 1
Firstly, I summarised the data frame to find the Mean_Tree_Diameter for each category of the Stand_density_index (see categories above).
Issues:
After I run the linear regression, NA's are being inserted into the results categories.
If anyone can help me understand why I would be deeply appreciative.
##Reformat the vectors correctly
##Stand_density_index = as.factor
Summarised_QuercusRobur1NewData$Stand_density_index<-as.factor(Summarised_QuercusRobur1NewData$Stand_density_index)
##Recheck the structure of the data frame
str(Summarised_QuercusRobur1NewData
##Linear Regression equation
SpeciesStemDensity<-lm(Mean_Tree_Diameter~Stand_density_index, data=Summarised_QuercusRobur1NewData)
##Summary Statistics
summary(SpeciesStemDensity)
##Summary Statistics Results
Method 2 - constructed with data frame 2
In this instance, I used the whole database (see data frame 2) and I reformated 'Stand_density_index' into a factor and run the linear regression model.
##as.factor
##Reformat stand_density_index vector into a categorical vector
QuercusRobur1$Stand_density_index<-as.factor(QuercusRobur1$Stand_density_index)
##Linear Regression
StemDensityStand<-lm(Tree_diameter~Stand_density_index, data=QuercusRobur1)
##Summary Statistics
summary(StemDensityStand)
##Results
Method 3 - Constructed from Data frame 2
I ran the linear regression model with the whole database but the 'Stand_density_index' was numeric.
##as numeric
##Reformat stand_density_index into a categorical vector
QuercusRobur1$Stand_density_index<-as.numeric(QuercusRobur1$Stand_density_index)
##Linear Regression
StemDensityStand<-lm(Tree_diameter~Stand_density_index, data=QuercusRobur1)
##Summary Statistics
summary(StemDensityStand)
##Results
Data frame 1
structure(list(Stand_density_index = structure(1:4, .Label = c("1",
"2", "3", "4"), class = "factor"), Species = structure(c(1L,
1L, 1L, 1L), .Label = "Quercus robur", class = "factor"), Obs_no = c(9L,
82L, 40L, 58L), Mean_Tree_Diameter = c(86.9222222222222, 121.717073170732,
82, 72.4275862068965), SD_Tree_Diameter = c(57.2766046867693,
134.510951231506, 60.202253131019, 61.1575440200358)), row.names = c(NA,
-4L), class = "data.frame")
Data frame 2
structure(list(Obs_.no = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 44L, 45L, 46L, 47L, 57L, 58L, 59L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 74L,
75L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 93L,
102L, 103L, 104L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L,
120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L,
131L, 135L, 136L, 137L, 138L, 143L, 144L, 145L, 146L, 147L, 148L,
149L, 150L, 151L, 152L, 153L, 154L, 155L, 158L, 159L, 160L, 161L,
162L, 163L, 164L, 165L, 169L, 170L, 171L, 172L, 177L, 178L, 179L,
180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L,
191L, 192L, 193L, 194L, 195L, 196L, 200L, 201L, 202L, 203L, 204L,
205L, 206L, 207L, 208L, 210L, 212L, 214L, 215L, 216L, 217L, 218L,
219L, 220L, 221L, 233L, 234L, 235L, 237L, 239L, 246L, 255L, 256L,
257L, 258L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 277L, 278L,
279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L,
290L, 291L, 292L, 293L, 294L, 295L, 296L), Date_observed = structure(c(4L,
15L, 6L, 6L, 6L, 6L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 6L, 6L,
6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L, 12L, 7L, 7L, 9L, 9L, 9L,
9L, 5L, 5L, 5L, 5L, 14L, 14L, 14L, 14L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 6L, 6L, 5L, 5L, 9L, 9L, 9L, 9L, 3L, 3L, 3L, 3L, 4L, 4L,
1L, 1L, 11L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 11L,
11L, 11L, 4L, 4L, 4L, 4L, 8L, 8L, 10L, 10L, 10L, 10L, 9L, 9L,
9L, 9L, 3L, 3L, 3L, 3L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 13L,
13L, 13L, 13L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 3L, 3L, 3L,
3L, 13L, 13L, 13L, 13L, 9L, 9L, 10L, 10L, 10L, 2L, 2L, 3L, 3L,
3L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 5L, 5L, 11L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 11L, 11L, 11L, 11L, 6L, 6L, 6L, 6L, 11L, 11L, 11L, 11L), .Label = c("10/1/18",
"10/19/18", "10/20/18", "10/21/18", "10/22/18", "10/23/18", "10/24/18",
"10/25/18", "10/26/18", "10/27/18", "10/28/18", "10/28/19", "10/29/18",
"12/9/18", "8/20/18"), class = "factor"), Latitude = c(51.4175,
52.12087, 52.0269, 52.0269, 52.0269, 52.0269, 52.947709, 52.947709,
51.491811, 51.491811, 52.59925, 52.59925, 52.59925, 52.59925,
51.60157, 51.60157, 52.6888, 52.6888, 52.6888, 52.6888, 50.697802,
50.697802, 50.697802, 50.697802, 53.62417, 50.446841, 50.446841,
53.959679, 53.959679, 53.959679, 53.959679, 51.78375, 51.78375,
51.78375, 51.78375, 51.456965, 51.456965, 51.456965, 51.456965,
51.3651, 51.3651, 51.3651, 51.3651, 52.01182, 52.01182, 52.01182,
52.01182, 50.114277, 50.114277, 51.43474, 51.43474, 51.10676,
51.10676, 51.10676, 51.10676, 50.435984, 50.435984, 50.435984,
50.435984, 51.78666, 51.78666, 52.441088, 52.441088, 52.552344,
49.259471, 49.259471, 49.259471, 49.259471, 50.461625, 50.461625,
50.461625, 50.461625, 51.746642, 51.746642, 51.746642, 51.746642,
52.2501, 52.2501, 52.2501, 52.2501, 52.423336, 52.423336, 52.423336,
52.423336, 53.615575, 53.615575, 53.615575, 53.615575, 51.08474,
51.08474, 51.08474, 53.19329, 53.19329, 53.19329, 53.19329, 55.96785,
55.96785, 56.52664, 56.52664, 56.52664, 56.52664, 51.8113, 51.8113,
51.8113, 51.8113, 52.580157, 52.580157, 52.580157, 52.580157,
50.52008, 50.52008, 50.52008, 50.52008, 51.48417, 51.48417, 51.48417,
51.48417, 54.58243, 54.58243, 54.58243, 54.58243, 52.58839, 52.58839,
52.58839, 52.58839, 52.717283, 52.717283, 52.717283, 52.717283,
50.740764, 50.740764, 50.740764, 50.740764, 52.57937, 52.57937,
52.57937, 52.57937, 50.736531, 50.736531, 50.79926, 50.79926,
50.79926, 53.675996, 53.675996, 48.35079, 48.35079, 48.35079,
48.35079, 51.36445, 51.36445, 51.36445, 51.36445, 52.122402,
52.122402, 52.122402, 52.16104, 52.16104, 55.91913, 51.6528,
51.6528, 51.6528, 51.6528, 51.88485, 51.88485, 51.88485, 51.88485,
52.34015, 52.34015, 52.34015, 52.026042, 52.026042, 52.026042,
52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 51.51357,
51.51357, 51.51357, 51.51357, 53.43202, 53.43202, 53.43202, 53.43202,
51.50823, 51.50823, 51.50823, 51.50823), Longitude = c(-0.32118,
-0.29293, -0.7078, -0.7078, -0.7078, -0.7078, -1.435407, -1.435407,
-3.210324, -3.210324, 1.33011, 1.33011, 1.33011, 1.33011, -3.67111,
-3.67111, -3.30909, -3.30909, -3.30909, -3.30909, -2.11692, -2.11692,
-2.11692, -2.11692, -2.43155, -3.706923, -3.706923, -1.061008,
-1.061008, -1.061008, -1.061008, -0.65046, -0.65046, -0.65046,
-0.65046, -2.624917, -2.624917, -2.624917, -2.624917, 0.70706,
0.70706, 0.70706, 0.70706, -0.70082, -0.70082, -0.70082, -0.70082,
-5.541128, -5.541128, 0.45981, 0.45981, -2.32071, -2.32071, -2.32071,
-2.32071, -4.105617, -4.105617, -4.105617, -4.105617, -0.71433,
-0.71433, -0.176158, -0.176158, -1.337177, -123.107788, -123.107788,
-123.107788, -123.107788, 3.560973, 3.560973, 3.560973, 3.560973,
0.486416, 0.486416, 0.486416, 0.486416, -0.8825, -0.8825, -0.8825,
-0.8825, -1.787563, -1.787563, -1.787563, -1.787563, -2.432959,
-2.432959, -2.432959, -2.432959, -0.73645, -0.73645, -0.73645,
-0.63793, -0.63793, -0.63793, -0.63793, -3.18084, -3.18084, -3.40313,
-3.40313, -3.40313, -3.40313, -0.22894, -0.22894, -0.22894, -0.22894,
-1.948571, -1.948571, -1.948571, -1.948571, -4.20756, -4.20756,
-4.20756, -4.20756, -0.34854, -0.34854, -0.34854, -0.34854, -5.93229,
-5.93229, -5.93229, -5.93229, -1.96843, -1.96843, -1.96843, -1.96843,
-2.410575, -2.410575, -2.410575, -2.410575, -2.361234, -2.361234,
-2.361234, -2.361234, -1.89325, -1.89325, -1.89325, -1.89325,
-2.011143, -2.011143, -3.19446, -3.19446, -3.19446, -1.272824,
-1.272824, 10.91812, 10.91812, 10.91812, 10.91812, -0.23106,
-0.23106, -0.23106, -0.23106, -0.487443, -0.487443, -0.487443,
0.18702, 0.18702, -3.20987, -1.57361, -1.57361, -1.57361, -1.57361,
-0.17844, -0.17844, -0.17844, -0.17844, -1.27795, -1.27795, -1.27795,
-0.503114, -0.503114, -0.503114, -0.503114, -0.472994, -0.472994,
-0.472994, -0.472994, -3.18738, -3.18738, -3.18738, -3.18738,
-2.27968, -2.27968, -2.27968, -2.27968, -0.25847, -0.25847, -0.25847,
-0.25847), Altitude = c(5L, 0L, 68L, 68L, 68L, 68L, 104L, 104L,
15L, 15L, 23L, 23L, 23L, 23L, 184L, 184L, 176L, 176L, 176L, 176L,
12L, 12L, 12L, 12L, 178L, 36L, 36L, 11L, 11L, 11L, 11L, 210L,
210L, 210L, 210L, 97L, 97L, 97L, 97L, 23L, 23L, 23L, 23L, 0L,
0L, 0L, 0L, 9L, 9L, 4L, 4L, 200L, 200L, 200L, 200L, 160L, 160L,
160L, 160L, 166L, 166L, 0L, 0L, 0L, 47L, 47L, 47L, 47L, 58L,
58L, 58L, 58L, 43L, 43L, 43L, 43L, 97L, 97L, 97L, 97L, 133L,
133L, 133L, 133L, 123L, 123L, 123L, 123L, 128L, 128L, 128L, 15L,
15L, 15L, 15L, 14L, 14L, 65L, 65L, 65L, 65L, 129L, 129L, 129L,
129L, 140L, 140L, 140L, 140L, 18L, 18L, 18L, 18L, 30L, 30L, 30L,
30L, 19L, 19L, 19L, 19L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 96L,
96L, 96L, 96L, 169L, 169L, 169L, 169L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 43L, 43L, 43L, 75L, 75L,
109L, 110L, 110L, 110L, 110L, 95L, 95L, 95L, 95L, 112L, 112L,
112L, 0L, 0L, 0L, 0L, 24L, 24L, 24L, 24L, 38L, 38L, 38L, 38L,
29L, 29L, 29L, 29L, 20L, 20L, 20L, 20L), Species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Quercus robur", class = "factor"),
Tree_diameter = c(68.8, 10, 98.5, 97, 32.5, 45.1, 847, 817,
62, 71, 140, 111.4, 114.6, 167.1, 29, 40.1, 68, 45, 60, 54,
104, 122, 85, 71, 81, 39.8, 43.6, 20.1, 17.8, 15.6, 12.1,
81.8, 102.5, 75.5, 57.3, 0.3, 0.2, 0.3, 0.3, 70, 36, 53,
44, 31.5, 27.1, 23.3, 22, 69.4, 37.3, 19.9, 14.6, 196, 122,
118, 180, 58.6, 54.1, 58, 61.5, 58.4, 61, 134, 64, 52.2,
170, 114, 127, 158, 147.4, 135.3, 122.9, 104.1, 263, 237,
322, 302, 175, 182, 141, 155, 89, 41, 70, 83, 141, 86.5,
82, 114.5, 129, 127, 143, 125, 92, 68, 90, 24.5, 20.1, 63.7,
39.8, 66.2, 112.4, 124.5, 94.1, 68.6, 74.4, 23.6, 27.7, 22.9,
25.2, 24.2, 54.7, 43, 33.1, 306, 274, 56, 60, 72.5, 128.5,
22, 16, 143, 103, 53, 130, 48.4, 69.8, 6.4, 18.6, 129.2,
41.7, 57.6, 14, 41.7, 30.2, 39.5, 24.2, 320, 352, 120.9,
108.3, 53.2, 274, 85, 52, 43, 38, 37, 219, 215, 216, 175,
85.9, 49.7, 97.1, 40.8, 62.4, 80.3, 43, 50.3, 28.7, 31.9,
181.5, 149.7, 122, 143.6, 148, 145, 99, 28, 32, 54, 54, 169,
152, 160, 138, 90.8, 87.9, 77.4, 81.2, 91.7, 62.7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 2, 2, 2, 2, 2,
2, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 3, 4,
4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4, 4, 2, 2, 2, 2,
2, 2, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 2, 1, 1, 2, 1, 1, 1, 4, 4, 4, 4, 3,
3, 3, 3, 4, 4, 4, 2, 3, 3, 3, 3, 2, 2, 2, 2), Canopy_Index = c(85L,
85L, 85L, 75L, 45L, 25L, 75L, 65L, 75L, 75L, 95L, 95L, 95L,
95L, 95L, 65L, 85L, 65L, 95L, 85L, 85L, 85L, 75L, 75L, 65L,
85L, 85L, 75L, 75L, 85L, 65L, 95L, 85L, 95L, 95L, 75L, 75L,
85L, 85L, 85L, 85L, 85L, 75L, 85L, 85L, 85L, 85L, 75L, 75L,
85L, 85L, 65L, 75L, 85L, 75L, 95L, 95L, 95L, 95L, 75L, 65L,
95L, 95L, 55L, 75L, 65L, 75L, 65L, 85L, 95L, 95L, 75L, 95L,
75L, 95L, 65L, 75L, 75L, 85L, 85L, 65L, 95L, 65L, 65L, 65L,
65L, 65L, 65L, 85L, 85L, 75L, 95L, 85L, 85L, 75L, 45L, 55L,
35L, 35L, 25L, 25L, 95L, 85L, 75L, 85L, 85L, 75L, 75L, 65L,
75L, 85L, 65L, 45L, 95L, 95L, 95L, 95L, 65L, 75L, 45L, 35L,
75L, 95L, 95L, 85L, 75L, 65L, 85L, 95L, 75L, 85L, 85L, 95L,
65L, 65L, 45L, 65L, 85L, 35L, 95L, 85L, 85L, 85L, 85L, 75L,
65L, 65L, 65L, 65L, 55L, 75L, 85L, 85L, 95L, 85L, 75L, 75L,
85L, 65L, 45L, 75L, 75L, 65L, 65L, 75L, 65L, 95L, 95L, 95L,
85L, 65L, 75L, 75L, 75L, 65L, 75L, 35L, 75L, 75L, 75L, 75L,
25L, 45L, 45L, 35L, 85L, 95L, 85L, 95L), Phenological_Index = c(2L,
4L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L,
3L, 3L, 4L, 3L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 4L, 2L, 4L,
3L, 3L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L)), row.names = c(NA, -189L
), class = "data.frame")
Alice!
The issue with you linear regression model is that you do not have enough data to perform a linear regression.
Because you have one dependent variable to explain each independent variable, you no need a model, just four equations with four variables to resolve.
That is why the intercept is equal to the Mean_Tree_Diameter for Stand_density_index==1 , intercept + Stand_density_index_2 equal to Mean_Tree_Diameter for Stand_density_index==2... Also, that is why your Multiple R Squared is 1. Your model is perfect!
So, either you do not use Stand_density_index in you model or you include more data (several values of Mean_Tree_Diameter for the same Mean_Tree_Diameter) or you will always get this results.
If you try your model with this data:
Summarised_QuercusRobur1NewData<-structure(list(Stand_density_index = structure(c(1,1,2,2), .Label = c("1",
"2"), class = "factor"), Species = structure(c(1L,
1L, 1L, 1L), .Label = "Quercus robur", class = "factor"), Obs_no = c(9L,
82L, 40L, 58L), Mean_Tree_Diameter = c(86.9222222222222, 121.717073170732,
82, 72.4275862068965), SD_Tree_Diameter = c(57.2766046867693,
134.510951231506, 60.202253131019, 61.1575440200358)), row.names = c(NA,
-4L), class = "data.frame")
You will get some results, because now you have 4 different independent variable results for only 2 different dependent variables.

Error using the step-function with glmmML

When I tried to use the step function I receive this error:
"Error in if (all(is.finite(c(n0, nnew))) && nnew != n0)
stop("number of rows in use has changed: remove missing values?") :
missing value where TRUE/FALSE needed"
Seems like it has something to do with missing values. I checked for this and there are none. I searched for more information around this error. I could only find one unanswered post from several years ago.
I've included random sample selection from my dataset, together with the R-code I used. (SD=integer. DIST,CD=numeric. Hunt,Region,DN,IDcat=categorical).
Sika.sample <- structure(list(ID = c(16L, 19L, 68L, 58L, 35L, 21L, 21L, 83L,
48L, 64L, 73L, 63L, 80L, 63L, 8L, 43L, 77L, 75L, 27L, 73L, 22L,
65L, 32L, 78L, 61L, 68L, 46L, 30L, 44L, 78L, 58L, 72L, 27L, 46L,
41L, 52L, 36L, 38L, 67L, 18L, 45L, 75L, 72L, 8L, 5L, 62L, 70L,
23L, 4L, 8L, 7L, 30L, 37L, 7L, 68L, 20L, 80L, 44L, 39L, 6L, 83L,
26L, 66L, 21L, 5L, 39L, 10L, 73L, 69L, 44L, 51L, 69L, 53L, 63L,
27L, 29L, 15L, 13L, 1L, 18L, 31L, 9L, 42L, 32L, 78L, 62L, 23L,
3L, 29L, 49L, 81L, 60L, 70L, 73L, 8L, 69L, 79L, 19L, 47L, 38L
), SD = c(8L, 3L, 4L, 6L, 2L, 1L, 8L, 0L, 4L, 2L, 8L, 2L, 0L,
8L, 0L, 0L, 2L, 2L, 0L, 3L, 0L, 2L, 25L, 0L, 18L, 28L, 0L, 10L,
1L, 0L, 0L, 1L, 0L, 10L, 1L, 0L, 0L, 7L, 0L, 0L, 18L, 0L, 0L,
0L, 0L, 28L, 1L, 0L, 10L, 1L, 0L, 2L, 0L, 0L, 3L, 7L, 0L, 0L,
8L, 0L, 5L, 1L, 3L, 33L, 1L, 3L, 0L, 1L, 0L, 0L, 19L, 0L, 3L,
3L, 0L, 1L, 0L, 3L, 5L, 2L, 0L, 0L, 0L, 2L, 0L, 10L, 0L, 0L,
0L, 0L, 2L, 0L, 2L, 0L, 8L, 1L, 0L, 0L, 0L, 0L), DIST = c(0,
0, 42.7, 800.6, 44.6, 0, 0, 19.3, 42.8, 570.7, 111.7, 348.2,
0, 348.2, 24, 0, 7.6, 3.1, 23.2, 111.7, 0, 404, 331.9, 0, 0,
42.7, 0, 97.7, 0, 0, 800.6, 295.5, 23.2, 0, 0, 0, 4.3, 29.5,
408.1, 37.7, 0, 3.1, 295.5, 24, 15.5, 0, 34.1, 0, 22.1, 24, 223.4,
97.7, 99.1, 223.4, 42.7, 75.2, 0, 0, 279.5, 28, 19.3, 58, 972.3,
0, 15.5, 279.5, 652.8, 111.7, 24.8, 0, 0, 24.8, 0, 348.2, 23.2,
278.8, 20.1, 30.6, 4.9, 37.7, 46.3, 735.7, 1.2, 331.9, 0, 0,
0, 5.8, 278.8, 817.6, 0, 190.4, 34.1, 111.7, 24, 24.8, 11.3,
0, 0, 29.5), CD = c(103.9, 25.3, 46.6, 99.4, 55, 95.2, 68, 62.5,
59, 78.8, 65.5, 46.6, 51.8, 78.2, 52.7, 15.7, 62.8, 81.3, 40.9,
82.5, 64.9, 50.1, 62, 56.1, 88.9, 77.2, 48.1, 69.2, 37.9, 101.8,
43.9, 82.4, 57, 75.1, 41.9, 42.2, 48.7, 53.3, 42, 61, 70.9, 38,
51.9, 39.3, 44.9, 69.7, 25.1, 49, 61.8, 58, 61.2, 41.1, 90.3,
45.8, 36.4, 103.1, 52.4, 84.6, 63.5, 53.5, 101.1, 64.4, 50, 80.8,
75.1, 47.5, 79.7, 44.9, 37, 29.1, 65.9, 49, 56.7, 61.4, 31.1,
102.7, 64.8, 51.4, 80.7, 61.6, 36, 50.3, 42.4, 47, 41.9, 68.4,
88.9, 56.2, 52.1, 50.1, 69.1, 55.1, 48.4, 34.1, 51, 77.9, 53.5,
36.8, 48.2, 38.7), DN = structure(c(1L, 2L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L), .Label = c("Day",
"Night"), class = "factor"), Hunt = structure(c(2L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L
), .Label = c("Hunt", "Nohunt"), class = "factor"), Region = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L), .Label = c("H", "S"), class = "factor"), IDcat = structure(c(16L,
19L, 68L, 58L, 35L, 21L, 21L, 83L, 48L, 64L, 73L, 63L, 80L, 63L,
8L, 43L, 77L, 75L, 27L, 73L, 22L, 65L, 32L, 78L, 61L, 68L, 46L,
30L, 44L, 78L, 58L, 72L, 27L, 46L, 41L, 52L, 36L, 38L, 67L, 18L,
45L, 75L, 72L, 8L, 5L, 62L, 70L, 23L, 4L, 8L, 7L, 30L, 37L, 7L,
68L, 20L, 80L, 44L, 39L, 6L, 83L, 26L, 66L, 21L, 5L, 39L, 10L,
73L, 69L, 44L, 51L, 69L, 53L, 63L, 27L, 29L, 15L, 13L, 1L, 18L,
31L, 9L, 42L, 32L, 78L, 62L, 23L, 3L, 29L, 49L, 81L, 60L, 70L,
73L, 8L, 69L, 79L, 19L, 47L, 38L), .Label = c("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"), class = "factor")), .Names = c("ID", "SD", "DIST",
"CD", "DN", "Hunt", "Region", "IDcat"), row.names = c(16L, 172L,
328L, 222L, 86L, 21L, 174L, 332L, 308L, 228L, 96L, 291L, 233L,
259L, 161L, 271L, 202L, 98L, 180L, 45L, 22L, 293L, 185L, 203L,
257L, 264L, 274L, 81L, 304L, 50L, 286L, 95L, 27L, 242L, 269L,
280L, 138L, 191L, 295L, 171L, 241L, 149L, 146L, 110L, 107L, 258L,
195L, 125L, 55L, 8L, 160L, 183L, 37L, 109L, 296L, 20L, 297L,
208L, 192L, 6L, 236L, 179L, 294L, 72L, 5L, 141L, 10L, 198L, 143L,
272L, 311L, 194L, 249L, 323L, 129L, 29L, 66L, 166L, 52L, 69L,
133L, 162L, 270L, 134L, 152L, 322L, 23L, 156L, 182L, 277L, 330L,
288L, 42L, 147L, 59L, 41L, 204L, 19L, 275L, 140L), class = "data.frame")
Glmm_full <- glmmML(SD~DIST*as.factor(Hunt)*as.factor(Region)*as.factor(DN),
offset=log(CD),data=Sika.sample,family="poisson",cluster=IDcat)
finalModel <-step(Glmm_full) #ERROR-MESSAGE

Resources