# How to pass a function in a variable inside a linear regression?

**URL:** <https://discourse.julialang.org/t/how-to-pass-a-function-in-a-variable-inside-a-linear-regression/43793>\
**Category:** New to Julia\
**Tags:** regression, glm\
**Created:** [July 27, 2020, 11:39pm UTC](https://discourse.julialang.org/t/how-to-pass-a-function-in-a-variable-inside-a-linear-regression/43793 "2020-07-27T23:39:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloat/32/16517_2.png) [@pauloat](https://discourse.julialang.org/u/pauloat)\
**Post date:** [July 27, 2020, 11:39pm UTC](https://discourse.julialang.org/t/how-to-pass-a-function-in-a-variable-inside-a-linear-regression/43793/1 "2020-07-27T23:39:57Z")

</div>

Hi, im trying to replicate [this excercises in R](https://ocw.mit.edu/courses/sloan-school-of-management/15-071-the-analytics-edge-spring-2017/linear-regression/assignment-2/detecting-flu-epidemics-via-search-engine-query-data/), but in Julia and wanted to know if there’s a way to do what they did in 2.2, that is, passing a linear model the next way

> linearRegressor = lm(log(dependente variable ) ~ independent variables, dataset)

I had to create a new column in my dataset with

> dataset.column = log.(dataset.column)

to solve the problem, but wanted to know if Julia have a way to do this.

Thank for any help.

---

<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:** [July 28, 2020, 12:06am UTC](https://discourse.julialang.org/t/how-to-pass-a-function-in-a-variable-inside-a-linear-regression/43793/2 "2020-07-28T00:06:00Z")

</div>

Did you try it? It should work.

```julia
julia> lm(@formula(log(y) ~ x), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

:(log(y)) ~ 1 + x

Coefficients:
────────────────────────────────────────────────────────────────────────────
              Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────────
(Intercept) 0.37625 0.0381661 9.85821 <1e-15 0.30051 0.451989
x 0.0331787 0.0735028 0.451394 0.6527 -0.112685 0.179043
────────────────────────────────────────────────────────────────────────────

```

---

<div class="post-metadata">

**Author:** ![pauloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauloat/32/16517_2.png) [@pauloat](https://discourse.julialang.org/u/pauloat)\
**Post date:** [July 28, 2020, 12:12am UTC](https://discourse.julialang.org/t/how-to-pass-a-function-in-a-variable-inside-a-linear-regression/43793/3 "2020-07-28T00:12:31Z")

</div>

Thanks, I had tried originally as

> fm = @formula(log.(y) ~ x)

and that gave an error and didn’t tough was the “.” the cause it.
