[R] Metro_Hastings
hms Dreams
cute_loomaa at hotmail.com
Thu Feb 19 14:23:04 CET 2015
Any suggestions :( ??From: cute_loomaa at hotmail.com
To: r-help at r-project.org
Subject: Metro_Hastings I wrote my code again
Date: Sun, 15 Feb 2015 21:47:25 +0300
Hi again :)
I wrote my code here:
library("MHadaptive")
baysianlog=function (param,data)
{ alpha=param[1]
gam=param[2]
delta=param[3]
x=data
n =length(x)
logl=n*log(alpha)+n*log(gam)+n*log(1/delta)+(alpha-1)*sum(log(x))-sum(log(1+(gam)*x^alpha))
p=prior(param)
return(logl+p)
}
prior=function(param)
{
alpha=param[1]
gam=param[2]
delta=param[3]
prior_alpha=dunif(alpha,min=0,
max=1,log=TRUE)
prior_gam=dunif(gam,0,1,log=TRUE)
prior_delta=dunif(delta,0,1,log=TRUE)
return(prior_alpha+ prior_gam +prior_delta)
}
n=7 ; m=15
alphaB=c();gamB=c();deltaB=c()
for( i
in 1:m){
alpha=1.8;gam=3;delta=0.8
v= runif(n)
x =delta*((1-v)^(-1/gam)-1)^(1/alpha )
mcmc_r=Metro_Hastings(li_func=baysianlog, pars=c(1,1,1),par_names=c('alpha','gamma','delta'),data=x
)
alphaB[i] =mean(mcmc_r $ trac[,1])
gamB[i]=
mean(mcmc_r $ trac[,2])
deltaB[i]=
mean(mcmc_r $ trac[,3])
}#end for
#####
The output is:
Error in optim(pars, li_func,
control = list(fnscale = -1), hessian = TRUE, :
non-finite finite-difference value [1]
________________
the problem I think in the :
mcmc_r=Metro_Hastings(li_func=baysianlog, pars=c(1,1,1),par_names=c('alpha','gamma','delta'),data=x )
because I did not write the prop_sigma because I don't know how can I calcalute the covariance matrix.
somebody told me to compute the cov without itreation then add the reasulting cov matrix to metro hasting using itreation
but it also gave me an error
Please anybody can check my code and correct it
Thank you,
Sara
[[alternative HTML version deleted]]
More information about the R-help
mailing list