[R] Help using mapply to run multiple models

Simon Kiss sjkiss at gmail.com
Thu Dec 19 22:49:23 CET 2013


Hi there: Just to tie this altogether.

Here is the final function

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]] <- as.name(modelType)
  call$responseName <- NULL # omit responseName=
  call$predictorNames <- NULL # omit 'predictorNames='
  eval(call, envir = envir)
}
  
Here I call the function to a list of predictor variables and one dependent variable. Note "glm" and not glm.
z <- lapply(list(c("hp","drat"), c("cyl"), c("am","gear")), FUN=function(preds)f("glm", "carb", preds, data=mtcars, family='binomial'))

I do get this error:
Error in glm.control(modelType = "glm") : 
  unused argument(s) (modelType = "glm")

But 
lapply(z, summary)

does seem to return a list of model summaries. It looks like it worked.

I also tried. 
z <- lapply(list(c("hp","drat"), c("cyl"), c("am","gear")), FUN=function(preds)f("lm", "mpg", preds, data=mtcars))

Here, I get:
1: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
  extra argument ‘modelType’ is disregarded.
2: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
  extra argument ‘modelType’ is disregarded.
3: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
  extra argument ‘modelType’ is disregarded.

But again, it actually looks like it worked.
So, thank you very much!
Yours, Simon Kiss

On 2013-12-19, at 1:55 PM, Simon Kiss <sjkiss at gmail.com> wrote:

> 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
> 
> 
> 

*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 905 746 7606



More information about the R-help mailing list