# Usage of lm with Unitful

**URL:** https://discourse.julialang.org/t/usage-of-lm-with-unitful/87394
**Category:** General Usage
**Tags:** glm, unitful
**Created:** [September 17, 2022, 6:25am UTC](https://discourse.julialang.org/t/usage-of-lm-with-unitful/87394 "2022-09-17T06:25:16Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![wheeheee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wheeheee/32/36663_2.png) [@wheeheee](https://discourse.julialang.org/u/wheeheee)
#### Post date: [September 17, 2022, 6:25am UTC](https://discourse.julialang.org/t/usage-of-lm-with-unitful/87394/1 "2022-09-17T06:25:16Z")

</div>

I tried to use `lm` from GLM.jl with units from Unitful, but then received this error below. Is there some way to make this work? Or should I stick to stripping the units first before using `lm`?

> ****
>
> ```julia
> julia> test_df = DataFrame(x=inv.([1u"m", 2u"m", 3u"m"]), y=[1u"m", 2u"m", 3u"m"])
> julia> lm(@formula(x~y), test_df)
> ERROR: promotion of types Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(m^-2,), 𝐋^-2, n
> othing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}} and Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}} fail
> ed to change any arguments
> Stacktrace:
> [1] error(::String, ::String, ::String)
> @ Base .\error.jl:44
> [2] sametype_error(input::Tuple{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(m^-2,),
> 𝐋^-2, nothing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}
> })
> @ Base .\promotion.jl:383
> [3] not_sametype(x::Tuple{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(m^-2,), 𝐋^-2,
> nothing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}} , y::Tuple{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(m^-2,), 𝐋^-2, nothing}}, Quantity{F
> loat64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}, Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}} )
> @ Base .\promotion.jl:377
> [4] promote(x::Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}} , y::Quantity{Float64, 𝐋^-2, Unitful.FreeUnits{(m^-2,), 𝐋^-2, nothing
> }}, z::Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}} , a::Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}} )
> @ Base .\promotion.jl:371
> [5] concrete_term(t::Term, xs::Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}} , #unused#::Type{ContinuousTerm})
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:202
> [6] concrete_term(t::Term, xs::Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}} , #unused#::Nothing)
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:198
> [7] concrete_term(t::Term, dt::NamedTuple{(:x, :y), Tuple{Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}}, Vector{Quantity{In
> t64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}} , hints::Dict{Symbol, Any})
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:184
> [8] (::StatsModels.var"#56#57"{NamedTuple{(:x, :y), Tuple{Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}}, Vector{Quantity{In
> t64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}}, Dict{Symbol, Any}}) (t::Term)
> @ StatsModels .\none:0
> [9] iterate
> @ .\generator.jl:47 [inlined]
> [10] Dict{Term, AbstractTerm}(kv::Base.Generator{Vector{Term}, StatsModels.var"#56#57"{NamedTuple{(:x, :y), Tuple{Vector{Quantity{Float64, 𝐋^-1, Unitful.F
> reeUnits{(m^-1,), 𝐋^-1, nothing}}}, Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}}, Dict{Symbol, Any}}} )
> @ Base .\dict.jl:103
> [11] Schema
> @ C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:37 [inlined]
> [12] schema
> @ C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:123 [inlined]
> [13] schema(f::FormulaTerm{Term, Term}, data::NamedTuple{(:x, :y), Tuple{Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}}, Vect
> or{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}} , hints::Dict{Symbol, Any})
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\schema.jl:126
> [14] ModelFrame(f::FormulaTerm{Term, Term}, data::NamedTuple{(:x, :y), Tuple{Vector{Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(m^-1,), 𝐋^-1, nothing}}},
> Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}} ; model::Type{LinearModel}, contrasts::Dict{Symbol, Any})
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\modelframe.jl:81
> [15] fit(::Type{LinearModel}, f::FormulaTerm{Term, Term}, data::DataFrame, args::Nothing; contrasts::Dict{Symbol, Any}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
> @ StatsModels C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\statsmodel.jl:85
> [16] fit
> @ C:\Users\whee\.julia\packages\StatsModels\s9GMM\src\statsmodel.jl:78 [inlined]
> [17] #lm#5
> @ C:\Users\whee\.julia\packages\GLM\P0Ris\src\lm.jl:184 [inlined]
> [18] lm (repeats 2 times)
> @ C:\Users\whee\.julia\packages\GLM\P0Ris\src\lm.jl:184 [inlined]
> [19] top-level scope
> @ REPL[51]:1
> 
> ```
