微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 ggplot R 中的最大似然函数中加入“sigma”或误差函数和 95% 置信区间?

如何解决如何在 ggplot R 中的最大似然函数中加入“sigma”或误差函数和 95% 置信区间?

我有一个带有 x.number 和 y.size 的数据框 df。我正在尝试使用标准偏差“sigma”来拟合具有 aa、K、Ka、q、c 的自定义 MLL 函数。我已经能够找到估计值,但是如何在结合 sigma 和 95% 置信区间的同时绘制实际函数

y.size <- c(2.69,4.1,8.04,3.1,5.27,5.033333333,3.2,7.25,6.29,4.55,6.1,2.65,3.145,3.775,3.46,5.73,5.31,4.425,3.725,4.32,5,3.09,5.25,5.65,3.48,10,9.666666667,6.06,5.9,2.665,3.816666667,3.69,5.8,3.72,3.045,4.485,3.642857143,5.5,6.333333333,4.75,6,7.466666667,5.03,5.23,4.85,5.59,5.96,5.33,4.92,4.255555556,6.346666667,4.13,6.33,4,7.35,6.35,4.63,5.13,7.4,4.28,4.233333333,4.3125,6.18,4.3,4.47,4.88,4.5,2.96,2.1,3.7,3.62,5.42,3.8,3.27,3.36,3.266666667,2.265,2.51,4.4,2.64,4.38,4.53,2.29,2.87,3.395,3.26,2.77,3.22,4.31,4.73,4.05,4.8,4.7,3.05,4.21,5.95,4.39,4.27,4.955,4.65,3.32,3.828571429,4.69,4.68,3.76,3.91,4.41,4.19,4.733333333,2.83,3.41,4.42,3.47,3.84,4.39)

x.number <- c(69,62,8,80,13,12,2,22,19,49,840,44,31,56,33,58,91,15,86,11,69,24,32,27,1,26,28,1516,41,20,29,14,3,52,92,30,18,38,78,57,17,45,7,37,164,76,82,273,122,662,434,126,374,1017,522,602,191,243,134,70,23,130,306,516,414,236,172,53,50,48,55,296,35,350,97,272,242,170,220,452,270,392,314,150,232)
df <- data.frame(x.number,y.size)
df <- df[df$x.number < 750,]

aa = -0.09 ; K = 100; Ka = 2; q = -4.5; sigma = 0.1; c = 2.5

skewfun <- function(aa,K,Ka,q,sigma,c){
  p <- x.number / K
  lnqp <- if (q == 0) log(p) else (p^q - 1) / q
  y.pred <- (aa * (p * K / Ka - 1) - 1) * lnqp + c
  ll <-   -sum(dnorm(y.size,mean = y.pred,sd = sigma,log=TRUE ))
  ll
}

mle2.model <- mle(skewfun,start = list(aa = -0.09,K = 200,Ka = 2,q= -4.5,sigma = 0.1,c=2.5))
summary(mle2.model)
-logLik(mle2.model)
AIC(mle2.model)


#Trying CI. I tried finding confidence intervals by profiling first and the the CI function as follows
p0 <- profile(mle2.model) 
#I get an error that says Error in onestep(step) : 
 profiling has found a better solution,so original fit had not converged
the next step according to cran handbook is confint(p0) .. but alas!!


#Plot with estimates

aa = -0.16 ; K = 189; Ka = -108; q = 0.015; sigma = 1.16; c = 3.52

ggplot(data=df,aes(x=x.number,y=y.size))+ geom_point(shape=21,fill="white",color="blue",size=3)+ stat_function(fun=function (x.number){p <- x.number / K
  lnqp <- if (q == 0) log(p) else (p^q - 1) / q
  y.pred <- (aa * (p * K / Ka - 1) - 1) * lnqp + c},color = "blue") + ylim(0,10)

目前我的绘图只有带有估计值的原始函数——没有 sigma 或 95% CI 带

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。