Change the dependent variable in GLM linear regression programmatically

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

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.

I would create a new formula object

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

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

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

@ 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.

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.

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


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)

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

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 +)

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

Apologies, fixed.