https://github.com/cran/bbmle
Raw File
Tip revision: 2fdcf71f4804d26c8df6ee5191e94caa3c1ae9b7 authored by Ben Bolker on 10 January 2011, 19:45:12 UTC
version 0.9.7
Tip revision: 2fdcf71
predict.Rout.save

R version 2.12.1 (2010-12-16)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i486-pc-linux-gnu (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(bbmle)
Loading required package: stats4
Loading required package: numDeriv
Loading required package: lattice
> 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)
> d <- data.frame(x,g,y)
> 
> 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=d)
> 
> 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)
> 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)
> 
> p1 = predict(fit3)
> ## manual prediction
> 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)))
> p2 = c(p2A,p2B)
> all(p1==p2)
[1] TRUE
> 
> 
> 
> proc.time()
   user  system elapsed 
  0.972   0.164   1.080 
back to top