Grokbase Groups R r-help May 2008
FAQ
Hi,

I am using lmer to analyze habitat selection in wolverines using the
following model:

(me.fit.of <-
lmer(USED~1+STEP+ALT+ALT2+relM+relM:ALT+(1|ID)+(1|ID:TRKPT2),data=vdata,
control=list(usePQL=TRUE),family=poisson,method="Laplace"))

Here, the habitat selection is calaculated using a so-called discrete
choice model where each used location has a certain number of
alternatives which the animal could have chosen. These sets of locations
are captured using the TRKPT2 random grouping. However, these sets are
also clustered over the different individuals (ID). USED is my binary
dependent variable which is 1 for used locations and zero for unused
locations. The other are my predictors.

I would like to predict the model fit at different values of the
predictors, but does anyone know whether it is possible to do this? I
have looked around at the R-sites and in help but it seems that there
doesn't exist a predict function for lmer???

I hope someone can help me with this; point me to the right functions or
tell me to just forget it....

Thanks in advance!

Cheers Roel

Roel May
Norwegian Institute for Nature Research
Tungasletta 2, NO-7089 Trondheim, Norway

Search Discussions

  • Bert Gunter at May 7, 2008 at 4:52 pm
    ?fixef

    gets you the coefficient vector, from which you can make your predictions.

    -- Bert Gunter
    Genentech

    -----Original Message-----
    From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
    Behalf Of May, Roel
    Sent: Wednesday, May 07, 2008 7:23 AM
    To: r-help at r-project.org
    Subject: [R] predict lmer

    Hi,

    I am using lmer to analyze habitat selection in wolverines using the
    following model:

    (me.fit.of <-
    lmer(USED~1+STEP+ALT+ALT2+relM+relM:ALT+(1|ID)+(1|ID:TRKPT2),data=vdata,
    control=list(usePQL=TRUE),family=poisson,method="Laplace"))

    Here, the habitat selection is calaculated using a so-called discrete
    choice model where each used location has a certain number of
    alternatives which the animal could have chosen. These sets of locations
    are captured using the TRKPT2 random grouping. However, these sets are
    also clustered over the different individuals (ID). USED is my binary
    dependent variable which is 1 for used locations and zero for unused
    locations. The other are my predictors.

    I would like to predict the model fit at different values of the
    predictors, but does anyone know whether it is possible to do this? I
    have looked around at the R-sites and in help but it seems that there
    doesn't exist a predict function for lmer???

    I hope someone can help me with this; point me to the right functions or
    tell me to just forget it....

    Thanks in advance!

    Cheers Roel

    Roel May
    Norwegian Institute for Nature Research
    Tungasletta 2, NO-7089 Trondheim, Norway


    [[alternative HTML version deleted]]

    ______________________________________________
    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.
  • Bert Gunter at May 7, 2008 at 5:13 pm
    Sorry, my reply below may be too terse. You'll need to also construct the
    appropriate design matrix to which to apply the fixef() results to.

    If newDat is a data.frame containing **exactly the same named regressor and
    response columns** as your original vdata dataframe, and if me.fit.of is
    your fitted lmer object as you have defined it below, then

    model.matrix(terms(me.fit.of),newDat) %*% fixef(me.fit.of)

    gives your predictions. Note that while the response column in newDat is
    obviously unnecessary for prediction (you can fill it with 0's,say), it is
    nevertheless needed for model.matrix to work. This seems clumsy to me, so
    there may well be better ways to do this, and **I would appreciate
    suggestions for improvement.***


    Cheers,
    Bert



    -----Original Message-----
    From: bgunter
    Sent: Wednesday, May 07, 2008 9:53 AM
    To: May, Roel; r-help at r-project.org
    Subject: RE: [R] predict lmer

    ?fixef

    gets you the coefficient vector, from which you can make your predictions.

    -- Bert Gunter
    Genentech

    -----Original Message-----
    From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
    Behalf Of May, Roel
    Sent: Wednesday, May 07, 2008 7:23 AM
    To: r-help at r-project.org
    Subject: [R] predict lmer

    Hi,

    I am using lmer to analyze habitat selection in wolverines using the
    following model:

    (me.fit.of <-
    lmer(USED~1+STEP+ALT+ALT2+relM+relM:ALT+(1|ID)+(1|ID:TRKPT2),data=vdata,
    control=list(usePQL=TRUE),family=poisson,method="Laplace"))

    Here, the habitat selection is calaculated using a so-called discrete
    choice model where each used location has a certain number of
    alternatives which the animal could have chosen. These sets of locations
    are captured using the TRKPT2 random grouping. However, these sets are
    also clustered over the different individuals (ID). USED is my binary
    dependent variable which is 1 for used locations and zero for unused
    locations. The other are my predictors.

    I would like to predict the model fit at different values of the
    predictors, but does anyone know whether it is possible to do this? I
    have looked around at the R-sites and in help but it seems that there
    doesn't exist a predict function for lmer???

    I hope someone can help me with this; point me to the right functions or
    tell me to just forget it....

    Thanks in advance!

    Cheers Roel

    Roel May
    Norwegian Institute for Nature Research
    Tungasletta 2, NO-7089 Trondheim, Norway


    [[alternative HTML version deleted]]

    ______________________________________________
    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.
  • Kingsford Jones at May 7, 2008 at 8:59 pm
    One question that arises is: at what level is the prediction desired?
    Within a given ID:TRKPT2 level? Within a given ID level? At the
    marginal level (which Bert's code appears to produce). Also, there is
    the question: how confident can you be in your predictions. This
    thread discusses possible ways to get prediction intervals:

    https://stat.ethz.ch/pipermail/r-sig-mixed-models/2008q2/thread.html#841

    Finally, why assume a Poisson error distribution for a binary response?

    Kingsford Jones
    On Wed, May 7, 2008 at 10:13 AM, Bert Gunter wrote:
    Sorry, my reply below may be too terse. You'll need to also construct the
    appropriate design matrix to which to apply the fixef() results to.

    If newDat is a data.frame containing **exactly the same named regressor and
    response columns** as your original vdata dataframe, and if me.fit.of is
    your fitted lmer object as you have defined it below, then

    model.matrix(terms(me.fit.of),newDat) %*% fixef(me.fit.of)

    gives your predictions. Note that while the response column in newDat is
    obviously unnecessary for prediction (you can fill it with 0's,say), it is
    nevertheless needed for model.matrix to work. This seems clumsy to me, so
    there may well be better ways to do this, and **I would appreciate
    suggestions for improvement.***


    Cheers,
    Bert




    -----Original Message-----
    From: bgunter
    Sent: Wednesday, May 07, 2008 9:53 AM
    To: May, Roel; r-help at r-project.org
    Subject: RE: [R] predict lmer

    ?fixef

    gets you the coefficient vector, from which you can make your predictions.

    -- Bert Gunter
    Genentech

    -----Original Message-----
    From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
    Behalf Of May, Roel
    Sent: Wednesday, May 07, 2008 7:23 AM
    To: r-help at r-project.org
    Subject: [R] predict lmer



    Hi,

    I am using lmer to analyze habitat selection in wolverines using the
    following model:

    (me.fit.of <-
    lmer(USED~1+STEP+ALT+ALT2+relM+relM:ALT+(1|ID)+(1|ID:TRKPT2),data=vdata,
    control=list(usePQL=TRUE),family=poisson,method="Laplace"))

    Here, the habitat selection is calaculated using a so-called discrete
    choice model where each used location has a certain number of
    alternatives which the animal could have chosen. These sets of locations
    are captured using the TRKPT2 random grouping. However, these sets are
    also clustered over the different individuals (ID). USED is my binary
    dependent variable which is 1 for used locations and zero for unused
    locations. The other are my predictors.

    I would like to predict the model fit at different values of the
    predictors, but does anyone know whether it is possible to do this? I
    have looked around at the R-sites and in help but it seems that there
    doesn't exist a predict function for lmer???

    I hope someone can help me with this; point me to the right functions or
    tell me to just forget it....

    Thanks in advance!

    Cheers Roel

    Roel May
    Norwegian Institute for Nature Research
    Tungasletta 2, NO-7089 Trondheim, Norway


    [[alternative HTML version deleted]]

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

    ______________________________________________
    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.
  • John Maindonald at May 10, 2008 at 11:37 pm
    The following function is designed to work with a logit link. It can
    easily be generalized to work with any link. The SEs and CIs are
    evaluated accounting for all sources of random variation. The plot
    may not be much help unless there is just one explanatory variate.

    `ciplot` <-
    function(obj=glmm2, data=data.site, xcol=2, nam="litter"){
    cilim <- function(obj, xcol){
    b <- fixef(obj)
    vcov <- summary(obj)@vcov
    X <- unique(model.matrix(obj))
    hat <- X%*%b
    pval <- exp(hat)/(1+exp(hat)) # NB, designed for logit link
    U <- chol(as.matrix(summary(obj)@vcov))
    se <- sqrt(apply(X%*%t(U), 1, function(x)sum(x^2)))
    list(hat=hat, se=se, x=X[,xcol])
    }
    limfo <- cilim(obj, xcol)
    hat <- limfo$hat
    se <- limfo$se
    x <- limfo$x
    upper <- hat+2*se
    lower <- hat-2*se
    ord <- order(x)
    plot(x, hat, yaxt="n", type="l", xlab=nam, ylab="")
    rug(x)
    lines(x[ord], lower[ord])
    lines(x[ord], upper[ord])
    ploc <- c(0.01, 0.05, 0.1, 0.2, 0.5, 0.8, 0.9)
    axis(2, at=log(ploc/(1-ploc)), labels=paste(ploc), las=2)
    }

    ## Usage
    glmm2 <- lmer(rcr ~ litter + (1 | Farm), family=binomial,
    data=data.site)
    ciplot(obj=glmm2)

    John Maindonald email: john.maindonald@anu.edu.au
    phone : +61 2 (6125)3473 fax : +61 2(6125)5549
    Centre for Mathematics & Its Applications, Room 1194,
    John Dedman Mathematical Sciences Building (Building 27)
    Australian National University, Canberra ACT 0200.

    On 8 May 2008, at 8:00 PM, r-help-request@r-project.org wrote:
    From: "May, Roel" <roel.may@nina.no>
    Date: 8 May 2008 12:23:15 AM
    To: r-help@r-project.org
    Subject: [R] predict lmer


    Hi,

    I am using lmer to analyze habitat selection in wolverines using the
    following model:

    (me.fit.of <-
    lmer(USED~1+STEP+ALT+ALT2+relM+relM:ALT+(1|ID)+(1|
    ID:TRKPT2),data=vdata,
    control=list(usePQL=TRUE),family=poisson,method="Laplace"))

    Here, the habitat selection is calaculated using a so-called discrete
    choice model where each used location has a certain number of
    alternatives which the animal could have chosen. These sets of
    locations
    are captured using the TRKPT2 random grouping. However, these sets are
    also clustered over the different individuals (ID). USED is my binary
    dependent variable which is 1 for used locations and zero for unused
    locations. The other are my predictors.

    I would like to predict the model fit at different values of the
    predictors, but does anyone know whether it is possible to do this? I
    have looked around at the R-sites and in help but it seems that there
    doesn't exist a predict function for lmer???

    I hope someone can help me with this; point me to the right
    functions or
    tell me to just forget it....

    Thanks in advance!

    Cheers Roel

    Roel May
    Norwegian Institute for Nature Research
    Tungasletta 2, NO-7089 Trondheim, Norway

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMay 7, '08 at 2:23p
activeMay 10, '08 at 11:37p
posts5
users4
websiter-project.org
irc#r

People

Translate

site design / logo © 2022 Grokbase