3535# # H1: slope beta < 0 (i.e., beta ~ Cauchy)
3636BayesFactorSlope <- function (fittedModel , parameter ,
3737 direction = " <" , plot = TRUE , ... ){
38+ parlabels <- rownames(fittedModel $ mcmc.summ )
3839
39- if (any(fittedModel $ mptInfo $ hyperprior $ IVprec != " dchisq(1)" ))
40- stop(" BayesFactorSlope requires that default priors are used for the slope parameter!" )
40+ IVprec <- fittedModel $ mptInfo $ hyperprior $ IVprec
41+ IVparsed <- sapply(strsplit(IVprec , " [\\ (\\ )]" ), " [[" , 1 )
42+ IVscale <- as.numeric(sapply(strsplit(IVprec , " [\\ (\\ )]" ), " [[" , 2 ))
43+ if (any(IVparsed != " dchisq" ))
44+ stop(" BayesFactorSlope requires that default JZS priors are used for the slope parameter!\n " ,
45+ " Example: traitMPT(..., IVprec='dchisq(s)') ## with s=scale parameter" )
4146
42- if (length(parameter )!= 1 || ! parameter %in% rownames( fittedModel $ mcmc.summ ) )
47+ if (length(parameter )!= 1 || ! parameter %in% parlabels )
4348 stop(" 'parameter' not in model or not of length=1." )
49+
4450 tmp <- strsplit(parameter , " _" )
4551 tmp [[1 ]][3 ] <- paste(tmp [[1 ]][- c(1 : 2 )], collapse = " _" )
4652 tmp <- tmp [[1 ]][1 : 3 ]
4753 if (length(tmp ) != 3 )
48- stop(" Parameter must be of the form 'slope_MPTparam_cov' ." )
54+ stop(" Parameter must be of the form 'slope_MPTparam_cov' (avoid '_' in parameters!) ." )
4955 if (tmp [[1 ]][1 ] != " slope" )
5056 stop(" Only valid for slope parameters." )
57+
5158 cov <- tmp [3 ]
59+ if (! cov %in% colnames(fittedModel $ mptInfo $ covData ))
60+ stop(" Covariate" , cov , " not in covariate data." )
61+ if (sum(grepl(paste0(tmp [1 ]," _" , tmp [2 ]), parlabels )) > 1 )
62+ stop(" Savage-Dickey provides incorrect Bayes factors for more than one predictor." )
63+
64+ # slope parameters are not standardized wrt covariate! => standardization
5265 s <- apply(fittedModel $ mptInfo $ covData , 2 , sd )[cov ]
53- samples <- unlist(fittedModel $ runjags $ mcmc [,parameter ]) * s # slope parameters are not standardized wrt covariate!
66+ samples <- unlist(fittedModel $ runjags $ mcmc [,parameter ]) * s
5467
5568 # approximation of posterior density
5669 if (direction == " <" ){
@@ -72,7 +85,7 @@ BayesFactorSlope <- function (fittedModel, parameter,
7285
7386 # posterior and prior density for beta=0:
7487 post0 <- dlogspline(0 , posterior )
75- prior0 <- dcauchy(0 ) * ifelse(direction == " !=" , 1 , 2 ) # one-sided
88+ prior0 <- dcauchy(0 , 0 , IVscale ) * ifelse(direction == " !=" , 1 , 2 ) # one-sided
7689
7790 # BF in favor of effect:
7891 bf <- data.frame (post0 / prior0 , prior0 / post0 )
@@ -81,11 +94,11 @@ BayesFactorSlope <- function (fittedModel, parameter,
8194 # illustration of Savage-Dickey method:
8295 dcauchy_trunc <- function (x ){
8396 if (direction == " >" ){
84- dx <- 2 * dcauchy(x )* ifelse(x > 0 , 1 , 0 )
97+ dx <- 2 * dcauchy(x , 0 , IVscale )* ifelse(x > 0 , 1 , 0 )
8598 } else if (direction == " <" ){
86- dx <- 2 * dcauchy(x )* ifelse(x < 0 , 1 , 0 )
99+ dx <- 2 * dcauchy(x , 0 , IVscale )* ifelse(x < 0 , 1 , 0 )
87100 } else if (direction == " !=" ){
88- dx <- dcauchy(x )
101+ dx <- dcauchy(x , 0 , IVscale )
89102 }
90103 dx
91104 }
0 commit comments