title: R - Using nested dataframe to run function with different sets of parameters
body: <p>I would like to create a wrapper for the Levenberg-Marquardt Nonlinear Least-Squares function <code>nls.lm</code> (minpack.lm library) similar to <code>nls2</code> (nls2 library) to give a brute force method for evaluating the fit of a model to observed data.</p>
  
  <p>The idea is to create a range of starting value combinations and either: </p>
  
  <ul>
  <li>pass these to a function, then compare the function output to the observed data to create an R^2 value for each of the starting value combinations and run the nls.lm fitting with the best one of them.</li>
  </ul>
  
  <p>or </p>
  
  <ul>
  <li>run nls.lm on all combinations and select the best returned fit.</li>
  </ul>
  
  <p>I wanted to do this without looping and after inspiration from <a href="http://edinbr.org/edinbr/2016/05/11/may-Hadley-Update2-PostingTalk.html" rel="nofollow">here</a> am trying to use nested dataframes, with one column for the parameter input list, one for the values returned by my function, one for the R^2 values, and one for the best fit models,something like:</p>
  
  <pre><code>df
  #   start_val fun_out       R^2   
  # 1 {a=2,b=2} {22,24,26...} 0.8   
  # 2 {a=3,b=5} {35,38,41...} 0.6   
  </code></pre>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>require(dplyr);require(tidyr)
  
  foo &lt;- function(x,a,b) a*x^2+b # function I am fitting
  x &lt;- 1:10 # independent variable
  y_obs &lt;- foo(x,1.5,2.5) + rnorm(length(x),0,10) # observed data (dependent variable)
  
  start_range &lt;- data.frame(a=c(1,2),b=c(2,3)) # range of allowed starting points for fitting
  reps &lt;- 2 # number of starting points to generate
  
  # Create a data frame of starting points
  df&lt;-as.data.frame(sapply(start_range, function(x) runif(reps,min=x[[1]],max=x[[2]]))) %&gt;%
    mutate(id=seq_len(reps)) %&gt;% # fudge to make nest behave as I want
    nest(1:ncol(start_range)) %&gt;%
    mutate(data=as.list(data)) %&gt;%
    as.data.frame()
  
  df
  #   id               data
  # 1  1 1.316356, 2.662923
  # 2  2 1.059356, 2.723081
  </code></pre>
  
  <p>I get stuck now trying to pass the parameters in data into the function <code>foo()</code>. I've tried using <code>do.call()</code>, and even with using constant parameters the following error appears:</p>
  
  <pre><code>mutate(df,y=do.call(foo,list(x,1,2)))
  # Error: wrong result size (5), expected 2 or 1
  </code></pre>
  
  <p>Is there a way to create columns of a dataframe which contain lists directly without using <code>nest()</code>?</p>
  
  <p>Also when trying to create the list to pass to <code>do.call()</code> using the dataframe columns, how do you create a list where the first element is the vector x, the second is the parameter a and the third is the parameter b? The follwing splits the list down the column:</p>
  
  <pre><code>mutate(df,my_list=list(x,data))
  #   id               data                                my_list
  # 1  1 1.316356, 2.662923          1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  # 2  2 1.059356, 2.723081 1.316356, 2.662923, 1.059356, 2.723081
  </code></pre>