richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
131 stars 48 forks source link

mu vs intercept #133

Closed mattansb closed 5 years ago

mattansb commented 5 years ago

From the manual:

This is due to the Bayesian model centering the covariates before analysis, so the mu parameter is the mean of y rather than the expected value of the response variable when all uncentered covariates are equal to 0.

From what I can tell, only the mu parameter is computed on the centered covariates - e.g., simple slopes in an interaction model are computed on the un-centered covariates:

library(BayesFactor)

set.seed(444)

df <- data.frame(
  X1 = rnorm(50,30,10),
  X2 = rnorm(50,300,100),
  Y = rnorm(50,400,10)
)

df2 <- df
df2$X1 <- df2$X1 - mean(df2$X1)
df2$X2 <- df2$X2 - mean(df2$X2)

BF1 <- lmBF(Y ~ X1*X2, df, progress = FALSE)
BF2 <- lmBF(Y ~ X1*X2, df2, progress = FALSE)

chains1 <- as.data.frame(posterior(BF1,iterations = 10000, progress = FALSE))
chains2 <- as.data.frame(posterior(BF2,iterations = 10000, progress = FALSE))
round(cbind(unlist(lapply(chains1, median)),
            unlist(lapply(chains2, median))),4)
#>             [,1]     [,2]
#> mu      401.5490 401.5243
#> X1        0.7142  -0.1366
#> X2        0.0786   0.0020
#> X1.&.X2  -0.0028  -0.0028
#> sig2     85.5184  85.5462
#> g         0.0680   0.0688

This makes it quite difficult to calculate the predicted value at a given predictor-value. Would it be possible to add the model's intercept to the output of the posterior? Or some center argument that can be set to TRUE/FALSE for all parameters in lmBF etc.?

mattansb commented 5 years ago

If there's a way to recover the intercept, I've actually solved #128 :)