繁体   English   中英

使用boot :: boot()引导多个值

[英]Bootstrapping of multiple values using boot::boot()

我尝试使用自举估计非线性模型的几个参数的置信区间。 现在,我为每个参数分别进行引导。 因此,我必须使模型服务器时间更短。

这是一个例子:

library(boot)

# generate some data:
x <- rnorm(300, mean = 5, sd = 2)
y <- xvalues^2*rnorm(300, mean = 1.5, sd = 1) + rnorm(300, mean = 3, sd = 1)
data <- data.frame(x = x, y = y)  

# this is my model: nls(y ~ b1*x^2+b2, data = data, start = list(b1 = 1.5,b2 = 3)) 

# functions for bootstrapping:
getParamB1 <- function(x1, idx){
    data <- x1 %>%
        dplyr::slice(idx) 

    model <- nls(y ~ b1*x^2+b2, data = data, start = list(b1 = 1.5,b2 = 3))

    coef(model)[['b1']]
}

getParamB2 <- function(x1, idx){
    data <- x1 %>%
        dplyr::slice(idx) 

    model <- nls(y ~ b1*x^2+b2, data = data, start = list(b1 = 1.5,b2 = 3))

    coef(model)[['b2']]
}

# Calculate bootstrap confidence intervals
btrpB1 <- boot(data, statistic = getParamB1, R=200)
btrpB2 <- boot(data, statistic = getParamB2, R=200)
ciB1 <- boot.ci(btrpB1)
ciB2 <- boot.ci(btrpB2)

这当然不是很好的代码。 有没有一种方法可以一次估计几个参数(在这里是b1和b2)的置信区间?

这个怎么样?

library(boot)

# generate some data:
x <- rnorm(300, mean = 5, sd = 2)
y <- x^2 * rnorm(300, mean = 1.5, sd = 1) + rnorm(300, mean = 3, sd = 1)
df <- data.frame(x = x, y = y)

m1 <- nls(y ~ b1 * x^2 + b2, data = df, start = list(b1 = 1.5, b2 = 3)) 

boot.coef <- function(mod, data, indices) {
  assign(deparse(mod$data), data[indices, ])
  m <- eval(mod$call)
  return(coef(m))
}

results <- boot(data = df, statistic = boot.coef,
                R = 1000, mod = m1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM