11 個のキーワードで学ぶ統計学入門 11 of 11 / ブートストラップ法

すでに存在しているデータからそのデータと同じ個数だけ無作為に抽出したデータでデータセットをいくつか作る。重複抽出を許容するので、元のデータを並べ替えるのとは異なる。

  • Calculator の場合:

randSamp(リスト, 試行回数, 0) は重複を許容する。これがデフォルト。
randSamp(リスト, 試行回数, 1) は重複を許容しない。

rand_choice(list,numofsets):=seq(randSamp(list,dim(list)),numofsets,1,numofsets)

実行結果:
5 要素のデータセットから新たに 5 要素のデータセットを 10 個作る。
f:id:ti-nspire:20180510125048p:plain:w400
 

  • Nspired Lua で実行する場合:
function randChoice(list, numOfSets)
   local temp = "{"..table.concat(list,",").."}"
   return math.eval(string.format("rand_choice(%s, %s)", temp, numOfSets))
end

seq1 = {10,20,30,40,50}
function on.resize()
   local sol = randChoice(seq1, 10)
   for i = 1, #sol do
      print(table.concat(sol[i],", "))
   end
end

実行結果:
f:id:ti-nspire:20180510125553p:plain
 

  • Lua だけで組む場合:
function randChoice(seq, num)
    local booted = {}
    for j = 1, num do
       local rnd = {}
       for i = 1, #seq do
          local pos = math.random(1, #seq)
          rnd[i] = seq[pos]  
       end
       booted[j] = rnd 
    end
    return booted
end

Booted = randChoice({10,20,30,40,50}, 10)
for i = 1, #Booted do
   print(table.concat(Booted[i],", "))
end

実行結果:
f:id:ti-nspire:20180510125731p:plain
――――――――――――――――――――――――――
Wolfram の場合:

s1={10,20,30,40,50};
numOfSets=10;
n=Length[s1];
randList[dim_]:=RandomInteger[{1,dim},dim];
Table[s1\[LeftDoubleBracket]randList[n] 〛,{m,1,numOfSets }];
MatrixForm[%] 

実行結果:
f:id:ti-nspire:20180510134322p:plain:w400
――――――――――――――――――――――――――
Excel の場合:
不明