Skip to content

Commit ba374d5

Browse files
Update logistic model equation and code
1 parent 95b42c9 commit ba374d5

1 file changed

Lines changed: 53 additions & 35 deletions

File tree

vignettes/04-synthesis-data.Rmd

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "~2018",
31+
date = "~2017",
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=2018-01-01&until=2018-12-31', flatten = FALSE)
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)
3939
weather <- weather$properties %>%
4040
mutate(time = ymd_hms(weather$end_time))
4141
daily_values = weather %>%
@@ -60,61 +60,79 @@ trait_weather_df <- full_join(trait_canopy_cover_day, daily_values, by = "day")
6060

6161
We are interested in how growing degree days affects canopy cover.
6262
To investigate this, we are going to model and plot their relationship.
63-
We want to know the relationship for each cultivar, so we'll start of by determining the parameters of the model for one of the cultivars in our dataset.
64-
We are using a logistic growth model here because it is appropriate for the shape of the GDD-cover relationship. (From Bolker? ... need to cite)
65-
66-
This needs to be flushed out ... and the variable names should be consistent b/w equations and code
63+
We are using a logistic growth model here because it is appropriate for the shape of the GDD-cover relationship.
6764

6865
The logistic growth model is specified as
6966

70-
$Y=\frac{z}{1+e^{-k(t-t_0)}}$
71-
72-
$Y = \frac{c}{1+e^{a + b * \textrm{gdd}}}$
67+
$$y = \frac{c}{1+e^{a + b * \textrm{x}}}$$
7368

74-
Where $Y$ is canopy cover, $c$ is the maximum canopy cover, $a$ is ... $b$ is ...
69+
where $y$ is the response variable canopy cover, $x$ is the predictor growing degree days, $c$ is the asymptote or maximum canopy cover, $a$ is the initial value for canopy cover, and $b$ is the steepness of the curve. (reference)
7570

76-
From this we can find the asymptote and maximum growth rate. 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_ $\textrm{gdd}_i=\frac{\log{a}}{b}$.
77-
<!--
78-
or maybe start from the bolker form of the model (I added )
79-
if $Y=\frac{e^{a+b(t-t_0)}}{1+e^{a+b(t-t_0)}}$
71+
We want to know the relationship for each cultivar, so we'll start of by determining the parameters of the model for one of the cultivars in our dataset.
72+
We provide estimated values for the asymptote $c$ and initial canopy cover value $a$, and provide canopy cover $y$ with corresponding growing degree days $x$ for one measurement of the chosen cultivar.
8073

81-
then $t-t_0$ is the number of days after planting.
74+
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.
8275

83-
then the inflection point is at $x=\frac{-a}{b}$ and the maximum rate of growth, $\max{\frac{dY}{dt}}=\frac{b}{4}$
84-
-->
8576
```{r model_get_parameters}
86-
single_cultivar <- trait_weather_df %>%
87-
filter(cultivar == "PI656026")
88-
cap <- 150
89-
initial <- 25
90-
mean <- single_cultivar$mean[15]
91-
gdd_cum <- single_cultivar$gdd_cum[15]
92-
rate <- ((log((cap/mean) - 1)) - initial)/gdd_cum
93-
model_single_cultivar <- nls(mean ~ cap / (1 + exp(initial + rate * gdd_cum)),
94-
start = list(cap = cap, initial = initial, rate = rate),
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))
81+
82+
c <- 51
83+
a <- 10
84+
y <- single_cultivar$mean[3]
85+
g <- single_cultivar$gdd_cum[3]
86+
b <- ((log((c/y) - 1)) - a)/g
87+
model_single_cultivar <- nls(mean ~ c / (1 + exp(a + b * gdd_cum)),
88+
start = list(c = c, a = a, b = b),
9589
data = single_cultivar, trace = TRUE)
90+
summary(model_single_cultivar)
91+
coef(model_single_cultivar)
92+
9693
single_cultivar <- single_cultivar %>%
9794
mutate(mean_predict = coef(model_single_cultivar)[1] / (1 + exp(coef(model_single_cultivar)[2] + coef(model_single_cultivar)[3] * gdd_cum)))
95+
ggplot(single_cultivar) +
96+
geom_point(aes(x = gdd_cum, y = mean)) +
97+
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +
98+
labs(x = "Cumulative growing degree days", y = "Canopy Height")
99+
```
100+
101+
We then calculate the inflection point for this cultivar's model.
102+
103+
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}$.
104+
105+
```{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])
109+
110+
ggplot(single_cultivar) +
111+
geom_point(aes(x = gdd_cum, y = mean)) +
112+
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +
113+
geom_hline(yintercept = inf_y, linetype = "dashed") +
114+
geom_vline(xintercept = inf_x) +
115+
labs(x = "Cumulative growing degree days", y = "Canopy Height")
98116
```
99117

100118
We then use the parameters from a single cultivar to run a model for each of the rest of the cultivars.
101119
These results are used to plot the model predictions, which are shown as an orange line.
102120
We also calculated the inflection point from each cultivar's model, which will be used in the following section.
103121

104-
```{r model_all_cultivars}
122+
```{r model_all_cultivars, eval=FALSE}
105123
all_cultivars <- c(day = as.double(), cultivar = as.character(), mean = as.numeric(),
106124
gdd_cum = as.numeric(), mean_predict = as.numeric())
107125
for(each_cultivar in unique(trait_weather_df$cultivar)){
108126
each_cultivar_df <- filter(trait_weather_df, cultivar == each_cultivar)
109-
each_cultivar_model <- nls(mean ~ cap / (1 + exp(initial + rate * gdd_cum)),
110-
start = list(cap = cap, initial = initial, rate = rate),
127+
each_cultivar_model <- nls(y ~ c / (1 + exp(a + b * g)),
128+
start = list(c = c, a = a, b = b),
111129
data = each_cultivar_df)
112-
model_cap <- coef(each_cultivar_model)[1]
113-
model_initial <- coef(each_cultivar_model)[2]
114-
model_rate <- coef(each_cultivar_model)[3]
130+
model_c <- coef(each_cultivar_model)[1]
131+
model_a <- coef(each_cultivar_model)[2]
132+
model_b <- coef(each_cultivar_model)[3]
115133
each_cultivar_df <- each_cultivar_df %>%
116-
mutate(mean_predict = model_cap / (1 + exp(model_initial + model_rate * gdd_cum)),
117-
inf_point = ((log((model_cap / 100) - 1)) - model_initial) / model_rate)
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)
118136
all_cultivars <- rbind(each_cultivar_df, all_cultivars)
119137
}
120138
ggplot(all_cultivars) +
@@ -130,7 +148,7 @@ The last thing that we are going to do is assess the difference in this relation
130148
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.
131149
The resulting inflection points for each cultivar are plotted as a histogram.
132150

133-
```{r plot_inflections}
151+
```{r plot_inflections, eval=FALSE}
134152
ggplot(all_cultivars) +
135153
geom_point(aes(x = gdd_cum, y = mean)) +
136154
geom_line(aes(x = gdd_cum, y = mean_predict), color = "orange") +

0 commit comments

Comments
 (0)