# How can I perform GLM regression on column names with spaces?

**URL:** https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442
**Category:** Statistics
**Tags:** dataframes, regression, glm, linear-regression
**Created:** [March 22, 2023, 11:18am UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442 "2023-03-22T11:18:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [March 22, 2023, 11:18am UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/1 "2023-03-22T11:18:37Z")

</div>

Hi all,

very noob question but I can’t find an answer. I’m trying to do a linear regression on some data I have:

```julia
julia> df_under_test[:, [instruction_sym, measure_power_sym, binary_weight_sym]]
1165×3 DataFrame
  Row │ Instruction Base power (W) Binary weight 
      │ String Quantity… Int64         
──────┼───────────────────────────────────────────────────────
    1 │ add_r0_r0_r0_ror_23 0.108844±7.4e-5 W 12
    2 │ add_r0_r0_r0_ror_21 0.108616±7.4e-5 W 11
    3 │ add_r0_r0_r0_ror_22 0.108496±7.3e-5 W 11
    4 │ add_r0_r0_r0_ror_25 0.108466±7.5e-5 W 11
    5 │ add_r0_r0_r0_ror_27 0.108389±7.4e-5 W 12
    6 │ add_r0_r0_r0_ror_18 0.108376±7.6e-5 W 10
    7 │ add_r0_r0_r0_ror_24 0.108354±7.5e-5 W 10
    8 │ add_r0_r0_r0_ror_7 0.108344±7.5e-5 W 11
    9 │ add_r0_r0_r0_ror_9 0.108283±7.6e-5 W 10
   10 │ add_r0_r0_r0_ror_26 0.108269±7.4e-5 W 11
   11 │ add_r0_r0_r0_ror_6 0.108252±7.4e-5 W 10
   12 │ add_r0_r0_r0_ror_20 0.108202±7.5e-5 W 10
   13 │ add_r0_r0_r0_ror_19 0.108196±7.5e-5 W 11
   14 │ add_r0_r0_r0_ror_14 0.10818±7.6e-5 W 11
  ⋮ │ ⋮ ⋮ ⋮
 1152 │ add_r6_r6_r1 0.073308±6.3e-5 W 5
 1153 │ add_r2_r5_r2 0.073302±6.0e-5 W 5
 1154 │ add_r1_r5_r1 0.073237±6.2e-5 W 5
 1155 │ add_r3_r3_r1 0.073213±6.3e-5 W 5
 1156 │ add_r4_r1_r4 0.073161±6.0e-5 W 4
 1157 │ add_r0_r0_r0 0.07305±6.2e-5 W 2
 1158 │ add_r4_r4_r1 0.072969±6.1e-5 W 4
 1159 │ add_r2_r1_r2 0.072941±5.9e-5 W 4
 1160 │ add_r0_r0_r5 0.072929±5.9e-5 W 4
 1161 │ add_r1_r1_r1 0.072881±6.1e-5 W 4
 1162 │ add_r2_r2_r1 0.072863±6.2e-5 W 4
 1163 │ add_r0_r5_r0 0.072772±6.0e-5 W 4
 1164 │ add_r0_r0_r1 0.072522±6.1e-5 W 3
 1165 │ add_r0_r1_r0 0.072425±6.1e-5 W 3
                                             1137 rows omitted

```

I want to correlate the power with the binary weight, like this:

```julia
ols = lm(@formula(measure_power_sym ~ 1 + binary_weight_sym), df_under_test)

```

where `measure_power_sym` and `binary_weight_sym` are the `Symbol`s corresponding to the columns, as you can see in the first listing.

However, this throws

```julia
julia> ols = lm(@formula(measure_power_sym ~ 1 + binary_weight_sym), df_under_test)
ERROR: ArgumentError: There isn't a variable called 'measure_power_sym' in your data; the nearest names appear to be: 
Stacktrace:
 [1] ModelFrame(f::FormulaTerm{Term, Tuple{ConstantTerm{Int64}, Term}}, data::NamedTuple{(:Instruction, Symbol("Base power (W)"), Symbol("Is conditional"), Symbol("Barrel shift amount"), Symbol("Has immediate operand"), Symbol("Immediate amount"), Symbol("Dest reg == source reg"), Symbol("Binary encoding"), Symbol("Binary weight"), :mnemonic), Tuple{SubArray{String, 1, Vector{String}, Tuple{Vector{Int64}}, false}, SubArray{Quantity{Measurement{Float64}, 𝐋^2 𝐌 𝐓^-3, Unitful.FreeUnits{(W,), 𝐋^2 𝐌 𝐓^-3, nothing}}, 1, Vector{Quantity{Measurement{Float64}, 𝐋^2 𝐌 𝐓^-3, Unitful.FreeUnits{(W,), 𝐋^2 𝐌 𝐓^-3, nothing}}}, Tuple{Vector{Int64}}, false}, SubArray{Bool, 1, BitVector, Tuple{Vector{Int64}}, false}, SubArray{Signed, 1, Vector{Signed}, Tuple{Vector{Int64}}, false}, SubArray{Bool, 1, BitVector, Tuple{Vector{Int64}}, false}, SubArray{Int64, 1, Vector{Int64}, Tuple{Vector{Int64}}, false}, SubArray{Bool, 1, BitVector, Tuple{Vector{Int64}}, false}, SubArray{String15, 1, Vector{String15}, Tuple{Vector{Int64}}, false}, SubArray{Int64, 1, Vector{Int64}, Tuple{Vector{Int64}}, false}, SubArray{String, 1, Vector{String}, Tuple{Vector{Int64}}, false}}}; model::Type{LinearModel}, contrasts::Dict{Symbol, Any})
   @ StatsModels ~/.julia/packages/StatsModels/G1ClG/src/modelframe.jl:78
 [2] fit(::Type{LinearModel}, f::FormulaTerm{Term, Tuple{ConstantTerm{Int64}, Term}}, data::SubDataFrame{DataFrame, DataFrames.Index, Vector{Int64}}, args::Nothing; contrasts::Dict{Symbol, Any}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ StatsModels ~/.julia/packages/StatsModels/G1ClG/src/statsmodel.jl:85
 [3] fit
   @ ~/.julia/packages/StatsModels/G1ClG/src/statsmodel.jl:78 [inlined]
 [4] #lm#5
   @ ~/.julia/packages/GLM/4A2DM/src/lm.jl:157 [inlined]
 [5] lm (repeats 2 times)
   @ ~/.julia/packages/GLM/4A2DM/src/lm.jl:157 [inlined]
 [6] top-level scope
   @ REPL[34]:1

```

So, **how should I do regression if the DataFrame’s column names have spaces in them?**

Thanks!

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 22, 2023, 1:16pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/2 "2023-03-22T13:16:37Z")

</div>

Well, the first thing I’d do is to never have a column name with a space 🙂

Otherwise you can try

```julia
lm(@formula(y~ 1 + x), (y=df_under_test[:,measure_power_sym],x=df_under_test[:,binary_weight_sym]))

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [March 22, 2023, 1:47pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/3 "2023-03-22T13:47:27Z")

</div>

You can do `term("outcome var") ~ term("Independent var")`

See [here](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-formula-programmatically)

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [March 22, 2023, 3:59pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/4 "2023-03-22T15:59:27Z")

</div>

> [@tbeason](#):
>
> Well, the first thing I’d do is to never have a column name with a space 🙂

I’m starting to realize its importance, the data source had those and I wanted to keep the naming scheme, but I can patch it up.

> [@tbeason](#):
>
> Otherwise you can try
> 
> ```julia
> lm(@formula(y~ 1 + x), (y=df_under_test[:,measure_power_sym],x=df_under_test[:,binary_weight_sym]))
> 
> ```

Great suggestion!

> [@pdeffebach](#):
>
> You can do `term("outcome var") ~ term("Independent var")`

Great suggestion as well, it’s what I ended up doing by looking at the docs more thorough.

Thank you very much to both!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 22, 2023, 4:09pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/5 "2023-03-22T16:09:09Z")

</div>

If you are getting your data from a csv, there’s a `normalizenames=false` kwarg which you can set to `true` to automatically fix the column names up into something usable.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 22, 2023, 4:13pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/6 "2023-03-22T16:13:22Z")

</div>

If the original names are important, early in the analysis just save them and rename to something usable, but later on for displaying, etc you can restore the original names. I’ve done stuff like that before. You might also be able to use DataFrames metadata for this: [Metadata · DataFrames.jl](https://dataframes.juliadata.org/stable/lib/metadata/)

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [March 22, 2023, 5:06pm UTC](https://discourse.julialang.org/t/how-can-i-perform-glm-regression-on-column-names-with-spaces/96442/7 "2023-03-22T17:06:37Z")

</div>

Wonderful suggestions, thanks!
