# @formula for Lathe.preprocess UndefVarError

**URL:** <https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504>\
**Category:** Statistics\
**Created:** [July 29, 2021, 3:46pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504 "2021-07-29T15:46:05Z")\
**Posts on this page:** 13\
**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:** [July 29, 2021, 3:46pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/1 "2021-07-29T15:46:05Z")

</div>

Hello Everyone:

I am attempting to perform a logistic regression and  
for some reason the @formula macro, is not reading.

```julia

using GLM
using Lathe: preprocess

fm = @formula(Outcome ~ Sesh1 + Shes2 + Sesh3)
logit = glm(fm, train, Binomial(), ProbitLink())

```

I attempted to prefix @formula and there are  
no package conflict warnings. One thing, I have  
been getting some deprecation warnings during  
my Lathe install. But for other sessions I have  
been able to use Lathe package and modules.

Any suggestions?

---

<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:** [July 29, 2021, 3:52pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/2 "2021-07-29T15:52:25Z")

</div>

Could you turn this into an MWE? Or at a minimum say what you mean by “the formula macro is not reading”?

---

<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:** [July 29, 2021, 4:20pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/3 "2021-07-29T16:20:04Z")

</div>

Sure

I am working with the following variables:

```julia
ST = ["Sesh1","Sesh2","Sesh3"]
RT = [1,2,3]
Avail = ["Y", "N"]

```

The data frame I am working with is is structure similar to:

```julia
DBH = DataFrame(Id = 1:400, 
		    SessionTypes = rand(ST, 400),
		     RequestTypes = rand(RT, 400), 
		     Available = rand(Avail, 400))

```

Then am encountering the error when:

```julia
fm = @formula(Available ~ SessionTypes + RequestTypes)
logit = glm(fm, train, Binomial(), ProbitLink())

```

---

<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:** [July 29, 2021, 4:25pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/4 "2021-07-29T16:25:03Z")

</div>

Okay first of all it’d be good if you had a look at [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) to get some tips on how to most effectively ask for help on here - your example isn’t exactly minimal I suppose if your error is related to `@formula`. Also, what is the actual error you’re seeing?

---

<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 29, 2021, 5:13pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/5 "2021-07-29T17:13:39Z")

</div>

Your problem is that `DBH.Available` is a vector of `String`s, not booleans. You can do

```julia
DBH.Available = DBH.Available .== "Y"

```

and your code will work.

GLM should definitely throw a better error in this scenario, though.

---

<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:** [July 29, 2021, 5:36pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/6 "2021-07-29T17:36:35Z")

</div>

Thank you @pdeffebach

Yes – I pre-fixed @ formula before your  
suggestion and received a similar error  
message. Am waiting for my REPL to  
load up in my browser.

Would you apply these instructions in the  
first block as:

```julia
Avail.== "Y"

OR

Avail .== ["Y","N"]

```

If so, the output will vector will auto-assign  
“Y” as 1 or else 0?

Or block two as:

```julia
DBH = DataFrame(Id = 1:400, 
		    SessionTypes = rand(ST, 400),
		     RequestTypes = rand(RT, 400), 
		    Available = rand(Available .== "Y")

```

I want to make sure the DBH.Available vectors are random.

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:** [July 29, 2021, 5:43pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/7 "2021-07-29T17:43:50Z")

</div>

> [@YummyPampers2](#):
>
> ```julia
> Avail.== "Y"
> 
> ```

Definitely this one. The 2nd one will throw an error (which you will soon realize as you try running the command…)

You can also do it on the `DataFrame` construction.

---

<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:** [July 29, 2021, 5:45pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/8 "2021-07-29T17:45:08Z")

</div>

@pdeffebach Appreciated.

What is the logic behind this, how  
does the system know to treat “Y”  
as a bool? Or some other wizardry  
at play?

---

<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 29, 2021, 5:48pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/9 "2021-07-29T17:48:13Z")

</div>

It sounds like you could use a tutorial. or read through the [Julia docs](https://docs.julialang.org/en/v1/). It’s just checking for equality with the string `"Y"`, which returns a `Bool`, as it does in all other languages.

Please read the documentation so you have a better idea what’s going on!

---

<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:** [July 29, 2021, 5:55pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/10 "2021-07-29T17:55:36Z")

</div>

Okay.

From your review, which you may  
have gotten from [HERE](https://stackoverflow.com/questions/37868940/convert-from-string-to-boolean-in-julia) the instantiation  
and data frame construction should  
involve these steps:

```julia
ST = ["Sesh1","Sesh2","Sesh3"]
RT = [1,2,3]
Avail .== "Y" 

```

Then,

```julia
DBH = DataFrame(Id = 1:400, 
		    SessionTypes = rand(ST, 400),
		     RequestTypes = rand(RT, 400), 
		     Available = rand(Avail, 400))

```

The output should replace the “Y” with 1 and  
any other value with 0.

I am still wondering, after reviewing the docs, at  
least on Strings… how the 0 is assigned randomly.  
We did not in this example instantiate “N”.

---

<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 29, 2021, 5:56pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/11 "2021-07-29T17:56:50Z")

</div>

```julia
Avail .== "Y" 

```

Again you need to try your code out. This will result in an error without `Avail` being previously defined.

---

<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:** [July 29, 2021, 5:59pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/12 "2021-07-29T17:59:34Z")

</div>

@pdeffebach Understood.

The way you worded what you said previously,  
made me think in the FIRST block you were  
instantiating Avail as:

```julia
Avail .=="Y"

```

Instead of:

```julia
Avail = ["Y","N"]

```

Then I realized you meant to apply  
Avail .==“Y” to the SECOND block as:

```julia
DBH = DataFrame(Id = 1:400, 
		    SessionTypes = rand(ST, 400),
		     RequestTypes = rand(RT, 400), 
		    Available = rand(Available .== "Y", 400)

```

Now I understand why the equality works and  
before thought it was some obscure instructions.

Thanks.

---

<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:** [July 29, 2021, 6:11pm UTC](https://discourse.julialang.org/t/formula-for-lathe-preprocess-undefvarerror/65504/13 "2021-07-29T18:11:48Z")

</div>

The error read:

```julia
no method matching fit(::Type{GLM.GeneralizedLinearModel}, ::Matrix{Float64}, ::Matrix{Float64}, ::Distributions.Binomial{Float64}, ::GLM.ProbitLink)

```
