[R-sig-ME] plot log transformed variable with Effects package
John Fox
jfox at mcmaster.ca
Thu Oct 10 05:01:24 CEST 2013
Dear Ben,
This problem is already fixed in the development version of the effects package on R-Forge. We expect to move this version to CRAN soon, but in the interim, it can be installed via install.packages("effects", repos="http://R-Forge.R-project.org").
I hope this helps,
John
On Wed, 9 Oct 2013 22:16:43 +0000 (UTC)
Ben Bolker <bbolker at gmail.com> wrote:
> Ben Bolker <bbolker at ...> writes:
>
> >
> > <John.Morrongiello <at> ...> writes:
> >
> > >
> > > Hi list
> > >
> > > I'm having trouble plotting a mixed model that includes log transformed
> > (or any transformed) terms.
> > >
> > library(lme4)
> > library(effects)
> > data(cake, package="lme4")
> > >
> > > ###this works
> > fm1 <- lmer(angle ~ recipe * temperature
> > + (1|recipe:replicate),
> > cake, REML = FALSE)
> >
> > plot(Effect(c('recipe','temperature'), fm1))
> > ##but this doesn't (log transformed angle)
> >
> > fm2 <- lmer(log(angle) ~ recipe * temperature + (1|recipe:replicate),
> > cake, REML = FALSE)
> >
> > plot(Effect(c('recipe','temperature'), fm2))
> > This is probably something to take up with the maintainer of
> > the 'effects' package, who in turn might have to consult the lme4
> > maintainers. The proximal problem occurs in
> >
> > plot -> Effect -> Effect.merMod -> Effect.mer -> Effect ->
> > mer.to.glm
> >
> > There is a 'data' object that appears to be coming from the
> > model.frame() of the original object, but I haven't tracked its
> > source down yet -- but the problem is that it has log(angle) rather
> > than angle as a column ...
> >
> >
>
> with a bit more work, I have come up with a way to
> hack the effects package to make this work. The function below
> needs to be defined, then the arguments of the effects:::mer.to.glm
> function have to be changed from
>
> function(mod, data=model.frame(mod))
>
> to
>
> function(mod, data=xdata(mod))
>
> You can hack the package yourself and/or request the maintainer
> to add this capability ...
>
> ## modeled after stats::expand.model.frame
> ## expand the model frame to include any variables present
> ## in the original 'data' object but missing from the model frame
> ## potentially fragile:
> ## * depends on 'data' still being present in the original environment
> ## * doesn't check for any potential mishaps
> xdata <- function(model, envir=environment(formula(model))) {
> fr <- model.frame(model)
> data <- eval(getCall(model)$data,envir)
> ## find missing variables
> newvars <- setdiff(all.vars(formula(model)),names(fr))
> if (length(newvars)>0) {
> fr <- data.frame(fr,data[newvars],check.names=FALSE)
> }
> fr
> }
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
------------------------------------------------
John Fox
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/
More information about the R-sig-mixed-models
mailing list