Description Usage Arguments Details Value Examples
View source: R/rIndependent_Normal_Gamma_reg_std.R
Generates iid samples from the posterior density for the standard independent normal-gamma regression
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | rindep_norm_gamma_reg_std_R(
n,
y,
x,
mu,
P,
alpha,
wt,
f2,
Envelope,
gamma_list,
UB_list,
family,
link,
progbar = 1L
)
|
n |
number of draws to generate. If |
y |
For For |
x |
For For |
mu |
Prior mean |
P |
Prior Precision |
alpha |
offset |
wt |
weights |
f2 |
Gradient function |
Envelope |
Envelope object |
gamma_list |
list with parameters used by to generate candidates from a restricted gamma |
UB_list |
list with constants used during accept-reject calculations. |
family |
a description of the error distribution and link
function to be used in the model. For |
link |
link function |
progbar |
progress bar |
The rindependent_norm_gamma_reg
function produces iid samples for the Bayesian the Standardized Bayesian
Independent Normal-Gamma Regression Distribution associated with the gaussian() family.
This generates candidates independently from a gamma distribution and a likelihood subgradient density and then uses an accept-reject procedure in order to get samples from the desired posterior density.
A series of bounding functions are used during the simulation process in order to bound the likelihood function associated with the posterior density
1) Similar to the procedure in the fixed dispersion case, the (modified) log-likelihood function is for a specific component of the grid is bounded
2) A portion of the bounding function above is in turn bounded by using a term that has been shifted to the rate parameter of the modified gamma candidate generating distribution. This term consist of the *RSS* associated with the maximum likelihood estimate.
3) Because the relative proportions of the
Currently mainly the draws for the dispersion and the regression coefficients will be updated to return outputs consistent with other function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group,y=TRUE,x=TRUE)
p_setup=Prior_Setup(lm.D9)
dispersion=sigma(lm.D9)^2
## Initialize dispersion
p=1/dispersion
mu=p_setup$mu
Sigma_prior=p_setup$Sigma
y=lm.D9$y
x=lm.D9$x
Prior_Check(weight ~ group,gaussian(),dNormal(mu=mu,Sigma=Sigma_prior))
## This simple "standardization" for the prior appears to
## correct the issue with the prior
## "Standardize" the prior for the intercept by setting the
## mean equal to the mean of the dependent variable
## and setting the variance equal to the variance of the dependent variable
mu[1,1]=mean(y)
Sigma_prior[1,1]=var(y)
## For factors, set the "standad prior to mean=0 and variance driven by the range
## of the dependent variable
mu[2,1]=0
Sigma_prior[2,2]=((max(y)-min(y))/1.96)^2
#Prior_Check(lm.D9,mu,Sigma_prior)
Prior_Check(weight ~ group,gaussian(),dNormal(mu=mu,Sigma=Sigma_prior))
#prior=list(mu=mu,Sigma=Sigma_prior,dispersion=dispersion)
glmb.D9=glmb(weight~group, family=gaussian(),dNormal(mu=mu,Sigma=Sigma_prior,dispersion=dispersion))
post_mode=glmb.D9$coef.mode
sum_out1=summary(glmb.D9)
## Try mean one standard deviation away to see how it works
#mu[1,1]=mu[1,1]-2*sum_out1$coefficients[1,3]
#mu[2,1]=mu[2,1]+2*sum_out1$coefficients[1,3]
#prior=list(mu=mu,Sigma=Sigma_prior,dispersion=dispersion)
# Temporarily lower the prior variance
Sigma_prior=1*Sigma_prior
glmb.D9_v2=glmb(n=1000,weight~group, family=gaussian(),
dNormal(mu=mu,Sigma=Sigma_prior,dispersion=dispersion))
n_prior=2
shape=n_prior/2
rate= dispersion*shape
summary(glmb.D9)
summary(glmb.D9_v2)
#lm_out=lm(y ~ x-1) # returns same model
RSS=sum(residuals(lm.D9)^2)
n_prior=4
n_data=length(y)
shape=(n_prior/2)
rate=n_prior*RSS/n_data
prior_list=list(mu=mu,Sigma=Sigma_prior,dispersion=dispersion,
shape=shape,rate=rate,Precision=solve(Sigma_prior))
set.seed(360)
ptm <- proc.time()
sim2=rindependent_norm_gamma_reg(n=1000,y,x,prior_list=prior_list,
offset=NULL,weights=1,max_disp_perc=0.99)
proc.time()-ptm
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.