insight icon indicating copy to clipboard operation
insight copied to clipboard

find_formula: remove double "+" symbols with lmBF

Open etiennebacher opened this issue 3 years ago • 6 comments
trafficstars

I noticed that using find_formula with lmBF() keeps two consecutive + in the conditional part of the formula. I suppose this is a bug so this PR removes the duplicate + (but I never had an issue with sth like that, it just looked like a bug).

Before:

library(BayesFactor)
library(insight)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)

model <- lmBF(mpg ~ cyl + gear + cyl:gear, mtcars, 
              progress = FALSE, whichRandom = c("gear", "cyl:gear"))

find_formula(model)
#> $conditional
#> mpg ~ cyl + +cyl:gear
#> <environment: 0x000001fe2b14b560>
#> 
#> $random
#> ~gear
#> <environment: 0x000001fe2b14b560>
#> 
#> attr(,"class")
#> [1] "insight_formula" "list"

Created on 2022-05-17 by the reprex package (v2.0.1)

After:

library(BayesFactor)
library(insight)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)

model <- lmBF(mpg ~ cyl + gear + cyl:gear, mtcars, 
              progress = FALSE, whichRandom = c("gear", "cyl:gear"))

find_formula(model)
#> $conditional
#> mpg ~ cyl + cyl:gear
#> <environment: 0x0000020ec4097bd8>
#> 
#> $random
#> ~gear
#> <environment: 0x0000020ec4097bd8>
#> 
#> attr(,"class")
#> [1] "insight_formula" "list"

Created on 2022-05-17 by the reprex package (v2.0.1)

etiennebacher avatar May 17 '22 15:05 etiennebacher

Shouldn't cyl:gear be in the random section too?

bwiernik avatar May 17 '22 16:05 bwiernik

@bwiernik I don't know, I never use this kind of models, I just stumbled upon this while looking at https://github.com/easystats/bayestestR/issues/505 because it seemed to be an issue I could try to solve.

I just assumed that the output of find_formula was good except for this duplicated +. If it's not the case, I will just close this PR

etiennebacher avatar May 17 '22 18:05 etiennebacher

@strengejacke based on the which_random argument, shouldn't the product term be in random too?

bwiernik avatar May 17 '22 18:05 bwiernik

I don't use the BayesFactor package, I think this is something @mattansb implemented?

strengejacke avatar May 17 '22 19:05 strengejacke

@bwiernik yeah, something is off there. I also think I implanted this support for BayesFactor. I will have a chance to take a look later this week / earlier next week.

Thanks @etiennebacher !

mattansb avatar May 17 '22 19:05 mattansb

This seems like a problem with BayesFactor.

  1. Data types (fixed / continuous / random) are per predictor, not per term.
  2. It seems like adding "cyl:gear" as random has no effect on the model?
library(BayesFactor)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)

model1 <- lmBF(mpg ~ cyl + gear + cyl:gear, mtcars, 
              progress = FALSE, whichRandom = c("gear", "cyl:gear"))

model2 <- lmBF(mpg ~ cyl + gear + cyl:gear, mtcars, 
               progress = FALSE, whichRandom = c("gear"))

model2 / model1
#> Bayes factor analysis
#> --------------
#> [1] cyl + gear + cyl:gear : 1 ±0%
#> 
#> Against denominator:
#>   mpg ~ cyl + gear + cyl:gear 
#> ---
#> Bayes factor type: BFlinearModel, JZS

Created on 2022-05-19 by the reprex package (v2.0.1)

mattansb avatar May 19 '22 06:05 mattansb

Okay it looks like the output is correct given the way that BF is implemented

bwiernik avatar Aug 23 '22 14:08 bwiernik

What's the state of this PR?

strengejacke avatar Oct 08 '22 11:10 strengejacke

Codecov Report

Merging #566 (3b6cf10) into main (365a898) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #566   +/-   ##
=======================================
  Coverage   55.10%   55.10%           
=======================================
  Files         124      124           
  Lines       14341    14343    +2     
=======================================
+ Hits         7902     7904    +2     
  Misses       6439     6439           
Impacted Files Coverage Δ
R/find_formula.R 64.56% <100.00%> (+0.09%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov-commenter avatar Oct 08 '22 11:10 codecov-commenter