You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在R语言BAS模型中指定自定义零模型并调整模型顺序

Got it, let's work through this to get your bas.lm setup exactly how you want it — with the model containing only Predictor1/2/3 as the baseline (BF=1) at the top of your summary.

First, Fix the include.always Syntax

Your current include.always has a small syntax issue: it shouldn’t include the response variable (Mean_Accuracy). The parameter only needs the predictors you want to force into every model. Correct it to:

include.always = ~ Predictor1 + Predictor2 + Predictor3

This locks in those three predictors, so your model space only has two possibilities: the baseline (no NewPredictor) and the extended model (baseline + NewPredictor).

Understand How bestmodel Maps to Your Predictors

The bestmodel parameter is a binary vector where each element corresponds to a predictor in your main formula (Mean_Accuracy ~ .). The order matches the column order of your predictors in the data frame:

  1. Predictor1
  2. Predictor2
  3. Predictor3
  4. NewPredictor

Since you want to start sampling from your baseline model (exclude NewPredictor), set bestmodel to c(1, 1, 1, 0) — the first three 1s enforce the forced predictors, and the 0 excludes NewPredictor.

Full Corrected Code

Here’s the updated code with all fixes:

# Your data (for reference)
df <- data.frame(
  Mean_Accuracy = c(0.73, 0.50, 0.86),
  Predictor1 = c(3, 4, 1),
  Predictor2 = c(7, 5, 3),
  Predictor3 = c(6, 1, 4),
  NewPredictor = c(8, 5, 1)
)

# Run the model with correct parameters
Accuracy <- bas.lm(
  Mean_Accuracy ~ ., 
  data = df, 
  method = "BAS", 
  bestmodel = c(1, 1, 1, 0),  # Initialize with baseline model
  prior = "JZS", 
  include.always = ~ Predictor1 + Predictor2 + Predictor3,  # Force these predictors
  modelprior = uniform()
)

# Check the summary
summary(Accuracy)

Why This Works

  • The corrected include.always ensures Predictor1/2/3 are in every model, so only NewPredictor is optional.
  • Setting bestmodel = c(1,1,1,0) starts the MCMC sampling from your baseline model. With only two possible models, this will make the baseline appear first in the summary (with BF=1, as it’s the reference point).
  • The second model (with NewPredictor) will show its Bayes Factor relative to this baseline, which is exactly what you’re aiming for.

Quick Note

If for some reason the baseline still doesn’t show up first, you can manually reorder the models in the output, but with such a small model space, the bestmodel initialization should prioritize it automatically.

内容的提问来源于stack exchange,提问作者Agata

火山引擎 最新活动