# Change the dependent variable in GLM linear regression programmatically

**URL:** <https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496>\
**Category:** General Usage\
**Tags:** statistics\
**Created:** [April 14, 2023, 10:16pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496 "2023-04-14T22:16:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rustysinsix](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rustysinsix](https://discourse.julialang.org/u/rustysinsix)\
**Post date:** [April 14, 2023, 10:16pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/1 "2023-04-14T22:16:07Z")

</div>

Hello Everyone, I am new to julia dn i have been trying to run a basic linear regression model using the GLM package, I have found how to programmatically change the independent variables but i am unable to find a way to change the dependent variable  
for example :

I have the following dataframe of adjusted close prices  
||MMM\_AdjClose|TM\_AdjClose|MRK\_AdjClose|GSPC\_AdjClose|RIO\_AdjClose|NYSE\_AdjClose|  
||Float64|Float64|Float64|Float64|Float64|Float64|  
|1|23.7|23.7|23.7|23.7|23.7|23.7|  
|2|22.0|22.0|22.0|22.0|22.0|22.0|  
|3|22.1|22.1|22.1|22.1|22.1|22.1|  
|4|22.0|22.0|22.0|22.0|22.0|22.0|  
|5|22.05|22.05|22.05|22.05|22.05|22.05|

now i am trying to write a function that contains a linear regession which will take the “NYSE\_AdjClose” as the dependent variable and [“MMM\_AdjClose”, “MRK\_AdjClose”] ] as the independent var. what i have found is that the operation runs with the following code

# prcdf is the above dataframe containing all the adjusted close prices

depVar = “NYSE\_AdjClose”  
idVar = [“MMM\_AdjClose”, “MRK\_AdjClose”]  
stMod = lm((Term(:NYSE\_AdjClose) ~ sum(Term.(Symbol.(names(prcdf[:, idVar]))))), prcdf)

however i am trying to find a way to be able to pass the data fo the dependent variable without actually writing the name of the column so that later when i chage the value of depVar i dont have to go to the lm line to change the dependent variable.

Any help will be truly appreciated.

---

<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:** [April 14, 2023, 10:25pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/2 "2023-04-14T22:25:26Z")

</div>

I would create a new formula object

```julia
julia> function form_maker(lhs, rhs::AbstractVector)
           term(lhs) ~ sum(term.(rhs))
       end
form_maker (generic function with 2 methods)

julia> form_maker("y", ["x"])
y(unknown)
x(unknown)

julia> form_maker("z", ["x"])
z(unknown)
x(unknown)

```

---

<div class="post-metadata">

**Author:** ![rustysinsix](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rustysinsix](https://discourse.julialang.org/u/rustysinsix)\
**Post date:** [April 18, 2023, 3:32am UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/3 "2023-04-18T03:32:10Z")

</div>

@ [pdeffebach](https://discourse.julialang.org/u/pdeffebach)  
Thank you for taking the time to answer my question, I am sorry but since i am a total beginner and the only experience i have is working with python may i request you to please elaborate. I truly appreciate your help, because i still keep getting an error when i try to pass the the arguments (tried them as vector, dataframe and as matrix too). I believe it may be because of my lack of understading of some fundamental in Julia. If there is a link you could provide that would be great too.

most of the errors are  
MethodError: no method matching terms(::Float64)  
Closest candidates are:  
terms(::FormulaTerm) at C:\Users\rubez.julia\packages\StatsModels\R1Mh9\src\schema.jl:11  
terms(::InteractionTerm) at C:\Users\rubez.julia\packages\StatsModels\R1Mh9\src\schema.jl:12  
terms(::FunctionTerm) at C:\Users\rubez.julia\packages\StatsModels\R1Mh9\src\schema.jl:13

I truly appreciate your help with this.

---

<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:** [April 18, 2023, 5:14am UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/4 "2023-04-18T05:14:14Z")

</div>

It’ll be easier to help if you provide an MWE that produces the error you’re seeing, but basically the error is telling you that you are trying to create a regression term from a Float64 number somewhere which doesn’t work.

---

<div class="post-metadata">

**Author:** ![mousum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mousum/32/35843_2.png) [@mousum](https://discourse.julialang.org/u/mousum)\
**Post date:** [April 19, 2023, 10:57am UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/5 "2023-04-19T10:57:52Z")

</div>

> [@rustysinsix](#):
>
> stMod = lm((Term(:NYSE\_AdjClose) ~ sum(Term.(Symbol.(names(prcdf[:, idVar]))))), prcdf)

Try with,  
`stMod = lm((Term(Meta.parse(depVar)) ~ sum(Term.(Symbol.(names(prcdf[:, idVar]))))), prcdf)`

Example:

```julia
df = DataFrame(y = randn(4), x1 = randn(4), x2 = randn(4), x3= randn(4))
depVar = "y"
idVars = ["x1", "x3"]
lm((Term(Meta.parse(depVar)) ~ sum(Term.(Symbol.(names(df[:, idVars]))))), df)
lm(@formula(y ~ x1 + x3), df)

```

---

<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:** [April 19, 2023, 2:55pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/6 "2023-04-19T14:55:00Z")

</div>

Again it would be helpful for OP to provide an MWE for his error. There’s no need for `Meta.parse` here, terms can be constructed from strings as Peter has shown in his answer. Your example works just fine as

```julia
lm(term(depVar) ~ sum(term.(idVars)), df)

```

(Note that where Peter put `term(lhs) + sum(term.(rhs))` in his answer he might have meant `~` instead of `+`)

---

<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:** [April 19, 2023, 4:32pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/7 "2023-04-19T16:32:26Z")

</div>

You definitely do not need `Meta.parse`. See `term` in my example above which allows for strings.

---

<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:** [April 19, 2023, 4:32pm UTC](https://discourse.julialang.org/t/change-the-dependent-variable-in-glm-linear-regression-programmatically/97496/8 "2023-04-19T16:32:45Z")

</div>

Apologies, fixed.
