1

I want to add the formulas I have used in a series of models to a data.frame

require(plyr)
require(nlme)
str(baseball)
ba <- baseball[1:100,]
m01 <- gls( g ~ ab+I(r^2)+cos(pi*h),data=ba,correlation = corARMA(p=1))
m02 <- gls( g ~ I(ab^3)+r+cos(pi*h)+sin(pi*h),data=ba,correlation = corARMA(p=1))
m03 <- gls( g ~ ab+r,data=ba,correlation = corARMA(p=1))

I have 3 models then I want two columns: the name of the model and the formula

mof <-ldply(ls(pattern=glob2rx("m0*")))

mof <-ddply(mof, .(V1),transform, form =formula(V1))

which gives

Error en as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class '"formula"' into a data.frame

I have tried a lot of different things but can't make it work.

Leosar
  • 2,010
  • 4
  • 21
  • 32

2 Answers2

3

When you are working with language elements some of the rules change. You apparently want a character column or two to label your results but what you have constructed in that first "mof" object is a character vector with just the names of the models, not the models themselves.

> str(mof)
'data.frame':   3 obs. of  1 variable:
 $ V1: chr  "m01" "m02" "m03"

To retrieve the models from the workspace using character vectors you need to use get. They would then be available for further processing with the functions formula and as.character. You then will need to "go back" to character mode at the end, since objects of class-formula are not valid dataframe components. All in one nested call this would be:

> forms.mat <- sapply( lapply( lapply(mof$V1, get) , formula), as.character)
> forms.mat
     [,1]                        [,2]                                      [,3]    
[1,] "~"                         "~"                                       "~"     
[2,] "g"                         "g"                                       "g"     
[3,] "ab + I(r^2) + cos(pi * h)" "I(ab^3) + r + cos(pi * h) + sin(pi * h)" "ab + r"

You could rearrange (to get the tilde back in between the LHS and the RHS expressions and paste together (with collapse="") with:

> apply(forms.mat[ c(2,1,3),], 2, paste, collapse="")
[1] "g~ab + I(r^2) + cos(pi * h)"               "g~I(ab^3) + r + cos(pi * h) + sin(pi * h)"
[3] "g~ab + r"   

You might have simplified this a bit by just working with a list:

> mof2 <- list(m01,m02,m03)  # Skipping the search of the workspace and reconstitution 
> lapply(mof2, formula)
[[1]]
g ~ ab + I(r^2) + cos(pi * h)
<environment: 0x2ef997c60>

[[2]]
g ~ I(ab^3) + r + cos(pi * h) + sin(pi * h)
<environment: 0x19fef52d0>

[[3]]
g ~ ab + r
<environment: 0x19fef68d8>

> sapply( lapply(mof2, formula), as.character )
     [,1]                        [,2]                                      [,3]    
[1,] "~"                         "~"                                       "~"     
[2,] "g"                         "g"                                       "g"     
[3,] "ab + I(r^2) + cos(pi * h)" "I(ab^3) + r + cos(pi * h) + sin(pi * h)" "ab + r"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • also u can get all the formulas applying m01$call[2], m02$call[2] ... I know this is simple and if u will have more than few calls it would be annoying to do it - but I think it's more informative - as it's for me; but agree that @DWin answer is more automatic – java_xof Dec 14 '12 at 21:31
  • Those are still language objects and as such will create problem if attempts are made to insert into dataframes. – IRTFM May 04 '13 at 02:48
0

The first colum is the name of the models that is ok,

mof <-ldply(ls(pattern=glob2rx("m0*")))

And the second column should be the formula of the models

names(mof)[1] <- "model"
mof$formula <- as.character(lapply(mof$model,formula))

and that works perfectly, after that I will add columns with corrected AIC and other things for model selection, but that is working fine.

Thanks to all commenters!

Leosar
  • 2,010
  • 4
  • 21
  • 32