Aesthetic Mapping Temperature Data in ggplot2 - r

I am very new to r and I think I am just missing something very simple. At least I hope. I am trying to plot temperature change by site using aes in ggplot2. However, when I run the code no data is pulling up in the chart. This is the code I am using.
My data is called diss, x=order, and y=Tempdiff.
tempplot<-ggplot(data = diss,mapping =
aes(order,Tempdiff))+geom_segment(data = diss , mapping =
aes(x=order,y=Tempdiff, xend=order, yend=Tempdiff, color="red"))
plot(tempplot)
There are no errors when I run the code and the graph pulls up, it is just empty. Is there something in my code that I am unaware of that is removing the data or have I just missed a critical code in actually adding the data?
If I have left something out that is necessary for answering this question I am happy to add more. Thank you in advance!
Addition:
> dput(diss)
structure(list(Paper = c(1L, 2L, 3L, 4L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 10L), Specimen = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 2L,
2L, 4L, 4L, 4L), .Label = c("", "Ochotona_collaris", "Ochotona_curzoniae",
"Ochotona_principes"), class = "factor"), Site = structure(c(15L,
16L, 6L, 10L, 3L, 7L, 14L, 18L, 8L, 17L, 17L, 17L, 17L, 11L,
2L, 13L, 9L, 9L, 4L, 12L, 5L), .Label = c("", "Arapaho_Basin",
"Bodi ", "Bouldera_Pass", "Calf_Robe", "Columbia_River_Gorge",
"Craters_of_Moon", "Grand_Teton ", "Kluane_Lake", "Lava_Butte Flow_OR",
"Loveland_Pass", "Mad_Wolf", "Niwat_Ridge", "Rocky_Mt", "South_Couliee",
"Sugar_Hill_Modoc", "Tibet_China", "Yellowstone"), class = "factor"),
elevation = c(2565L, 2203L, 316L, 1200L, 2600L, 1700L, 3462L,
2424L, 2815L, 3846L, 3846L, 3846L, 3846L, 3654L, 3878L, 3744L,
2000L, 2000L, 2133L, 2026L, 2004L), yr1 = c(2012L, 1894L,
2011L, 2011L, 1972L, 2010L, 2010L, 2010L, 2010L, 2005L, 2006L,
2007L, 2008L, 1981L, 1981L, 1981L, 1999L, 2003L, 2007L, 2008L,
2008L), yr1pop = c(9L, 4L, 9L, 9L, 48L, 55L, 39L, 71L, 54L,
182L, 219L, 223L, 231L, 6L, 4L, 10L, 5L, 3L, 2L, 3L, 7L),
yr1temp = c(25, 30, 32.1, 20, 20, 27.1, 15.4, 21.2, 18, 11,
13, 14, 12.5, 24.7, 26.5, 28.4, 12, 17, 20, 20, 20), yr2 = c(2013L,
1910L, 2012L, 2012L, 2006L, 2011L, 2011L, 2011L, 2011L, 2006L,
2007L, 2008L, 2009L, 1983L, 1983L, 1983L, 2002L, 2006L, 2009L,
2009L, 2009L), yr2temp = c(26, 31.4, 33.2, 17.1, 24.76, 30.7,
18.2, 22, 20, 13, 14, 12.5, 13, 26.1, 27.3, 28.3, 15, 16,
20, 20, 10), yr2pop = c(9L, 2L, 9L, 7L, 4L, 40L, 20L, 37L,
37L, 219L, 223L, 231L, 214L, 5L, 4L, 8L, 4L, 2L, 2L, 7L,
2L), delta_temp = c(1, 1.4, 1.1, 2.9, 4.76, 3.6, 2.8, 0.8,
2, 2, 1, 1.5, 0.5, 1.4, 0.8, 0.1, 3, 1, 0, 0, 10), avg_temp = c(25.5,
30.7, 32.65, 18.55, 44.76, 28.9, 16.8, 21.6, 19, 12, 13.5,
13.25, 12.75, 25.4, 26.9, 28.35, 13, 16.5, 20, 20, 15), delta_pop = c(0L,
2L, 0L, 2L, 44L, 15L, 19L, 34L, 17L, 37L, 4L, 8L, 17L, 1L,
0L, 2L, 1L, 1L, 0L, 4L, 5L), avg_pop = c(9, 3, 9, 8, 26,
47.5, 29.5, 54, 45.5, 200.5, 221, 227, 222.5, 5.5, 4, 9,
4.5, 3.5, 2, 4, 4.5), SE_temp = c(0.499, 0.7, 0.6, 1.45,
2.381, 1.8, 1.4, 0.4, 1, 1.414, 0.707, 1.06, 0.353, 0.989,
0.565, 0.07, 2.121, 0.707, 0, 0, 7.07), CI_low_temp = c(25.025,
30.035, 32.03, 17.172, 20.119, 27.19, 15.47, 21.22, 18.05,
11.05, 13.025, 12.537, 12.512, 24.735, 26.52, 28.302, 12.075,
16.025, 20, 20, 10.25), CI_upper_temp = c(25.975, 31.365,
33.17, 19.927, 24.641, 30.61, 18.13, 21.98, 19.95, 12.95,
13.974, 13.962, 12.987, 26.065, 27.78, 28.397, 14.925, 16.975,
20, 20, 19.75), SE_pop = c(0, 1.414, 0, 1.414, 31.112, 10.606,
13.425, 24.041, 12.02, 26.162, 2.828, 5.656, 12.02, 0.707,
0, 1.414, 0.707, 0.707, 0, 2.828, 3.535), CI_pop_low = c(9,
2.05, 9, 7.05, 5.1, 40.375, 20.475, 37.85, 37.425, 182.925,
219.1, 223.2, 214.425, 5.025, 4, 8.05, 4.025, 2.025, 2, 3.1,
2.125), CI_pop_upper = c(9, 3.95, 9, 8.95, 46.9, 54.625,
38.525, 70.15, 53.575, 218.075, 222.9, 230.8, 230.575, 5.975,
4, 9.95, 4.975, 2.975, 2, 6.9, 6.875), Tempdiff = c(1, 1.4,
1.1, -2.9, 4.76, 3.6, 2.8, 0.800000000000001, 2, 2, 1, -1.5,
0.5, 1.4, 0.800000000000001, -0.0999999999999979, 3, -1,
0, 0, -10), popdiff = c(0L, -2L, 0L, -2L, -44L, -15L, -19L,
-34L, -17L, 37L, 4L, 8L, -17L, -1L, 0L, -2L, -1L, -1L, 0L,
4L, -5L), order = 1:21), .Names = c("Paper", "Specimen",
"Site", "elevation", "yr1", "yr1pop", "yr1temp", "yr2", "yr2temp",
"yr2pop", "delta_temp", "avg_temp", "delta_pop", "avg_pop", "SE_temp",
"CI_low_temp", "CI_upper_temp", "SE_pop", "CI_pop_low", "CI_pop_upper",
"Tempdiff", "popdiff", "order"), row.names = c(NA, 21L), class = "data.frame")

Related

How to get the confidence interval after sum in R?

I have a dataset, which is on an event basis, like below,
Young<- structure(list(Year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2012L), Month = c(10L, 10L,
10L, 12L, 12L, 12L, 3L, 3L, 3L, 8L, 8L, 8L, 9L, 9L, 9L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 8L, 8L, 8L), Day = c(19L, 19L, 19L, 20L, 20L, 20L,
28L, 28L, 28L, 16L, 16L, 16L, 19L, 19L, 19L, 24L, 24L, 24L, 9L,
9L, 9L, 20L, 20L, 20L, 7L, 7L, 7L, 12L, 12L, 12L, 28L, 28L, 28L,
25L, 25L, 25L, 9L, 9L, 9L), Hour = c("11:00:00", "12:00:00",
"12:00:00", "16:00:00", "16:00:00", "16:00:00", "15:00:00", "16:00:00",
"16:00:00", "13:00:00", "13:00:00", "14:00:00", "13:00:00", "13:00:00",
"14:00:00", "15:00:00", "15:00:00", "16:00:00", "15:00:00", "15:00:00",
"15:00:00", "16:00:00", "16:00:00", "17:00:00", "14:00:00", "15:00:00",
"15:00:00", "16:00:00", "16:00:00", "16:00:00", "15:00:00", "15:00:00",
"16:00:00", "15:00:00", "16:00:00", "16:00:00", "14:00:00", "15:00:00",
"15:00:00"), Treatment = c("Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control", "Control",
"Control", "Control", "Control", "Control", "Control"), Age = c("Young",
"Young", "Young", "Young", "Young", "Young", "Young", "Young",
"Young", "Young", "Young", "Young", "Young", "Young", "Young",
"Young", "Young", "Young", "Young", "Young", "Young", "Young",
"Young", "Young", "Young", "Young", "Young", "Young", "Young",
"Young", "Young", "Young", "Young", "Young", "Young", "Young",
"Young", "Young", "Young"), Plot = c(2L, 4L, 3L, 3L, 2L, 4L,
3L, 4L, 2L, 4L, 2L, 3L, 3L, 2L, 4L, 4L, 2L, 3L, 2L, 4L, 3L, 2L,
4L, 3L, 2L, 4L, 3L, 3L, 4L, 2L, 4L, 2L, 3L, 2L, 4L, 3L, 3L, 2L,
4L), CutUncut = c("Uncut", "Uncut", "Uncut", "Uncut", "Uncut",
"Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut",
"Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut",
"Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut",
"Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut",
"Uncut", "Uncut", "Uncut", "Uncut", "Uncut", "Uncut"), DOY = c(293L,
293L, 293L, 355L, 355L, 355L, 88L, 88L, 88L, 229L, 229L, 229L,
263L, 263L, 263L, 24L, 24L, 24L, 40L, 40L, 40L, 80L, 80L, 80L,
128L, 128L, 128L, 164L, 164L, 164L, 180L, 180L, 180L, 207L, 207L,
207L, 222L, 222L, 222L), Season = c("Autumn", "Autumn", "Autumn",
"Winter", "Winter", "Winter", "Spring", "Spring", "Spring", "Summer",
"Summer", "Summer", "Autumn", "Autumn", "Autumn", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Spring", "Spring", "Spring",
"Spring", "Spring", "Spring", "Summer", "Summer", "Summer", "Summer",
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer", "Summer",
"Summer"), ParNEE = c(290.7248731, 599.7403381, 620.7083338,
32.78885425, 62.01252568, 45.52391483, 1071.670139, 1093.367386,
800.6788483, 419.0408238, 412.778134, 1327.117535, 839.7914373,
644.0409088, 279.2245603, 191.8809568, 198.8807589, 140.3205729,
467.648241, 165.6642216, 96.16016992, 762.2635568, 540.4431615,
785.3492862, 1732.119114, 1129.006272, 1057.400471, 600.8070811,
494.626709, 716.4676621, 874.3964178, 766.3067725, 722.804594,
1841.223679, 1857.914923, 1851.643175, 1885.19335, 1874.080518,
1886.155761), TsoilNEE = c(8.20641798, 8.87167614, 9.25483531,
5.35562467, 5.001391598, 5.142751522, 5.447442626, 2.774806172,
5.73145326, 16.39260095, 16.63689439, 16.00032251, 13.80826586,
13.70293495, 14.05993565, -148.3114899, -125.1212398, -39.50172764,
-0.153912547, 0.250569622, -0.180588761, 7.026564873, 6.613643378,
6.750276902, 10.9441073, 9.815122309, 10.96556178, 13.72630399,
13.91668339, 14.75620705, 16.07427164, 17.02269846, 15.89705194,
17.48706912, 18.1624207, 17.27015855, 15.88739078, -313.4308453,
-200.2484216), TairNEE = c(11.84885994, 12.25500113, 13.33651023,
9.929997869, 9.802112232, 9.682167101, 22.46247535, 25.07250631,
25.27596815, 27.62653943, 29.58273819, 29.58725575, 24.72666853,
24.55716774, 20.72507946, 9.805187988, 9.747996729, 9.705873269,
4.765671568, 3.773874476, 3.891349753, 20.38603849, 20.30943844,
20.69971814, 22.20306747, 24.75430113, 24.77324059, 23.4390849,
23.41247396, 23.06330721, 35.67870836, 34.35199338, 33.98895943,
40.66750538, 37.41804298, 37.19870659, 29.66119092, 31.58723976,
30.10279262), FluxNEE = c(-3.206353246, -4.163998348, -9.420162768,
0.037262928, -1.336588509, -0.224057957, -0.508366229, -0.303823561,
0.451989541, -4.629461578, -4.128336306, -19.31431488, -17.41555463,
-10.76842377, -3.773743494, -4.005118396, -3.10139429, -3.896511575,
-0.230811062, -0.387848425, 0.106525664, -2.093040652, -2.140873245,
-3.342366374, -4.283554855, -3.90440069, -6.694529951, -10.79442231,
-7.57943893, -8.668064652, -9.796745843, -9.068490841, -11.23867962,
-2.881690108, -6.139469398, -11.55337646, -17.73289009, -11.81309017,
-10.19259206), ParER = c(0.079582144, 0.078538246, 0.076666822,
0.075928068, 0.071107485, 0.067917311, 0.079550601, 0.079545707,
0.076062537, 0.066680977, 0.044227931, 0.064819571, 0.084869874,
0.085468009, 0.073554044, 0.078895503, 0.070483716, 0.079795927,
0.071635126, 0.074950603, 0.085683638, 0.067656389, 0.061385558,
0.063857453, 0.071950763, 0.09015295, 0.077798096, 0.05698102,
0.075428953, 0.048109063, 0.06919603, 0.068823704, 0.082922817,
1843.888325, 1855.967312, 1856.078984, 1887.559762, 1877.441029,
1881.282226), TsoilER = c(8.212540539, 8.913773301, 9.37379959,
5.352583624, 5.003265737, 5.129246633, 3.585953441, 4.227443559,
6.270562446, 16.35403573, 16.89910109, 15.72318925, 13.51008097,
13.64622863, 14.01804184, -136.13081, -112.691455, -29.0108879,
-0.250624887, 0.234876446, -0.192253951, 7.242462111, 6.581003774,
7.304472456, 10.73760849, 9.970181595, 11.00729429, 13.62692044,
13.94769746, 14.73160985, 16.18365114, 16.87261231, 15.8735923,
17.35071047, 18.44087514, 16.87495921, 15.93538216, -217.3594646,
18.1232224), TairER = c(11.52528319, 12.64744211, 13.63468403,
9.899656846, 9.727256358, 9.639747971, 24.83383448, 25.18722803,
25.49742845, 27.23686553, 28.59409676, 29.95920761, 24.58571143,
24.69913034, 20.61829429, 9.669081306, 9.732249093, 9.544825508,
5.041718517, 3.73260756, 3.753481235, 20.24927008, 20.38445081,
21.03628444, 22.96717296, 23.93519561, 24.59680342, 23.274129,
23.18265753, 23.30700413, 35.65016436, 34.33754475, 33.67091287,
40.44720666, 37.56765173, 37.26631998, 29.50898978, 31.78674575,
30.593999), FluxER = c(1.057273055, 1.131077804, 1.286326278,
0.933058574, 1.162736073, 1.056358927, 2.210981831, 1.401988009,
1.500512866, 5.052906165, 4.628101935, 6.530351267, 3.258632563,
2.530428826, 2.847726462, 1.041944383, 0.591929214, 0.888592631,
0.085761062, 0.137706908, 0.124532256, 1.102969996, 1.210864425,
1.550348555, 1.981517329, 1.691527815, 3.072262372, 4.667099959,
3.568560761, 3.798178884, 6.782221267, 5.772727381, 7.887602279,
7.772539297, 7.873300514, 10.72683275, 8.24859913, 5.51910253,
5.920298815), Photosynth = c(-4.263626301, -5.295076152, -10.706489046,
-0.895795646, -2.499324582, -1.280416884, -2.71934806, -1.70581157,
-1.048523325, -9.682367743, -8.756438241, -25.844666147, -20.674187193,
-13.298852596, -6.621469956, -5.047062779, -3.693323504, -4.785104206,
-0.316572124, -0.525555333, -0.018006592, -3.196010648, -3.35173767,
-4.892714929, -6.265072184, -5.595928505, -9.766792323, -15.461522269,
-11.147999691, -12.466243536, -16.57896711, -14.841218222, -19.126281899,
-10.654229405, -14.012769912, -22.28020921, -25.98148922, -17.3321927,
-16.112890875), DayNumber = c(4705L, 4705L, 4705L, 4767L, 4767L,
4767L, 4500L, 4500L, 4500L, 4641L, 4641L, 4641L, 4675L, 4675L,
4675L, 4802L, 4802L, 4802L, 4818L, 4818L, 4818L, 4858L, 4858L,
4858L, 4906L, 4906L, 4906L, 4942L, 4942L, 4942L, 4958L, 4958L,
4958L, 4985L, 4985L, 4985L, 5000L, 5000L, 5000L), Date = c("2011-10-19",
"2011-10-19", "2011-10-19", "2011-12-20", "2011-12-20", "2011-12-20",
"2011-03-28", "2011-03-28", "2011-03-28", "2011-08-16", "2011-08-16",
"2011-08-16", "2011-09-19", "2011-09-19", "2011-09-19", "2012-01-24",
"2012-01-24", "2012-01-24", "2012-02-09", "2012-02-09", "2012-02-09",
"2012-03-20", "2012-03-20", "2012-03-20", "2012-05-07", "2012-05-07",
"2012-05-07", "2012-06-12", "2012-06-12", "2012-06-12", "2012-06-28",
"2012-06-28", "2012-06-28", "2012-07-25", "2012-07-25", "2012-07-25",
"2012-08-09", "2012-08-09", "2012-08-09"), Location = c(19L,
21L, 20L, 20L, 19L, 21L, 20L, 21L, 19L, 21L, 19L, 20L, 20L, 19L,
21L, 21L, 19L, 20L, 19L, 21L, 20L, 19L, 21L, 20L, 19L, 21L, 20L,
20L, 21L, 19L, 21L, 19L, 20L, 19L, 21L, 20L, 20L, 19L, 21L),
MossBiomass = c(0.81122449, 0.870408163, 0.532653061, 0.532653061,
0.81122449, 0.870408163, 0.532653061, 0.870408163, 0.81122449,
0.870408163, 0.81122449, 0.532653061, 0.532653061, 0.81122449,
0.870408163, 0.870408163, 0.81122449, 0.532653061, 0.81122449,
0.870408163, 0.532653061, 0.81122449, 0.870408163, 0.532653061,
0.81122449, 0.870408163, 0.532653061, 0.532653061, 0.870408163,
0.81122449, 0.870408163, 0.81122449, 0.532653061, 0.81122449,
0.870408163, 0.532653061, 0.532653061, 0.81122449, 0.870408163
), CallunaBiomass = c(0.730612245, 0.766326531, 0.774489796,
0.774489796, 0.730612245, 0.766326531, 0.774489796, 0.766326531,
0.730612245, 0.766326531, 0.730612245, 0.774489796, 0.774489796,
0.730612245, 0.766326531, 0.766326531, 0.730612245, 0.774489796,
0.730612245, 0.766326531, 0.774489796, 0.730612245, 0.766326531,
0.774489796, 0.730612245, 0.766326531, 0.774489796, 0.774489796,
0.766326531, 0.730612245, 0.766326531, 0.730612245, 0.774489796,
0.730612245, 0.766326531, 0.774489796, 0.774489796, 0.730612245,
0.766326531), TotalBiomass = c(1.541836735, 1.636734694,
1.307142857, 1.307142857, 1.541836735, 1.636734694, 1.307142857,
1.636734694, 1.541836735, 1.636734694, 1.541836735, 1.307142857,
1.307142857, 1.541836735, 1.636734694, 1.636734694, 1.541836735,
1.307142857, 1.541836735, 1.636734694, 1.307142857, 1.541836735,
1.636734694, 1.307142857, 1.541836735, 1.636734694, 1.307142857,
1.307142857, 1.636734694, 1.541836735, 1.636734694, 1.541836735,
1.307142857, 1.541836735, 1.636734694, 1.307142857, 1.307142857,
1.541836735, 1.636734694), Efflux = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), SM = c(0.284347997493598, 0.284347997493598,
0.284347997493598, 0.245463893974316, 0.245463893974316,
0.245463893974316, 0.222652267105158, 0.222652267105158,
0.222652267105158, 0.252836852714222, 0.252836852714222,
0.252836852714222, 0.278406340793348, 0.278406340793348,
0.278406340793348, 0.259937125926954, 0.259937125926954,
0.259937125926954, 0.232801146590399, 0.232801146590399,
0.232801146590399, 0.227096876335852, 0.227096876335852,
0.227096876335852, 0.229079210892252, 0.229079210892252,
0.229079210892252, 0.230726906942308, 0.230726906942308,
0.230726906942308, 0.232011241533852, 0.232011241533852,
0.232011241533852, 0.220105334847203, 0.220105334847203,
0.220105334847203, 0.236470079582821, 0.236470079582821,
0.236470079582821), SoilTemp = c(9.950000286, 10.04999971,
10.04999971, 4.700000048, 4.700000048, 4.700000048, NA, NA,
NA, 15.75, 15.75, 16.10000038, 13.3499999, 13.3499999, 13.54999971,
4.200000048, 4.200000048, 4.299999952, 0.100000001, 0.100000001,
0.100000001, 6.700000048, 6.700000048, 6.75, 9.950000286,
10.25, 10.25, 14.19999981, 14.19999981, 14.19999981, 16.44999981,
16.44999981, 16.65000057, 17.19999981, 17.5, 17.5, 16.64999962,
16.80000019, 16.80000019), RelHumid = c(0.88, 0.84, 0.84,
0.86, 0.86, 0.86, 0.68, 0.68, 0.68, 0.6, 0.6, 0.54, 0.76,
0.76, 0.75, 0.93, 0.93, 0.94, 0.87, 0.87, 0.87, 0.62, 0.62,
0.64, 0.45, 0.43, 0.43, 0.74, 0.74, 0.74, 0.63, 0.63, 0.64,
0.43, 0.46, 0.46, 0.58, 0.56, 0.56), AirTemp = c(8.9, 10.4,
10.4, 6.6, 6.6, 6.6, 9.4, 9.7, 9.7, 20.9, 20.9, 21.2, 16.1,
16.1, 16.2, 4.9, 4.9, 4.8, 0.1, 0.1, 0.1, 13.4, 13.4, 12.9,
14, 14.7, 14.7, 17.2, 17.2, 17.2, 26.8, 26.8, 26.9, 30.3,
29.8, 29.8, 21.6, 21.6, 21.6), Solar = c(166.6, 272.8, 272.8,
12.8, 12.8, 12.8, 221.6, 258.2, 258.2, 578.6, 578.6, 525.5,
364.4, 364.4, 309.4, 111.7, 111.7, 73.2, 144.7, 144.7, 144.7,
368, 368, 227.1, 767.2, 651.9, 651.9, 375.4, 375.4, 375.4,
446.8, 446.8, 391.8, 796.5, 730.6, 730.6, 269.2, 787.4, 787.4
), PAR = c(300.3, 503.5, 503.5, 14.6, 14.6, 14.6, 419.3,
479.7, 479.7, 1100.5, 1100.5, 996.1, 703.1, 703.1, 607.9,
194.1, 194.1, 120.8, 263.7, 263.7, 263.7, 714.1, 714.1, 428.5,
1433.7, 1192, 1192, 705, 705, 705, 869.8, 869.8, 750.7, 1536.3,
1411.7, 1411.7, 540.2, 1534.4, 1534.4)), row.names = c(11L,
12L, 13L, 61L, 62L, 64L, 94L, 95L, 96L, 140L, 141L, 143L, 165L,
166L, 168L, 206L, 208L, 210L, 227L, 228L, 231L, 254L, 256L, 258L,
273L, 279L, 281L, 301L, 302L, 303L, 324L, 325L, 331L, 341L, 348L,
349L, 365L, 372L, 373L), class = "data.frame")
Then I use these data to fit a model
GPPyoung = nls(-Photosynth ~ (ParNEE*(a*SoilTemp))/(ParNEE+Pk),
data = Young,
start=list(a = 0.1,Pk=100), lower=0.0001, algorithm='port')
AIC(GPPyoung) # 201.99
coefficients(GPPyoung) #
summary(GPPyoung)#
confint(GPPyoung) # to get the 95% confident intervals for the perameters.
# Compare the observed vs predicted
Pk = coefficients(GPPyoung)['Pk']
a = coefficients(GPPyoung)['a']
Based on these data, I got the results of parameters Pk and a. Then I use this model to fit another dataset Meteo, which is an hourly dataset, to get the hourly results of the Photosynth. Finally, I used the sum() function to get the annual value of Photosynth.
But now my question is how to calculate the confidence interval or the uncertainty of the annual value of Photosynth. Becuase it's just a sum value not an average value at all. But we really want to know the uncertainty of the whole model.
Meteo$GPPyoung <- (Meteo$PAR*(a*Meteo$SoilTemp))/(Meteo$PAR+Pk)
Meteo<- structure(list(Date = c("1/06/2010 0:00", "1/06/2010 1:00", "1/06/2010 2:00",
"1/06/2010 3:00", "1/06/2010 4:00", "1/06/2010 5:00", "1/06/2010 6:00",
"1/06/2010 7:00", "1/06/2010 8:00", "1/06/2010 9:00", "1/06/2010 10:00",
"1/06/2010 11:00", "1/06/2010 12:00", "1/06/2010 13:00", "1/06/2010 14:00",
"1/06/2010 15:00", "1/06/2010 16:00", "1/06/2010 17:00", "1/06/2010 18:00",
"1/06/2010 19:00", "1/06/2010 20:00", "1/06/2010 21:00", "1/06/2010 22:00",
"1/06/2010 23:00", "2/06/2010 0:00", "2/06/2010 1:00", "2/06/2010 2:00",
"2/06/2010 3:00", "2/06/2010 4:00", "2/06/2010 5:00", "2/06/2010 6:00",
"2/06/2010 7:00", "2/06/2010 8:00", "2/06/2010 9:00", "2/06/2010 10:00",
"2/06/2010 11:00", "2/06/2010 12:00", "2/06/2010 13:00", "2/06/2010 14:00",
"2/06/2010 15:00", "2/06/2010 16:00", "2/06/2010 17:00", "2/06/2010 18:00",
"2/06/2010 19:00", "2/06/2010 20:00", "2/06/2010 21:00", "2/06/2010 22:00",
"2/06/2010 23:00", "3/06/2010 0:00", "3/06/2010 1:00"), Year = c(2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L), Month = c(6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), Day = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L), Hour = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L), RelHumid = c(0.95,
0.96, 0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.98, 0.95, 0.82, 0.76,
0.7, 0.67, 0.62, 0.63, 0.59, 0.64, 0.65, 0.69, 0.73, 0.78, 0.84,
0.91, 0.94, 0.96, 0.96, 0.97, 0.97, 0.97, 0.97, 0.98, 0.93, 0.73,
0.65, 0.6, 0.52, 0.45, 0.42, 0.41, 0.42, 0.44, 0.46, 0.45, 0.43,
0.49, 0.62, 0.78, 0.9, 0.94), AirTemp = c(6.7, 6.2, 5.3, 4.2,
4, 3.4, 3.9, 5.7, 9.7, 11.6, 14, 15.2, 17.2, 18.2, 19.6, 19,
19.7, 19.1, 18.6, 17.6, 16.6, 15.9, 14.3, 13.3, 11.2, 9.2, 6.9,
5.7, 4.9, 4.1, 3.6, 6.9, 11.5, 14.7, 16.4, 18.3, 20.1, 21.4,
22.1, 22.8, 22.8, 22.8, 22.3, 21.6, 20, 17.9, 14.9, 10.6, 7,
5.8), Solar = c(1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 12.8, 86.1, 212.4,
254.5, 375.4, 368, 629.9, 640.9, 705, 448.6, 465.1, 373.5, 234.4,
166.6, 102.5, 53.1, 12.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8,
14.6, 113.5, 238, 395.5, 529.2, 679.3, 792.8, 862.4, 884.4, 851.4,
785.5, 699.5, 554.8, 390, 227.1, 87.9, 12.8, 1.8, 1.8, 1.8),
PAR = c(0, 0, 0, 0, 0, 0, 14.6, 155.6, 391.8, 485.2, 734.3,
721.4, 1259.8, 1292.7, 1422.7, 869.8, 917.4, 734.3, 454.1,
311.3, 184.9, 86.1, 12.8, 0, 0, 0, 0, 0, 0, 0, 18.3, 197.8,
459.6, 791, 1073, 1355, 1618.7, 1759.6, 1779.8, 1730.3, 1571,
1386.1, 1080.3, 754.4, 448.6, 168.5, 22, 0, 0, 0), SoilTemp = c(10.23626137,
9.870399475, 9.595145226, 9.342157364, 9.180820465, 9.042314529,
9.019209862, 9.180820465, 9.549196243, 9.984869003, 10.41872025,
10.87355137, 11.3039856, 11.91307545, 12.81039906, 13.16781235,
13.3907938, 13.52444077, 13.25704098, 12.87747669, 12.56419373,
12.25024033, 11.9355793, 11.62017345, 11.23612499, 10.76001549,
10.28190517, 9.893303871, 9.61811161, 9.36518383, 9.203886032,
9.226944923, 9.572173119, 9.961985588, 10.32752895, 10.69184017,
11.19086361, 12.13794422, 13.3907938, 14.83355808, 16.04715157,
16.37726593, 15.95906639, 14.83355808, 13.90255451, 12.98920727,
12.3400116, 11.62017345, 10.94161892, 10.44150639), Hour1 = c("0:00:00",
"1:00:00", "2:00:00", "3:00:00", "4:00:00", "5:00:00", "6:00:00",
"7:00:00", "8:00:00", "9:00:00", "10:00:00", "11:00:00",
"12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00",
"17:00:00", "18:00:00", "19:00:00", "20:00:00", "21:00:00",
"22:00:00", "23:00:00", "0:00:00", "1:00:00", "2:00:00",
"3:00:00", "4:00:00", "5:00:00", "6:00:00", "7:00:00", "8:00:00",
"9:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00",
"14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00",
"19:00:00", "20:00:00", "21:00:00", "22:00:00", "23:00:00",
"0:00:00", "1:00:00"), DayNumber = c(4200, 4200, 4200, 4200,
4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200,
4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200, 4200,
4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201,
4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201, 4201,
4201, 4201, 4201, 4201, 4202, 4202), Measurement.Time = structure(list(
sec = c(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), min = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L), mday = 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, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L), mon = c(5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L), year = c(110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L),
wday = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L), yday = c(151L,
151L, 151L, 151L, 151L, 151L, 151L, 151L, 151L, 151L,
151L, 151L, 151L, 151L, 151L, 151L, 151L, 151L, 151L,
151L, 151L, 151L, 151L, 151L, 152L, 152L, 152L, 152L,
152L, 152L, 152L, 152L, 152L, 152L, 152L, 152L, 152L,
152L, 152L, 152L, 152L, 152L, 152L, 152L, 152L, 152L,
152L, 152L, 153L, 153L), isdst = 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), zone = c("CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST", "CEST", "CEST",
"CEST", "CEST", "CEST", "CEST"), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), class = c("POSIXlt",
"POSIXt")), GPPyoung = c(0, 0, 0, 0, 0, 0, 0.590594499464422,
4.25789413130195, 7.13964983293584, 8.09364257311046, 9.59430060918616,
9.96591619422887, 11.703172902607, 12.3885249598014, 13.5318467495935,
12.6479996031782, 13.0198166013451, 12.4542695460619, 10.4923575609493,
8.71893654495587, 6.4730880637325, 3.76653362912719, 0.689650416319457,
0, 0, 0, 0, 0, 0, 0, 0.74554835122207, 4.94356227894899,
7.60948634247972, 9.35134022285773, 10.3784721078367, 11.2061336432827,
12.0500200644512, 13.2185959001941, 14.6046656499378, 16.1185068720173,
17.205706913021, 17.2282182581709, 16.0592648798236, 13.7569296564082,
10.9537918957097, 6.32934387849538, 1.1861821961113, 0, 0,
0)), row.names = 745:794, class = "data.frame")
Young <- sum((Meteo$GPPyoung* 60 * 60 * 12 * (1/1000000)), na.rm=TRUE)
My idea is: the parameters Pk and a follow the normal distribution, so maybe I can use a loop function for the model fit with a range of Pk and a and then got a range of results, which then I could choose the confidence intervals based on the results. Finally, sum up them to get the annual confidence interval?
I am not sure about this idea.
I'm adding this as a partial solution with some caveats. First is that I'm assuming your initial model is correct, that is the observations can be treated as independent of each other given the covariates and that the shape of the curve is appropriate. If I was doing this for real I would think very hard about the resampling, that is whether I need to sample observations in clusters corresponding to the experimental design.
Second, the boostrapping doesn't always work, the model fails to converge for sometimes so you'll need find a way around this if you want to run a reasonable number of boostrap replicates.
You can use bootstrapping to estimate the confidence interval for the sum you requested as follows. I'm using predict to get the fitted values for the new data, and so I need to make the Meteo dataframe have predictor names that correspond to the names in the model. Then I use boot to estimate the confidence interval.
When I ran the bootstrap just to get the CIs for the model coefficients they were very close to the CIs reported by running confint on the original model so it seems to work OK. That also suggests it might be OK to resample the coefficients from the joint distribution of the estimates (you can get the covariance from vcov(GPPyoung) so maybe the boostrapping isn't the best way!
Meteo$ParNEE <-Meteo$PAR
getEstimates <- function(dat, w){
GPPyoung = nls(-Photosynth ~ (ParNEE*(a*SoilTemp))/(ParNEE+Pk),
data = dat[w,],
start=list(a = 0.1,Pk=100), lower=0.0001, algorithm='port')
sum((predict(GPPyoung, newdata = Meteo)* 60 * 60 * 12 * (1/1000000)), na.rm=TRUE)
}
library(boot)
b1 <- boot(Young, getEstimates, 100)
b1
boot.ci(b1)
> b1
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = Young, statistic = getEstimates, R = 100)
Bootstrap Statistics :
original bias std. error
t1* 14.19008 0.1035427 0.9388517
> boot.ci(b1)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 100 bootstrap replicates
CALL :
boot.ci(boot.out = b1)
Intervals :
Level Normal Basic
95% (12.25, 15.93 ) (11.71, 15.98 )
Level Percentile BCa
95% (12.40, 16.67 ) (12.38, 16.46 )
Calculations and Intervals on Original Scale
Some basic intervals may be unstable
Some percentile intervals may be unstable
Some BCa intervals may be unstable

How to omit NA in a subset of variables but where other variables are still present?

there are 8 tests and numerous demographic variables. I want to omit those who dont have complete test data, and compare demographics with the original dataset to see if there is selection bias. which I will do with chi-square.
I already tried na.omit and only ended up with a new data frame of the 8 variables.
dput(head(df))
structure(list(ï..leerlingnr2013 = 10048001:10048006, schoolnr = c(1004L,
1004L, 1004L, 1004L, 1004L, 1004L), toets_ws = c(78, 91, 75,
98, 79, 92), toets_dmt = c(103, 97, 112, 98, 71, 112), toets_bl = c(35,
57, 55, 63, 15, 46), toets_rw = c(109, 100, 115, 113, 92, 99),
citotaal = c(72L, 81L, 81L, 82L, 61L, 85L), citorekwisk = c(50L,
49L, 49L, 42L, 40L, 46L), citostudiev = c(31L, 36L, 35L,
34L, 31L, 34L), citowereld = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), gebmaand = c(6L,
6L, 3L, 6L, 7L, 1L), gebjaar = c(2001L, 2002L, 2002L, 2001L,
2001L, 2002L), geslacht = c(1L, 2L, 2L, 2L, 1L, 1L), oplei_vader = c(3L,
3L, 3L, NA, 2L, NA), oplei_moeder = c(1L, 2L, 1L, 3L, 2L,
2L), CoolSES = c(3L, 3L, 3L, 4L, 2L, 2L), zorgleerling = c(0L,
0L, 0L, 0L, 1L, 0L), welblk = c(3.71428571428571, 3.71428571428571,
4.28571428571429, 3.71428571428571, 3.71428571428571, 3.42857142857143
), welbmll = c(3.66666666666667, 3.66666666666667, 3.83333333333333,
2.83333333333333, 2.66666666666667, 4.16666666666667), zelfvertr = c(4.16666666666667,
2.16666666666667, 3.66666666666667, 4.16666666666667, 3,
3.66666666666667), taak = c(3.8, 3.8, 4.6, 4.6, 4.2, 3.4),
bekwaming = c(3.77777777777778, 3.44444444444444, 4.11111111111111,
4.66666666666667, 3, 3.33333333333333), extrinsiek = c(3,
2.66666666666667, 3.66666666666667, 3.44444444444444, 2.11111111111111,
3.33333333333333), prestatie = c(2.57142857142857, 3.85714285714286,
3.28571428571429, 1.57142857142857, 1.71428571428571, 2.28571428571429
), sociaal = c(3.57142857142857, 2.57142857142857, 3.42857142857143,
3.57142857142857, 3.28571428571429, 3.28571428571429)), row.names = c(NA,
6L), class = "data.frame")

R 3d plot error with intersecting planes

This has stumped me all day long. Can anyone help me to understand why my data is providing me these plots, which contain intersecting planes alongside of my 3D-scatterplot? The planes occur using all 3 of the methods below.
There may be an issue in my use of unlist of the matrix that created the attached dataset.
Link
Source Data
open3d()
bg3d(color="white")
points3d(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count)
plot3d(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count)
rgl.open()
rgl.bg(color="white")
rgl.points(freq_subdata$x_ft,freq_subdata$y_ft,freq_subdata$count,color="black")
> dput(head(freq_subdata,20))
structure(list(x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), y = 1:20, x_ft = c(2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5), y_ft = c(2.5, 7.5, 12.5, 17.5,
22.5, 27.5, 32.5, 37.5, 42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5,
77.5, 82.5, 87.5, 92.5, 97.5), count = c(2L, 4L, 6L, 8L, 10L,
12L, 14L, 16L, 18L, 20L, 22L, 24L, 26L, 28L, 30L, 32L, 34L, 36L,
38L, 40L), proportion = c(6.18888476296571e-05, 0.000123777695259314,
0.000185666542888971, 0.000247555390518629, 0.000309444238148286,
0.000371333085777943, 0.0004332219334076, 0.000495110781037257,
0.000556999628666914, 0.000618888476296571, 0.000680777323926229,
0.000742666171555886, 0.000804555019185543, 0.0008664438668152,
0.000928332714444857, 0.000990221562074514, 0.00105211040970417,
0.00111399925733383, 0.00117588810496349, 0.00123777695259314
)), .Names = c("x", "y", "x_ft", "y_ft", "count", "proportion"
), row.names = c("Var11", "Var12", "Var13", "Var14", "Var15",
"Var16", "Var17", "Var18", "Var19", "Var110", "Var111", "Var112",
"Var113", "Var114", "Var115", "Var116", "Var117", "Var118", "Var119",
"Var120"), class = "data.frame")
Link
Source Data

Error: (maxstephalfit) PIRLS step-halvings failed to reduce deviance in pwrssUpdate when trying to run binomial GLMER with user-defined link function

Relatively new R user here, trying to run a GLMER for avian nest success that incorporates exposure days with a binomial response variable (success = 1, failure = 0). I am using Ben Bolker's code for a user-defined link function, obtained here.
Here is the full code:
NestSuccessExposure<-read.csv("NestSuccessExposure.csv")
NestSuccessExposure<-na.omit(NestSuccessExposure)
NestSuccessExposureScaled<-scale(NestSuccessExposure[,c(3:10)])
NestSuccessExposureScaled<-cbind(NestSuccessExposureScaled,NestSuccessExposure[,c(1,2,11,12,13)])
library(MASS)
library(lme4)
logexp <- function(exposure = 1)
{
linkfun <- function(mu) qlogis(mu^(1/exposure))
linkinv <- function(eta) plogis(eta)^exposure
logit_mu_eta <- function(eta) {
ifelse(abs(eta)>30,.Machine$double.eps,
exp(eta)/(1+exp(eta))^2)
}
mu.eta <- function(eta) {
exposure * plogis(eta)^(exposure-1) *
logit_mu_eta(eta)
}
valideta <- function(eta) TRUE
link <- paste("logexp(", deparse(substitute(exposure)), ")",
sep="")
structure(list(linkfun = linkfun, linkinv = linkinv,
mu.eta = mu.eta, valideta = valideta,
name = link),
class = "link-glm")
}
#Run GLMER (linear mixed-effects model)
NSExposure<-glmer(SUCCESS~SLOPE+BEERS+TALL+CANOPY+DISTROAD+DISTSTREAM+NESTHT+
QUAL+VINENT+MGMT+(1|YEAR),
family=binomial(link=logexp(NestSuccessExposureScaled$EXPOSURE)),
data=NestSuccessExposureScaled)
summary(NSExposure)
I have continuous, binomial, and categorical predictor variables and I re-scaled the continuous variables (including exposure days) first.
The problem is that I keep getting the error messages:
"Error: (maxstephalfit) PIRLS step-halvings failed to reduce deviance in pwrssUpdate. In addition: Warning message:
In qlogis(mu^(1/exposure)) : NaNs produced"
I've found that NaN means "not a number" but am unsure why this is occurring. Any help is greatly appreciated!
Below is my nest success dataset:
dput(NestSuccessExposure)
structure(list(SUCCESS = c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L,
0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 1L, 0L), YEAR = c(2011L, 2011L, 2011L, 2011L,
2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 2012L,
2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L), EXPOSURE = c(17, 17,
9, 9, 10, 2, 9, 9, 7, 1, 9, 7, 15, 3, 17, 8, 8, 1.5, 24, 6, 23,
2, 22, 5.5, 2.5, 17, 16, 1.5, 0.5, 7.5, 21, 1.5, 17, 3.5, 1,
0.5, 4, 1, 17, 5, 28, 22, 12, 5, 6, 2, 14, 21, 22, 9, 15, 1,
11, 15, 0, 14, 9, 6, 4, 14, 23, 3, 2, 4.5), SLOPE = c(12, 35.5,
48.75, 18, 31, 55, 27.5, 31, 31.25, 24.75, 43.75, 36, 18.75,
17, 19.75, 45, 25, 42, 17.5, 29.5, 39, 26, 27, 39.5, 47.5, 44.5,
51.5, 51, 48.5, 6, 45.25, 33.75, 36, 19, 22.75, 57.25, 30.5,
54, 30.75, 34, 38, 20, 27, 48, 10.25, 5.25, 38.5, 39, 23, 12.75,
48, 51, 35.5, 30.5, 24, 53.5, 67.5, 4, 68.5, 73, 54.5, 18.5,
41.5, 7), BEERS = c(1.97, 1.71, 1.42, 0.44, 1.99, 1.8, 1.09,
1.99, 1.92, 0.05, 1.39, 1.78, 1.71, 1.71, 1.17, 1.66, 1.71, 1.98,
2, 0.12, 1.85, 1.99, 0.08, 0.67, 1.82, 1.91, 1.97, 1.97, 1.97,
0.01, 1.57, 0.03, 0.05, 0.74, 2, 1.95, 1.39, 1.71, 1.85, 0.64,
0.71, 1.97, 0.07, 1.98, 0.03, 0.46, 1.97, 1.07, 1.17, 1.84, 2,
1.96, 1.97, 1.57, 0.91, 2, 1.98, 0.84, 1.98, 1.91, 1.97, 1.91,
1.84, 1.6), TALL = c(23.85, 28.9, 22.925, 25, 14.7, 24.6, 17.3,
24.05, 18.675, 25.6, 20.15, 32.75, 28.5, 20.075, 21.35, 23.425,
27.2, 25.025, 21.9, 20.85, 22.75, 23.35, 29.05, 21.125, 29.65,
27.575, 27.175, 27.95, 29.35, 23.225, 26.35, 27.6, 23.2, 22.05,
22.45, 32, 26.267, 23.9, 21.6, 23.55, 23.9, 26.93, 14.98, 23.55,
24.8, 17.6, 31.38, 28.05, 22.95, 24.8, 27.2, 31.83, 30.48, 21.63,
18.3, 26.33, 23, 24.95, 14.75, 21.35, 25.55, 23.2, 26.05, 20.45
), CANOPY = c(0.9, 0.85, 0.9, 0.95, 0.95, 0.95, 1, 0.85, 0.7,
0.95, 0.95, 0.8, 0.9, 0.9, 1, 1, 0.95, 0.9, 0.85, 0.8, 0.6, 0.85,
0.85, 1, 1, 0.95, 0.95, 0.75, 0.9, 0.75, 0.95, 0.9, 0.85, 0.95,
0.9, 0.85, 0.9, 0.95, 0.85, 0.85, 1, 0.85, 0.9, 0.95, 0.4, 0.75,
0.8, 0.95, 0.85, 0.9, 0.9, 0.8, 0.75, 0.85, 0.8, 0.95, 0.75,
0.9, 0.85, 0.65, 0.8, 0.85, 0.85, 0.85), DISTROAD = c(6.81, 19.02,
158.83, 7.56, 70.87, 263.68, 31.28, 39.32, 181.36, 246.67, 48.58,
38.63, 75.47, 4.51, 80.56, 362.92, 82.1, 197.36, 361.38, 82.29,
59.05, 31.32, 81.46, 179.79, 211.74, 238.64, 270.93, 318.72,
329.96, 14.53, 158.3, 104.38, 94.61, 293.89, 99.84, 178.64, 56.28,
204.82, 425.32, 4.02, 119.75, 8.31, 1.17, 63.24, 4.62, 119.46,
65.45, 121.6, 4.38, 17.36, 205.12, 243.77, 349.98, 3.98, 60.29,
209.79, 247.05, 114.51, 100.86, 331.62, 306.9, 0.95, 27.26, 34.58
), DISTSTREAM = c(348.37742, 233.394296, 149.503103, 181.305039,
138.067932, 13.087182, 58.590507, 288.128836, 149.061785, 126.667503,
220.6535, 194.269426, 115.265352, 275.771326, 78.528016, 118.476224,
174.095054, 44.340495, 82.174798, 62.745934, 227.662779, 55.671038,
200.910084, 34.939781, 96.984957, 42.842466, 25.45655, 72.374004,
32.353038, 134.254137, 184.571521, 58.354152, 78.176574, 22.294154,
137.078915, 51.516704, 244.946159, 16.681571, 62.556975, 80.092302,
84.607328, 336.424256, 23.248202, 206.251649, 279.365454, 14.091524,
198.226118, 534.630654, 102.796308, 217.190835, 15.414713, 43.516136,
42.080907, 67.19459, 417.021499, 64.101315, 14.218346, 1.932141,
34.491616, 38.305397, 20.481007, 411.768426, 404.101887, 8.842676
), NESTHT = c(12.6, 26.6, 18, 10, 14, 18, 15.5, 23, 17, 21.5,
26, 14.5, 18, 29.5, 12.5, 16, 16.5, 24, 20, 14.2, 16.8, 20.2,
13.4, 20.4, 25.8, 19.6, 18, 18.4, 15.2, 24, 19.8, 16.8, 20.4,
20, 15, 18, 24, 17.2, 13.4, 23, 16.8, 9, 20, 26.8, 22.2, 13,
26.4, 14.6, 11.4, 15, 20.6, 20, 14, 24.5, 21.8, 18.8, 11.2, 21.5,
12.4, 19.4, 17.2, 15.6, 13, 21), QUAL = c(0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,
0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L), VINENT = c(0L, 0L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), MGMT = structure(c(2L,
2L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L,
2L, 1L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 3L,
2L, 3L, 2L, 3L, 3L, 1L, 3L, 1L, 2L, 2L, 2L, 1L, 3L, 2L, 1L, 3L,
1L, 3L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L), .Label = c("C",
"E", "U"), class = "factor")), .Names = c("SUCCESS", "YEAR",
"EXPOSURE", "SLOPE", "BEERS", "TALL", "CANOPY", "DISTROAD", "DISTSTREAM",
"NESTHT", "QUAL", "VINENT", "MGMT"), row.names = c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 15L, 16L, 17L, 19L, 20L, 21L,
23L, 24L, 26L, 27L, 28L, 30L, 31L, 33L, 34L, 35L, 36L, 37L, 39L,
40L, 41L, 42L, 43L, 44L, 45L, 47L, 48L, 52L, 53L, 54L, 55L, 57L,
58L, 59L, 60L, 61L, 62L, 63L, 65L, 67L, 68L, 69L, 70L, 72L, 73L,
74L, 77L, 78L, 80L, 81L, 82L, 88L, 90L), class = "data.frame", na.action = structure(c(12L,
13L, 14L, 18L, 22L, 25L, 29L, 32L, 38L, 46L, 49L, 50L, 51L, 56L,
64L, 66L, 71L, 75L, 76L, 79L, 83L, 84L, 85L, 86L, 87L, 89L), .Names = c("12",
"13", "14", "18", "22", "25", "29", "32", "38", "46", "49", "50",
"51", "56", "64", "66", "71", "75", "76", "79", "83", "84", "85",
"86", "87", "89"), class = "omit"))
tl;dr you can't have non-positive exposure values in a power-logit link function. I figured this out partly by thinking about it, partly by putting strategic print() statements in the link functions to figure out what was going on. So:
you shouldn't be scaling the EXPOSURE variable
even after not-scaling, there's still one observation (row 55) with a zero exposure value - I omitted it (I guess you could also set it to a small non-zero value)
after this is adjusted, the model seems to run OK
you are probably trying to fit too complex a model to this data set - you have 35 failures and 28 successes -> an effective sample size of 28 (sensu Harrell's Regression Modeling Strategies book) -> you probably can't reliably fit more than at most 3 parameters
NestSuccessExposure <- na.omit(NestSuccessExposure)
noscaleVars <- c("SUCCESS","YEAR","EXPOSURE","QUAL","VINENT","MGMT")
noscaleCols <- which(names(NestSuccessExposure) %in% noscaleVars)
NestSuccessExposureScaled<- NestSuccessExposure
NestSuccessExposureScaled[-noscaleCols] <-
scale(NestSuccessExposure[-noscaleCols])
NestSuccessExposureScaled <- subset(NestSuccessExposureScaled,
EXPOSURE>0)
... after this the model seems to fit OK ... (although the among-year variation gets reduced to nearly zero in the fit.
You're also near the lower practical limit for the number of levels of a random-effects grouping factor).

batch plots with ggplot2

I have a problem with a "serial" plotting with ggplot2. This is my df:
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L), .Label = c("P1", "P21", "P24", "P25"
), class = "factor"), Date = structure(c(21L, 22L, 24L, 25L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 1L, 2L, 3L, 4L, 6L, 7L, 8L,
9L, 10L, 12L, 22L, 23L, 26L, 27L, 29L, 30L, 31L, 32L, 1L, 2L,
3L, 4L, 5L, 7L, 9L, 8L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L), .Label = c("1996-05-30", "1996-12-06", "1997-03-18", "1997-06-27",
"1997-09-29", "1997-09-30", "1997-11-24", "1998-03-13", "1998-05-07",
"1998-07-09", "1998-07-14", "1998-10-26", "1998-10-30", "1999-03-15",
"1999-06-16", "1999-09-06", "1999-11-10", "2000-03-15", "2000-06-21",
"2000-11-28", "2007-09-04", "2007-10-16", "2008-05-21", "2008-05-22",
"2008-08-06", "2008-08-12", "2008-10-16", "2008-10-17", "2009-07-01",
"2009-10-14", "2010-07-02", "2010-09-29", "2011-06-09", "2011-08-23"
), class = "factor"), T = c(11.1, 11.1, 10.9, 10.9, 10.6, 11,
10.5, 11.2, 10.9, 10.7, 11.1, 10.9, 10.2, 10.2, 10.9, 10.2, 9.9,
10, 10.3, 10.5, 10.1, 11.1, 11.1, 11.1, 10.9, 11.1, 10.7, 11.3,
11, 11.4, 10.2, 10.2, 10.7, 10.3, 9.9, 10.2, 10.1, 10.2, 10.2,
10.2, 10.6, 10.7, 10.2, 10.3, 11, 10.6), ph = c(6.76, 6.72, 6.9,
6.91, 6.96, 6.98, 6.94, 7.02, 7, 6.92, 6.94, 6.5, 6.4, 6.7, 6.52,
6.6, 6.6, 6.5, 6.55, 6.51, 6.59, 6.72, 6.76, 6.82, 6.8, 6.76,
6.76, 6.88, 6.82, 6.7, 6.7, 6.9, 6.71, 6.9, 6.8, 6.7, 6.69, 6.79,
6.69, 6.68, 6.5, 6.67, 6.65, 6.73, 6.78, 6.68), EC = c(1499L,
2120L, 881L, 902L, 870L, 541L, 891L, 876L, 860L, 868L, 877L,
3630L, 3400L, 2470L, 2330L, 1810L, 2190L, 2810L, 2200L, 2440L,
1111L, 2120L, 1654L, 1746L, 1781L, 761L, 1627L, 1733L, 1633L,
2440L, 3130L, 3180L, 2530L, 2710L, 2450L, 2630L, 3610L, 2190L,
973L, 3650L, 3060L, 3280L, 2930L, 879L, 3040L, 3030L), Month = structure(c(9L,
8L, 6L, 1L, 8L, 3L, 8L, 3L, 9L, 4L, 1L, 6L, 2L, 5L, 4L, 9L, 7L,
5L, 6L, 3L, 8L, 8L, 6L, 1L, 8L, 3L, 8L, 3L, 9L, 6L, 2L, 5L, 4L,
9L, 7L, 6L, 5L, 3L, 8L, 5L, 4L, 9L, 7L, 5L, 4L, 7L), .Label = c("August",
"December", "July", "June", "March", "May", "November", "October",
"September"), class = "factor"), Year = c(2007L, 2007L, 2008L,
2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 1996L,
1996L, 1997L, 1997L, 1997L, 1997L, 1998L, 1998L, 1998L, 1998L,
2007L, 2008L, 2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 1996L,
1996L, 1997L, 1997L, 1997L, 1997L, 1998L, 1998L, 1998L, 1998L,
1999L, 1999L, 1999L, 1999L, 2000L, 2000L, 2000L)), .Names = c("ID",
"Date", "T", "ph", "EC", "Month", "Year"), class = "data.frame", row.names = c(NA,
-46L))
What I want to do is a function that plot for every ID, EC value versus Month and grouping them in Years. First I changed the Month column in an ordered factor:
df$Month<-factor(df$Month, levels=month.name, ordered=T)
Then I tried to do that with this code:
by (df,df$ID,function(i){
ggplot(df) +
geom_point(aes(i$Month, i$EC, group=i$Year))
})
But I receive an error. What is weird is that the same code works for lattice:
by (df,df$ID,function(i){
xyplot(i$EC~i$Month, data=df, group=i$Year, main=list(unique(i$ID)),
xlab="Months", ylab="EC",type=c('p','l','g'),
auto.key=list(columns=3,lines=TRUE))
})
I really don't know where I'm missing with ggplot. Any suggestions?
You should put i inside ggplot() call because this is name for subsetted data frame now. And you don't need to use i$ inside aes().
by(df,df$ID,function(i) {
ggplot(i) +
geom_point(aes(Month, EC, group=Year))
})

Resources