[R-sig-ME] formula objects and glmer

Douglas Bates bates at stat.wisc.edu
Wed Feb 9 00:10:11 CET 2011


On Tue, Feb 8, 2011 at 2:21 PM, Christopher Chizinski <chizi001 at umn.edu> wrote:
> I am trying to automate running through different model subsets in
> lme4 (version 0.999375-37) using glmer and storing the model output as
> a list.  It runs through the sequence fine but the problem I am having
> is the Formula stored in each list element has the formula object,
> "form," I created rather than the specific formula.  I have looked
> through several lists and can not really see where I am going wrong.
> I am not sure if this is something specific to glmer or something more
> basic that I am missing. Thank you for any help or direction you can
> provide.

It is general to formulas.  If you want to manipulate a formula it is
best to use substitute as in

> nm <- "bar"
> (newform <- substitute(baz ~ foo, list(baz = as.name(nm))))
bar ~ foo

Sorry if this response is too cryptic.  Right now I have a bus to catch.
>
> Here is an example:
> set.seed(2147483647)
> dat<-data.frame(event=rpois(15,
> 1.5),mort=c(0,0,0,0,1,1,0,0,1,0,1,0,1,0,1),X1=rnorm(15),X2=rnorm(15),X3=rnorm(15),X4=rnorm(15))
> cand.vars<-data.frame(cbind(c('X1','X2'),c('X2','X4'),c('X3','X1')))
> Cand.models<-list()
> for(i in 1:ncol(cand.vars)){
>     form<-as.formula(paste("mort~",paste(cand.vars[,i],collapse
> ='+'),"+(1|event)"))
>     Cand.models[[i]]<-glmer(form,family = binomial, data = dat)
> }
> print(Cand.models)
>
>
> # output
> [[1]]
> Generalized linear mixed model fit by the Laplace approximation
> Formula: form
>    Data: dat
>    AIC   BIC logLik deviance
>  26.29 29.12 -9.143    18.29
> Random effects:
>  Groups Name        Variance Std.Dev.
>  event  (Intercept)  0        0
> Number of obs: 15, groups: event, 6
> Fixed effects:
>             Estimate Std. Error z value Pr(>|z|)
> (Intercept)  -0.5155     0.5770  -0.893    0.372
> X1           -0.8158     0.6354  -1.284    0.199
> X2           -0.1028     0.5714  -0.180    0.857
> Correlation of Fixed Effects:
>    (Intr) X1
> X1  0.194
> X2 -0.047  0.279
> [[2]]
> Generalized linear mixed model fit by the Laplace approximation
> Formula: form
>    Data: dat
>    AIC   BIC logLik deviance
>  25.76 28.59 -8.878    17.76
> Random effects:
>  Groups Name        Variance  Std.Dev.
>  event  (Intercept) 3.898e-13 6.2434e-07
> Number of obs: 15, groups: event, 6
> Fixed effects:
>             Estimate Std. Error z value Pr(>|z|)
> (Intercept)  -0.4947     0.5828  -0.849    0.396
> X2            0.1890     0.5898   0.320    0.749
> X4           -1.0695     0.8021  -1.333    0.182
> Correlation of Fixed Effects:
>    (Intr) X2
> X2 -0.175
> X4  0.089 -0.167
> [[3]]
> Generalized linear mixed model fit by the Laplace approximation
> Formula: form
>    Data: dat
>   AIC   BIC logLik deviance
>  26.3 29.13  -9.15     18.3
> Random effects:
>  Groups Name        Variance Std.Dev.
>  event  (Intercept)  0        0
> Number of obs: 15, groups: event, 6
> Fixed effects:
>             Estimate Std. Error z value Pr(>|z|)
> (Intercept)  -0.5675     0.6611  -0.858    0.391
> X3            0.1465     1.0263   0.143    0.886
> X1           -0.7904     0.6103  -1.295    0.195
> Correlation of Fixed Effects:
>    (Intr) X3
> X3 -0.497
> X1  0.202 -0.070
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>




More information about the R-sig-mixed-models mailing list