我有以下代码在执行代码块时运行:
library(boot)
data("FishMercury")
# part a
hist(FishMercury$Mercury)
boxplot(FishMercury$Mercury)
#part b
boot.fish = numeric(10^4)
for(i in 1: 10^4)
{
boot.fish[i] = mean(sample(FishMercury$Mercury, 10, replace = TRUE))
}
boot.fishMean = mean(boot.fish)
boot.fishSD = sd(boot.fish)
quantile(boot.fish, .025, .0975)
#part c
FishMercury2 <- FishMercury[FishMercury < max(FishMercury)]
for (i in 1:n){boot.mean[i] <- mean(sample(FishMercury2, length(FishMercury2),replace = TRUE))}
mean(boot.mean)
sd(boot.mean)
quantile(boot.mean, prob= c(.025,.975))
但是,当我尝试编织成pdf时,出现错误:
Error in eval(expr, envir, enclos) : object 'boot.mean' not found
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
有人可以帮助我解决此问题吗?
答案 0 :(得分:0)
您的代码#part c
中缺少一些内容,这会导致错误。
#part c
FishMercury2 <- FishMercury[ FishMercury < max( FishMercury)]
# first error, you use an uninitiated object n
n <- length( FishMercury2)
# second error: you are replacing elements in boot.mean, but never initiated boot.mean.
# so, for example, create one:
boot.mean <- numeric( length = n)
for (i in 1: n){
boot.mean[i] <- mean(sample(FishMercury2, length(FishMercury2),replace = TRUE))
}
mean(boot.mean)
sd(boot.mean)
quantile(boot.mean, prob= c(.025,.975))