# GLM with multiplicative coefficients

**URL:** <https://discourse.julialang.org/t/glm-with-multiplicative-coefficients/88995>\
**Category:** Statistics\
**Created:** [October 20, 2022, 7:15am UTC](https://discourse.julialang.org/t/glm-with-multiplicative-coefficients/88995 "2022-10-20T07:15:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [October 20, 2022, 7:15am UTC](https://discourse.julialang.org/t/glm-with-multiplicative-coefficients/88995/1 "2022-10-20T07:15:22Z")

</div>

Let’s say I’ve got this dataset, where I’m interested in Weight Gain as a function of calories consumed at Breakfast, Lunch, Dinner.

```julia
4×6 DataFrame
 Row │ Participant Day B L D WG      
     │ String Int64 Int64 Int64 Int64 Float64 
─────┼──────────────────────────────────────────────────
   1 │ Bob 1 65 231 345 0.5
   2 │ Bob 2 300 777 674 -0.3
   3 │ Mary 1 100 856 321 2.0
   4 │ Mary 2 555 845 656 1.0

```

(etc)

I know (for the sake of example) that calories consumed at breakfast might be more readily absorbed than at lunch, or dinner (not participant-specific), and I know that each Participant may have a different general Absorption rate a\_p, which affects all intakes. So I’d like to fit something like this:

WG = a\_p \* (b \* B + l \* L + d \* D)

(maybe with an intercept, but that looks mathematically irrelevant?)

For N participants, that is N + 3 coefficients to fit. How should I fit this in Julia / GLM? Since the equation is linear holding b, l, d constant, I can fit a\_p. Likewise I can fit b, l, d holding a\_p constant. So unless I’m mistaken I could alternatively fit both of these coefficient sets, until convergence. Doing this with `\` looks like a hassle. `@formula` looks appealing, but I’m a total newbie with GLM.jl, and a bit overwhelmed. I can see how I can build a dataframe where I’ve done the A\_p B multiplication in order to fit b, l, d, but urg, is there anything simpler? Any help / pointer / `@formula` code appreciated.

(Side note: I know that this problem is underspecified, so throw in a constraint that `b+l+d=1` if that’s better)

---

<div class="post-metadata">

**Author:** ![Eric](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric/32/25565_2.png) [@Eric](https://discourse.julialang.org/u/Eric)\
**Post date:** [October 29, 2022, 8:32am UTC](https://discourse.julialang.org/t/glm-with-multiplicative-coefficients/88995/2 "2022-10-29T08:32:54Z")

</div>

Your dataset represents panel data on your subject matter; there are observations of the same entities at several times.  
Typically this calls for regression with Fixed Effects regression or multi-level bayesian models. Some books can give you an introduction to these types of models, such as “Introduction to Econometrics” from James H Stock and Mark W. Watson or for Bayesian " Statistical Rethinking" by Richard McElreath, or “Doing Bayesian data analysis” by John Kruschke.  
For the related Julia packages I would look into Fixed Effects ([GitHub - FixedEffects/FixedEffectModels.jl: Fast Estimation of Linear Models with IV and High Dimensional Categorical Variables](https://github.com/FixedEffects/FixedEffectModels.jl)), and Turing ([Turing.jl - Turing.jl](https://turing.ml/stable/)).

---

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [October 29, 2022, 9:56am UTC](https://discourse.julialang.org/t/glm-with-multiplicative-coefficients/88995/3 "2022-10-29T09:56:42Z")

</div>

I suppose you can use workaround: make factor (for example ‘a’) with vector of ones, and use zero-intercept model, something like this: ‘0 + a_F1 + a_F2 + … a\*Fn’ if you have correlated data - look at MixedModels.jl and Metida.jl
