1

I am doing a simple change point analysis using the mcp R package, but my results still vary each time I rerun my code even after including set.seed. Appreciate any help on this! (Below is my code, thank you!)

library(rjags)
library(mcp)
model = list(y~1+x,~0+x,~0+x,~0+x,~0+x,~0+x)
set.seed(42)
fit_mcp = mcp(model, data=hosp_df)
summary(fit_mcp)
plot(fit_mcp)
newdata = data.frame(x = c(2023:2040))
prediction <- fitted(fit_mcp, newdata = newdata)
Limey
  • 10,234
  • 2
  • 12
  • 32
Laura Lim
  • 11
  • 1
  • What is `hosp_df`? Please include the results of [`dput(head(hosp_df))`](https://stackoverflow.com/questions/49994249/example-of-using-dput) in your question. – Kitswas Aug 19 '22 at 12:20
  • It appears that `fit_mcp` has a `seed` argument, suggesting that it has its own mechanism for setting the seed of the psuedo-random number generator it uses. – Limey Aug 19 '22 at 12:23
  • 1
    `mcp` calls `rjags`. `rjags` uses its own samplers in c++ and so has its own mechanism to set the seed. Within `rjags` you do this within `jags.model` using the `inits` parameter e.g. for one chain you could use `inits=list(.RNG.name="base::Wichmann-Hill" .RNG.seed=1))` (for more than one chain you need a separate seed for each chain). The `mcp` function has an `inits` parameter whch it passes to `mpc::run_jags` which passes to `rjags::jags.model`. So specifying the `inits` as documented by `rjags` should work. – user20650 Sep 07 '22 at 15:14

0 Answers0