※ 引述《siscom (妹控)》之铭言:
: 大家好
: 最近在上程式语言的课
: 教授请我们跑bootstrap
: 但不使用sample的function 而是自己写function
: 条件为
: Create a function call my.sample with three arguments:
: x:the data to be resampled
: n:number of data points to sample
: Replacement:resample with replacement or not
: Only ‘runif’ ‘order’ and ‘if else’ statements are allowed
: 请问这要怎么撰写呢?
: 想破头还是想不出来
my.sample <- function(x, n, replace = FALSE) {
if (!replace && length(x) < n)
stop("The size must be less than the size of x when replace is TRUE.")
if (replace) {
prob <- 0:length(x)/length(x)
return(x[findInterval(runif(n), prob)])
} else {
return(x[order(runif(length(x)))][1:n])
}
}
findInterval如果不能用就自己用循环做一个吧XD