# Error when using fit(LinearModel,...) through GLM

**URL:** https://discourse.julialang.org/t/error-when-using-fit-linearmodel-through-glm/5781
**Category:** General Usage
**Tags:** glm
**Created:** [September 8, 2017, 9:48am UTC](https://discourse.julialang.org/t/error-when-using-fit-linearmodel-through-glm/5781 "2017-09-08T09:48:59Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [September 8, 2017, 9:48am UTC](https://discourse.julialang.org/t/error-when-using-fit-linearmodel-through-glm/5781/1 "2017-09-08T09:48:59Z")

</div>

I want to perform a simple linear regression using this code:

`fit(LinearModel, @formula(Y ~ t + t2 + t3), regdata)`

I get the following error message:

```julia
MethodError: no method matching getindex(::DataFrames.DataFrame, ::DataArrays.DataArray{Any,1}, ::Colon)
#ModelFrame#124(::Dict{Any,Any}, ::Type{T} where T, ::DataFrames.Terms, ::DataFrames.DataFrame) at formula.jl:320
(::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.ModelFrame}, ::DataFrames.Terms, ::DataFrames.DataFrame) at <missing>:0
#ModelFrame#127(::Array{Any,1}, ::Type{T} where T, ::DataFrames.Formula, ::DataFrames.DataFrame) at formula.jl:333
(::Core.#kw#Type)(::Array{Any,1}, ::Type{DataFrames.ModelFrame}, ::DataFrames.Formula, ::DataFrames.DataFrame) at <missing>:0
#fit#153(::Dict{Any,Any}, ::Array{Any,1}, ::Function, ::Type{GLM.GeneralizedLinearModel}, ::DataFrames.Formula, ::DataFrames.DataFrame) at statsmodel.jl:52
#glm#17(::Array{Any,1}, ::Function, ::DataFrames.Formula, ::DataFrames.DataFrame) at glmfit.jl:317
glm(::DataFrames.Formula, ::DataFrames.DataFrame) at glmfit.jl:317
include_string(::String, ::String) at loading.jl:515
include_string(::String, ::String, ::Int64) at eval.jl:30
include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at eval.jl:34
(::Atom.##49#52{String,Int64,String})() at eval.jl:50
withpath(::Atom.##49#52{String,Int64,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:38
macro expansion at eval.jl:49 [inlined]
(::Atom.##48#51{Dict{String,Any}})() at task.jl:80

```

This is my matrix:

```julia
10×4 DataFrames.DataFrame
│ Row │ Y │ t │ t2 │ t3 │
├─────┼───────┼──────┼─────────┼────────────┤
│ 1 │ 82.21 │ 1981 │ 3924361 │ 7774159141 │
│ 2 │ 83.67 │ 1982 │ 3928324 │ 7785938168 │
│ 3 │ 84.8 │ 1983 │ 3932289 │ 7797729087 │
│ 4 │ 86.25 │ 1984 │ 3936256 │ 7809531904 │
│ 5 │ 88.08 │ 1985 │ 3940225 │ 7821346625 │
│ 6 │ 87.55 │ 1986 │ 3944196 │ 7833173256 │
│ 7 │ 88.45 │ 1987 │ 3948169 │ 7845011803 │
│ 8 │ 86.99 │ 1988 │ 3952144 │ 7856862272 │
│ 9 │ 86.3 │ 1989 │ 3956121 │ 7868724669 │
│ 10 │ 88.8 │ 1990 │ 3960100 │ 7880599000 │

```

Can somebody help me fix the problem? I do not quite understand the error message.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 19, 2021, 6:14pm UTC](https://discourse.julialang.org/t/error-when-using-fit-linearmodel-through-glm/5781/2 "2021-08-19T18:14:59Z")

</div>

The following works but the coefficients estimated have large errors:

```julia
using DataFrames, GLM

t = 1981:1990
n = length(t)
t2 = sort(rand(4_000_000:4_001_000, n))
t3 = sort(rand(7_000_000_000:7_000_010_000, n))
bᵢ = [π, 2, 0.5, 0.1]
Y = bᵢ[1] .+ bᵢ[2]*t + bᵢ[3]*t2 + bᵢ[4]*t3

regdata = DataFrame(Y=Y, t=t, t2=t2, t3=t3)
lm = fit(LinearModel, @formula(Y ~ t + t2 + t3), regdata, dropcollinear=false)
coeff_fit = coef(lm)

```

For multi-linear regression with such heterogeneous data, it is recommended to scale first the input data and then to re-scale the obtained coefficients back using [published formulas](http://home.iitk.ac.in/~shalab/regression/Chapter3-Regression-MultipleLinearRegressionModel.pdf).
