首页 > 解决方案 > 数据有 101 个值 为什么结果有 303 个值?

问题描述

这里的数据是“销售”数据,包含两列价格和销售额以及 16 行,例如,

price sales
49    81996
46    91735
50    70830
45    101192
51    78319
47    105369
47    68564
46    95523
49    88834
46    89511
45    107836
52    81410
50    67817
54    59207
50    83310
46    71431

在下面的代码中,使用“dim(my.boot.price)”可以显示输入数据中有 101 个值,但结果显示我们有 303 行。为什么?请更正随附的脚本。

#=== bootstrapping prediction price range
library(boot)

# bootstrap function 
my.boot <- function(formula, data, indices, price) {
  d <- data[indices,] 
  fit <- lm(formula, data=d)

  my.new.data<-data.frame(price)
  pred_interval <- predict(fit, newdata=my.new.data, interval="prediction",
                           level = 0.95)
  colnames(pred_interval)[2:3]<-c("pred.lwr","pred.upr")
  # return the prediction 
  return(pred_interval)
} 

###############################################
# run the bootstrap 
# determine the single value to bootstrap
my.boot.price=data.frame(price=seq(45,55,.1))
dim(my.boot.price)
results <- boot(data=sales, statistic=my.boot, 
                R=2000, formula=sales~price, price=my.boot.price)

# view results
results

标签: rstatistics-bootstrap

解决方案


推荐阅读