# Simple Linear Regression: Domain Error with 0.0

**URL:** <https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717>\
**Category:** New to Julia\
**Created:** [March 30, 2022, 3:38am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717 "2022-03-30T03:38:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 3:38am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/1 "2022-03-30T03:38:00Z")

</div>

Greetings Julians:

I have produced a dataframe whose  
columns are all eltype ‘Float64’ as:

```julia
Col1 = rand(1:0.01:500,6)
Col2 = rand(1:0.01:500,6)
Col3 = rand(1:0.01:500,6)
Matr = hcat(Col1,Col2,Col3)

```

I normalized the Matrix as:

```julia
using LinearAlgebra
matr_norm = la.normalize(Matr, 1000)

```

Converted to a dataframe as:

```julia
MetroDF = DataFrame(matr_norm, :auto)

```

I am encountering an issue when I attempted  
to generate a regression model with

```julia
using GLM
ols = lm(@formula(Col3~Col1+Col2), MetroDF)

```

The error reads:

```julia
Failed to show value:
DomainError with 0.0:
FDist: the condition ν2 > zero(ν2) is not satisfied

```

Might anyone have an idea how to  
address this error?

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 30, 2022, 4:54am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/2 "2022-03-30T04:54:14Z")

</div>

When you create the data frame, the names are x1, x2, and x3:

```julia
julia> MetroDF = DataFrame(matr_norm, :auto)
6×3 DataFrame
 Row │ x1 x2 x3       
     │ Float64 Float64 Float64  
─────┼───────────────────────────────
   1 │ 0.136877 0.223588 0.466813
   2 │ 1.0 0.755881 0.545698
   3 │ 0.534322 0.523622 0.177383
   4 │ 0.566349 0.40899 0.710739
   5 │ 0.674702 0.317932 0.329718
   6 │ 0.0358678 0.567678 0.43628

```

so you need to call the linear fit as  
`ols = lm(@formula(x1~x2+x3), MetroDF)`

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [March 30, 2022, 5:00am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/3 "2022-03-30T05:00:08Z")

</div>

Notice how you construct `MetroDF`, yet use `Metro_DF` in the regression. Perhaps you constructed `Metro_DF` another way that makes something (perhaps a component in the F-test?) go to 0.0, which causes the error. Also see the post above by @mcreel.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 10:38am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/4 "2022-03-30T10:38:56Z")

</div>

Thank you – @mcreel

I followed your approach, however, am wondering  
if an imputation I performed to fill missing data  
records or a renaming, had some impact.

The MetroDF is about the same, with the only  
difference being, the column names and some of  
the row values mirroring those nearby. I used:

```julia
Impute.interp(OriginalDF) |> Impute.locf() |> Impute.nocb()

```

There were no missing values after this. However, is  
there a chance the lm(@formula…) operation is treating  
some value as NaN or 0?

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 10:40am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/5 "2022-03-30T10:40:24Z")

</div>

Thank you @amrods – it was a transcription error  
but the original workspace did not have this error.  
Do you think the methods I addressed to @mcreel  
above potentially had some impact? Perhaps the  
scaling (1000) I used during the normalization step?

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 30, 2022, 11:19am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/6 "2022-03-30T11:19:02Z")

</div>

Sorry, can’t tell from this information. You need to provide a MWE as described by [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 5:04pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/7 "2022-03-30T17:04:24Z")

</div>

@mcreel

The original DF (metro) had value set:

![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/411b19390be27348fafcaeb28ab141951b6e7005.png)

I converted all columns to float, as a general quality  
assurance check using the broadcast float function as:

```julia
metro[!, [1,2,3] = float.(metro[!, [1,2,3]])

```

From here I applied the imputation I described as:

```julia
METRO = Impute.interp(metro) |> Impute.locf() |> Impute.nocb()

```

I converted this METRO to a Matrix as:

```julia
METRO_matr = Matrix(METRO)

```

Followed by normalization as:

```julia
using LinearAlgebra
METRO_norm = la.normalize(METRO_matr, 1000) 

```

Then, I converted the matrix to a dataframe as:

```julia
METRO_DF = DataFrame(METRO_norm, :auto)

```

From here, I applied the GLM commands you  
expressed before as:

```julia
ols = lm(@formula(x3~x1+x2), METRO_DF)

```

Which is returning the error I expressed before  
as:

```julia
DomainError with 0.0:
FDist: the condition ν2 > zero(ν2) is not satisfied

```

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 30, 2022, 5:29pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/8 "2022-03-30T17:29:56Z")

</div>

That is saying that the second degrees of freedom of the F(q,n-k) test is not positive. n is the number of observations, and k is the number of regressors, including the constant, 3 in your case. So, it seems that your number of observations is 3 or less. What’s the number of rows of the data frame, after dropping missings? If the screenshot is the entire sample, it is 3, which is in agreement with these comments.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 5:38pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/9 "2022-03-30T17:38:23Z")

</div>

@mcreel – thanks for your explanation here.

The Imputation step I applied does not drop any  
missing values, instead it replaces the records  
with adjacent values (assuming the observations  
are based on the same individual). After the  
imputation, there are 6 rows.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 30, 2022, 5:47pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/10 "2022-03-30T17:47:35Z")

</div>

The columns must not be linearly independent with this replacement strategy.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 5:56pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/11 "2022-03-30T17:56:39Z")

</div>

How might you troubleshoot this?

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 30, 2022, 6:07pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/12 "2022-03-30T18:07:49Z")

</div>

There is a large literature, just search for “rank deficient regression”. There’s no clear best solution to the problem.

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [March 30, 2022, 6:12pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/13 "2022-03-30T18:12:41Z")

</div>

When I run this, it seems to work fine. Can you try running this in a fresh environment to make sure there isn’t something else messing this up?

On a side note, you don’t typically need to normalize in a linear regression like this.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 6:22pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/14 "2022-03-30T18:22:27Z")

</div>

Thank you for your note @junder873

Since each of the columns are linearly  
independent, I thought normalization  
would not confound the regression  
model. You are saying, without this  
process step, given this knowledge,  
I could generate a sensible model?

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [March 30, 2022, 6:40pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/15 "2022-03-30T18:40:08Z")

</div>

[Here](https://stats.stackexchange.com/questions/201909/when-to-normalize-data-in-regression) is a stack overflow answer that does a far better job than I could.

The short version is that an OLS regression really doesn’t care, you could multiply all your values by a billion but the coefficients would stay the same. You can also multiply a single column by any number and the T-stat will remain the same, the coefficient will be scaled by the inverse of what you multiplied it by.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 30, 2022, 6:52pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/16 "2022-03-30T18:52:03Z")

</div>

Thank you @junder873

What I took away from the Stack Overflow stream was  
normalization can help with printability for presentations  
but is not altogether necessary, especially on modern  
machines that perform some standardization by design.

For a general decision-making reference, OLS is invariant  
where normalization will not significantly influence coefficient  
values. Alternatively, tests like Ridge or Lasso are variant, so  
normalization is encouraged for those and similar test  
conditions.

In response to

> Can you try running this in a fresh environment

I restarted the Julia session, started a new  
environment, did not normalize, and am  
getting the same issue as above

```julia
DomainError with 0.0:
FDist: the condition ν2 > zero(ν2) is not satisfied.

```

---

<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 30, 2022, 7:58pm UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/17 "2022-03-30T19:58:52Z")

</div>

Can you run this in a fresh session:

```julia
using DataFrames, Impute, GLM, LinearAlgebra

df = DataFrame(x1 = [missing, 4.15, 4.33, missing, 4.4, missing], 
   x2 = [missing, 58.57, 56.94, missing, 49.4, missing], 
   x3 = [3.0, 4.45, 3.71, 2.6, 3.41, missing])

df = Impute.interp(df) |> Impute.locf() |> Impute.nocb()

df_matrix = Matrix(df)

df = DataFrame(normalize(df_matrix, 1000), :auto)

lm(@formula(x3 ~ x1 + x2), df)

```

With this I get:

```julia
julia> lm(@formula(x3 ~ x1 + x2), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}

x3 ~ 1 + x1 + x2

Coefficients:
────────────────────────────────────────────────────────────────────────────
                   Coef. Std. Error t Pr(>|t|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────────
(Intercept) 0.21061 0.592283 0.36 0.7457 -1.6743 2.09552
x1 -2.04389 6.12876 -0.33 0.7607 -21.5483 17.4606
x2 -0.00233653 0.168541 -0.01 0.9898 -0.538709 0.534036
────────────────────────────────────────────────────────────────────────────

```

So the likeliest explanation is that you’re not actually running the code you’ve posted above.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 31, 2022, 1:00am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/18 "2022-03-31T01:00:59Z")

</div>

The issue I identified prompted by  
your response was that one should  
not use too many predictors when  
evaluating the ols. In my case the  
original DF had 13 columns, and I  
attempted to apply all of them to  
the lm(@formula) instruction.

---

<div class="post-metadata">

**Author:** ![huang\_min](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huang_min/32/34337_2.png) [@huang\_min](https://discourse.julialang.org/u/huang_min)\
**Post date:** [March 31, 2022, 1:32am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/19 "2022-03-31T01:32:03Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 31, 2022, 1:40am UTC](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717/20 "2022-03-31T01:40:46Z")

</div>

@huang_min

I normalized for presentation purposes  
not to change the coefficient outputs from  
the ols.

[Next page](https://discourse.julialang.org/t/simple-linear-regression-domain-error-with-0-0/78717.md?page=2)
