predict-methods            package:bbmle            R Documentation

_P_r_e_d_i_c_t_e_d _v_a_l_u_e_s _f_r_o_m _a_n _m_l_e_2 _f_i_t

_D_e_s_c_r_i_p_t_i_o_n:

     Given an 'mle2' fit and an optional list of new data, return
     predictions (more generally, summary statistics of the predicted
     distribution)

_U_s_a_g_e:

         ## S4 method for signature 'mle2':
         predict(object, newdata=NULL,
                              location="mean", newparams=NULL, ...)
         ## S4 method for signature 'mle2':
         simulate(object, nsim,
                              seed, newdata=NULL, newparams=NULL, ...)
         ## S4 method for signature 'mle2':
         residuals(object,type=c("pearson","response"),
                        location="mean",...)

_A_r_g_u_m_e_n_t_s:

  object: an mle2 object

 newdata: optional list of new data

newparams: optional vector of new parameters

location: name of the summary statistic to return

    nsim: number of simulations

    seed: random number seed

    type: residuals type

     ...: additional arguments (for generic compatibility)

_M_e_t_h_o_d_s:


     _x = "_m_l_e_2" an 'mle2' fit

_E_x_a_m_p_l_e_s:

     set.seed(1002)
     lymax <- c(0,2)
     lhalf <- 0
     x <- runif(200)
     g <- factor(rep(c("a","b"),each=100))
     y <- rnbinom(200,mu=exp(lymax[g])/(1+x/exp(lhalf)),size=2)
     dat <- data.frame(y,g,x)

     fit3 <- mle2(y~dnbinom(mu=exp(lymax)/(1+x/exp(lhalf)),size=exp(logk)),
         parameters=list(lymax~g),
         start=list(lymax=0,lhalf=0,logk=0),
     data=dat)

     plot(y~x,col=g)
     ## true curves
     curve(exp(0)/(1+x/exp(0)),add=TRUE)
     curve(exp(2)/(1+x/exp(0)),col=2,add=TRUE)
     ## model predictions
     xvec = seq(0,1,length=100)
     lines(xvec,predict(fit3,newdata=list(g=factor(rep("a",100),levels=c("a","b")),
                                     x = xvec)),col=1,lty=2)
     lines(xvec,predict(fit3,newdata=list(g=factor(rep("b",100),levels=c("a","b")),
                                     x = xvec)),col=2,lty=2)

     ## comparing automatic and manual predictions
     p1 = predict(fit3)
     p2A =
     with(as.list(coef(fit3)),exp(`lymax.(Intercept)`)/(1+x[1:100]/exp(lhalf)))
     p2B =
     with(as.list(coef(fit3)),exp(`lymax.(Intercept)`+lymax.gb)/(1+x[101:200]/exp(lhalf)))
     all(p1==c(p2A,p2B))
     ##
     simulate(fit3)

