Understanding splat operator in return statement

Hi, I followed the DataFrames.jl workshop earlier this week at JuliaCon, and it was my first time using DataFrames in Julia, hence why I am posting in First Steps.

I was able to follow until the last part of the worshop when an example of bootstrapping was shown. The function below computes a probit model on a dataframe and is called on bootstrapped dataframes.

I am having a hard time understading the return statement. I was hoping someone could walk me through all that is happening on that line, especially what happens with the splat … operator and the semi colon.

function boot_sample(df)
    df_boot = df[rand(1:nrow(df), nrow(df)), :]
    probit_boot = glm(@formula(lfp ~ lnnlinc + age + age^2 + educ + nyc + noc + foreign),
                      df_boot, Binomial(), ProbitLink())
    return (; (Symbol.(coefnames(probit_boot)) .=> coef(probit_boot))...)
end
  1. coefnames(probit_boot) returns a Vector of Strings corresponding to "lnnlinc", "age", etc.
  2. Symbol.(coefnames(probit_boot)) casts these strings as Symbols, which is necessary because it uses the (; ags...) constructor for a NamedTuple.
  3. In Julia, a NamedTuple is kind of like a dictionary, in that it can be used to look up objects based on a Symbol. The syntax (; :a => 1, :b => 2) can be used to create the named tuple (a = 1, b = 2). Using => instead of = means you can construct named tuples programmatically, i.e. not writing out the literal a but rather using something like a_sym = :a; (; a_sym => 1).
1 Like

Thanks, Peter, this is exactly what I needed. I was unaware of the named tuple constructor with the semi colon. Very elegant!