如何在 ggplot2 中绘制来自 frair 包的 mle2 拟合?

How do I plot a mle2 fit from frair package in ggplot2?

所以我试图在 ggplot2 中绘制函数响应曲线,为此我使用了 fair 包中的 frait_fit() 和 frair_boot() 函数(https://cran.r-project.org/web/packages/frair/frair.pdf). 所以我打电话给 frair_fit:

FRAM18.fitII<- frair_fit(eaten~density, data=FRAM18, 
                       response="rogersII", 
                       start = list(a = 1, h = 0.1), 
                       fixed = list(T = 3))

得到这个

> summary(FRAM18.fitII)
             Length Class  Mode     
call          6     -none- call     
x            56     -none- numeric  
y            56     -none- numeric  
response      1     -none- character
xvar          1     -none- character
yvar          1     -none- character
optimvars     2     -none- character
fixedvars     1     -none- character
coefficients  3     -none- numeric  
sample       56     -none- numeric  
fit           1     mle2   S4 

frair_boot 看起来像这样:

FRAM18.bootII <- frair_boot(FRAM18.fitII, nboot=3000)
> summary(FRAM18.bootII)
             Length Class  Mode     
call              6 -none- call     
x                56 -none- numeric  
y                56 -none- numeric  
response          1 -none- character
xvar              1 -none- character
yvar              1 -none- character
optimvars         2 -none- character
fixedvars         1 -none- character
coefficients      3 -none- numeric  
bootcoefs      9000 -none- numeric  
sample       168000 -none- numeric  
n_failed          1 -none- numeric  
n_duplicated      1 -none- numeric  
n_boot            1 -none- numeric  
stratified        1 -none- logical  
fit              11 boot   list

我已尝试使用此问题中概述的预测和熔化函数 只得到:

> FRAM18.fitII2$mle2 <- predict(FRAM18.fitII,newdata=FRAM18.fitII2)
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('frfit', 'list')"

与引导函数输出的结果相似。

我可以使用

创建图表
plot(FRAM18.fitII$x,FRAM18.fitII$y, pch=20, col='skyblue2', xlim=c(0,300))
lines(FRAM18.fitII, col='skyblue3') 

但我想要更具视觉吸引力的东西。

frair 包告诉我它使用 mle2 进行计算,所以我想知道是否还有办法将其输出仍然输入 ggplot2

我的数据是这样的:

density eaten
180 40
180 4
160 70
180 55
100 13
50 16
25 4
15 15
140 46
160 22
25 25
50 0
25 18
160 11
100 6
50 50
100 75
15 9
15 15
140 138
140 140

加载包和数据,进行初始拟合:

library(frair)
library(emdbook)
data(ReedfrogFuncresp)
newfit <- frair_fit(Killed~Initial, data=ReedfrogFuncresp,
                    response="rogersII", 
                    start = list(a = 1, h = 0.1), 
                    fixed = list(T = 1))

这是拟合模型的 predict 方法,它使用 bootstrap 对象(如果提供)来构造置信区间:

#' @param object fitted frair model
#' @param newdata specified densities for prediction; data frame
#' with density column name matching that of original model (\code{object$xvar})
#' @param boot output of \code{frair_boot}
#' @param quantiles for confidence intervals

predict.frfit <- function(object, newdata = NULL, boot = NULL, quantiles = c(0.025, 0.975)) {
    fitfun <- get(object$response, pos = "package:frair")
    if (!is.null(newdata)) {
        newx <- newdata[[object$xvar]]
    } else {
        newx <- object$x
    }
    fitted <- fitfun(newx, as.list(object$coefficients))
    if (is.null(boot)) return(fitted)
    nms <- names(object$coefficients)
    bootvars <- boot$fit$t[,names(boot$fit$t0) %in% nms]
    colnames(bootvars) <- nms
    bootres <- apply(bootvars, 1,
                     function(b) fitfun(newx, as.list(b)))
    envelope <- t(apply(bootres, 1, quantile, quantiles))
    ret <- data.frame(fitted, envelope)
    names(ret) <- c(object$yvar, "lwr", "upr")
    return(ret)
}
library(ggplot2)
pframe0 <- with(ReedfrogFuncresp,
               data.frame(Initial = seq(0, max(Initial), length = 101)))

## basic (predicted value only)
pframe <- data.frame(pframe0, Killed= predict(newfit, newdata=pframe0))

## with confidence intervals
b <- frair_boot(newfit, ncores=3, nboot=250)
pframe_b <- data.frame(pframe0, predict(newfit, newdata=pframe0, boot = b))

ggplot(ReedfrogFuncresp, aes(Initial, Killed)) +
    geom_point() +
    geom_line(data = pframe_b) +
    geom_ribbon(data = pframe_b, aes(ymin=lwr, ymax=upr),
                colour = NA, alpha = 0.4)