Created
March 22, 2023 11:04
-
-
Save padpadpadpad/8a812317e49b9badd37dc639eb53080c to your computer and use it in GitHub Desktop.
Example of using map2 to pass arguments to a model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# random modelling example for Stephen | |
# load in packages | |
librarian::shelf(rTPC,tidyverse, nls.multstart) | |
# create data | |
d <- chlorella_tpc %>% | |
filter(curve_id < 4) %>% | |
nest(data = c(everything(), -curve_id)) | |
# lets have a look | |
d | |
d$data[[1]] | |
# create data frame of parameters to pass to each curve fit | |
# want to pass individual start values and individual lower limits | |
d_params <- tibble(curve_id = rep(1:3, each = 3), start_vals = c(get_start_vals(d$data[[1]]$rate, d$data[[1]]$temp, 'gaussian_1987'), get_start_vals(d$data[[2]]$rate, d$data[[2]]$temp, 'gaussian_1987'),get_start_vals(d$data[[3]]$rate, d$data[[3]]$temp, 'gaussian_1987')), | |
lower_lims = c(get_lower_lims(d$data[[1]]$rate, d$data[[1]]$temp, 'gaussian_1987'), get_lower_lims(d$data[[2]]$rate, d$data[[2]]$temp, 'gaussian_1987'),get_lower_lims(d$data[[3]]$rate, d$data[[3]]$temp, 'gaussian_1987'))) %>% | |
nest(params = -curve_id) | |
d_params | |
# have a look at the first one | |
d_params$params[[1]] | |
# bind these into the data frame | |
d <- left_join(d, d_params) | |
d | |
# fit the model, can pass individual arguments to each fit using .y$ | |
d <- mutate(d, fit = map2(data, params, | |
~nls_multstart(rate~gaussian_1987(temp = temp, rmax, topt, a), | |
data = .x, | |
start_lower = .y$start_vals -10, | |
start_upper = .y$start_vals + 10, iter = 100))) | |
d$fit[[1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment