Re: 重複取樣不放回(不使用sample)

看板R_Language作者 (天)時間5年前發表 (2019/10/13 10:31), 5年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.14.59.166 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1570962668.A.FA0.html ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:33:29 ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:34:14 ※ 編輯: celestialgod (119.14.59.166 臺灣), 10/13/2019 18:34:46
文章代碼(AID): #1Telpi-W (R_Language)
討論串 (同標題文章)
本文引述了以下文章的的內容:
3
10
完整討論串 (本文為第 2 之 2 篇):
3
10
文章代碼(AID): #1Telpi-W (R_Language)