问题描述
我为 X 和 Y 数据集开发了一个股票招募模型。从那里我已经能够通过引导程序开发 CI。虽然我可以轻松地从特定 X 值生成 Y 预测,但我无法弄清楚如何确定拟合曲线上特定点的 CI。这样做的最佳方法是什么?
[![带 Bootstrap CI 的拟合模型][1]][1]
这是我的代码:
### Fit the stock recruit model
library(bbmle)
library(nlraa)
Dat43 <- Dat[c(1:19),c(3,4)]
names(Dat43) <- c("X","Y")
bevholt = function(S,a,b) {
S * a/(b + S)
}
maxY <- max(Dat43$Y)
fit43 <- nls(Y ~ bevholt(X,b),start = list(a = maxY,b = maxY),data = Dat43)
###Bootstrap CI's
library(nlraa)
fit43.Bt <- boot_nls(fit43,psim = 3)
fit43.Bt.prms <- na.omit(fit43.Bt$t)
nrb <- length(unique(Dat43$X))
nrp <- nrow(fit43.Bt.prms)
for(i in 1:nrp){
a.i <- fit43.Bt.prms[i,1]
b.i <- fit43.Bt.prms[i,2]
prd.dat[c(1 + (nrb*(i - 1))):c(i * nrb),3] <- bevholt(unique(Dat43$X),a.i,b.i)
}
fit43nls <- nls(Y ~ bevholt(X,data = Dat43)
fit43nls.bt1 <- boot_nls(fit43nls,fitted,psim = 3,R = 999)
lwr1.q <- apply(t(fit43nls.bt1$t),1,quantile,probs = 0.05,na.rm = TRUE)
upr1.q <- apply(t(fit43nls.bt1$t),probs = 0.95,na.rm = TRUE)
ggplot() +
+ geom_point(data = Dat43,aes(x = X,y = Y)) +
+ geom_line(data = Dat43,y = fitted(fit43nls))) +
+ geom_ribbon(aes(x = Dat43$X,ymin = lwr1.q,ymax = upr1.q),+ fill = "purple",alpha = 0.2) +
+ ggtitle("B-H fit with bootstrapped 90% confidence band for the mean function")+
+ ylab("Age 4") + xlab("Age 3") +
+ theme_bw()
###Predict a value
new.3s<-data.frame(X=c(5000))
###> new.3s
### X
###1 5000
predict(fit43,newdata = new.3s)
###[1] 4279.435
[1]: https://i.stack.imgur.com/glrbC.jpg
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)