Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing lapply on a list of models
    primarykey
    data
    text
    <p>I have generated a list of models, and would like to create a summary table.</p> <p>As and example, here are two models:</p> <pre><code>x &lt;- seq(1:10) y &lt;- sin(x)^2 model1 &lt;- lm(y ~ x) model2 &lt;- lm(y ~ x + I(x^2) + I(x^3)) </code></pre> <p>and two formulas, the first generating the equation from components of formula</p> <pre><code>get.model.equation &lt;- function(x) { x &lt;- as.character((x$call)$formula) x &lt;- paste(x[2],x[1],x[3]) } </code></pre> <p>and the second generating the name of model as a string</p> <pre><code>get.model.name &lt;- function(x) { x &lt;- deparse(substitute(x)) } </code></pre> <p>With these, I create a summary table</p> <pre><code>model.list &lt;- list(model1, model2) AIC.data &lt;- lapply(X = model.list, FUN = AIC) AIC.data &lt;- as.numeric(AIC.data) model.models &lt;- lapply(X = model.list, FUN = get.model) model.summary &lt;- cbind(model.models, AIC.data) model.summary &lt;- as.data.frame(model.summary) names(model.summary) &lt;- c("Model", "AIC") model.summary$AIC &lt;- unlist(model.summary$AIC) rm(AIC.data) model.summary[order(model.summary$AIC),] </code></pre> <p>Which all works fine. I'd like to add the model name to the table using get.model.name</p> <pre><code>x &lt;- get.model.name(model1) </code></pre> <p>Which gives me "model1" as I want.</p> <p>So now I apply the function to the list of models</p> <pre><code>model.names &lt;- lapply(X = model.list, FUN = get.model.name) </code></pre> <p>but now instead of <em>model1</em> I get <em>X[[1L]]</em></p> <p>How do I get <em>model1</em> rather than <em>X[[1L]]</em>?</p> <p>I'm after a table that looks like this:</p> <pre><code> Model Formula AIC model1 y ~ x 11.89136 model2 y ~ x + I(x^2) + I(x^3) 15.03888 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload