Fitting multiple nls functions with dplyr

nathaneastwood

I wish to fit multiple nls functions using a group_by from the dplyr package but I am unsure how I can pass multiple starting values. Let's take a simpler example (see ?nls for the inspiration).

DNase1 <- subset(DNase, Run == 1)
modelDNase1 <- DNase1 %>% 
  do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
             data = .,
             start = list(xmid = 0, scal = 1),
             algorithm = "plinear"))

So here I am fitting a single model. But what if I want to extend this so I am fitting the following:

DNase$Run <- factor(DNase$Run)
modelDNase <- DNase %>%
  group_by(Run) %>% 
  do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
             data = .,
             start = list(xmid = 0, scal = 1),
             algorithm = "plinear"))

How would I pass on multiple start parameters? Would the purrr package be of any use?

Gregor Thomas

(Comment to answer.) My first guess was correct, the .$ syntax seems to work.

As a convenient way of picking starting values, create a table with the unique group values and the desired starting values in new columns. Knowing nothing about this data, I assigned them randomly:

starts = data.frame(Run = unique(DNase$Run),
           xmid_start = rnorm(11, sd = 0.1),
           scale_start = 1 + rnorm(11, sd = 0.1))

We can then join this to the data and proceed, pulling the first starting value from each grouping to give to the model:

mods = DNase %>% 
    left_join(starts) %>%
    group_by(Run) %>%
    do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
             data = .,
             start = list(xmid = first(.$xmid_start),
                          scal = first(.$scale_start)),
             algorithm = "plinear"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fitting nls to grouped data R

From Dev

R nls: fitting a curve to data

From Dev

R nls: fitting a curve to data

From Dev

Fitting a curve to weibull distribution in R using nls

From Dev

error fitting function to data using nls

From Java

Fitting several regression models with dplyr

From Dev

nls2 with nested functions

From Dev

Fitting with ggplot2, geom_smooth and nls

From Dev

Fitting a nls with ggplot2 - error object of type 'symbol' is not subsettable

From Dev

R nlsLM / nls fitting quality with low datapoint number

From Dev

fitting hyperbolic and harmonic functions with curvefit

From Dev

fitting hyperbolic and harmonic functions with curvefit

From Dev

What is the pandas equivalent of dplyr summarize/aggregate by multiple functions?

From Dev

Creating multiple functions with dplyr non-standard evaluation

From Dev

Using functions of multiple columns in a dplyr mutate_at call

From Dev

errors when passing multiple arguments to dplyr functions in R

From Dev

How to apply multiple functions on grouped tibble using dplyr

From Dev

Fitting a linear model with multiple LHS

From Dev

`nls` fitting error: always reach maximum number of iterations regardless starting values

From Dev

call custom functions with {{}} in dplyr

From Java

Using strings in functions in dplyr

From Dev

Pass arguments to dplyr functions

From Dev

Major dplyr functions in a function

From Dev

Using 'window' functions in dplyr

From Dev

Fitting ImageView elements on layout with multiple screen resolutions

From Dev

Fitting multiple different regression lines with ggplot

From Dev

Fitting multiple different regression lines with ggplot

From Dev

how to do line fitting multiple lines MATLAB?

From Dev

Fitting ImageView elements on layout with multiple screen resolutions

Related Related

  1. 1

    Fitting nls to grouped data R

  2. 2

    R nls: fitting a curve to data

  3. 3

    R nls: fitting a curve to data

  4. 4

    Fitting a curve to weibull distribution in R using nls

  5. 5

    error fitting function to data using nls

  6. 6

    Fitting several regression models with dplyr

  7. 7

    nls2 with nested functions

  8. 8

    Fitting with ggplot2, geom_smooth and nls

  9. 9

    Fitting a nls with ggplot2 - error object of type 'symbol' is not subsettable

  10. 10

    R nlsLM / nls fitting quality with low datapoint number

  11. 11

    fitting hyperbolic and harmonic functions with curvefit

  12. 12

    fitting hyperbolic and harmonic functions with curvefit

  13. 13

    What is the pandas equivalent of dplyr summarize/aggregate by multiple functions?

  14. 14

    Creating multiple functions with dplyr non-standard evaluation

  15. 15

    Using functions of multiple columns in a dplyr mutate_at call

  16. 16

    errors when passing multiple arguments to dplyr functions in R

  17. 17

    How to apply multiple functions on grouped tibble using dplyr

  18. 18

    Fitting a linear model with multiple LHS

  19. 19

    `nls` fitting error: always reach maximum number of iterations regardless starting values

  20. 20

    call custom functions with {{}} in dplyr

  21. 21

    Using strings in functions in dplyr

  22. 22

    Pass arguments to dplyr functions

  23. 23

    Major dplyr functions in a function

  24. 24

    Using 'window' functions in dplyr

  25. 25

    Fitting ImageView elements on layout with multiple screen resolutions

  26. 26

    Fitting multiple different regression lines with ggplot

  27. 27

    Fitting multiple different regression lines with ggplot

  28. 28

    how to do line fitting multiple lines MATLAB?

  29. 29

    Fitting ImageView elements on layout with multiple screen resolutions

HotTag

Archive