# Understanding Linear Regression and finding R-Squared

**URL:** <https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079>\
**Category:** Statistics\
**Created:** [March 17, 2020, 6:15am UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079 "2020-03-17T06:15:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 6:15am UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/1 "2020-03-17T06:15:32Z")

</div>

I entered this dataframe and the GLM function below:

```julia
DEmployment = [0.036327933,0.034107058,0.030691709,0.029333333,-0.008635579,0.010452962,0.017672414,0.024565862,0.030177759,0.01565008,0.02094034,0.011609907,0.00841622,-0.012139605,0.006184067,0.006869125,0.008617594,-0.004271983,0.002860207,0.01426025,-0.006678383,-0.009200283,-0.003928571,0.000358551,0.006810036,0.005695977,-0.001769912,-0.007446809,0.009646302,0.009907997,0.001751927,0.036327933]

DBirths = [-0.014116817,-0.028458922,0.00036846,-0.010865562,-0.025507354,-0.007260222,-0.011932256,-0.002337359,-0.012495119,0,-0.012653223,0.057669203,0.007194245,0.030075188,-0.018691589,0.00952381,-0.009433962,0.00952381,0.028301887,0.027522936,0.008928571,0,-0.017699115,-0.009009009,0,-0.018181818,0,-0.009259259,-0.009345794,-0.028301887,-0.019417476,-0.028458922]

df = DataFrame(A=DEmployment, B=DBirths)

using GLM
ols = lm(@formula(A ~ B), df)

```

I recived this output

```julia
A ~ 1 + B

Coefficients:
──────────────────────────────────────────────────────────────────────────
             Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────────────────
(Intercept) 2.5 1.11803 2.23607 0.1548 -2.31051 7.31051
B: M 0.0 1.58114 0.0 1.0000 -6.80309 6.80309
──────────────────────────────────────────────────────────────────────────

```

Does B:M, refer to the probability of the slope? Where do I find R^2 for the relationship between A and B?

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [March 17, 2020, 7:12am UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/2 "2020-03-17T07:12:35Z")

</div>

```julia
?r2

```

---

<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 17, 2020, 1:44pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/3 "2020-03-17T13:44:28Z")

</div>

I don’t think the output you’re showing is produced by the inputs you’ve posted above. Both `DBirths` and `DEmployment` are just floating point numbers, but your output `B: M` suggests that you have a categorical variable where one of the levels is `M`.

As @Mattriks says above the R-squared of the model can be obtained by calling

```julia
julia> r2(ols)
0.0685632968228097

```

This is also explained in [the docs here](https://juliastats.org/GLM.jl/stable/manual/#Methods-applied-to-fitted-models-1), which I recommend you read if you want to work with the GLM package.

Your question about the interpretation of the `B: M` coefficient suggest that it would also be helpful to consult introductory level statistics or econometrics textbooks. A popular one that I’ve used myself to teach first year undergrads is Wooldridge’s Introductory Econometrics.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 17, 2020, 2:47pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/4 "2020-03-17T14:47:08Z")

</div>

I agree with @nilshg that something’s not right here. I ran your code, exactly as posted, and I got the following output:

```julia
julia> ols = lm(@formula(A ~ B), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

A ~ 1 + B

Coefficients:
────────────────────────────────────────────────────────────────────────────────
                Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────────────
(Intercept) 0.00946811 0.00249931 3.7883 0.0007 0.00436385 0.0145724
B -0.194064 0.130591 -1.48604 0.1477 -0.460767 0.0726395
────────────────────────────────────────────────────────────────────────────────

```

I’m not an expert in this arena but I’ve spent a lot of time re-teaching myself linear regression and I’ve actually been spending a lot of time recently trying to learn generalized linear models at a deeper level so I think I can contribute having been in your shoes.

Regarding the interpretation of the estimates for the coefficients, there are loads of great resources online for learning about linear regression. I usually don’t like telling people to “just Google it” but I think this is one of those cases where you should just Google it and start reading. You need to not only understand what the coefficients mean, but you also need to understand the rest of the statistics in the coefficient table. For example, it’s really important in this case that you know how to interpret the p-value for your coefficient B, reported in the table under the column `Pr(>|t|)` (hint: can you say with confidence that the coefficient for B is not 0?).

Regarding R2 specifically, there is the `r2()` function but you can also compute this yourself quite easily in this case, which I think will help you gain a deeper understanding of what R2 is actually measuring:

```julia
julia> r2(ols)
0.0685632968228097 # this is the GLM r2 function

```

To understand how to compute this manually, I would recommend reading [this](https://en.wikipedia.org/wiki/Coefficient_of_determination). Here’s the code to do it:

```julia
using Statistics

# Total sum of squares
SSₜ = sum((DEmployment .- mean(DEmployment)).^2)

# Regression sum of squares
SSᵣ = sum((predict(ols) .- mean(DEmployment)).^2)

julia> R² = SSᵣ / SSₜ
0.06856329682280965 # this matches the r2 value above

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 2:51pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/5 "2020-03-17T14:51:51Z")

</div>

> [@nilshg](#):
>
> you’re showing is produced by the inputs you’ve posted above. Both `DBirths` and `DEmployment` are just floating point numbers, but your output `B: M` suggests that you have a categorical variable where one of the levels is `M` .

Thanks that explained it, although I’m getting 0.00, wherease your answer looks like what I got in Excel.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 3:05pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/6 "2020-03-17T15:05:02Z")

</div>

> [@mthelm85](#):
>
> SSₜ = sum((DEmployment .- mean(DEmployment)).^2)

what package did you use to find the mean?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 17, 2020, 3:08pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/7 "2020-03-17T15:08:07Z")

</div>

Statistics:

`using Statistics`

It’s a standard Julia library so you shouldn’t have to Pkg.add it.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 3:10pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/8 "2020-03-17T15:10:24Z")

</div>

> [@mthelm85](#):
>
> ols = lm(@formula(A ~ B), df)

They must be, but I don’t know how. I ran the lm formula again, and got the same output. I also get the same r2 value. So that’s good, although I’d like to know what I did wrong the first time.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 4:18pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/9 "2020-03-17T16:18:56Z")

</div>

OK, thanks. would doing a signed rank test mess up the regression?

---

<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 17, 2020, 5:49pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/10 "2020-03-17T17:49:23Z")

</div>

no. it’s not clear how you got `B : M` but that wouldn’t have caused your problem.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 17, 2020, 6:08pm UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/11 "2020-03-17T18:08:38Z")

</div>

It works! and I should have known to add using Statistics. That’s why it said that mean was undefined.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 24, 2020, 12:15am UTC](https://discourse.julialang.org/t/understanding-linear-regression-and-finding-r-squared/36079/12 "2020-03-24T00:15:05Z")

</div>

do you mean B= {b|M}
