rshrinkage: Sample from the model-specific posterior of the shrinkage...

View source: R/rshrinkage.R

rshrinkageR Documentation

Sample from the model-specific posterior of the shrinkage factor t = g / (1 + g)

Description

Sample from the model-specific posterior of the shrinkage factor t = g / (1 + g), using inverse sampling. This function is internal only because the arguments are not very user-friendly.

Usage

rshrinkage(n=1, R2, nObs, p, alpha)

Arguments

n

number of samples (default: 1)

R2

coefficient of determination in the model

nObs

number of observations used to fit the model

p

number of design matrix columns without counting the intercept

alpha

used hyperparameter for hyper-g prior

Value

n posterior shrinkage factor samples

Author(s)

Daniel Saban\'es Bov\'e

Examples

## construct a BayesMfp object
set.seed(29)
nObs <- 15

x1 <- rnorm (n=nObs)
x2 <- rbinom (n=nObs, size=20, prob=0.5) 
x3 <- rexp (n=nObs)

y <- rt (n=nObs, df=2)

test <- BayesMfp (y ~ bfp (x2, max = 3) + bfp(x3, max=3), nModels = 200, method="exhaustive")

## get the best found model
map <- test[[1]]

## produce many samples from the shrinkage factor
samples <- bfp:::rshrinkage(n=1e+5,
			    R2=map$R2,
		            nObs=nObs,
			    p=length(unlist(map$powers)),
			    alpha=4)
hist(samples,
     nclass=50, prob=TRUE)

## compare the mean with the analytical mean
abline(v=c(sampled <- mean(samples),
	   true <- map$postExpectedShrinkage),
       col=c(1, 2))
stopifnot(abs(sampled - true) < 0.0001)

bfp documentation built on Nov. 1, 2022, 1:05 a.m.

Related to rshrinkage in bfp...