首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用boot::boot()引导多个值

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

Stack Overflow用户
提问于 2017-10-18 00:07:50
回答 1查看 625关注 0票数 1

我尝试使用bootstrapping来估计非线性模型的几个参数的置信区间。现在,我分别为每个参数执行引导。因此,我不得不多次修改模型。

下面是一个示例:

代码语言:javascript
复制
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)的置信区间?

EN

回答 1

Stack Overflow用户

发布于 2017-10-18 00:27:52

这个怎么样?

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46794553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档