# Generating the equation of a curve across attributes

**URL:** <https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617>\
**Category:** New to Julia\
**Tags:** dataframes, regression\
**Created:** [October 29, 2021, 11:59am UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617 "2021-10-29T11:59:58Z")\
**Posts on this page:** 8\
**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:** [October 29, 2021, 11:59am UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/1 "2021-10-29T11:59:59Z")

</div>

Good Day Folks,

I have a dataframe that constructed as

```julia
DF = Dataframe(Price= rand(70:5:170,10), Col1 = rand(1:1:50,10), Col2 = rand(1:1:30,10), Col3 = rand(1:5:100))

```

I would like to treat Price as the outcome  
variable, and the other attributes as the  
inputs so that

Price = [Coefficient1]\*Col1 + [Coefficient2]\*Col2 + [Coefficient3]\*Col3

How might I create the regression line with coefficients?

Thank you,

---

<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:** [October 29, 2021, 2:12pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/2 "2021-10-29T14:12:20Z")

</div>

[GLM.jl](https://juliastats.org/GLM.jl/stable/) will do that

---

<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:** [October 29, 2021, 2:34pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/3 "2021-10-29T14:34:34Z")

</div>

Or if for some reason you don’t want to use a package:

```julia
X = [df.Col1 df.Col2 df.Col3]
Y = df.Price

β = X \ Y

```

and if you want the intercept

```julia
β = hcat(ones(size(X,1)), X) \ Y

```

---

<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:** [October 29, 2021, 5:46pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/4 "2021-10-29T17:46:04Z")

</div>

Thank you for this.

Could I then apply the coefficient as

```julia
βCol1 + βCol2 + βCol3 + randn()

```

This works for polynomial functions?

Thank you again,

---

<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:** [October 29, 2021, 6:50pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/5 "2021-10-29T18:50:38Z")

</div>

`β` is a vector so you can index into it with square brackets `[]` as discussed [here](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing). So, after this `β = hcat(ones(size(X,1)), X) \ Y` you could do something like:

```julia
ŷ(x) = β[1] + sum(β[2:4] .* x)

julia> ŷ([49, 6, 96])
85.40338136038268

```

The `\` operator in this case is solving a linear system of equations. For polynomials, have a look at [`fit` in Polynomials.jl](https://juliamath.github.io/Polynomials.jl/stable/reference/#Polynomials.fit).

---

<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:** [October 29, 2021, 10:49pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/6 "2021-10-29T22:49:35Z")

</div>

As:

```julia
using GLM
ols = lm(@formula("y ~.", data)

```

This assume Y is in the left-most column, yes?

Thanks,

---

<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:** [October 29, 2021, 11:01pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/7 "2021-10-29T23:01:54Z")

</div>

Did you read the documentation? That will throw an error.

```julia
julia> df = DataFrame(y = rand(10), x1 = rand(10), x2 = rand(10));

julia> ols = lm(@formula("y ~ .", df))
ERROR: LoadError: MethodError: no method matching var"@formula"(::LineNumberNode, ::Module, ::String, ::Symbol)
Closest candidates are:
  var"@formula"(::LineNumberNode, ::Module, ::Any) at /Users/peterdeffebach/.julia/packages/StatsModels/m1jYD/src/formula.jl:60
in expression starting at REPL[5]:1

```

please read the documentation before trying out code. And when you try out code, please think about why it errors.

---

<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:** [October 29, 2021, 11:12pm UTC](https://discourse.julialang.org/t/generating-the-equation-of-a-curve-across-attributes/70617/9 "2021-10-29T23:12:41Z")

</div>

Issue identified – with only looking at the instructions I wrote –  
that the SYMBOLS were numeric and needed to be  
renamed. THEN implement the @formula(Y~X…) using  
those SYMBOL names.
