Skip to content

Commit 9193a9f

Browse files
Get first half of synthesis vignette working with full data
1 parent ba374d5 commit 9193a9f

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

vignettes/04-synthesis-data.Rmd

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The first contains all the canopy cover values for 2018, which was created in th
1313
The second is the cumulative growing degree days for all of 2018, which were calculated from the daily minimum and maximum temperatures in the weather vignette.
1414
They are combined by their common column, the date.
1515

16-
```{r synth_setup}
16+
```{r synth_setup, message=FALSE}
1717
library(dplyr)
1818
library(ggplot2)
1919
library(jsonlite)
@@ -28,14 +28,14 @@ options(betydb_url = "https://terraref.ncsa.illinois.edu/bety/",
2828
```{r get_trait_data, message = FALSE}
2929
trait_canopy_cover <- betydb_query(table = "search",
3030
trait = "canopy_cover",
31-
date = "~2017",
31+
date = "~2018",
3232
limit = "none")
3333
trait_canopy_cover_day = trait_canopy_cover %>%
3434
mutate(day = as.Date(raw_date))
3535
```
3636

3737
```{r get_weather_data}
38-
weather <- fromJSON('https://terraref.ncsa.illinois.edu/clowder/api/geostreams/datapoints?stream_id=46431&since=2017-01-01&until=2017-12-31', flatten = FALSE)
38+
weather <- fromJSON('https://terraref.ncsa.illinois.edu/clowder/api/geostreams/datapoints?stream_id=46431&since=2018-01-01&until=2018-12-31', flatten = FALSE)
3939
weather <- weather$properties %>%
4040
mutate(time = ymd_hms(weather$end_time))
4141
daily_values = weather %>%
@@ -74,24 +74,26 @@ We provide estimated values for the asymptote $c$ and initial canopy cover value
7474
The below provides better estimates for the $c$, $a$, and $b$ parameters, which are used to plot the model as an orange line on top of the black points which are actual values.
7575

7676
```{r model_get_parameters}
77-
# TODO: replace with actual data
78-
# single_cultivar <- trait_weather_df %>%
79-
# filter(cultivar == "PI656026")
80-
single_cultivar <- data.frame(gdd_cum = c(5, 6.2, 7, 18, 25, 30, 36, 39, 40), mean = c(10, 10.11, 10.5, 20, 32.63432, 40, 50, 50.5, 50.5))
77+
single_cultivar <- trait_weather_df %>%
78+
filter(cultivar == "PI656026")
8179
82-
c <- 51
83-
a <- 10
80+
c <- 90
81+
a <- 0.1
8482
y <- single_cultivar$mean[3]
8583
g <- single_cultivar$gdd_cum[3]
8684
b <- ((log((c/y) - 1)) - a)/g
8785
model_single_cultivar <- nls(mean ~ c / (1 + exp(a + b * gdd_cum)),
8886
start = list(c = c, a = a, b = b),
89-
data = single_cultivar, trace = TRUE)
87+
data = single_cultivar)
9088
summary(model_single_cultivar)
9189
coef(model_single_cultivar)
9290
91+
single_c <- coef(model_single_cultivar)[1]
92+
single_a <- coef(model_single_cultivar)[2]
93+
single_b <- coef(model_single_cultivar)[3]
94+
9395
single_cultivar <- single_cultivar %>%
94-
mutate(mean_predict = coef(model_single_cultivar)[1] / (1 + exp(coef(model_single_cultivar)[2] + coef(model_single_cultivar)[3] * gdd_cum)))
96+
mutate(mean_predict = single_c / (1 + exp(single_a + single_b * gdd_cum)))
9597
ggplot(single_cultivar) +
9698
geom_point(aes(x = gdd_cum, y = mean)) +
9799
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +
@@ -103,9 +105,8 @@ We then calculate the inflection point for this cultivar's model.
103105
The maximum growth rate is the change in canopy cover per day at the rate of maximum growth. The growing degree day at which maximum growth is obtained is called the _inflection point_. This occurs near the midpoint of the y-axis, or $\frac{c - a}{2}$.
104106

105107
```{r}
106-
inf_y <- (as.numeric(coef(model_single_cultivar)[1]) - as.numeric(coef(model_single_cultivar)[2])) / 2
107-
108-
inf_x <- ((log((as.numeric(coef(model_single_cultivar)[1]) / inf_y) - 1)) - as.numeric(coef(model_single_cultivar)[2])) / as.numeric(coef(model_single_cultivar)[3])
108+
inf_y <- (as.numeric(single_c) - as.numeric(single_a)) / 2
109+
inf_x <- ((log((as.numeric(single_c) / inf_y) - 1)) - as.numeric(single_a)) / as.numeric(single_b)
109110
110111
ggplot(single_cultivar) +
111112
geom_point(aes(x = gdd_cum, y = mean)) +
@@ -119,26 +120,33 @@ We then use the parameters from a single cultivar to run a model for each of the
119120
These results are used to plot the model predictions, which are shown as an orange line.
120121
We also calculated the inflection point from each cultivar's model, which will be used in the following section.
121122

122-
```{r model_all_cultivars, eval=FALSE}
123+
```{r model_all_cultivars}
123124
all_cultivars <- c(day = as.double(), cultivar = as.character(), mean = as.numeric(),
124-
gdd_cum = as.numeric(), mean_predict = as.numeric())
125+
gdd_cum = as.numeric(), mean_predict = as.numeric(),
126+
inf_y = as.numeric(), inf_x = as.numeric())
127+
125128
for(each_cultivar in unique(trait_weather_df$cultivar)){
126129
each_cultivar_df <- filter(trait_weather_df, cultivar == each_cultivar)
127-
each_cultivar_model <- nls(y ~ c / (1 + exp(a + b * g)),
128-
start = list(c = c, a = a, b = b),
129-
data = each_cultivar_df)
130+
each_cultivar_model <- nls(mean ~ c / (1 + exp(a + b * gdd_cum)),
131+
start = list(c = c, a = a, b = b),
132+
data = each_cultivar_df)
130133
model_c <- coef(each_cultivar_model)[1]
131134
model_a <- coef(each_cultivar_model)[2]
132135
model_b <- coef(each_cultivar_model)[3]
133136
each_cultivar_df <- each_cultivar_df %>%
134-
mutate(mean_predict = model_c / (1 + exp(model_a + model_b * g)),
135-
inf_point = ((log((model_c / 100) - 1)) - model_a) / model_b)
137+
mutate(mean_predict = model_c / (1 + exp(model_a + model_b * gdd_cum)),
138+
inf_y = (as.numeric(model_c) - as.numeric(model_a)) / 2,
139+
inf_x = ((log((as.numeric(model_c) / inf_y) - 1)) -
140+
as.numeric(single_a)) / as.numeric(single_b))
136141
all_cultivars <- rbind(each_cultivar_df, all_cultivars)
137142
}
143+
138144
ggplot(all_cultivars) +
139145
geom_point(aes(x = gdd_cum, y = mean)) +
140146
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +
141147
facet_wrap(~cultivar, scales = "free_y") +
148+
geom_hline(yintercept = inf_y, linetype = "dashed") +
149+
geom_vline(xintercept = inf_x) +
142150
labs(x = "Cumulative growing degree days", y = "Canopy Height")
143151
```
144152

@@ -148,15 +156,9 @@ The last thing that we are going to do is assess the difference in this relation
148156
We are going to use the inflection point from the logistic growth model, which indicates when canopy cover stops increasing as quickly with increasingly more warm days.
149157
The resulting inflection points for each cultivar are plotted as a histogram.
150158

151-
```{r plot_inflections, eval=FALSE}
152-
ggplot(all_cultivars) +
153-
geom_point(aes(x = gdd_cum, y = mean)) +
154-
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +
155-
geom_vline(aes(xintercept = inf_point)) +
156-
facet_wrap(~cultivar, scales = "free_y") +
157-
labs(x = "Cumulative growing degree days", y = "Canopy Height")
158-
ggplot(data.frame(inf_points = unique(all_cultivars$inf_point))) +
159-
geom_histogram(aes(x = inf_points)) +
159+
```{r plot_inflections, warning=FALSE}
160+
ggplot(data.frame(inf_points = unique(all_cultivars$inf_x))) +
161+
geom_histogram(aes(x = inf_points), bins = 300) +
160162
xlim(min(all_cultivars$gdd_cum), max(all_cultivars$gdd_cum)) +
161163
labs(x = "Inflection points", y = "Number")
162164
```

0 commit comments

Comments
 (0)