[R] Help using mapply to run multiple models

David Winsemius dwinsemius at comcast.net
Thu Dec 19 20:30:49 CET 2013


On Dec 19, 2013, at 11:10 AM, William Dunlap wrote:

>>>               call[[1]] <- quote(modelType) # '
> 
> makes call[[1]] the same as as.name("modelType").  You want
> as.name(modelType).

Just so I can see if I understand ... that is because `as.name` will evaluate `modelType` whereas as.name("modelType") would look for the function `modelType` and not find such a name in the namespace? So modelType needs to be a language-object and `f` needs to be called with:

f(glm, ....) rather than f("glm", ...)


> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> 
> 
>> -----Original Message-----
>> From: Simon Kiss [mailto:sjkiss at gmail.com]
>> Sent: Thursday, December 19, 2013 10:56 AM
>> To: William Dunlap
>> Cc: Dennis Murphy; r-help at r-project.org
>> Subject: Re: [R] Help using mapply to run multiple models
>> 
>> Hello Bill, that is fantastic and it's quite a bit above what I could write. Is there a way to
>> make the model type an argument to the function so that you can specify whether one is
>> running glm, lm and such?
>> I tried to modify it by inserting an argument modelType below, but that doesn't work.
>> Yours, simon Kiss
>>> f <- function (modelType, responseName, predictorNames, data, ..., envir =
>> parent.frame())
>>>   {
>>>       call <- match.call()
>>>       call$formula <- formula(envir = envir, paste(responseName, sep = " ~ ",
>>>           paste0("`", predictorNames, "`", collapse = " + ")))
>>>               call[[1]] <- quote(modelType) # '
>>>       call$responseName <- NULL # omit responseName=
>>>       call$predictorNames <- NULL # omit 'predictorNames='
>>>               eval(call, envir = envir)
>>>   }
>> On 2013-12-18, at 3:07 PM, William Dunlap <wdunlap at tibco.com> wrote:
>> 
>>> f <- function (responseName, predictorNames, data, ..., envir = parent.frame())
>>>   {
>>>       call <- match.call()
>>>       call$formula <- formula(envir = envir, paste(responseName, sep = " ~ ",
>>>           paste0("`", predictorNames, "`", collapse = " + ")))
>>>               call[[1]] <- quote(glm) # 'f' -> 'glm'
>>>       call$responseName <- NULL # omit responseName=
>>>       call$predictorNames <- NULL # omit 'predictorNames='
>>>               eval(call, envir = envir)
>>>   }
>>> as in
>>>   z <- lapply(list(c("hp","drat"), c("cyl"), c("am","gear")), FUN=function(preds)f("carb",
>> preds, data=mtcars, family=poisson))
>>>   lapply(z, summary)
>> 
>> *********************************
>> Simon J. Kiss, PhD
>> Assistant Professor, Wilfrid Laurier University
>> 73 George Street
>> Brantford, Ontario, Canada
>> N3T 2C9
>> Cell: +1 905 746 7606
>> 
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list