Josep Espasa
Josep Espasa

Reputation: 759

Bootstraping weighted functions in Julia

I am trying to use Bootstrap.jl functions to obtain the Standard Error (SE) of a weighted function (e.g. a weighted median).

See below the Bootstrap.bootstrap code to obtain the SE of an unweighted median.

using StatsBase, DataFrames, Bootstrap

v = collect(1:1:20)

bootstrap(median, v, BasicSampling(100))

I would now need to pass a second argument to median above to obtain the SE of the weighted median. Outside of the bootstrap function, this looks like:

w = collect(0.1:0.1:2)
median(v, Weights(w))          

How can I pass a second argument to the median function inside bootstrap to include the weights? Notice that the bootstrap resampling should be applied to both vectors, drawing the same indices for both of them.

Upvotes: 0

Views: 53

Answers (1)

Josep Espasa
Josep Espasa

Reputation: 759

You can pass a DataFrame containing both vectors to the second argument of bootstrap. Then write an anonymous function to use each of the columns within median. E.g.

df = DataFrame(v = collect(1:1:20),
          w = collect(0.1:0.1:2))

bootstrap(d -> median(d[!,:v], Weights(d[!,:w])), df, BasicSampling(100))

Upvotes: 1

Related Questions