RMarkdown knit fail when works in editor

Hi

Please help with below erratic error in Rmarkdown.

When I run the R & Rmarkdown below which is based on the example code from bbmle vignette p21, it works within the source editor and plots a Square Root of deviance chart similar to those from the example code.
However, when I knit to HTML this code fails with the message:

Line 186 Error inUseMethod(”profile”) :
no applicable method
for ‘profile' applied
to an object of class
“mle2” calls:
... handle
-> withcallingHandlers
-> withvisible -> eval
-> eval Execution
halted

However, profile is shown in the bbmle example and so I dont understand this error or why the same code works fine when testing in the source editor.

Package bbmle2
M12 dataframe:

"spd" "test"
-0.192 244
-0.0920000000000001 850
-0.0559999999999998 2800
-0.40 95
-0.31 490

n.max <- max(M12$test)
n.min <- min(M12$test)
k=10
(fit<-mle2(test~dpois((n.max - n.min)/(1+exp(-(k*spd))) + n.min),data=M12,start=list(k=10)))

p1 <- suppressWarnings(profile(fit))
d1 <- as.data.frame(p1)
library(lattice)
xyplot(abs(z)~focal|param,data=d1,
subset=abs(z)<3,
type="b",
xlab="",
ylab=expression(paste(abs(z),
" (square root of ",Delta," deviance)")),
scale=list(x=list(relation="free")))

A complete reproducible example, called a reprex will help draw other answers. You can cut and paste it from page 21 of the manual (there's no vignette).

library(bbmle)
#> Loading required package: stats4
    x <- 0:10
    y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
    d <- data.frame(x,y)
    ## we have a choice here: (1) don't impose boundaries on the parameters,
    ##  put up with warning messages about NaN values:
    fit1 <- mle2(y~dpois(lambda=ymax/(1+x/xhalf)),
         start=list(ymax=1,xhalf=1),
         data=d)
#> Warning in dpois(x = c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8), lambda =
#> c(15.9069797531213, : NaNs produced
#> Warning in dpois(x = c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8), lambda =
#> c(33.2333275588048, : NaNs produced
    p1 <- suppressWarnings(profile(fit1))
    plot(p1,main=c("first","second"),
         xlab=c(~y[max],~x[1/2]),ylab="Signed square root deviance",
         show.points=TRUE)

    suppressWarnings(confint(fit1)) ## recomputes profile
#>           2.5 %    97.5 %
#> ymax  17.885691 34.619764
#> xhalf  1.663451  6.479047
    confint(p1)  ## operates on existing profile
#>           2.5 %    97.5 %
#> ymax  17.885691 34.619764
#> xhalf  1.663451  6.479047
    suppressWarnings(confint(fit1,method="uniroot"))
#>           2.5 %   97.5 %
#> ymax  17.881505 34.61863
#> xhalf  1.661424  6.47848
    ## alternatively, we can use box constraints to keep ourselves
    ##  to positive parameter values ...
    fit2 <- update(fit1,method="L-BFGS-B",lower=c(ymax=0.001,xhalf=0.001))
    ## Not run:
    p2 <- profile(fit2)
    plot(p2,show.points=TRUE)

    ## but the fit for ymax is just bad enough that the spline gets wonky
    confint(p2)  ## now we get a warning
#>           2.5 %    97.5 %
#> ymax  17.885796 34.619764
#> xhalf  1.663472  6.479015
    confint(fit2,method="uniroot")
#>           2.5 %    97.5 %
#> ymax  17.881506 34.618626
#> xhalf  1.661407  6.478479
    ## bobyqa is a better-behaved bounded optimizer ...
    ##  BUT recent (development, 2012.5.24) versions of
    ##    optimx no longer allow single-parameter fits!
    if (require(optimx)) {
      fit3 <- update(fit1,
          optimizer="optimx",
          method="bobyqa",lower=c(ymax=0.001,xhalf=0.001))
       p3 <- profile(fit3)
       plot(p3,show.points=TRUE)
      confint(p3)
    }
#> Loading required package: optimx
#> Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
#> logical.return = TRUE, : there is no package called 'optimx'

Created on 2019-03-11 by the reprex package (v0.2.1)

I was unable to reproduce the error on Mojave. The script runs identically in R console and in R Studio knitting to the distill (nee radix) template.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.